Skip to content

Provider: the local API applies its request limits by resolved route, on every path spelling - #2527

Draft
0xkydo wants to merge 4 commits into
devfrom
fix/local-ingress-default-deny
Draft

0xkydo wants to merge 4 commits into
devfrom
fix/local-ingress-default-deny

Conversation

@0xkydo

@0xkydo 0xkydo commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The local API's request checks compared the request path as the client wrote it. The SDK router's trie drops empty path segments, so /v1/chat/completions/ and //v1/chat/completions resolved to the same route but skipped the JSON nesting and NUL scan, the batch count and the tool-call arguments check, and were served by the SDK handler. That reopened #2130, #2143 and #2188 for a caller holding the local API key. Hummingbird's 2 MiB body ceiling still held.

Those three fixes each placed a check by hand on a list of paths. This PR replaces the list with one guard keyed on the route the router actually resolves:

  • LocalRoute reads a path the way the trie does.
  • LocalIngress.admit applies the route's limits to the body before any parser reads it: body ceiling, then nesting scan, then item count without decoding.
  • Default for unknown routes. A route with no limits of its own, including one a later SDK adds, gets the default ceiling and the nesting scan instead of none.

Scope of the exposure: the Mac's own local API, to a holder of its local API key. Nothing a coordinator sees changes.

Linked issue

Part of #2524 (decision 6: route and ingress are resolved once, at the boundary). Independent of the other PRs in the epic.

Before

flowchart LR
  classDef bad fill:#cf222e,stroke:#82071e,color:#ffffff
  classDef ok fill:#1a7f37,stroke:#0f5323,color:#ffffff
  classDef step fill:#0969da,stroke:#0a3069,color:#ffffff
  R["Request to the local API"]:::step --> P{"Path equals a listed path, exactly"}
  P -- "yes" --> G["Nesting scan, batch count, body ceiling"]:::ok
  P -- "no: extra slash, or a route nobody listed" --> T["Router trie drops empty segments"]:::step
  G --> H["Handler"]:::step
  T --> U["SDK handler parses the body unchecked"]:::bad
Loading

Behaviour: POST /v1/chat/completions/ with a body nested 65 deep reached engine acquire and answered 404 "Requested model is unavailable", where the exact path answers 400 at the guard. Code flow: path predicates in three files, each added by a separate fix.

After

flowchart LR
  classDef bad fill:#cf222e,stroke:#82071e,color:#ffffff
  classDef ok fill:#1a7f37,stroke:#0f5323,color:#ffffff
  classDef step fill:#0969da,stroke:#0a3069,color:#ffffff
  classDef note fill:#9a6700,stroke:#633c01,color:#ffffff
  R["Request to the local API"]:::step --> L["LocalRoute: path read as the trie reads it"]:::step
  L --> K{"Route"}
  K -- "has limits of its own" --> A["LocalIngress.admit with the route's limits"]:::ok
  K -- "any other route or method" --> D["LocalIngress.admit with the default limits"]:::note
  A --> C{"Within limits"}
  D --> C
  C -- "yes" --> H["Handler reads the admitted body"]:::step
  C -- "no" --> X["400 or 413, before any parser"]:::bad
Loading

Behaviour: every spelling of a path gets its route's limits and answers. Code flow: one guard, two call sites (the local router before a pass-on, and the engine routes); the per-endpoint predicates are deleted. Sources: 5 files, 248 lines added and 167 removed, including the new LocalIngress.swift.

What a client sees

Request Before After
Listed route, exact path Its limits Unchanged, byte for byte
Listed route, extra slashes Served by the SDK handler with no nesting scan, batch count or arguments check Same limits and answers as the exact path
POST /v1/responses/{id}/cancel, an unserved path, PUT or DELETE, with a body over 2 MiB or nested past 64 Body ignored, or 404 413 or 400
Batch of more than 64 items behind a UTF-8 byte-order mark Items decoded before the count; undecodable items answer a decode error batch_too_large before decoding
GET, HEAD Body unread Unchanged

Test plan

Red, on unmodified dev (0f78662e6b) plus the new test file only. 741 issues: every POST route, on a trailing slash, a doubled leading slash and a doubled inner slash, in process and over a loopback connection. Sample:

POST /v1/chat/completions        nestedPastTheLimit  trailingSlash,leadingSlashDoubled,innerSlashDoubled -> 404 'Requested model is unavailable.'
POST /v1/chat/completions/batch  tooManyItems        trailingSlash,leadingSlashDoubled,innerSlashDoubled -> 404 'Requested model is unavailable.'
POST /v1/responses/{response_id}/cancel  nestedPastTheLimit  exact,... -> 404 'Internal inference failure.'

The oracle, LocalIngressTests, reads the routes from the real router (MLXServerApplication.buildRouter(...).routes), so a route a later SDK adds joins the table without anyone listing it. It checks every body route x 4 spellings x 8 over-limit bodies for the exact status and message, the same answer as the exact path, zero engine acquires and nothing passed on.

Would it have caught the earlier fixes? By construction: the depth and NUL rows fail when no scan runs (#2130), 65 items reach acquire (#2143), and undecodable items answer a type mismatch instead of batch_too_large on a decode-first path (#2188).

Byte-order mark, red then green:

POST /v1/chat/completions/batch exact tooManyItemsThatDoNotDecodeBehindAByteOrderMark: 400 message=Type mismatch: Expected to decode Dictionary<String, Any> but found number instead.
LocalIngress.itemCount(ByteBuffer(bytes: Self.byteOrderMark + Array(overTheBound.utf8))) → 0
  • swift build --jobs 6 --build-tests: complete
  • swift test --skip-build --no-parallel --filter 'LocalIngressTests|LocalRequestNestingTests|LocalRouterTests|LocalChatBatchLimitTests': 45 tests in 4 suites pass (rerun independently after the last commit)
  • Existing suites that cover the changed files, 42 suites: 378 of 380 pass. The 2 failures are in ClusterPairMemberRelayTests; they hardcode the binary name darkbloom, a default build on dev is darkbloom-dev, and they fail identically on unmodified source
  • make docs-check (216 OK), make changelog-check (61 valid), make docs-impact-check BASE=origin/dev (OK)
  • Whole provider suite and live-model suites: not run locally; CI

Refactor pass

Done by an independent reviewer, second commit. Verdict: the design holds.

  • A throwaway differential of 5,390 method and path cases between LocalRoute and the real router found no disagreement.
  • itemCount was byte-identical to the counter it replaced.
  • Changes: one rename where a single name meant two things, one comment that overclaimed, a comment recording that a layer reading a body must take it from admit, and one more row in the router differential.
  • The reviewer found the byte-order-mark undercount, which dev has too. It is fixed in the third commit, test-first, because counting before decoding on every input is what this PR claims.

Limitations and what this PR leaves open

  • admit has two call sites, not one. A single guard in front of the router would change the pair gate's 503 precedence and the error envelopes. Nothing mechanical stops a future layer above the guard from reading a body; a comment says it must not.
  • GET /metrics/ with a trailing slash skips LocalMetricsResponder (the same exact-path compare, in a file this PR does not own) and returns SDK metrics without the MTP lines. /health/ requires a key, which fails closed.
  • A comma after the last batch item counts one item more, so 64 items ending ,] are refused. It errs toward refusing.
  • No depth scan on the Desktop control POST or on add-on IPC; both are authenticated local peers.
  • The local router and the engine each scan the body, as before.

Components touched

  • provider-swift (Swift CLI)
  • native runtime / MLX
  • landing (Next.js)
  • enclave (Swift)
  • CI / provider release
  • docs

Protocol / interface changes

  • No protocol/interface changes
  • Yes — described above and matching side updated

Local HTTP API only, as in the table above. No WebSocket message, coordinator-visible status, heartbeat field or config key changes. No version bump.

Documentation impact

  • Canonical documentation updated: docs/provider/direct-mode.md, new "Request limits" section (rule "provider local API")
  • No documentation needed — reason:

Changelog: changelog.d/local-api-request-limits-every-path-spelling.md.

🤖 Generated with Claude Code


See it in action
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

0xkydo and others added 3 commits November 22, 2026 02:04
… on every path spelling

The local API's checks compared the request path as the client wrote it. The
SDK router's trie drops empty path segments, so /v1/chat/completions/ and
//v1/chat/completions resolved to the same route but skipped the JSON
nesting and NUL scan, the batch count and the tool-call arguments check, and
were served by the SDK handler. That reopened #2130, #2143 and #2188 for a
caller holding the local API key. Only Hummingbird's 2 MiB body ceiling held.

LocalRoute reads a path the way the trie does, and LocalIngress.admit applies
the route's limits to the body before any parser reads it. A route with no
limits of its own, including one a later SDK adds, gets the default ceiling
and the nesting scan instead of none.

LocalIngressTests reads the routes from the real router and checks every
body route on four spellings against five over-limit bodies, in process and
over a loopback connection. Limits, statuses and error bodies on the exact
paths are unchanged, and nothing a coordinator sees changes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…e, sharper comments)

Independent behaviour-preserving cleanup of the previous commit.

- LocalRouterResponder: the LocalRoute parameter is apiRoute. A later local
  named route (the saved LocalSettings.Route) shadowed it, so one name meant
  two things.
- LocalIngress: the header said no check compares a path; that holds for the
  limits and the local router, and now says so.
- LocalInferenceHTTP: the stack comment records that a layer above the
  interception that reads a body must take it from admit.
- The router differential gains a dot-dot row and says why its rows stay
  ASCII. Docs and changelog: grammar, and OPTIONS is answered by CORS before
  the guard.

A differential of 5,390 method and path cases between LocalRoute and the
real router found no disagreement.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…coded

LocalIngress.itemCount returned 0 for an array that starts with a UTF-8
byte-order mark, and Foundation's parsers skip that mark. So a batch of more
than 64 items behind one was decoded before its count was checked, the
ordering #2188 fixed, surviving through three bytes. With items that do not
decode it answered a decode error instead of batch_too_large. The counter on
dev had the same gap.

The counter now skips the mark. A differential holds it at or above what
JSONSerialization and JSONDecoder read over six prefixes and six lists.
The nesting scan was never affected: it reads every byte.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@0xkydo 0xkydo added security Security-sensitive issue or fix area:provider Provider (Rust, runs on Apple Silicon) labels Nov 22, 2026
@vercel

vercel Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
d-inference-landing Ready Ready Preview Nov 22, 2026 10:47am UTC
darkbloom-status Ready Ready Preview Nov 22, 2026 10:47am UTC
eigen-homepages-darkbloom Ready Ready Preview Nov 22, 2026 10:47am UTC

Request Review

…oute table

The assertion that the nesting scan is not fooled by a leading byte-order
mark was a single direct call. It is now an over-limit body in the table,
so it is checked on every body route and every path spelling like the
others. The item counter also gets a short exact case behind a mark.

Test-only.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch was successfully deployed

3 active deployments
Preview – d-inference-landing — d3af41f0 Deployed Nov 22, 2026 by vercel[bot]
Preview – eigen-homepages-darkbloom — d3af41f0 Deployed Nov 22, 2026 by vercel[bot]
Preview – darkbloom-status — d3af41f0 Deployed Nov 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider Provider (Rust, runs on Apple Silicon) security Security-sensitive issue or fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant