Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion serve/mlx_omarchy_laya/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,11 @@ def do_POST(self):
self._send_json(400, {"error": "body must be 1..%d bytes" % MAX_BODY_BYTES})
return
raw = self.rfile.read(length)
# ValueError also covers invalid UTF-8 and over-long integer
# literals; RecursionError is nesting too deep for the decoder
try:
req = json.loads(raw)
except json.JSONDecodeError as exc:
except (ValueError, RecursionError) as exc:
self._send_json(400, {"error": "invalid JSON: %s" % exc})
return
if not isinstance(req, dict):
Expand Down
14 changes: 13 additions & 1 deletion tests/test_laya_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def tearDownClass(cls):
def _post(self, body):
conn = http.client.HTTPConnection(*self.httpd.server_address, timeout=10)
try:
conn.request("POST", "/v1/decisions", json.dumps(body))
conn.request("POST", "/v1/decisions", body if isinstance(body, bytes) else json.dumps(body))
r = conn.getresponse()
return r.status, json.loads(r.read())
finally:
Expand All @@ -226,6 +226,18 @@ def test_non_object_body_is_400(self):
with self.subTest(body=body):
self.assertEqual(self._post(body), (400, {"error": "request body must be a JSON object"}))

def test_undecodable_body_is_400(self):
cases = [
("invalid UTF-8", b'{"state": "\xff"}'),
("deep nesting", b"[" * 200000),
("over-long integer", b'{"state": ' + b"1" * 5000 + b"}"),
]
for name, raw in cases:
with self.subTest(name):
code, out = self._post(raw)
self.assertEqual(code, 400)
self.assertTrue(out["error"].startswith("invalid JSON: "), out)

def test_unrenderable_criteria_is_400(self):
cases = [
("choice", {}),
Expand Down
Loading