Spaces:
Sleeping
Sleeping
File size: 1,007 Bytes
0a5d897 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import re
import html
def sanitize_input(text: str) -> str:
"""
Ensures input is treated as literal text.
1. Removes any null bytes.
2. Escapes HTML entities to prevent XSS.
3. Normalizes whitespace.
"""
if not isinstance(text, str):
return ""
# Remove null bytes
text = text.replace("\x00", "")
# Escape HTML characters (prevents XSS)
text = html.escape(text)
# Normalize whitespace (optional but keeps things clean)
text = re.sub(r'\s+', ' ', text).strip()
return text
def is_suspicious(text: str) -> bool:
"""
Detects common injection patterns (SQL, Script, etc.)
just for logging/alerting purposes.
"""
patterns = [
r"(?i)SELECT.*FROM",
r"(?i)DROP.*TABLE",
r"(?i)UNION.*SELECT",
r"(?i)<script.*?>",
r"(?i)OR.*1=1",
r"(?i)INSERT.*INTO"
]
for pattern in patterns:
if re.search(pattern, text):
return True
return False
|