[UPDT] HORILLA: Update xss filter for common attempts

This commit is contained in:
Horilla
2025-08-22 10:41:20 +05:30
parent f25c6893c3
commit d8f6d52602
5 changed files with 14 additions and 8 deletions

View File

@@ -39,12 +39,21 @@ def url(self: FieldFile):
setattr(FieldFile, "url", url)
def has_xss(value):
"""Basic check for common XSS patterns."""
def has_xss(value: str) -> bool:
"""Detect common XSS attempts (script tags, event handlers, js URLs)."""
if not isinstance(value, str):
return False
xss_pattern = re.compile(r"<.*?script.*?>|javascript:|on\w+=", re.IGNORECASE)
return bool(xss_pattern.search(value))
xss_patterns = [
r"<\s*script.*?>.*?<\s*/\s*script\s*>",
r"javascript\s*:",
r"on\w+\s*=",
r"<\s*script.*?>.*?(eval|setTimeout|setInterval|new\s+Function|XMLHttpRequest|fetch|\$\s*\().*?<\s*/\s*script\s*>",
r"on\w+\s*=\s*['\"]?\s*(eval|setTimeout|setInterval|new\s+Function|XMLHttpRequest|fetch|\$\s*\()[^>]*",
]
combined = re.compile("|".join(xss_patterns), re.IGNORECASE | re.DOTALL)
return bool(combined.search(value))
def upload_path(instance, filename):