Browse Source

Blacklist

pull/1/head
Jan-Lukas Else 10 months ago
parent
commit
308d1b6073
5 changed files with 50 additions and 1 deletions
  1. +1
    -0
      README.md
  2. +1
    -0
      config.go
  3. +7
    -0
      config_test.go
  4. +15
    -1
      spamcheck.go
  5. +26
    -0
      spamcheck_test.go

+ 1
- 0
README.md View File

@ -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


+ 1
- 0
config.go View File

@ -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) {


+ 7
- 0
config_test.go View File

@ -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()


+ 15
- 1
spamcheck.go View File

@ -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


+ 26
- 0
spamcheck_test.go View File

@ -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()
}
})
}

Loading…
Cancel
Save