From 308d1b607313f033453b65621a2a8c490de990f0 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 22 Mar 2020 10:43:45 +0100 Subject: [PATCH] Blacklist --- README.md | 1 + config.go | 1 + config_test.go | 7 +++++++ spamcheck.go | 16 +++++++++++++++- spamcheck_test.go | 26 ++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 spamcheck_test.go diff --git a/README.md b/README.md index a64889a..1c001da 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ To run the server, you must set a few environment variables from the list below. | **`PORT`** | optional | `8080` | The port on which the server should listen | | **`HONEYPOTS`** | optional | `_t_email` | Honeypot form fields (separated by `,`) | | **`GOOGLE_API_KEY`** | optional | - | Google API Key for the [Google Safe Browsing API](https://developers.google.com/safe-browsing/v4/) | +| **`BLACKLIST`** | optional | `gambling,casino` | List of spam words | ## Special form fields diff --git a/config.go b/config.go index fd83d7d..4c7bb05 100644 --- a/config.go +++ b/config.go @@ -16,6 +16,7 @@ type config struct { SmtpHost string `env:"SMTP_HOST"` SmtpPort int `env:"SMTP_PORT" envDefault:"587"` GoogleApiKey string `env:"GOOGLE_API_KEY"` + Blacklist []string `env:"BLACKLIST" envSeparator:"," envDefault:"gambling,casino"` } func parseConfig() (config, error) { diff --git a/config_test.go b/config_test.go index 3dfdf3e..083b78e 100644 --- a/config_test.go +++ b/config_test.go @@ -23,6 +23,9 @@ func Test_parseConfig(t *testing.T) { if cfg.SmtpPort != 587 { t.Error("SMTP Port not 587") } + if len(cfg.Blacklist) != 2 || cfg.Blacklist[0] != "gambling" || cfg.Blacklist[1] != "casino" { + t.Error("Default Blacklist is wrong") + } }) t.Run("Correct config parsing", func(t *testing.T) { os.Clearenv() @@ -36,6 +39,7 @@ func Test_parseConfig(t *testing.T) { _ = os.Setenv("SMTP_HOST", "smtp.example.com") _ = os.Setenv("SMTP_PORT", "100") _ = os.Setenv("GOOGLE_API_KEY", "abc") + _ = os.Setenv("BLACKLIST", "test,abc") cfg, err := parseConfig() if err != nil { t.Error() @@ -71,6 +75,9 @@ func Test_parseConfig(t *testing.T) { if !reflect.DeepEqual(cfg.GoogleApiKey, "abc") { t.Error("Google API Key is wrong") } + if !reflect.DeepEqual(cfg.Blacklist, []string{"test", "abc"}) { + t.Error("Blacklist is wrong") + } }) t.Run("Error when wrong config", func(t *testing.T) { os.Clearenv() diff --git a/spamcheck.go b/spamcheck.go index cd4f1f7..9ac482f 100644 --- a/spamcheck.go +++ b/spamcheck.go @@ -9,8 +9,10 @@ import ( // Returns true when it spam func checkValues(values FormValues) bool { var urlsToCheck []string + var allValues []string for _, value := range values { for _, singleValue := range value { + allValues = append(allValues, singleValue) if strings.Contains(singleValue, "http") { parsed, e := url.Parse(singleValue) if parsed != nil && e == nil { @@ -19,7 +21,19 @@ func checkValues(values FormValues) bool { } } } - return checkUrls(urlsToCheck) + return checkBlacklist(allValues) || checkUrls(urlsToCheck) +} + +func checkBlacklist(values []string) bool { + for _, value := range values { + for _, blacklistedString := range appConfig.Blacklist { + if strings.Contains(strings.ToLower(value), strings.ToLower(blacklistedString)) { + return true + } + } + + } + return false } // Only tests when GOOGLE_API_KEY is set diff --git a/spamcheck_test.go b/spamcheck_test.go new file mode 100644 index 0000000..5b75282 --- /dev/null +++ b/spamcheck_test.go @@ -0,0 +1,26 @@ +package main + +import ( + "os" + "testing" +) + +func Test_checkBlacklist(t *testing.T) { + prepare := func() { + os.Clearenv() + _ = os.Setenv("BLACKLIST", "test1,test2") + appConfig, _ = parseConfig() + } + t.Run("Allowed values", func(t *testing.T) { + prepare() + if checkBlacklist([]string{"Hello", "How are you?"}) == true { + t.Error() + } + }) + t.Run("Forbidden values", func(t *testing.T) { + prepare() + if checkBlacklist([]string{"How are you?", "Hello TeSt1"}) == false { + t.Error() + } + }) +}