From 28cddd788540327582a28bcd9998406bebc14f82 Mon Sep 17 00:00:00 2001 From: Blake Ridgway Date: Sun, 30 Nov 2025 19:54:11 -0600 Subject: [PATCH] Add stricter contact form validation and spam filtering --- internal/handlers/handlers.go | 96 ++++++++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 14 deletions(-) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index e4a0775..a95fedb 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -374,17 +374,29 @@ func isEnglishText(text string) bool { nonASCIICount := 0 totalCharCount := 0 + // Common English words to boost score + commonEnglish := []string{ + "the ", "and ", "is ", "to ", "of ", "for ", "that ", "with ", "this ", "have ", + "from ", "would ", "could ", "about ", "more ", "which ", "been ", "their ", + } + + lowerText := strings.ToLower(text) + englishWordBoost := 0 + for _, word := range commonEnglish { + if strings.Contains(lowerText, word) { + englishWordBoost += 10 + } + } + for _, r := range text { - // Count letters and numbers if unicode.IsLetter(r) || unicode.IsNumber(r) || unicode.IsSpace(r) || unicode.IsPunct(r) { totalCharCount++ - // English ASCII range (a-z, A-Z, 0-9, common punctuation/spaces) if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == ' ' || r == '.' || r == ',' || r == '!' || r == '?' || r == '-' || r == '\'' || r == '"' || r == ';' || r == ':' || r == '(' || r == ')' || r == '\n' || r == '\t' { englishCharCount++ - } else if r > 127 { // Non-ASCII character + } else if r > 127 { nonASCIICount++ } } @@ -394,11 +406,15 @@ func isEnglishText(text string) bool { return true } - // Allow up to 10% non-ASCII characters (for names, etc) - // But require at least 70% English ASCII + // If more than 3 non-ASCII characters, likely spam/bot + if nonASCIICount > 3 { + return false + } + englishPercentage := float64(englishCharCount) / float64(totalCharCount) - return englishPercentage >= 0.7 + // Stricter requirements with word boost + return englishPercentage >= 0.75 || (englishPercentage >= 0.65 && englishWordBoost > 0) } // isSpamMessage checks if a message looks like spam @@ -415,6 +431,9 @@ func isSpamMessage(message string) bool { "free money", "make money fast", "work from home", "nigerian", "inheritance", "transfer funds", "