Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
fix(api): refuse ambiguous HTTP request framing
Rudy Celeklid341994a8cb9
diff --git a/c/openai_server.py b/c/openai_server.pyindex 2838036f..f9cf053a 100644--- a/c/openai_server.py+++ b/c/openai_server.py@@ -5969,6 +5969,15 @@ class APIServer(ThreadingHTTPServer):super().close_request(request)+def _content_length(value):+ # int() also accepts signs and underscores, but HTTP lengths are 1*DIGIT.+ # Read and early-error drain must agree on that exact framing grammar.+ value = value.strip()+ if re.fullmatch(r"[0-9]+", value) is None:+ raise ValueError("invalid Content-Length")+ return int(value)++class _DeadlineReader:"""rfile wrapper enforcing a CUMULATIVE deadline on reading one request.@@ -6107,11 +6116,12 @@ class APIHandler(BaseHTTPRequestHandler):if self._body_read:returnself._body_read = True- if self.headers.get("Transfer-Encoding"):+ if (self.headers.get_all("Transfer-Encoding")+ or len(self.headers.get_all("Content-Length", [])) > 1):self.close_connection = True # not framed by Content-Length; we don't de-chunkreturntry:- remaining = int(self.headers.get("Content-Length", "0"))+ remaining = _content_length(self.headers.get("Content-Length", "0"))except ValueError:self.close_connection = True # unparseable framing: the body length is unknownreturn@@ -6209,9 +6219,20 @@ class APIHandler(BaseHTTPRequestHandler):None, "forbidden")def read_json(self):+ # No transfer decoder lives here: accepting TE+CL would choose CL even+ # though HTTP gives TE precedence. Multiple CL fields are likewise not+ # a boundary we should guess. Refuse before engine work and never reuse+ # the connection after an ambiguous frame (RFC 9112 section 6.3).+ if self.headers.get_all("Transfer-Encoding"):+ self.close_connection = True+ raise APIError(400, "Transfer-Encoding is not supported; send one Content-Length.")+ if len(self.headers.get_all("Content-Length", [])) > 1:+ self.close_connection = True+ raise APIError(400, "Multiple Content-Length headers are not supported.")try:- length = int(self.headers.get("Content-Length", "0"))+ length = _content_length(self.headers.get("Content-Length", "0"))except ValueError:+ self.close_connection = Trueraise APIError(400, "Invalid Content-Length header.")if length < 1 or length > MAX_BODY:raise APIError(400, f"Request body must be between 1 and {MAX_BODY} bytes.")diff --git a/c/tests/test_http_request_framing.py b/c/tests/test_http_request_framing.pynew file mode 100644index 00000000..a6fd0fb4--- /dev/null+++ b/c/tests/test_http_request_framing.py@@ -0,0 +1,100 @@+"""The gateway must not execute a request with an ambiguous HTTP body boundary."""+import json+import socket+import threading+import unittest++from openai_server import APIServer+from test_openai_server import ScriptedEngine+++class RequestFramingTest(unittest.TestCase):+ def setUp(self):+ self.engine = ScriptedEngine()+ self.server = APIServer(("127.0.0.1", 0), self.engine, "test-model", "secret", 16)+ self.thread = threading.Thread(target=self.server.serve_forever,+ args=(0.01,), daemon=True)+ self.thread.start()+ self.addCleanup(self.thread.join, timeout=2)+ self.addCleanup(self.server.server_close)+ self.addCleanup(self.server.shutdown)+ self.addCleanup(self.server.scheduler.close)+ self.payload = json.dumps({"model": "test-model", "prompt": "hello"}).encode()++ def exchange(self, headers, *, authenticated=True, followup=False):+ auth = b"Authorization: Bearer secret\r\n" if authenticated else b""+ wire = (b"POST /v1/completions HTTP/1.1\r\nHost: localhost\r\n" + auth+ + headers + b"\r\n" + self.payload)+ if followup:+ wire += b"GET /health HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"+ with socket.create_connection(self.server.server_address, timeout=2) as client:+ client.sendall(wire)+ response = bytearray()+ while True:+ part = client.recv(65536)+ if not part:+ return bytes(response)+ response.extend(part)++ def test_transfer_encoding_is_refused_before_generation(self):+ response = self.exchange(+ f"Content-Length: {len(self.payload)}\r\n".encode()+ + b"Transfer-Encoding: chunked\r\n", followup=True)+ self.assertTrue(response.startswith(b"HTTP/1.1 400"), response)+ self.assertEqual(response.count(b"HTTP/1.1 "), 1, response)+ self.assertEqual(self.engine.calls, [])++ def test_identical_duplicate_lengths_are_refused_consistently(self):+ response = self.exchange(+ (f"Content-Length: {len(self.payload)}\r\n" * 2).encode(), followup=True)+ self.assertTrue(response.startswith(b"HTTP/1.1 400"), response)+ self.assertEqual(response.count(b"HTTP/1.1 "), 1, response)++ def test_conflicting_lengths_are_refused_and_connection_closed(self):+ response = self.exchange(+ f"Content-Length: {len(self.payload)}\r\nContent-Length: 1\r\n".encode(),+ followup=True)+ self.assertTrue(response.startswith(b"HTTP/1.1 400"), response)+ self.assertEqual(response.count(b"HTTP/1.1 "), 1, response)+ self.assertEqual(self.engine.calls, [])++ def test_early_auth_error_does_not_reuse_conflicting_framing(self):+ response = self.exchange(+ f"Content-Length: {len(self.payload)}\r\nContent-Length: 1\r\n".encode(),+ authenticated=False, followup=True)+ self.assertTrue(response.startswith(b"HTTP/1.1 401"), response)+ self.assertEqual(response.count(b"HTTP/1.1 "), 1, response)++ def test_normal_body_still_generates(self):+ response = self.exchange(+ f"Content-Length: {len(self.payload)}\r\nConnection: close\r\n".encode())+ self.assertTrue(response.startswith(b"HTTP/1.1 200"), response)+ self.assertEqual(len(self.engine.calls), 1)++ def test_non_decimal_lengths_are_refused(self):+ length = str(len(self.payload))+ for value in ("+" + length, "_".join(length)):+ with self.subTest(value=value):+ response = self.exchange(f"Content-Length: {value}\r\n".encode(), followup=True)+ self.assertTrue(response.startswith(b"HTTP/1.1 400"), response)+ self.assertEqual(response.count(b"HTTP/1.1 "), 1, response)+ self.assertEqual(self.engine.calls, [])++ def test_early_auth_error_closes_non_decimal_lengths(self):+ length = str(len(self.payload))+ for value in ("+" + length, "_".join(length)):+ with self.subTest(value=value):+ response = self.exchange(f"Content-Length: {value}\r\n".encode(),+ authenticated=False, followup=True)+ self.assertTrue(response.startswith(b"HTTP/1.1 401"), response)+ self.assertEqual(response.count(b"HTTP/1.1 "), 1, response)++ def test_decimal_leading_zeroes_still_generate(self):+ response = self.exchange(+ f"Content-Length: 000{len(self.payload)}\r\nConnection: close\r\n".encode())+ self.assertTrue(response.startswith(b"HTTP/1.1 200"), response)+ self.assertEqual(len(self.engine.calls), 1)+++if __name__ == "__main__":+ unittest.main()