Blacklist
This commit is contained in:
parent
7d2bab59ce
commit
308d1b6073
|
@ -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 |
|
| **`PORT`** | optional | `8080` | The port on which the server should listen |
|
||||||
| **`HONEYPOTS`** | optional | `_t_email` | Honeypot form fields (separated by `,`) |
|
| **`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/) |
|
| **`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
|
## Special form fields
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ type config struct {
|
||||||
SmtpHost string `env:"SMTP_HOST"`
|
SmtpHost string `env:"SMTP_HOST"`
|
||||||
SmtpPort int `env:"SMTP_PORT" envDefault:"587"`
|
SmtpPort int `env:"SMTP_PORT" envDefault:"587"`
|
||||||
GoogleApiKey string `env:"GOOGLE_API_KEY"`
|
GoogleApiKey string `env:"GOOGLE_API_KEY"`
|
||||||
|
Blacklist []string `env:"BLACKLIST" envSeparator:"," envDefault:"gambling,casino"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig() (config, error) {
|
func parseConfig() (config, error) {
|
||||||
|
|
|
@ -23,6 +23,9 @@ func Test_parseConfig(t *testing.T) {
|
||||||
if cfg.SmtpPort != 587 {
|
if cfg.SmtpPort != 587 {
|
||||||
t.Error("SMTP Port not 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) {
|
t.Run("Correct config parsing", func(t *testing.T) {
|
||||||
os.Clearenv()
|
os.Clearenv()
|
||||||
|
@ -36,6 +39,7 @@ func Test_parseConfig(t *testing.T) {
|
||||||
_ = os.Setenv("SMTP_HOST", "smtp.example.com")
|
_ = os.Setenv("SMTP_HOST", "smtp.example.com")
|
||||||
_ = os.Setenv("SMTP_PORT", "100")
|
_ = os.Setenv("SMTP_PORT", "100")
|
||||||
_ = os.Setenv("GOOGLE_API_KEY", "abc")
|
_ = os.Setenv("GOOGLE_API_KEY", "abc")
|
||||||
|
_ = os.Setenv("BLACKLIST", "test,abc")
|
||||||
cfg, err := parseConfig()
|
cfg, err := parseConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error()
|
t.Error()
|
||||||
|
@ -71,6 +75,9 @@ func Test_parseConfig(t *testing.T) {
|
||||||
if !reflect.DeepEqual(cfg.GoogleApiKey, "abc") {
|
if !reflect.DeepEqual(cfg.GoogleApiKey, "abc") {
|
||||||
t.Error("Google API Key is wrong")
|
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) {
|
t.Run("Error when wrong config", func(t *testing.T) {
|
||||||
os.Clearenv()
|
os.Clearenv()
|
||||||
|
|
16
spamcheck.go
16
spamcheck.go
|
@ -9,8 +9,10 @@ import (
|
||||||
// Returns true when it spam
|
// Returns true when it spam
|
||||||
func checkValues(values FormValues) bool {
|
func checkValues(values FormValues) bool {
|
||||||
var urlsToCheck []string
|
var urlsToCheck []string
|
||||||
|
var allValues []string
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
for _, singleValue := range value {
|
for _, singleValue := range value {
|
||||||
|
allValues = append(allValues, singleValue)
|
||||||
if strings.Contains(singleValue, "http") {
|
if strings.Contains(singleValue, "http") {
|
||||||
parsed, e := url.Parse(singleValue)
|
parsed, e := url.Parse(singleValue)
|
||||||
if parsed != nil && e == nil {
|
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
|
// Only tests when GOOGLE_API_KEY is set
|
||||||
|
|
|
@ -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()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Reference in New Issue