From f5c3cf774d05105f3c3ec53060bb602ad90c2735 Mon Sep 17 00:00:00 2001 From: Cizz22 Date: Wed, 4 Mar 2026 14:41:08 +0700 Subject: [PATCH] fix: Harden security middleware by logging detailed violations internally and returning generic error messages, and streamline user logging to use user_id. --- src/logging.py | 2 -- src/main.py | 7 ++---- src/middleware.py | 58 +++++++++++++++++++++++++++++++---------------- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/logging.py b/src/logging.py index c261b38..787d24b 100644 --- a/src/logging.py +++ b/src/logging.py @@ -60,8 +60,6 @@ class JSONFormatter(logging.Formatter): # Add Context information if available if request_id: log_record["request_id"] = request_id - if role: - log_record["role"] = role # Add Error context if available if hasattr(record, "error_id"): diff --git a/src/main.py b/src/main.py index 104a059..0f1bbf0 100644 --- a/src/main.py +++ b/src/main.py @@ -181,10 +181,8 @@ async def db_session_middleware(request: Request, call_next): set_role(role) user_info_str = "" - if username: - user_info_str = f" | User: {username}" - if role: - user_info_str += f" ({role})" + if user_id: + user_info_str = f" | User ID: {user_id}" error_id = getattr(request.state, "error_id", None) log_msg = f"HTTP {request.method} {request.url.path} completed in {round(process_time, 2)}ms{user_info_str}" @@ -199,7 +197,6 @@ async def db_session_middleware(request: Request, call_next): "status_code": response.status_code, "duration_ms": round(process_time, 2), "user_id": user_id, - "role": role, "error_id": error_id, }, ) diff --git a/src/middleware.py b/src/middleware.py index 6754c91..90992e6 100644 --- a/src/middleware.py +++ b/src/middleware.py @@ -103,6 +103,7 @@ ALLOWED_HEADERS = { "x-forwarded-path", "x-forwarded-prefix", "cookie", + "x-kong-request-id" } MAX_QUERY_PARAMS = 50 @@ -193,33 +194,38 @@ def inspect_value(value: str, source: str): if XSS_PATTERN.search(value): + log.warning(f"Security violation: Potential XSS payload detected in {source}, value: {value}") raise HTTPException( status_code=422, - detail=f"Potential XSS payload detected in {source}", + detail="Invalid request parameters", ) if SQLI_PATTERN.search(value): + log.warning(f"Security violation: Potential SQL injection payload detected in {source}, value: {value}") raise HTTPException( status_code=422, - detail=f"Potential SQL injection payload detected in {source}", + detail="Invalid request parameters", ) if RCE_PATTERN.search(value): + log.warning(f"Security violation: Potential RCE payload detected in {source}, value: {value}") raise HTTPException( status_code=422, - detail=f"Potential RCE payload detected in {source}", + detail="Invalid request parameters", ) if TRAVERSAL_PATTERN.search(value): + log.warning(f"Security violation: Potential Path Traversal payload detected in {source}, value: {value}") raise HTTPException( status_code=422, - detail=f"Potential Path Traversal payload detected in {source}", + detail="Invalid request parameters", ) if has_control_chars(value): + log.warning(f"Security violation: Invalid control characters detected in {source}") raise HTTPException( status_code=422, - detail=f"Invalid control characters detected in {source}", + detail="Invalid request parameters", ) @@ -227,15 +233,17 @@ def inspect_json(obj, path="body", check_whitelist=True): if isinstance(obj, dict): for key, value in obj.items(): if key in FORBIDDEN_JSON_KEYS: + log.warning(f"Security violation: Forbidden JSON key detected: {path}.{key}") raise HTTPException( status_code=422, - detail=f"Forbidden JSON key detected: {path}.{key}", + detail="Invalid request parameters", ) if check_whitelist and key not in ALLOWED_DATA_PARAMS: + log.warning(f"Security violation: Unknown JSON key detected: {path}.{key}") raise HTTPException( status_code=422, - detail=f"Unknown JSON key detected: {path}.{key}", + detail="Invalid request parameters", ) # Recurse. If the key is a dynamic container, we stop whitelist checking for children. @@ -266,9 +274,10 @@ class RequestValidationMiddleware(BaseHTTPMiddleware): ALLOW_DUPLICATE_HEADERS = {'accept', 'accept-encoding', 'accept-language', 'accept-charset', 'cookie'} real_duplicates = [h for h in duplicate_headers if h not in ALLOW_DUPLICATE_HEADERS] if real_duplicates: + log.warning(f"Security violation: Duplicate headers detected: {real_duplicates}") raise HTTPException( status_code=422, - detail=f"Duplicate headers are not allowed: {real_duplicates}", + detail="Invalid request parameters", ) # Whitelist headers @@ -276,9 +285,10 @@ class RequestValidationMiddleware(BaseHTTPMiddleware): if unknown_headers: filtered_unknown = [h for h in unknown_headers if not h.startswith('sec-')] if filtered_unknown: + log.warning(f"Security violation: Unknown headers detected: {filtered_unknown}") raise HTTPException( status_code=422, - detail=f"Unknown headers detected: {filtered_unknown}", + detail="Invalid request parameters", ) # Inspect header values @@ -290,25 +300,28 @@ class RequestValidationMiddleware(BaseHTTPMiddleware): # 1. Query string limits # ------------------------- if len(request.url.query) > MAX_QUERY_LENGTH: + log.warning(f"Security violation: Query string too long") raise HTTPException( status_code=422, - detail="Query string too long", + detail="Invalid request parameters", ) params = request.query_params.multi_items() if len(params) > MAX_QUERY_PARAMS: + log.warning(f"Security violation: Too many query parameters") raise HTTPException( status_code=422, - detail="Too many query parameters", + detail="Invalid request parameters", ) # Check for unknown query parameters unknown_params = [key for key, _ in params if key not in ALLOWED_DATA_PARAMS] if unknown_params: + log.warning(f"Security violation: Unknown query parameters detected: {unknown_params}") raise HTTPException( status_code=422, - detail=f"Unknown query parameters detected: {unknown_params}", + detail="Invalid request parameters", ) # ------------------------- @@ -321,9 +334,10 @@ class RequestValidationMiddleware(BaseHTTPMiddleware): ] if duplicates: + log.warning(f"Security violation: Duplicate query parameters detected: {duplicates}") raise HTTPException( status_code=422, - detail=f"Duplicate query parameters are not allowed: {duplicates}", + detail="Invalid request parameters", ) # ------------------------- @@ -338,19 +352,22 @@ class RequestValidationMiddleware(BaseHTTPMiddleware): try: size_val = int(value) if size_val > 50: + log.warning(f"Security violation: Pagination size too large ({size_val})") raise HTTPException( status_code=422, - detail=f"Pagination size '{key}' cannot exceed 50", + detail="Invalid request parameters", ) if size_val % 5 != 0: + log.warning(f"Security violation: Pagination size not multiple of 5 ({size_val})") raise HTTPException( status_code=422, - detail=f"Pagination size '{key}' must be a multiple of 5", + detail="Invalid request parameters", ) except ValueError: + log.warning(f"Security violation: Pagination size invalid value") raise HTTPException( status_code=422, - detail=f"Pagination size '{key}' must be an integer", + detail="Invalid request parameters", ) # ------------------------- @@ -361,7 +378,8 @@ class RequestValidationMiddleware(BaseHTTPMiddleware): content_type.startswith(t) for t in ("application/json", "multipart/form-data", "application/x-www-form-urlencoded") ): - raise HTTPException(status_code=422, detail="Unsupported Content-Type") + log.warning(f"Security violation: Unsupported Content-Type: {content_type}") + raise HTTPException(status_code=422, detail="Invalid request parameters") # ------------------------- # 5. Single source check (Query vs JSON Body) @@ -377,9 +395,10 @@ class RequestValidationMiddleware(BaseHTTPMiddleware): has_body = True if has_query and has_body: + log.warning(f"Security violation: Mixed parameters (query + JSON body)") raise HTTPException( status_code=422, - detail="Parameters must be from a single source (query string or JSON body), mixed sources are not allowed", + detail="Invalid request parameters", ) # ------------------------- @@ -394,7 +413,8 @@ class RequestValidationMiddleware(BaseHTTPMiddleware): try: payload = json.loads(body) except json.JSONDecodeError: - raise HTTPException(status_code=422, detail="Invalid JSON body") + log.warning(f"Security violation: Invalid JSON body") + raise HTTPException(status_code=422, detail="Invalid request parameters") inspect_json(payload)