|
|
| import re |
| import logging |
|
|
| class InputSanitizer: |
| """Sanitize and inspect strings for newline‑based injection or other malicious patterns.""" |
|
|
| |
| _newline_patterns = [ |
| r"\\n", |
| r"\\r", |
| r"
", |
| r"
", |
| r"%0a", |
| r"%0d" |
| ] |
| _compiled_newline = re.compile('|'.join(_newline_patterns), re.IGNORECASE) |
|
|
| |
| _blacklist = [ |
| r"<script", |
| r"<iframe", |
| r";--", |
| ] |
| _compiled_black = re.compile('|'.join(_blacklist), re.IGNORECASE) |
|
|
| def sanitize(self, text: str) -> str: |
| """Remove dangerous patterns and log incidents.""" |
| original = text |
| |
| text = self._compiled_newline.sub(' ', text) |
| |
| text = self._compiled_black.sub('[REDACTED]', text) |
|
|
| if text != original: |
| logging.warning("Input sanitized due to suspicious patterns") |
| return text |
|
|
| def detect(self, text: str) -> bool: |
| """Return True if malicious pattern detected.""" |
| return bool(self._compiled_newline.search(text) or self._compiled_black.search(text)) |
|
|