diff --git a/backend/shared/infra/security/sanitize.go b/backend/shared/infra/security/sanitize.go
index 8813822..56785fa 100644
--- a/backend/shared/infra/security/sanitize.go
+++ b/backend/shared/infra/security/sanitize.go
@@ -8,6 +8,42 @@ import (
"unicode"
)
+const (
+ // PlayerNameMaxLength defines the maximum length for player names.
+ PlayerNameMaxLength = 50
+ // AnswerMaxLength defines the maximum length for answer submissions.
+ AnswerMaxLength = 500
+ // QuestionTextMaxLength defines the maximum length for admin-authored question text.
+ QuestionTextMaxLength = 1000
+ // ThemeMaxLength defines the maximum length for theme names.
+ ThemeMaxLength = 100
+)
+
+var (
+ // Sanitization order is trim -> collapse spaces -> HTML escape -> clamp -> allowed pattern.
+ spaceRegex = regexp.MustCompile(`\s+`)
+
+ // Player names allow letters, numbers, spaces, '-', '_' and '.'.
+ playerNameAllowedPattern = regexp.MustCompile(`^[a-zA-Z0-9\s\-_.]+$`)
+ // Themes allow letters, numbers, spaces, '-' and '_'.
+ themeAllowedPattern = regexp.MustCompile(`^[a-zA-Z0-9\s\-_]+$`)
+
+ tagRegex = regexp.MustCompile(`<[^>]*>`)
+ scriptRegex = regexp.MustCompile(`(?i)`)
+
+ dangerousPatterns = []string{
+ "javascript:",
+ "data:",
+ "vbscript:",
+ "`)
result = scriptRegex.ReplaceAllString(result, "")
return result
@@ -113,8 +147,8 @@ func SanitizeTheme(input string) string {
TrimWhitespace: true,
RemoveMultipleSpaces: true,
HTMLEscape: true,
- MaxLength: 100,
- AllowedPattern: regexp.MustCompile(`^[a-zA-Z0-9\s\-_]+$`),
+ MaxLength: ThemeMaxLength,
+ AllowedPattern: themeAllowedPattern,
}
result := Sanitize(input, opts)
@@ -137,24 +171,11 @@ func SanitizeTheme(input string) string {
// RemoveHTMLTags removes all HTML tags from a string.
func RemoveHTMLTags(input string) string {
- tagRegex := regexp.MustCompile(`<[^>]*>`)
return tagRegex.ReplaceAllString(input, "")
}
// ContainsDangerousPatterns checks if input contains potentially dangerous patterns.
func ContainsDangerousPatterns(input string) bool {
- dangerousPatterns := []string{
- "javascript:",
- "data:",
- "vbscript:",
- "