Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
Reject incomplete HTTP request body framing
Hulkd467b3d2bb1d
diff --git a/serve/server.py b/serve/server.pyindex f29eb3ff7..341792a8d 100644--- a/serve/server.py+++ b/serve/server.py@@ -4197,7 +4197,7 @@ def body_limit() -> int:class BadBody(Exception):- """A request body that cannot be read (a malformed or oversized chunked body): the status and the sentence."""+ """A request body that cannot be read (malformed, incomplete or oversized): the status and the sentence."""def __init__(self, status: int, message: str):super().__init__(message)@@ -4252,10 +4252,13 @@ def make_handler(svc: Service):if size < 0:raise BadBody(400, "malformed chunk size in the request body")if size == 0:- for _ in range(64): # the trailers, up to the blank line- if self.rfile.readline(8193).strip() == b"":- break- return b"".join(parts)+ for _ in range(64): # the trailers, including the final blank line+ trailer = self.rfile.readline(8193)+ if trailer == b"\r\n":+ return b"".join(parts)+ if not trailer.endswith(b"\r\n"):+ raise BadBody(400, "malformed or incomplete chunked request trailers")+ raise BadBody(400, "too many chunked request trailers")total += sizeif total > limit:self.close_connection = True@@ -4286,7 +4289,10 @@ def make_handler(svc: Service):self.close_connection = True # the body is not read: the connection ends with the answerraise BadBody(413, f"the request body is larger than {limit >> 20} MiB ""(STRATA_MAX_BODY_MIB raises the limit)")- return self.rfile.read(length)+ body = self.rfile.read(length)+ if len(body) != length:+ raise BadBody(400, "the request body ended early")+ return bodydef _drain_body(self):"""An answer sent before the body was read (a 401, a 403, /load, a method with no handler) must not close@@ -4597,7 +4603,7 @@ def make_handler(svc: Service):def do_POST(self):try:self._post()- except BadBody as e: # #893: a malformed or oversized chunked body+ except BadBody as e: # a malformed, incomplete or oversized bodyself._json(e.status, {"error": {"type": "invalid_request_error", "message": str(e)}})def _post(self):diff --git a/serve/test_chunked_body.py b/serve/test_chunked_body.pyindex 45428a949..7fc25e442 100644--- a/serve/test_chunked_body.py+++ b/serve/test_chunked_body.py@@ -1,5 +1,6 @@"""#893 / #894: a request body sent as Transfer-Encoding: chunked (a relay or proxy) is decoded, on every route that-reads a body; a malformed or oversized one is a 400 / 413, not an empty body.+reads a body; a malformed or oversized one is a 400 / 413, not an empty body. Incomplete framing must not apply an+otherwise valid JSON body, for either Content-Length or chunked requests.python -m unittest serve.test_chunked_body -v"""@@ -100,6 +101,52 @@ class ChunkedBody(unittest.TestCase):status, got = self.send(head, chunked(body))self.assertEqual(status, 200, got)+ def assert_settings_body_rejected(self, extra: bytes, body: bytes):+ previous = dict(self.svc.shared)+ self.addCleanup(self.svc.set_shared, previous)+ before = self.svc.set_shared({"temperature": 0.7})+ head = b"/settings HTTP/1.1" + CRLF + b"Content-Type: application/json" + CRLF + extra+ status, got = self.send(head, body)+ self.assertEqual((status, self.svc.shared), (400, before), got)++ def test_a_short_content_length_body_does_not_change_settings(self):+ body = json.dumps({"defaults": {"temperature": 0.5}}).encode()+ self.assert_settings_body_rejected(b"Content-Length: %d" % (len(body) + 1) + CRLF, body)++ def test_incomplete_chunked_trailers_do_not_change_settings(self):+ body = json.dumps({"defaults": {"temperature": 0.5}}).encode()+ prefix = chunked(body)[:-len(CRLF)] # the zero chunk, without the terminating blank line+ for tail in (b"", b"X-Trailer: 1", b"X-Trailer: 1" + CRLF, b"\r", b"\n", b" " + CRLF):+ with self.subTest(tail=tail):+ self.assert_settings_body_rejected(b"Transfer-Encoding: chunked" + CRLF, prefix + tail)++ def test_chunked_trailer_limit_does_not_change_settings(self):+ body = json.dumps({"defaults": {"temperature": 0.5}}).encode()+ prefix = chunked(body)[:-len(CRLF)]+ for end in (b"", CRLF):+ with self.subTest(end=end):+ trailers = (b"X-Trailer: 1" + CRLF) * 64 + end+ self.assert_settings_body_rejected(b"Transfer-Encoding: chunked" + CRLF, prefix + trailers)++ def test_an_overlong_chunked_trailer_does_not_change_settings(self):+ body = json.dumps({"defaults": {"temperature": 0.5}}).encode()+ trailers = b"X-Trailer: " + b"x" * 8193 + CRLF+ self.assert_settings_body_rejected(b"Transfer-Encoding: chunked" + CRLF, chunked(body, trailers=trailers))++ def test_complete_settings_bodies_still_apply(self):+ previous = dict(self.svc.shared)+ self.addCleanup(self.svc.set_shared, previous)+ body = json.dumps({"defaults": {"temperature": 0.5}}).encode()+ for extra, framed in ((b"Content-Length: %d" % len(body) + CRLF, body),+ (b"Transfer-Encoding: chunked" + CRLF, chunked(body)),+ (b"Transfer-Encoding: chunked" + CRLF,+ chunked(body, trailers=(b"X-Trailer: 1" + CRLF) * 63))):+ with self.subTest(extra=extra, size=len(framed)):+ self.svc.set_shared({"temperature": 0.7})+ head = b"/settings HTTP/1.1" + CRLF + b"Content-Type: application/json" + CRLF + extra+ status, got = self.send(head, framed)+ self.assertEqual((status, self.svc.shared), (200, {"temperature": 0.5}), got)+def test_a_chunked_control_body_is_consumed(self):head = (b"/unload HTTP/1.1" + CRLF + b"Content-Type: application/json" + CRLF +b"Transfer-Encoding: chunked" + CRLF)