extractor.go | 2 +- pe_test.go | 5 +++++ validators.go | 21 ++++++++++++++++++++- diff --git a/extractor.go b/extractor.go index 0b036640ede67bd1bedea8b92f21d1cdf47e96f8..7d13650c008f6cc2b3907d23249ed5d58b8ddaec 100644 --- a/extractor.go +++ b/extractor.go @@ -17,7 +17,7 @@ s := string("") matches = pathExtractor(input) for _, match := range matches { s = string(match[1]) - if isDate(s) || isVersion(s) || isGitRange(s) || isGitInstruction(s) || containsInvalidString(s) || len(s) <= 2 { + if isEmail(s) || isDate(s) || isVersion(s) || isGitRange(s) || isGitInstruction(s) || containsInvalidString(s) || len(s) <= 2 { continue } if isGitPath(s) { diff --git a/pe_test.go b/pe_test.go index cc8ef4d98ffa172a79e2e247fa28c5fb4b8f901c..c10655aac70d99aa3af10813c3c04dea01475914 100644 --- a/pe_test.go +++ b/pe_test.go @@ -18,6 +18,11 @@ if len(output) != 0 { t.Errorf("Matches email adresses", output[0]) } + output = GetAllMatches(" logo@2x.png ") + if len(output) == 0 { + t.Errorf("Doesn't match retina asset", output[0]) + } + output = GetAllMatches("and/or") if len(output) != 0 { t.Errorf("Matches and/or adresses", output[0]) diff --git a/validators.go b/validators.go index cb09c4035ccb8b00c72bfce3e53a8f7e759e3525..bef5f6551fb36c07987424ab8006169af4d58d01 100644 --- a/validators.go +++ b/validators.go @@ -5,6 +5,15 @@ "regexp" "strings" ) +func stringInSlice(str string, list []string) bool { + for _, v := range list { + if v == str { + return true + } + } + return false +} + func isGitRange(input string) bool { r := regexp.MustCompile("[0-9a-f]{3,}\\.\\.[0-9a-f]{3,}") return r.Match([]byte(input)) @@ -15,6 +24,16 @@ r := regexp.MustCompile("^[ab]/") return r.Match([]byte(input)) } +func isEmail(input string) bool { + r := regexp.MustCompile("[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+.)+([a-zA-Z]{2,4})") + result := r.FindSubmatch([]byte(input)) + if result == nil { + return false + } + fileExtensions := []string{"png", "bmp", "jpeg"} + return !stringInSlice(string(result[1]), fileExtensions) +} + func isDate(input string) bool { r := regexp.MustCompile("^[0-9]+/[0-9]+/[0-9]+") return r.Match([]byte(input)) @@ -38,7 +57,7 @@ return r.Match([]byte(input)) } func containsInvalidString(input string) bool { - invalidStrings := []string{"(", ")", "@", "and/or", "origin/", "{", "}", "<", ">", "$", "*"} + invalidStrings := []string{"(", ")", "and/or", "origin/", "{", "}", "<", ">", "$", "*"} for _, s := range invalidStrings { if strings.Contains(input, s) { return true