Repository navigation
Commit 8eccf51
fix(runtime): on the standalone server, an anonymous request whose JSON body cannot be parsed gets the domain's 401, not the transport's 400 (#24250)
Fixes #24151
Clause-②: no
## What
On the standalone server (`os dev`, `os serve`: `HonoServerPlugin` + the
dispatcher plugin), every mount in
`packages/runtime/src/dispatcher-plugin.ts` that handed `dispatch()`
`req.body` now hands it `deferRequestBody(req)`, and each domain reads
the body with `readDispatchedBody(body)` once its anonymous decision
(ADR-0138 D2) has admitted the caller. An anonymous caller's answer no
longer depends on whether its JSON body parses; an admitted caller with
an unparseable body still gets the transport's `400 INVALID_REQUEST`.
- `packages/runtime/src/deferred-request-body.ts` (new, module-private
to the package, not exported from the index): the two halves.
`deferRequestBody(req)` returns a value that holds the request and reads
nothing; `readDispatchedBody(body)` reads `req.body` for a deferred
value and returns any other value unchanged (what `createHonoApp`, tests
and internal callers hand `dispatch()`). A deferred value read WITHOUT
being resolved throws an `UnresolvedRequestBodyRead` error on every
trap, so a domain that forgets the resolve fails loudly at its first
read instead of acting on an invented empty body.
- `dispatcher-plugin.ts`: 24 call sites, `req.body` becomes
`deferRequestBody(req)` (the card's 20, plus 4 the card's grep missed,
below). One note at the shared `sendResult` / `errorResponse` helpers
says why.
- Nine domains, one statement each, right after the anonymous decision:
`analytics`, `mcp`, `keys`, `notifications`, `automation` (after the
inbound-hook hand-off, which verifies raw bytes and never reads a parsed
body), `actions`, `ai`, `packages`, `security`. `ai` decides per route:
an admitted caller's body is read at the domain-wide decision, and an
anonymous caller a route's `auth: false` admits has it read in the route
loop.
- Untouched: `packages/spec` (`IHttpRequest`'s body contract),
`packages/adapters/hono`, `plugin-hono-server`'s adapter, PR #24244's
fallback path, the domains' logic beyond when they read the body.
## H1: domain by domain (measured on a real socket, base `cbfa5e433`,
head `8e9fd41b2`)
Anonymous request, `content-type: application/json`, body `{"name":
"probe"` (closing brace missing). "Anonymous first?" is read in the
domain's source on `cbfa5e433`.
| site(s) on `cbfa5e433` | domain | anonymous decided first? | base |
head |
|---|---|---|---|---|
| `:1282` `POST /analytics/query`, `:1300` `POST /analytics/sql` |
`analytics` | yes, first statement | 400 `INVALID_REQUEST` | 401
`UNAUTHENTICATED` |
| `:1326` `/mcp` (POST, GET, DELETE) | `mcp` | **no**:
`isMcpServerEnabled()` 404 and the service probe 501 come first (finding
below) | 400 | 401 (MCP on and installed); 404 / 501 otherwise, body
unread |
| `:1362` `/mcp/skill` (POST, GET, DELETE) | `mcp/skill` | no anonymous
decision: public by design ("Served PUBLIC like `/discovery`") | 400 |
405 `METHOD_NOT_ALLOWED`, its own answer to any non-GET |
| `:1395` `POST /keys`, `:1409` `/keys` (GET, PUT, PATCH, DELETE) |
`keys` | yes, first statement | 400 | 401 |
| `:1447` `POST /notifications/read`, `:1456` `POST
/notifications/read/all` | `notifications` | yes, first statement | 400
| 401 |
| `:1676`–`:1857`, the nine `/automation` mounts | `automation` | yes,
first statement | 400 | 401 |
| `:1947`, `:1955`, `:1963`, the three `POST /actions/...` shapes |
`actions` | yes, first statement | 400 | 401 |
| `:1909` the `/ai/*` wildcards (GET, POST, PUT, PATCH, DELETE), not in
the card's grep | `ai` | yes, decided once ahead of every capability
answer; a route's `auth: false` opens it | 400 | 401 |
| `:1553` the `/packages` registrar (16 mounts), not in the card's grep
| `packages` | yes, first statement | 400 | 401 |
| `:2006` `POST /security/_activation/:type/:name`, `:2020` `POST
/security/permission-sets/:name/clone`, not in the card's grep |
`security` | yes, first statement | 400 | 401 |
At base the same anonymous requests with a well-formed body answered 401
everywhere (405 on `/mcp/skill`): only the parse decided the 400. Base
was measured on the unscoped prefix; the scoped
(`/environments/:environmentId`) mounts are the same registrar code, and
head pins both prefixes.
## H2, H3, H4
- **H2, partly falsified.** `createHonoApp` answers 401 because
`readDispatchBody` hands `dispatch()` either the parsed value or
`refusedOnRead(...)`: a Proxy over `Object.create(null)` whose every
trap throws the refusal and records the read, which the arm then
answers. Not a getter or a thunk. `dispatch()`'s `body` parameter is
`any` and `dispatch()` never reads it (it passes it to
`domainRoute.handler`), so the shape is accepted. But a standalone mount
cannot build it: it can learn whether the body parsed only by reading
`req.body`, the one door `IHttpRequest` declares, and that read IS the
refusal. A lazily forwarding Proxy would have needed no domain change,
but it is not faithful: `Array.isArray`, `typeof` and `== null` read the
target, fixed at creation, so a JSON array (an MCP JSON-RPC batch;
`@objectstack/mcp` passes `parsedBody` to the SDK, which branches on
`Array.isArray`), `null` or a primitive would reach the domain as an
object. So the domains read the body explicitly, one statement each.
- **H3, confirmed.** The adapter's `get body()` throws the refusal and
records `refusedBodyRead`; the adapter then answers that refusal
whatever the handler did. Deferring the read into the domain needs no
adapter change: all anonymous pins are green with `adapter.ts`
untouched.
- **H4, confirmed with two named exceptions.** A signed-in caller with
the same body gets `400 INVALID_REQUEST`, message "The request body
could not be parsed as JSON …" (the transport's refusal, unchanged) on
every site whose domain reads a body once it admits the caller.
Exceptions, both answers the domain gives without reading a body, as on
`createHonoApp`: `/mcp/skill` (its 405; base answered 400, measured) and
`/mcp` where MCP is switched off or not installed (404 / 501; base
answered 400 by construction, the mount reading the body before
`dispatch()`).
## The four mounts outside the card's grep (bounded in-place fix)
The card's grep `dispatcher\.dispatch\([^)]*req\.body` stops at the
first `)`, so it missed the `/ai/*` wildcard (`method.toUpperCase()`
precedes `req.body`) and the three registrars that spell the call over
several lines (`/packages`, `/security/_activation`,
`/security/permission-sets/:name/clone`). Same defect class, same
mechanical change, same file, same gate families. Evidence, base
`cbfa5e433`, anonymous: `POST /ai/chat`, `PUT` and `PATCH
/ai/conversations/c1`, `POST /packages`, `PATCH /packages/pkg_x`, `PATCH
/packages/pkg_x/enable`, `POST /packages/pkg_x/publish`, `POST
/packages/pkg_x/rollback`, `POST
/security/_activation/permission_set/ps_x`, `POST
/security/permission-sets/ps_x/clone`: each 400 `INVALID_REQUEST` with
the unparseable body, 401 `UNAUTHENTICATED` with the well-formed one.
Head: 401 for both. Pinned below.
Two `req.body` reads remain in the file, both deliberately: the
service-emitted route mount (the mount decides `route.auth` before it
reads the body, so an anonymous answer never depends on it), and the
declarative-endpoint fallback, which is not a `dispatch()` mount (see
Acceptance notes).
## Tests
-
`packages/runtime/src/dispatcher-body-after-anonymous-floor.hono.integration.test.ts`
(new): real Hono socket, `LiteKernel`, both prefixes for the registrars
`enableProjectScoping` mounts twice. 60 URLs, two pins each: anonymous +
unparseable gets the domain's anonymous answer in the dispatcher's
envelope, equal to the anonymous + well-formed answer; signed-in +
unparseable gets the transport's refusal (or `/mcp/skill`'s 405). 120
tests.
- `packages/runtime/src/deferred-request-body.test.ts` (new): the
value's own contract, 5 tests: nothing is read until it is resolved;
resolving returns the exact value (object, array, `null`, string,
number, boolean); the transport's refusal propagates as the same object;
values it did not defer pass through untouched; an unresolved read
throws `UnresolvedRequestBodyRead` on every trap and never reads the
request.
- `pnpm --filter @objectstack/runtime exec vitest run --project local
--maxWorkers=2`: 415 files passed, 6536 tests passed, 19 skipped (run at
`bde3873ed`; `8e9fd41b2` adds only the changeset).
- `--project repo` (it reads `dispatcher-plugin.ts` as text): 3 files,
808 tests passed at `8e9fd41b2`.
- `pnpm --filter @objectstack/runtime typecheck`: exit 0;
`check:test-typecheck` OK, the two new test files add no debt.
**Ablation (one, as asked).** `deferRequestBody` mutated to `return
req.body;` (eager read again) through `scripts/ablation-replace.mjs`:
anchor 1 to 0, blob `7d7452c90112` to `dfaf76e93a8e`. The test imports
`./dispatcher-plugin.js` from `src` by relative path, so no `dist/` is
on the path. Result: 62 failed, 58 passed of 120. Red: all 60 anonymous
pins, and the two signed-in `/mcp/skill` pins, whose expected answer is
the domain's own 405, which an eager read pre-empts with 400. Green: the
58 signed-in refusal controls. Restore proven: blob back to
`7d7452c90112` equal to HEAD, `git diff HEAD` empty.
## Gates (at `8e9fd41b2`)
`node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack`: 65 commands. 64 exit 0. 1 NOT MEASURED:
`pnpm check:dual-build-cjs-loads` exit 3, PREREQUISITE NOT MET (it reads
every package's `dist/`; only the runtime closure is built here).
`--ran` with exit codes: "65 derived famil(ies) accounted for — 64 run,
1 NOT-MEASURED". Also, for the roster families whose list lives under a
changed directory: `check:route-ledger-census`,
`check:error-code-casing`, `check:authz-resolver`,
`check:filter-alias-parity`, `check-changeset-fixed.mjs`, each exit 0.
`pnpm exec eslint --no-inline-config` over the 13 changed `.ts` files:
exit 0. The repo-wide `pnpm lint`, the type-check lanes, Test Core,
Build Core and dogfood are CI's.
## Acceptance notes
- **`/mcp` does not decide anonymous first (H1).** `handleMcpRequest`
answers `404` when `OS_MCP_SERVER_ENABLED` is off and `501` when no MCP
service is installed, both ahead of its `401`. Measured in process on
the standalone composition: anonymous `POST /api/v1/mcp` answers `501
NOT_IMPLEMENTED` with no MCP service and `404 RESOURCE_NOT_FOUND` with
MCP off, for a well-formed and an unparseable body alike. Not changed
here: the dispatch order says a domain that does not decide anonymous
first is a finding, not something to add a check for.
- **The declarative-endpoint fallback reads the body before its own
decisions.** The fallback seam builds `execution.request.body` from
`req.body` before `runAppEndpointStep` matches an endpoint or applies
`authRequired`. Measured: anonymous `POST /api/v1/apps/ns/x` answers
`400 INVALID_REQUEST` with an unparseable body and `404
ENDPOINT_NOT_FOUND` with a well-formed one; signed in, the same. A
declared `authRequired: true` endpoint was not driven, so whether an
anonymous caller gets 400 where 401 is owed there is inferred, NOT
MEASURED. Not a `dispatch()` mount, so outside this change.
- `/mcp/skill` is public by design and reads no body, so its row pins
its own 405 rather than 401.
- A signed-in caller's answer moved only where the domain answers
without reading a body (the two H4 exceptions); everywhere else it is
byte-for-byte what base answered.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01QyoWKK9RwUNxBxmevy9Neb)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent e74ec56 commit 8eccf51
14 files changed
Lines changed: 521 additions & 25 deletions
File tree
- .changeset
- packages/runtime/src
- domains
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
0 commit comments