diff --git a/apps/demo/app.py b/apps/demo/app.py index b8bf4ca..b507805 100644 --- a/apps/demo/app.py +++ b/apps/demo/app.py @@ -5,8 +5,14 @@ ROLE = os.getenv("APP_ROLE", "web") INSTANCE = os.getenv("INSTANCE", ROLE) +def sanitize_header_val(val: str | None) -> str | None: + if not val: + return None + cleaned = val.replace("\r", "").replace("\n", "").strip() + return cleaned if cleaned else None + async def health(request): - req_id = request.headers.get("X-Request-ID") + req_id = sanitize_header_val(request.headers.get("X-Request-ID")) data = {"status": "ok", "service": INSTANCE} if req_id: data["requestId"] = req_id @@ -14,7 +20,7 @@ async def health(request): return web.json_response(data, headers=headers) async def index(request): - req_id = request.headers.get("X-Request-ID") + req_id = sanitize_header_val(request.headers.get("X-Request-ID")) headers = {"X-Request-ID": req_id} if req_id else {} if ROLE == "web": return web.Response( diff --git a/nginx/snippets/proxy.conf b/nginx/snippets/proxy.conf index 7d9899c..dab90e0 100644 --- a/nginx/snippets/proxy.conf +++ b/nginx/snippets/proxy.conf @@ -11,3 +11,4 @@ proxy_set_header Connection ""; proxy_hide_header X-Powered-By; proxy_next_upstream error timeout http_502 http_503 http_504; proxy_next_upstream_tries 2; +proxy_next_upstream_timeout 10s; diff --git a/tests/test_project.py b/tests/test_project.py index b448bc1..a1d9ad6 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -76,6 +76,7 @@ def test_proxy_forwarding_invariants(self): self.assertIn('proxy_hide_header X-Powered-By;', proxy) self.assertIn('proxy_next_upstream error timeout http_502 http_503 http_504;', proxy) self.assertIn('proxy_next_upstream_tries 2;', proxy) + self.assertIn('proxy_next_upstream_timeout 10s;', proxy) def test_rate_limiting_and_http_status(self): nginx_cfg = load('nginx/nginx.conf') @@ -131,7 +132,8 @@ def test_gzip_compression_and_buffer_tuning(self): def test_trace_id_propagation_in_demo_app(self): source = load('apps/demo/app.py') - self.assertIn('request.headers.get("X-Request-ID")', source) + self.assertIn('def sanitize_header_val', source) + self.assertIn('sanitize_header_val(request.headers.get("X-Request-ID"))', source) self.assertIn('data["requestId"] = req_id', source) self.assertIn('"X-Request-ID": req_id', source)