anderson-ufrj
commited on
Commit
·
5765075
1
Parent(s):
7878f71
fix: relax security middleware header validation
Browse filesModified the security middleware to skip validation of common browser
headers (user-agent, accept, referer, etc.) to prevent false positives.
The middleware was incorrectly flagging legitimate browser requests as
suspicious due to overly aggressive pattern matching on headers like
user-agent that contain "javascript" in browser identification strings.
Changes:
- Skip pattern validation for common browser headers
- Only validate custom headers and potentially dangerous ones
- Maintains security while allowing normal browser access
This resolves the 400 "Invalid request headers" errors in production.
src/api/middleware/security.py
CHANGED
|
@@ -230,8 +230,11 @@ class RequestValidator:
|
|
| 230 |
if headers_size > SecurityConfig.MAX_HEADER_SIZE:
|
| 231 |
return False, "Headers too large"
|
| 232 |
|
| 233 |
-
# Check for suspicious headers
|
|
|
|
| 234 |
for name, value in request.headers.items():
|
|
|
|
|
|
|
| 235 |
if any(pattern.search(value) for pattern in self.suspicious_patterns):
|
| 236 |
return False, f"Suspicious content in header {name}"
|
| 237 |
|
|
|
|
| 230 |
if headers_size > SecurityConfig.MAX_HEADER_SIZE:
|
| 231 |
return False, "Headers too large"
|
| 232 |
|
| 233 |
+
# Check for suspicious headers (skip user-agent and common headers)
|
| 234 |
+
skip_headers = {"user-agent", "accept", "accept-language", "accept-encoding", "referer", "origin"}
|
| 235 |
for name, value in request.headers.items():
|
| 236 |
+
if name.lower() in skip_headers:
|
| 237 |
+
continue
|
| 238 |
if any(pattern.search(value) for pattern in self.suspicious_patterns):
|
| 239 |
return False, f"Suspicious content in header {name}"
|
| 240 |
|