A fast, keyboard-driven TUI API client & webhook inspector. No cloud, no Electron, 100% local-first.
fix: chunked webhook resource bounds, strict 400 validation, and dev test dependencies (v0.1.6)
4yk5899dafb72948
diff --git a/.gitignore b/.gitignoreindex 9974c55..ce57318 100644--- a/.gitignore+++ b/.gitignore@@ -12,6 +12,11 @@ __pycache__/venv/env/+# Distribution / packaging+dist/+build/+*.egg-info/+# Editor & OS files.vscode/.idea/diff --git a/network.py b/network.pyindex 2dd1dbc..03baa9a 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.5"+ headers["User-Agent"] = "Ghostman/0.1.6"if body and not any(k.lower() == "content-type" for k in headers):try:diff --git a/pyproject.toml b/pyproject.tomlindex 27b7d36..f20643e 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"[project]name = "ghostman"-version = "0.1.5"+version = "0.1.6"description = "A fast, keyboard-driven terminal API client and webhook inspector"readme = "README.md"authors = [{ name = "4yk5" }]@@ -25,6 +25,11 @@ dependencies = ["rich>=14.0.0",]+[project.optional-dependencies]+dev = [+ "cryptography>=44.0.0",+]+[project.urls]Homepage = "https://github.com/4yk5/Ghostman"Repository = "https://github.com/4yk5/Ghostman"diff --git a/tests/test_vulnerabilities_fixed.py b/tests/test_vulnerabilities_fixed.pyindex 726d5da..831af62 100644--- a/tests/test_vulnerabilities_fixed.py+++ b/tests/test_vulnerabilities_fixed.py@@ -8,11 +8,16 @@ import datetimeimport ipaddressimport httpx-from cryptography import x509-from cryptography.x509.oid import NameOID-from cryptography.hazmat.primitives import hashes-from cryptography.hazmat.primitives.asymmetric import rsa-from cryptography.hazmat.primitives import serialization+try:+ from cryptography import x509+ from cryptography.x509.oid import NameOID+ from cryptography.hazmat.primitives import hashes+ from cryptography.hazmat.primitives.asymmetric import rsa+ from cryptography.hazmat.primitives import serialization+except ImportError:+ print("[!] Error: 'cryptography' is required to run test_vulnerabilities_fixed.py.")+ print(" Install test dependencies with: pip install -e .[dev]")+ sys.exit(1)sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))@@ -276,6 +281,109 @@ async def test_chunked_transfer_encoding_webhook():await writer3.wait_closed()assert b"413 Payload Too Large" in resp3, f"Expected 413, got: {resp3[:100]}"+ # D) Malformed chunk size (invalid hex) MUST return 400 Bad Request and NOT record hook+ malformed_size_http = (+ b"POST /malformed-size 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"ZZ_NOT_HEX\r\n"+ b"payload\r\n"+ b"0\r\n\r\n"+ )+ reader4, writer4 = await asyncio.open_connection("127.0.0.1", 9096)+ writer4.write(malformed_size_http)+ await writer4.drain()+ resp4 = await reader4.read(1024)+ writer4.close()+ await writer4.wait_closed()+ assert b"400 Bad Request" in resp4, f"Expected 400 Bad Request, got: {resp4[:100]}"+ await asyncio.sleep(0.1)+ assert len(received_hooks) == 2, "VULNERABILITY: Malformed chunk size was accepted into captured_hooks!"++ # E) Malformed trailing delimiter (missing/corrupted CRLF) MUST return 400 Bad Request+ malformed_crlf_http = (+ b"POST /bad-crlf 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"helloXXXX"+ b"0\r\n\r\n"+ )+ reader5, writer5 = await asyncio.open_connection("127.0.0.1", 9096)+ writer5.write(malformed_crlf_http)+ await writer5.drain()+ resp5 = await reader5.read(1024)+ writer5.close()+ await writer5.wait_closed()+ assert b"400 Bad Request" in resp5, f"Expected 400 Bad Request, got: {resp5[:100]}"+ await asyncio.sleep(0.1)+ assert len(received_hooks) == 2, "VULNERABILITY: Malformed chunk delimiter was accepted into captured_hooks!"++ # F) Premature EOF before terminal 0-size chunk MUST NOT record hook+ premature_eof_http = (+ b"POST /premature 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"+ )+ reader6, writer6 = await asyncio.open_connection("127.0.0.1", 9096)+ writer6.write(premature_eof_http)+ await writer6.drain()+ writer6.close()+ await writer6.wait_closed()+ await asyncio.sleep(0.1)+ assert len(received_hooks) == 2, "VULNERABILITY: Incomplete chunked stream was accepted into captured_hooks!"++ # G) Enforce max_chunks limit+ ws_bounded = WebhookServer(port=9097, max_chunks=3, total_timeout=5.0)+ await ws_bounded.start()+ try:+ excess_chunks_http = (+ b"POST /excess HTTP/1.1\r\n"+ b"Host: 127.0.0.1:9097\r\n"+ b"Transfer-Encoding: chunked\r\n"+ b"Connection: close\r\n"+ b"\r\n"+ b"1\r\na\r\n"+ b"1\r\nb\r\n"+ b"1\r\nc\r\n"+ b"1\r\nd\r\n"+ b"0\r\n\r\n"+ )+ reader_ex, writer_ex = await asyncio.open_connection("127.0.0.1", 9097)+ writer_ex.write(excess_chunks_http)+ await writer_ex.drain()+ resp_ex = await reader_ex.read(1024)+ writer_ex.close()+ await writer_ex.wait_closed()+ assert b"413 Payload Too Large" in resp_ex, f"Expected 413, got: {resp_ex[:100]}"+ assert len(ws_bounded.captured_hooks) == 0+ finally:+ await ws_bounded.stop()++ # H) Enforce total_timeout (absolute request deadline)+ ws_timeout = WebhookServer(port=9094, total_timeout=0.3, read_timeout=2.0)+ await ws_timeout.start()+ try:+ reader_to, writer_to = await asyncio.open_connection("127.0.0.1", 9094)+ writer_to.write(b"POST /slowloris HTTP/1.1\r\nHost: 127.0.0.1:9094\r\n")+ await writer_to.drain()+ await asyncio.sleep(0.4)+ resp_to = await reader_to.read(1024)+ writer_to.close()+ await writer_to.wait_closed()+ assert b"408 Request Timeout" in resp_to or len(resp_to) == 0+ assert len(ws_timeout.captured_hooks) == 0+ finally:+ await ws_timeout.stop()+finally:await ws.stop()@@ -328,7 +436,7 @@ async def test_tui_headers_and_gui_state():async def main():print("\n" + "=" * 60)- print("RUNNING GHOSTMAN AUDIT VULNERABILITY VERIFICATION (v0.1.5)")+ print("RUNNING GHOSTMAN AUDIT VULNERABILITY VERIFICATION (v0.1.6)")print("=" * 60)await test_tls_verification_default_and_toggle()diff --git a/webhook_server.py b/webhook_server.pyindex 92619bc..d88bf3f 100644--- a/webhook_server.py+++ b/webhook_server.py@@ -10,13 +10,17 @@ class WebhookServer:port: int = 9090,on_hook_received: Optional[Callable] = None,max_payload_size: int = 10 * 1024 * 1024,- read_timeout: float = 10.0+ max_chunks: int = 10000,+ read_timeout: float = 10.0,+ total_timeout: float = 30.0):self.host = hostself.port = portself.on_hook_received = on_hook_receivedself.max_payload_size = max_payload_size+ self.max_chunks = max_chunksself.read_timeout = read_timeout+ self.total_timeout = total_timeoutself.server: Optional[asyncio.AbstractServer] = Noneself.is_running = Falseself.captured_hooks = []@@ -34,119 +38,212 @@ class WebhookServer:self.server = Noneself.is_running = False+ async def _send_error_response(self, writer: asyncio.StreamWriter, status_line: bytes, message: str = ""):+ try:+ body = f'{{"error":"{message}"}}'.encode("utf-8") if message else b""+ content_len = len(body)+ resp = (+ b"HTTP/1.1 " + status_line + b"\r\n"+ b"Content-Type: application/json\r\n"+ b"Content-Length: " + str(content_len).encode("ascii") + b"\r\n"+ b"Connection: close\r\n\r\n" + body+ )+ writer.write(resp)+ await writer.drain()+ except Exception:+ pass+async def _handle_client(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):try:- line = await asyncio.wait_for(reader.readline(), timeout=self.read_timeout)- if not line:+ # Enforce an absolute total deadline for the entire request+ await asyncio.wait_for(+ self._process_request(reader, writer),+ timeout=self.total_timeout+ )+ except asyncio.TimeoutError:+ try:+ writer.write(b"HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n")+ await writer.drain()+ except Exception:+ pass+ except Exception:+ pass+ finally:+ try:writer.close()await writer.wait_closed()- return+ except Exception:+ pass++ async def _process_request(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):+ try:+ line = await asyncio.wait_for(reader.readline(), timeout=self.read_timeout)+ except (asyncio.TimeoutError, asyncio.IncompleteReadError):+ return- req_line = line.decode("utf-8", errors="ignore").strip()- parts = req_line.split(" ")- method = parts[0] if parts else "GET"- path = parts[1] if len(parts) > 1 else "/"+ if not line:+ return- headers = {}- content_length = 0- transfer_encoding = ""- while True:+ req_line = line.decode("utf-8", errors="ignore").strip()+ parts = req_line.split(" ")+ method = parts[0] if parts else "GET"+ path = parts[1] if len(parts) > 1 else "/"++ headers = {}+ content_length = 0+ transfer_encoding = ""+ while True:+ try:header_line = await asyncio.wait_for(reader.readline(), timeout=self.read_timeout)- if not header_line or header_line in (b"\r\n", b"\n"):- break- decoded_header = header_line.decode("utf-8", errors="ignore").strip()- if ":" in decoded_header:- k, v = decoded_header.split(":", 1)- k_clean = k.strip()- v_clean = v.strip()- headers[k_clean] = v_clean- lower_k = k_clean.lower()- if lower_k == "content-length":- try:- content_length = int(v_clean)- except ValueError:- content_length = 0- elif lower_k == "transfer-encoding":- transfer_encoding = v_clean.lower()-- body_bytes = b""- # Support Chunked Transfer Encoding (RFC 7230 / RFC 9112) with resource limits- if "chunked" in transfer_encoding:- chunks = []- total_size = 0- while True:- size_line = await asyncio.wait_for(reader.readline(), timeout=self.read_timeout)- if not size_line:- break- # Strip any chunk extensions (e.g., "1a;ext=val")- size_str = size_line.decode("utf-8", errors="ignore").split(";")[0].strip()- if not size_str:- continue+ except (asyncio.TimeoutError, asyncio.IncompleteReadError):+ await self._send_error_response(writer, b"400 Bad Request", "Connection interrupted during headers")+ return++ if not header_line or header_line in (b"\r\n", b"\n"):+ break+ decoded_header = header_line.decode("utf-8", errors="ignore").strip()+ if ":" in decoded_header:+ k, v = decoded_header.split(":", 1)+ k_clean = k.strip()+ v_clean = v.strip()+ headers[k_clean] = v_clean+ lower_k = k_clean.lower()+ if lower_k == "content-length":try:- chunk_size = int(size_str, 16)+ content_length = int(v_clean)except ValueError:- break- if chunk_size == 0:- # Consume trailer headers until empty CRLF line- while True:+ content_length = 0+ elif lower_k == "transfer-encoding":+ transfer_encoding = v_clean.lower()++ body_bytes = b""+ # Support Chunked Transfer Encoding (RFC 7230 / RFC 9112) with resource limits+ if "chunked" in transfer_encoding:+ # Memory bounded accumulation using bytearray instead of unbounded Python list of bytes objects+ body_buffer = bytearray()+ chunk_count = 0+ has_terminated = False++ while True:+ chunk_count += 1+ if chunk_count > self.max_chunks:+ await self._send_error_response(+ writer,+ b"413 Payload Too Large",+ f"Exceeded maximum chunk count limit ({self.max_chunks})"+ )+ return++ try:+ size_line = await asyncio.wait_for(reader.readline(), timeout=self.read_timeout)+ except (asyncio.TimeoutError, asyncio.IncompleteReadError):+ await self._send_error_response(writer, b"400 Bad Request", "Unexpected EOF or timeout reading chunk size")+ return++ if not size_line:+ # Premature EOF before terminal chunk+ await self._send_error_response(writer, b"400 Bad Request", "Incomplete chunked stream before terminator")+ 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:+ await self._send_error_response(writer, b"400 Bad Request", "Missing chunk size")+ return++ try:+ chunk_size = int(size_str, 16)+ except ValueError:+ await self._send_error_response(writer, b"400 Bad Request", f"Invalid hex chunk size: {size_str}")+ return++ if chunk_size < 0:+ await self._send_error_response(writer, b"400 Bad Request", "Negative chunk size")+ return++ if chunk_size == 0:+ # Consume trailer headers until empty CRLF line+ while True:+ try:trailer = await asyncio.wait_for(reader.readline(), timeout=self.read_timeout)- if not trailer or trailer in (b"\r\n", b"\n"):- break- break+ except (asyncio.TimeoutError, asyncio.IncompleteReadError):+ break+ if not trailer or trailer in (b"\r\n", b"\n"):+ break+ has_terminated = True+ break- # Guard against memory exhaustion- if total_size + chunk_size > self.max_payload_size:- writer.write(b"HTTP/1.1 413 Payload Too Large\r\nConnection: close\r\n\r\n")- await writer.drain()- return+ # Guard against total payload size limit+ if len(body_buffer) + chunk_size > self.max_payload_size:+ await self._send_error_response(+ writer,+ b"413 Payload Too Large",+ f"Payload exceeded maximum size of {self.max_payload_size} bytes"+ )+ return+ try:chunk_data = await asyncio.wait_for(reader.readexactly(chunk_size), timeout=self.read_timeout)- chunks.append(chunk_data)- total_size += chunk_size+ except (asyncio.IncompleteReadError, asyncio.TimeoutError):+ await self._send_error_response(writer, b"400 Bad Request", "Incomplete chunk data read")+ return++ body_buffer.extend(chunk_data)- # Verify and consume trailing CRLF after chunk data+ # Verify and consume trailing CRLF after chunk data+ try:crlf = await asyncio.wait_for(reader.readline(), timeout=self.read_timeout)- if crlf not in (b"\r\n", b"\n"):- break- body_bytes = b"".join(chunks)- elif content_length > 0:- if content_length > self.max_payload_size:- writer.write(b"HTTP/1.1 413 Payload Too Large\r\nConnection: close\r\n\r\n")- await writer.drain()+ except (asyncio.IncompleteReadError, asyncio.TimeoutError):+ await self._send_error_response(writer, b"400 Bad Request", "Missing chunk trailing CRLF")return- body_bytes = await asyncio.wait_for(reader.readexactly(content_length), timeout=self.read_timeout)- body_str = body_bytes.decode("utf-8", errors="ignore")+ if crlf not in (b"\r\n", b"\n"):+ await self._send_error_response(writer, b"400 Bad Request", "Malformed chunk trailing delimiter (expected CRLF)")+ return++ if not has_terminated:+ await self._send_error_response(writer, b"400 Bad Request", "Chunked stream did not terminate cleanly")+ return++ body_bytes = bytes(body_buffer)++ elif content_length > 0:+ if content_length > self.max_payload_size:+ await self._send_error_response(+ writer,+ b"413 Payload Too Large",+ f"Payload exceeded maximum size of {self.max_payload_size} bytes"+ )+ returntry:- formatted_body = json.dumps(json.loads(body_str), indent=2, ensure_ascii=False)- except Exception:- formatted_body = body_str-- hook_entry = {- "id": len(self.captured_hooks) + 1,- "timestamp": datetime.now().strftime("%H:%M:%S"),- "method": method,- "path": path,- "headers": headers,- "body": formatted_body,- "raw_body": body_str- }- self.captured_hooks.append(hook_entry)-- response = b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n{\"status\":\"ok\",\"source\":\"ghostman\"}"- writer.write(response)- await writer.drain()+ body_bytes = await asyncio.wait_for(reader.readexactly(content_length), timeout=self.read_timeout)+ except (asyncio.IncompleteReadError, asyncio.TimeoutError):+ await self._send_error_response(writer, b"400 Bad Request", "Incomplete body read")+ return- if self.on_hook_received:- if asyncio.iscoroutinefunction(self.on_hook_received):- await self.on_hook_received(hook_entry)- else:- self.on_hook_received(hook_entry)+ body_str = body_bytes.decode("utf-8", errors="ignore")+ try:+ formatted_body = json.dumps(json.loads(body_str), indent=2, ensure_ascii=False)except Exception:- pass- finally:- try:- writer.close()- await writer.wait_closed()- except Exception:- pass+ formatted_body = body_str++ hook_entry = {+ "id": len(self.captured_hooks) + 1,+ "timestamp": datetime.now().strftime("%H:%M:%S"),+ "method": method,+ "path": path,+ "headers": headers,+ "body": formatted_body,+ "raw_body": body_str+ }+ self.captured_hooks.append(hook_entry)++ response = b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n{\"status\":\"ok\",\"source\":\"ghostman\"}"+ writer.write(response)+ await writer.drain()++ if self.on_hook_received:+ if asyncio.iscoroutinefunction(self.on_hook_received):+ await self.on_hook_received(hook_entry)+ else:+ self.on_hook_received(hook_entry)