A fast, keyboard-driven TUI API client & webhook inspector. No cloud, no Electron, 100% local-first.
fix: ssl context for custom CA, webhook size/timeout limits, and local self-signed TLS test (v0.1.5)
4yk524ba41a686a6
diff --git a/network.py b/network.pyindex 2fc790d..2dd1dbc 100644--- a/network.py+++ b/network.py@@ -1,5 +1,6 @@import timeimport json+import sslfrom typing import Optional, Unionimport httpx@@ -22,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.3"+ headers["User-Agent"] = "Ghostman/0.1.5"if body and not any(k.lower() == "content-type" for k in headers):try:@@ -35,12 +36,15 @@ class NetworkEngine:headers["Content-Type"] = "text/plain"# Determine SSL verification mode (defaults to True for MITM protection)- if ca_bundle:- verify_target: Union[bool, str] = ca_bundle+ active_ca = ca_bundle or self.ca_bundle+ if active_ca:+ try:+ # Use ssl.create_default_context for HTTPX 0.28+ deprecation compliance+ verify_target: Union[bool, ssl.SSLContext] = ssl.create_default_context(cafile=active_ca)+ except Exception:+ verify_target = active_caelif verify_ssl is not None:verify_target = verify_ssl- elif self.ca_bundle:- verify_target = self.ca_bundleelse:verify_target = self.verify_ssldiff --git a/pyproject.toml b/pyproject.tomlindex a4cd9bd..27b7d36 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"[project]name = "ghostman"-version = "0.1.4"+version = "0.1.5"description = "A fast, keyboard-driven terminal API client and webhook inspector"readme = "README.md"authors = [{ name = "4yk5" }]diff --git a/tests/test_all.py b/tests/test_all.pyindex 7a0b082..7636c9d 100644--- a/tests/test_all.py+++ b/tests/test_all.py@@ -89,12 +89,12 @@ async def run_tests():test_tls_verification_default_and_toggle,test_saved_and_custom_headers_propagation,test_chunked_transfer_encoding_webhook,- test_tui_headers_and_ssl_controls,+ test_tui_headers_and_gui_state,)await test_tls_verification_default_and_toggle()await test_saved_and_custom_headers_propagation()await test_chunked_transfer_encoding_webhook()- await test_tui_headers_and_ssl_controls()+ await test_tui_headers_and_gui_state()print("\nAll tests passed successfully.")diff --git a/tests/test_vulnerabilities_fixed.py b/tests/test_vulnerabilities_fixed.pyindex ba56e72..726d5da 100644--- a/tests/test_vulnerabilities_fixed.py+++ b/tests/test_vulnerabilities_fixed.py@@ -2,8 +2,18 @@ import asyncioimport osimport sysimport json+import ssl+import tempfile+import datetime+import 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+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))if sys.platform == "win32":@@ -20,28 +30,97 @@ from storage import StorageManagerfrom textual.widgets import Input, TextArea, Select, ListView, Tabs, Label+def generate_self_signed_cert_files():+ """Generate ephemeral self-signed SSL cert and key for offline HTTPS testing."""+ key = rsa.generate_private_key(public_exponent=65537, key_size=2048)+ subject = issuer = x509.Name([+ x509.NameAttribute(NameOID.COMMON_NAME, "localhost"),+ ])+ cert = (+ x509.CertificateBuilder()+ .subject_name(subject)+ .issuer_name(issuer)+ .public_key(key.public_key())+ .serial_number(x509.random_serial_number())+ .not_valid_before(datetime.datetime.now(datetime.timezone.utc))+ .not_valid_after(datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=1))+ .add_extension(+ x509.SubjectAlternativeName([+ x509.DNSName("localhost"),+ x509.IPAddress(ipaddress.IPv4Address("127.0.0.1")),+ ]),+ critical=False,+ )+ .sign(key, hashes.SHA256())+ )++ cert_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pem")+ cert_file.write(cert.public_bytes(serialization.Encoding.PEM))+ cert_file.close()++ key_file = tempfile.NamedTemporaryFile(delete=False, suffix=".key")+ key_file.write(key.private_bytes(+ encoding=serialization.Encoding.PEM,+ format=serialization.PrivateFormat.TraditionalOpenSSL,+ encryption_algorithm=serialization.NoEncryption(),+ ))+ key_file.close()++ return cert_file.name, key_file.name++async def test_tls_verification_default_and_toggle():- print("[*] 1. Testing TLS Certificate Verification Default & Toggles...")+ print("[*] 1. Testing TLS Certificate Verification Default & Toggles (Local HTTPS)...")++ cert_path, key_path = generate_self_signed_cert_files()- # NetworkEngine must default to verify_ssl=True (no hardcoded verify=False)- net = NetworkEngine()- assert net.verify_ssl is True, "VULNERABILITY: NetworkEngine did not default to verify_ssl=True!"+ ssl_server_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)+ ssl_server_ctx.load_cert_chain(certfile=cert_path, keyfile=key_path)++ async def handle_https(reader, writer):+ await reader.read(1024)+ writer.write(b"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 2\r\nConnection: close\r\n\r\nOK")+ await writer.drain()+ writer.close()+ await writer.wait_closed()++ server = await asyncio.start_server(handle_https, "127.0.0.1", 9099, ssl=ssl_server_ctx)- # Testing self-signed or invalid SSL domain: must fail under verify_ssl=Truetry:- res = await net.execute_request("GET", "https://self-signed.badssl.com/")- assert not res["success"], "VULNERABILITY: Untrusted self-signed certificate was accepted under verify_ssl=True!"- assert "ssl" in res["body"].lower() or "certificate" in res["body"].lower() or "failed" in res["body"].lower()- except Exception as e:- # Rejection by httpx/ssl is expected and secure- pass+ net = NetworkEngine()+ assert net.verify_ssl is True, "VULNERABILITY: NetworkEngine did not default to verify_ssl=True!"++ # A) Standard request with verify_ssl=True MUST fail because self-signed cert is untrusted+ res = await net.execute_request("GET", "https://127.0.0.1:9099/")+ assert res["success"] is False, "VULNERABILITY: Self-signed certificate was accepted under verify_ssl=True!"+ error_text = (str(res.get("error", "")) + " " + str(res.get("body", ""))).lower()+ assert (+ "certificate_verify_failed" in error_text or+ "certificate verify failed" in error_text or+ "self-signed" in error_text or+ "verification failed" in error_text+ ), f"Expected SSL certificate verification error, got: {error_text}"++ # B) Explicitly insecure request with verify_ssl=False MUST succeed+ res_insecure = await net.execute_request("GET", "https://127.0.0.1:9099/", verify_ssl=False)+ assert res_insecure["success"] is True, f"Insecure request failed: {res_insecure.get('error')}"+ assert res_insecure["status_code"] == 200+ assert res_insecure["body"] == "OK"++ # C) Request with custom CA bundle pointing to the self-signed cert MUST succeed via ssl.SSLContext+ res_ca = await net.execute_request("GET", "https://127.0.0.1:9099/", verify_ssl=True, ca_bundle=cert_path)+ assert res_ca["success"] is True, f"Request with valid custom CA bundle failed: {res_ca.get('error')}"+ assert res_ca["status_code"] == 200+ assert res_ca["body"] == "OK"- # When verify_ssl=False is explicitly specified, it should bypass verification- res_insecure = await net.execute_request(- "GET", "https://self-signed.badssl.com/", verify_ssl=False- )- # The request should succeed or reach the server without TLS certificate rejection- assert "Certificate Verification Failed" not in res_insecure.get("body", "")+ finally:+ server.close()+ await server.wait_closed()+ for p in (cert_path, key_path):+ try:+ os.remove(p)+ except Exception:+ passprint(" [+] TLS Certificate Verification: PASS")@@ -49,7 +128,6 @@ async def test_tls_verification_default_and_toggle():async def test_saved_and_custom_headers_propagation():print("[*] 2. Testing Custom & Saved HTTP Headers Propagation...")- # Start local WebhookServer to inspect actual received headersreceived_hooks = []ws = WebhookServer(port=9095, on_hook_received=lambda h: received_hooks.append(h))await ws.start()@@ -74,7 +152,6 @@ async def test_saved_and_custom_headers_propagation():assert len(received_hooks) == 1, "WebhookServer did not capture the request"captured_headers = received_hooks[0]["headers"]- # Case-insensitive header lookuplower_headers = {k.lower(): v for k, v in captured_headers.items()}assert lower_headers.get("authorization") == "Bearer gh_secret_token_12345", (@@ -87,20 +164,17 @@ async def test_saved_and_custom_headers_propagation():"VULNERABILITY: Accept header was not sent!")- # Header parsing and formatting utilitiesheader_text = "Authorization: Bearer secret\nContent-Type: application/xml\nUser-Agent: Custom"parsed = GhostmanApp.parse_headers_text(header_text)assert parsed["Authorization"] == "Bearer secret"assert parsed["Content-Type"] == "application/xml"assert parsed["User-Agent"] == "Custom"- # JSON header input supportjson_header_text = '{"Authorization": "Bearer token", "X-Key": "val"}'parsed_json = GhostmanApp.parse_headers_text(json_header_text)assert parsed_json["Authorization"] == "Bearer token"assert parsed_json["X-Key"] == "val"- # Roundtrip formatformatted = GhostmanApp.format_headers_text(parsed)re_parsed = GhostmanApp.parse_headers_text(formatted)assert re_parsed == parsed@@ -112,14 +186,20 @@ async def test_saved_and_custom_headers_propagation():async def test_chunked_transfer_encoding_webhook():- print("[*] 3. Testing Chunked Transfer Encoding Webhook Reception...")+ print("[*] 3. Testing Chunked Transfer Encoding Webhook Reception & Resource Limits...")received_hooks = []- ws = WebhookServer(port=9096, on_hook_received=lambda h: received_hooks.append(h))+ # Set small max_payload_size to test 413 guard+ ws = WebhookServer(+ port=9096,+ on_hook_received=lambda h: received_hooks.append(h),+ max_payload_size=1024,+ read_timeout=5.0+ )await ws.start()try:- # 1. Standard RFC 7230 Chunked Body+ # A) Standard RFC 7230 Chunked Bodyraw_http = (b"POST /chunked-endpoint HTTP/1.1\r\n"b"Host: 127.0.0.1:9096\r\n"@@ -150,7 +230,7 @@ async def test_chunked_transfer_encoding_webhook():f"VULNERABILITY: Chunked payload was not properly reconstructed! Got: {received_hooks[0]['raw_body']}")- # 2. Chunked JSON with Chunk Extensions (e.g., "11;ext=foo")+ # B) Chunked JSON with Chunk Extensionsjson_chunked = (b"POST /chunked-json HTTP/1.1\r\n"b"Host: 127.0.0.1:9096\r\n"@@ -180,13 +260,29 @@ async def test_chunked_transfer_encoding_webhook():f"VULNERABILITY: Chunked JSON payload failed! Got: {received_hooks[1]['raw_body']}")+ # C) Payload exceeding max_payload_size must receive 413 Payload Too Large+ oversized_http = (+ b"POST /oversized HTTP/1.1\r\n"+ b"Host: 127.0.0.1:9096\r\n"+ b"Content-Length: 5000\r\n"+ b"Connection: close\r\n"+ b"\r\n" + (b"X" * 5000)+ )+ reader3, writer3 = await asyncio.open_connection("127.0.0.1", 9096)+ writer3.write(oversized_http)+ await writer3.drain()+ resp3 = await reader3.read(1024)+ writer3.close()+ await writer3.wait_closed()+ assert b"413 Payload Too Large" in resp3, f"Expected 413, got: {resp3[:100]}"+finally:await ws.stop()- print(" [+] Chunked Transfer Encoding: PASS")+ print(" [+] Chunked Transfer Encoding & Limits: PASS")-async def test_tui_headers_and_ssl_controls():+async def test_tui_headers_and_gui_state():print("[*] 4. Testing TUI Headers Tab & Integration...")app = GhostmanApp()@@ -232,16 +328,16 @@ async def test_tui_headers_and_ssl_controls():async def main():print("\n" + "=" * 60)- print("RUNNING GHOSTMAN AUDIT VULNERABILITY VERIFICATION (v0.1.3)")+ print("RUNNING GHOSTMAN AUDIT VULNERABILITY VERIFICATION (v0.1.5)")print("=" * 60)await test_tls_verification_default_and_toggle()await test_saved_and_custom_headers_propagation()await test_chunked_transfer_encoding_webhook()- await test_tui_headers_and_ssl_controls()+ await test_tui_headers_and_gui_state()print("\n" + "=" * 60)- print("ALL 3 AUDIT ISSUES RESOLVED AND VERIFIED 100% PASSING!")+ print("ALL AUDIT ISSUES RESOLVED AND VERIFIED 100% PASSING!")print("=" * 60 + "\n")diff --git a/webhook_server.py b/webhook_server.pyindex 6b5e678..92619bc 100644--- a/webhook_server.py+++ b/webhook_server.py@@ -4,10 +4,19 @@ from datetime import datetimefrom typing import Callable, Optionalclass WebhookServer:- def __init__(self, host: str = "127.0.0.1", port: int = 9090, on_hook_received: Optional[Callable] = None):+ def __init__(+ self,+ host: str = "127.0.0.1",+ port: int = 9090,+ on_hook_received: Optional[Callable] = None,+ max_payload_size: int = 10 * 1024 * 1024,+ read_timeout: float = 10.0+ ):self.host = hostself.port = portself.on_hook_received = on_hook_received+ self.max_payload_size = max_payload_size+ self.read_timeout = read_timeoutself.server: Optional[asyncio.AbstractServer] = Noneself.is_running = Falseself.captured_hooks = []@@ -27,7 +36,7 @@ class WebhookServer:async def _handle_client(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):try:- line = await reader.readline()+ line = await asyncio.wait_for(reader.readline(), timeout=self.read_timeout)if not line:writer.close()await writer.wait_closed()@@ -42,7 +51,7 @@ class WebhookServer:content_length = 0transfer_encoding = ""while True:- header_line = await reader.readline()+ 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"):breakdecoded_header = header_line.decode("utf-8", errors="ignore").strip()@@ -61,11 +70,12 @@ class WebhookServer:transfer_encoding = v_clean.lower()body_bytes = b""- # Support Chunked Transfer Encoding (RFC 7230 / RFC 9112)+ # Support Chunked Transfer Encoding (RFC 7230 / RFC 9112) with resource limitsif "chunked" in transfer_encoding:chunks = []+ total_size = 0while True:- size_line = await reader.readline()+ 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")@@ -79,19 +89,32 @@ class WebhookServer:if chunk_size == 0:# Consume trailer headers until empty CRLF linewhile True:- trailer = await reader.readline()+ trailer = await asyncio.wait_for(reader.readline(), timeout=self.read_timeout)if not trailer or trailer in (b"\r\n", b"\n"):breakbreak- chunk_data = await reader.readexactly(chunk_size)+ # 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++ chunk_data = await asyncio.wait_for(reader.readexactly(chunk_size), timeout=self.read_timeout)chunks.append(chunk_data)+ total_size += chunk_size- # Consume trailing CRLF after chunk data- await reader.readline()+ # Verify and consume trailing CRLF after chunk data+ crlf = await asyncio.wait_for(reader.readline(), timeout=self.read_timeout)+ if crlf not in (b"\r\n", b"\n"):+ breakbody_bytes = b"".join(chunks)elif content_length > 0:- body_bytes = await reader.readexactly(content_length)+ 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()+ return+ body_bytes = await asyncio.wait_for(reader.readexactly(content_length), timeout=self.read_timeout)body_str = body_bytes.decode("utf-8", errors="ignore")try: