Repository navigation
fix(mlx_tf): refuse mis-shaped request bodies instead of panicking - #6510
Conversation
|
READY to land on this head. mlx_tf's completion and stream handlers now answer a mis-shaped request body with 400 invalid_request_error instead of panicking on a union-field access. Well-formed requests take the same path as before, so tokens are unchanged; the new request-shape host test covers each refused shape.
|
e339489 to
ba54ab2
Compare
|
Rebased onto main after #6505 landed (shared build.zig hoist + test-step hunks). test-request-shape-host and test-config-parse-host both green host-side. New head |
|
READY to land on this head. Same change as the head we signed off, rebased onto today's main: the only difference is the test-filter option, which main now declares once itself. Host-only; the request-shape tests and config-parse tests are wired into the test step.
|
Both HTTP handlers dereferenced parsed JSON unchecked — a non-object body, a non-array messages/streams field, a non-object item, a missing or non-string role/id, or a top_k wider than u32 all panicked the server where vulkan_tf's request layer returns 400s. server_qwen4.zig already gates its own route the same way; this adds the equivalent shape gate (server_request_shape.zig, pure and host-testable) in front of the completion and stream-parallel paths, keeps presence errors in the handlers, and clamps top_k to u32. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
ba54ab2 to
6cf542d
Compare
|
Thank you @joshuaswarren. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes. |
Fixes #6509
Sibling of #6508, on the remote-request boundary rather than the local
config.jsonboundary. Both HTTP handlers inengines/mlx_tfdereference parsed request JSON unchecked — wherevulkan_tf's request layer (server_request.zig+ its malformed-request tests) already returns400 invalid_request_error, and whereserver_qwen4.zigalready gates its own route the same way.Panic classes verified on the merge base
Repro executing the handlers' verbatim dereference lines — 17 mis-shaped bodies, all panic on
93f187b13:parsed.value.objecton a non-object body (server_completion.zig:25,server_stream.zig:21)msgs_val.array.itemson"messages":"x";item.objecton[42](server_completion.zig:70–134)mo.get("role").?.stringon missing/non-string roles (server_completion.zig:95/134)prompt.?.string-style access on"prompt":7(server_completion.zig:201)@intCast(getJsonUsize(obj,"top_k",…))on5000000000(server_completion.zig:61)streams_val.array, per-streamitem.object,idv.string,dm.array,mi.object,dp.string,sm.array,sp.string(server_stream.zig:29–105)What changes
src/server_request_shape.zig— no engine imports, host-testable.checkCompletion(obj, is_chat)andcheckStreamParallel(obj)validate only the invariants the consumers dereference;getJsonU32clamps u32-wider integers (vulkan parity).400 invalid JSON body; non-object body →400 JSON body must be an object; gate refusal →400 <reason>via the existingsendError.top_kreads switch togetJsonU32.seedstays@intCast(getJsonUsize)— its field isu64, so the cast cannot overflow.messages/prompt/streamsstill gets the handler's own 400); tolerated shapes stay tolerated (free-formcontent, optional fields absent).Tests
New
test-request-shape-hoststep (wired intozig build test), 5 tests / ~45 cases — every refusal string plus the tolerance cases. 5/5 pass on this head; on the base the same bodies panic, per the repro above.Notes:
build.zigalso carries thetest-filteroption hoist —zig buildpanicsoption declared twiceon zig 0.17 without it (same prerequisite hunk as #6505/#6508, disclosed there too). The server files sit on the Metal-bound engine graph, so host-side verification isast-checkplus the pure-module tests.Generated with Devin