From cd415e7d4d2d25f365c898e200450e8df686145d Mon Sep 17 00:00:00 2001 From: Osamaali313 <86572800+Osamaali313@users.noreply.github.com> Date: Mon, 29 Jun 2026 22:19:13 +0300 Subject: [PATCH] fix(proxy): set Content-Length: 0 on empty-body replacement build_raw_request only auto-adds Content-Length for a non-empty body, so replacing the body with an empty string produced a request with no Content-Length. Set it to 0 explicitly in apply_modifications (scoped to the body-replacement path, so body-less requests like GET are unaffected). Adds a test. --- strix/tools/proxy/caido_api.py | 4 ++++ tests/test_proxy_caido_api.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/strix/tools/proxy/caido_api.py b/strix/tools/proxy/caido_api.py index 5ce03478..bb7a5bff 100644 --- a/strix/tools/proxy/caido_api.py +++ b/strix/tools/proxy/caido_api.py @@ -269,6 +269,10 @@ def apply_modifications( if "Content-Length" not in explicit_cl: for key in [k for k in headers if k.title() == "Content-Length"]: del headers[key] + # build_raw_request only auto-adds Content-Length for a non-empty + # body, so an empty replacement body needs it set explicitly. + if body == "": + headers["Content-Length"] = "0" if "cookies" in modifications: cookies: dict[str, str] = {} if headers.get("Cookie"): diff --git a/tests/test_proxy_caido_api.py b/tests/test_proxy_caido_api.py index 3299db18..dde2fbd8 100644 --- a/tests/test_proxy_caido_api.py +++ b/tests/test_proxy_caido_api.py @@ -57,3 +57,19 @@ def test_no_body_modification_keeps_content_length() -> None: } result = apply_modifications(components, {"headers": {"X-Test": "1"}}, "http://x.test/") assert result["headers"].get("Content-Length") == "3" + + +def test_empty_body_replacement_sets_content_length_zero() -> None: + components = { + "method": "POST", + "headers": {"Host": "x.test", "Content-Length": "3"}, + "body": "old", + } + result = apply_modifications(components, {"body": ""}, "http://x.test/") + _conn, raw = build_raw_request( + method=result["method"], + url=result["url"], + headers=result["headers"], + body=result["body"], + ) + assert _content_length(raw) == "0"