From 453aabe59d0c4274a53ec3326a4fc1b943b46ecc Mon Sep 17 00:00:00 2001 From: hardik-devops Date: Wed, 1 Jul 2026 20:04:09 +0530 Subject: [PATCH] resolve(proxy): emit Content-Length: 0 for empty replaced body build_raw_request's Content-Length guard was "if body and ...", so a falsy empty-string body (a valid replacement via repeat_request's modifications["body"]) skipped Content-Length injection entirely. Sending no Content-Length is valid HTTP/1.1, but some strict servers or WAFs expect an explicit Content-Length: 0, so silently omitting it can still cause inconsistent replay behavior. Change the guard to "body is not None" so an empty body still gets Content-Length: 0 while a None body (not applicable here, but kept for clarity) is still skipped. Adds test_build_raw_request_sets_content_length_zero_for_empty_body, verified RED against the prior "if body and ..." guard before GREEN. Addresses review feedback from usestrix/strix#614. --- strix/tools/proxy/caido_api.py | 2 +- tests/test_caido_api.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/strix/tools/proxy/caido_api.py b/strix/tools/proxy/caido_api.py index 525be944..c56db672 100644 --- a/strix/tools/proxy/caido_api.py +++ b/strix/tools/proxy/caido_api.py @@ -157,7 +157,7 @@ def build_raw_request( final_headers = {**headers} final_headers.setdefault("Host", parsed.netloc) final_headers.setdefault("User-Agent", "strix") - if body and "Content-Length" not in {k.title() for k in final_headers}: + if body is not None and "Content-Length" not in {k.title() for k in final_headers}: final_headers["Content-Length"] = str(len(body.encode("utf-8"))) lines = [f"{method.upper()} {path} HTTP/1.1"] diff --git a/tests/test_caido_api.py b/tests/test_caido_api.py index e287f6de..ba577ea7 100644 --- a/tests/test_caido_api.py +++ b/tests/test_caido_api.py @@ -60,3 +60,20 @@ def test_build_raw_request_recomputes_content_length_after_replacement() -> None ) head = raw.decode("utf-8").split("\r\n\r\n", 1)[0] assert f"Content-Length: {len(new_body.encode('utf-8'))}" in head + + +def test_build_raw_request_sets_content_length_zero_for_empty_body() -> None: + components = _components(body="old") + modified = apply_modifications( + components, + {"body": ""}, + "http://example.com/submit", + ) + _, raw = build_raw_request( + method=modified["method"], + url=modified["url"], + headers=modified["headers"], + body=modified["body"], + ) + head = raw.decode("utf-8").split("\r\n\r\n", 1)[0] + assert "Content-Length: 0" in head