Skip to content

A fast, keyboard-driven TUI API client & webhook inspector. No cloud, no Electron, 100% local-first.

fix: strict RFC 9112 trailer termination, CRLF validation, and Transfer-Encoding validation (v0.1.7)

4yk5c033fdf56adc
diff --git a/network.py b/network.py
index 03baa9a..14d564b 100644
--- a/network.py
+++ b/network.py
@@ -23,7 +23,7 @@ class NetworkEngine:
headers = dict(headers) if headers else {}
if not any(k.lower() == "user-agent" for k in headers):
- headers["User-Agent"] = "Ghostman/0.1.6"
+ headers["User-Agent"] = "Ghostman/0.1.7"
if body and not any(k.lower() == "content-type" for k in headers):
try:
diff --git a/pyproject.toml b/pyproject.toml
index f20643e..63b2c67 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "ghostman"
-version = "0.1.6"
+version = "0.1.7"
description = "A fast, keyboard-driven terminal API client and webhook inspector"
readme = "README.md"
authors = [{ name = "4yk5" }]
diff --git a/tests/test_vulnerabilities_fixed.py b/tests/test_vulnerabilities_fixed.py
index 831af62..cfe60bf 100644
--- a/tests/test_vulnerabilities_fixed.py
+++ b/tests/test_vulnerabilities_fixed.py
@@ -384,6 +384,90 @@ async def test_chunked_transfer_encoding_webhook():
finally:
await ws_timeout.stop()
+ # I) Premature EOF after zero chunk (before final terminating CRLF)
+ premature_trailer_eof = (
+ b"POST /trailer-eof HTTP/1.1\r\n"
+ b"Host: 127.0.0.1:9096\r\n"
+ b"Transfer-Encoding: chunked\r\n"
+ b"Connection: close\r\n"
+ b"\r\n"
+ b"5\r\n"
+ b"hello\r\n"
+ b"0\r\n"
+ # Closed abruptly without the final terminating \r\n
+ )
+ reader7, writer7 = await asyncio.open_connection("127.0.0.1", 9096)
+ writer7.write(premature_trailer_eof)
+ await writer7.drain()
+ writer7.close()
+ await writer7.wait_closed()
+ await asyncio.sleep(0.1)
+ assert len(received_hooks) == 2, "VULNERABILITY: Unterminated trailer EOF was accepted into captured_hooks!"
+
+ # J) Trailer section timeout -> MUST respond 408 Request Timeout
+ ws_trailer = WebhookServer(port=9093, read_timeout=0.3, total_timeout=5.0)
+ await ws_trailer.start()
+ try:
+ reader_tr, writer_tr = await asyncio.open_connection("127.0.0.1", 9093)
+ writer_tr.write(
+ b"POST /trailer-to HTTP/1.1\r\n"
+ b"Host: 127.0.0.1:9093\r\n"
+ b"Transfer-Encoding: chunked\r\n"
+ b"Connection: close\r\n"
+ b"\r\n"
+ b"0\r\n"
+ # Stop sending, wait for trailer read_timeout to expire
+ )
+ await writer_tr.drain()
+ await asyncio.sleep(0.5)
+ resp_tr = await reader_tr.read(1024)
+ writer_tr.close()
+ await writer_tr.wait_closed()
+ assert b"408 Request Timeout" in resp_tr, f"Expected 408 on trailer timeout, got: {resp_tr[:100]}"
+ assert len(ws_trailer.captured_hooks) == 0
+ finally:
+ await ws_trailer.stop()
+
+ # K) Bare LF delimiter (not CRLF) MUST be rejected with 400 Bad Request
+ bare_lf_http = (
+ b"POST /bare-lf HTTP/1.1\r\n"
+ b"Host: 127.0.0.1:9096\r\n"
+ b"Transfer-Encoding: chunked\r\n"
+ b"Connection: close\r\n"
+ b"\r\n"
+ b"5\r\n"
+ b"hello\n" # Bare LF instead of \r\n
+ b"0\r\n\r\n"
+ )
+ reader8, writer8 = await asyncio.open_connection("127.0.0.1", 9096)
+ writer8.write(bare_lf_http)
+ await writer8.drain()
+ resp8 = await reader8.read(1024)
+ writer8.close()
+ await writer8.wait_closed()
+ assert b"400 Bad Request" in resp8, f"Expected 400 for bare LF, got: {resp8[:100]}"
+ await asyncio.sleep(0.1)
+ assert len(received_hooks) == 2, "VULNERABILITY: Bare LF delimiter was accepted into captured_hooks!"
+
+ # L) Unsupported Transfer-Encoding (gzip, deflate) MUST return 501 Not Implemented
+ unsupported_te_http = (
+ b"POST /unsupported-te HTTP/1.1\r\n"
+ b"Host: 127.0.0.1:9096\r\n"
+ b"Transfer-Encoding: gzip, chunked\r\n"
+ b"Connection: close\r\n"
+ b"\r\n"
+ b"0\r\n\r\n"
+ )
+ reader9, writer9 = await asyncio.open_connection("127.0.0.1", 9096)
+ writer9.write(unsupported_te_http)
+ await writer9.drain()
+ resp9 = await reader9.read(1024)
+ writer9.close()
+ await writer9.wait_closed()
+ assert b"501 Not Implemented" in resp9, f"Expected 501 for unsupported Transfer-Encoding, got: {resp9[:100]}"
+ await asyncio.sleep(0.1)
+ assert len(received_hooks) == 2, "VULNERABILITY: Unsupported Transfer-Encoding was accepted into captured_hooks!"
+
finally:
await ws.stop()
@@ -436,7 +520,7 @@ async def test_tui_headers_and_gui_state():
async def main():
print("\n" + "=" * 60)
- print("RUNNING GHOSTMAN AUDIT VULNERABILITY VERIFICATION (v0.1.6)")
+ print("RUNNING GHOSTMAN AUDIT VULNERABILITY VERIFICATION (v0.1.7)")
print("=" * 60)
await test_tls_verification_default_and_toggle()
diff --git a/webhook_server.py b/webhook_server.py
index d88bf3f..dd98d2c 100644
--- a/webhook_server.py
+++ b/webhook_server.py
@@ -91,7 +91,7 @@ class WebhookServer:
headers = {}
content_length = 0
- transfer_encoding = ""
+ transfer_encodings = []
while True:
try:
header_line = await asyncio.wait_for(reader.readline(), timeout=self.read_timeout)
@@ -114,11 +114,37 @@ class WebhookServer:
except ValueError:
content_length = 0
elif lower_k == "transfer-encoding":
- transfer_encoding = v_clean.lower()
+ # Multiple transfer encodings can be comma-separated or repeated
+ parts = [p.strip().lower() for p in v_clean.split(",") if p.strip()]
+ transfer_encodings.extend(parts)
+
+ # Validate Transfer-Encoding (RFC 9112 §6.1, §6.3)
+ if transfer_encodings:
+ unsupported = [enc for enc in transfer_encodings if enc != "chunked"]
+ if unsupported:
+ # RFC 9112 §6.1: Server MUST respond with 501 (Not Implemented) for unsupported codings
+ await self._send_error_response(
+ writer,
+ b"501 Not Implemented",
+ f"Unsupported transfer coding: {', '.join(unsupported)}"
+ )
+ return
+
+ if transfer_encodings[-1] != "chunked":
+ await self._send_error_response(
+ writer,
+ b"400 Bad Request",
+ "Chunked transfer coding must be the final encoding"
+ )
+ return
+
+ is_chunked = True
+ else:
+ is_chunked = False
body_bytes = b""
# Support Chunked Transfer Encoding (RFC 7230 / RFC 9112) with resource limits
- if "chunked" in transfer_encoding:
+ if is_chunked:
# Memory bounded accumulation using bytearray instead of unbounded Python list of bytes objects
body_buffer = bytearray()
chunk_count = 0
@@ -145,6 +171,11 @@ class WebhookServer:
await self._send_error_response(writer, b"400 Bad Request", "Incomplete chunked stream before terminator")
return
+ # RFC 9112 §7.1: Chunk size line MUST end with CRLF
+ if not size_line.endswith(b"\r\n"):
+ await self._send_error_response(writer, b"400 Bad Request", "Malformed chunk size delimiter (expected CRLF)")
+ return
+
# Strip chunk extension if present (e.g., "1a;ext=val")
size_str = size_line.decode("utf-8", errors="ignore").split(";")[0].strip()
if not size_str:
@@ -162,16 +193,42 @@ class WebhookServer:
return
if chunk_size == 0:
+ # RFC 9112 §7.1.2: trailer-section CRLF
# Consume trailer headers until empty CRLF line
while True:
try:
trailer = await asyncio.wait_for(reader.readline(), timeout=self.read_timeout)
- except (asyncio.TimeoutError, asyncio.IncompleteReadError):
- break
- if not trailer or trailer in (b"\r\n", b"\n"):
+ except asyncio.TimeoutError:
+ await self._send_error_response(writer, b"408 Request Timeout", "Timeout reading trailer")
+ return
+ except asyncio.IncompleteReadError:
+ await self._send_error_response(writer, b"400 Bad Request", "Incomplete trailer section")
+ return
+
+ if not trailer:
+ # Unexpected EOF before terminating empty line
+ await self._send_error_response(writer, b"400 Bad Request", "Premature EOF in trailer section")
+ return
+
+ if trailer == b"\r\n":
+ # Clean RFC-compliant termination of chunked stream
+ has_terminated = True
break
- has_terminated = True
- break
+ elif trailer == b"\n":
+ # Disallow bare LF per RFC 9112
+ await self._send_error_response(writer, b"400 Bad Request", "Malformed trailer delimiter (bare LF)")
+ return
+ else:
+ # Trailer header line (must end with CRLF)
+ if not trailer.endswith(b"\r\n"):
+ await self._send_error_response(writer, b"400 Bad Request", "Malformed trailer line (missing CRLF)")
+ return
+
+ if has_terminated:
+ break
+ else:
+ await self._send_error_response(writer, b"400 Bad Request", "Chunked stream did not terminate cleanly")
+ return
# Guard against total payload size limit
if len(body_buffer) + chunk_size > self.max_payload_size:
@@ -190,14 +247,14 @@ class WebhookServer:
body_buffer.extend(chunk_data)
- # Verify and consume trailing CRLF after chunk data
+ # Verify and consume trailing CRLF after chunk data (RFC 9112 §7.1: MUST be CRLF, not bare LF)
try:
crlf = await asyncio.wait_for(reader.readline(), timeout=self.read_timeout)
except (asyncio.IncompleteReadError, asyncio.TimeoutError):
await self._send_error_response(writer, b"400 Bad Request", "Missing chunk trailing CRLF")
return
- if crlf not in (b"\r\n", b"\n"):
+ if crlf != b"\r\n":
await self._send_error_response(writer, b"400 Bad Request", "Malformed chunk trailing delimiter (expected CRLF)")
return