Public: anyone can read this; the crew can change it.

Every route that reads a request body has a size cap, and a test names any that does not #14688

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

Every request-body cap is now one table, BODYCAPS in src/http/limits.ts, with a reason on each row. A new test walks the route table and names any route that reads a body with no cap. The walk found no uncapped route in either router today, so no route needed a new cap or a new 413.

Notes

shipped production is serving it deploy 404563e

opened this

Every request-body cap is now one table, BODY_CAPS in src/http/limits.ts, with a reason on each row. A new test walks the route table and names any route that reads a body with no cap. The walk found no uncapped route in either router today, so no route needed a new cap or a new 413.

What moved where

  • src/http/limits.ts: BODY_CAPS holds push, fetch, gzip, body, stranger and drain. It also holds the caps that used to sit in their own modules:
    • beacon (web/measure)
    • cspReport, scriptError, stripeHook
    • originHook (origins/hook), brokerCall (spawn/origin_broker)
    • timerBody, talkImage, buildShot
    • printPdf, printRaster
    • dropForm (the Drop form)
  • The modules that defined those caps now read their row of the table. Every exported name and byte value is unchanged; a script compared each one with its old value. src/http/api.ts and src/http/server.ts are not touched.
  • AGENTS.md gains one line: a route reads a body through the capped readers, with a cap that is a row of BODY_CAPS.

test/body-caps.test.ts

  • Route probe: every non-GET route in buildRouter() and buildApiRouter() (693) is called in-process with an endless body, once with an admin token and once with no credentials. The test fails, naming the route, if a route reads more than the largest cap in the table plus the drain budget.
  • 413 shape: a 413 under /api/ must be { error: "too_large" }.
  • Guards on the test itself:
    • The probe is shown to catch a handler that reads the whole body.
    • At least 300 routes must answer the admin's probe with 413, so a broken seed cannot pass silently.
    • A route that deletes the seeded repository is followed by a new one, so later routes are still reached.
  • Blind spot: a route whose path names nothing seeded answers 404 before reading. A source scan covers it: no module may read a request body whole (request.json(), .text(), .formData() and the like) unless it is in UNCAPPED with its reason.
  • Mutation check: I replaced the disk-readings route's capped read with ctx.request.text(). Both the probe and the scan failed and named PUT /api/v1/admin/machines/:machine/disks/:device; then I restored the file.
  • The file runs in about 6 s.

Not done

  • handleHook in src/http/server.ts still calls request.json() with no cap. It serves the internal hook socket, checks the per-boot secret first, and is not in the route table. The issue says not to edit server.ts, so it is the one UNCAPPED entry; a follow-up can cap it there.
  • A literal readBounded(ctx.request, 1024) in src/http/api.ts stays where it is, for the same reason.
  • The Drop form's plain-text 413 ("payload too large") is a page answer and was left as it is.

Checks run: bun scripts/gate.ts --fix (it rewrapped comments, now committed), then bun x tsc --noEmit. scripts/fast.ts named 2,608 test files (about 220 minutes) because limits.ts is imported almost everywhere. Instead I ran the 30 test files that cover each moved cap, plus open-routes-floor and router: 781 pass.

The desk hold "Releasing trevato/forge: production is rolling" was noted. This change keeps every value as it was.

To try it: bun test test/body-caps.test.ts. On the preview, sending a 20 MB body to PUT /api/v1/admin/machines/m/disks/d as an admin should answer 413 too_large.

🤖 Generated with Claude Code

Closes #14677

1 earlier comment · 1 event
commented

Every request-body cap is now one table, BODY_CAPS in src/http/limits.ts, with a reason on each row. A new test walks the route table and names any route that reads a body with no cap. The walk found no uncapped route in either router today, so no route needed a new cap or a new 413.

What moved where

  • src/http/limits.ts: BODY_CAPS holds push, fetch, gzip, body, stranger and drain. It also holds the caps that used to sit in their own modules:
    • beacon (web/measure)
    • cspReport, scriptError, stripeHook
    • originHook (origins/hook), brokerCall (spawn/origin_broker)
    • timerBody, talkImage, buildShot
    • printPdf, printRaster
    • dropForm (the Drop form)
  • The modules that defined those caps now read their row of the table. Every exported name and byte value is unchanged; a script compared each one with its old value. src/http/api.ts and src/http/server.ts are not touched.
  • AGENTS.md gains one line: a route reads a body through the capped readers, with a cap that is a row of BODY_CAPS.

test/body-caps.test.ts

  • Route probe: every non-GET route in buildRouter() and buildApiRouter() (693) is called in-process with an endless body, once with an admin token and once with no credentials. The test fails, naming the route, if a route reads more than the largest cap in the table plus the drain budget.
  • 413 shape: a 413 under /api/ must be { error: "too_large" }.
  • Guards on the test itself:
    • The probe is shown to catch a handler that reads the whole body.
    • At least 300 routes must answer the admin's probe with 413, so a broken seed cannot pass silently.
    • A route that deletes the seeded repository is followed by a new one, so later routes are still reached.
  • Blind spot: a route whose path names nothing seeded answers 404 before reading. A source scan covers it: no module may read a request body whole (request.json(), .text(), .formData() and the like) unless it is in UNCAPPED with its reason.
  • Mutation check: I replaced the disk-readings route's capped read with ctx.request.text(). Both the probe and the scan failed and named PUT /api/v1/admin/machines/:machine/disks/:device; then I restored the file.
  • The file runs in about 6 s.

Not done

  • handleHook in src/http/server.ts still calls request.json() with no cap. It serves the internal hook socket, checks the per-boot secret first, and is not in the route table. The issue says not to edit server.ts, so it is the one UNCAPPED entry; a follow-up can cap it there.
  • A literal readBounded(ctx.request, 1024) in src/http/api.ts stays where it is, for the same reason.
  • The Drop form's plain-text 413 ("payload too large") is a page answer and was left as it is.

Checks run: bun scripts/gate.ts --fix (it rewrapped comments, now committed), then bun x tsc --noEmit. scripts/fast.ts named 2,608 test files (about 220 minutes) because limits.ts is imported almost everywhere. Instead I ran the 30 test files that cover each moved cap, plus open-routes-floor and router: 781 pass.

The desk hold "Releasing trevato/forge: production is rolling" was noted. This change keeps every value as it was.

To try it: bun test test/body-caps.test.ts. On the preview, sending a 20 MB body to PUT /api/v1/admin/machines/m/disks/d as an admin should answer 413 too_large.

🤖 Generated with Claude Code

Changes requested — before the latest push

The probe and the table are good work, but two things stop it. (1) Layering: core/, devices/, origins/, spawn/ and web/ now import from http/limits.ts only to read a number, so the lowest modules depend on the HTTP layer. Fix first: either keep those constants where they are and have limits.ts hold only the caps the http layer reads, or move the table to a neutral module (e.g. src/kernel/caps.ts) that http/limits.ts re-exports. (2) The table is not the one place: handleHook in src/http/server.ts still buffers request.json() with no cap, and readBounded(ctx.request, 1024) in api.ts is a literal outside the table. An uncapped body-reading handler is exactly what the issue asks to remove; give handleHook a BODY_CAPS row and a capped read (the internal socket is still a door), and move the 1024 literal into a row. Then UNCAPPED in the test can be empty and the scan can fail on any literal cap argument to readBounded. No design, README or terminal findings: the web change is a constant only.

commented

Every route that reads a request body now stops at a size cap, and each cap is a named row of one table, BODY_CAPS, with the reason for its size. test/body-caps.test.ts sends a body that never ends to every non-GET route in both routers, once as the admin and once as a stranger. It names any route that reads the whole thing.

Review changes

  • Layering: the table moved to src/kernel/body_caps.ts, which holds only plain numbers. http/limits.ts re-exports it and keeps the code that enforces the caps. core/timers.ts, core/conversation_files.ts, devices/print.ts, devices/pwg.ts, origins/hook.ts, spawn/origin_broker.ts, web/measure.ts and web/studio/drop.ts now import the table from kernel/, not from the HTTP layer.
  • handleHook (src/http/server.ts) used to read the whole body with request.json() and no limit. It now reads it with readBounded at a new gitHook row (16 MiB, enough for a push's ref updates). A body past the cap gets 413 and one that isn't JSON gets 400; before, bad JSON threw an error.
  • Caps written as numbers at the call are now rows too: the heartbeat's 1024 becomes heartbeat, the funnel's 200 becomes funnel, and the timer route's ONCE_BODY_MAX + 4096 becomes timerArm.
  • The source scan: UNCAPPED is gone. The scan fails on any module that reads a request body whole, and on any digit in the cap argument of readBounded or counted. A small test checks that the scan catches both forms.
  • A test for the internal listener: it posts to the hook port with the boot secret. A body one byte over the cap gets 413 and one that isn't JSON gets 400.
  • AGENTS.md says where the table lives and what the test enforces.

Checks: bun scripts/gate.ts --fix and bun x tsc --noEmit pass. scripts/fast.ts exited 2 (the diff is too broad to name), so I ran the test files that cover the change: body caps and body limits, limits, funnel, beat, family heartbeat, security-floor doors, print, origin broker, conversation files, contribute push, apps measure, drop page and the timers tests. 202 passed and 1 failed: test/timers-sync.test.ts failed once during that run. Run alone it passes, both with and without this change, so the failure came from load, not from the change.

The gate's "pipeline first" rule counted the new module as a feature. Since a pushed commit can't be amended, an empty commit carries the Direction: line explaining that it is a move.

No page changed (the web/ edits only change an import), so there is no review block.

🤖 Generated with Claude Code

Approved

Earlier asks are met. The table is in kernel/ and limits.ts re-exports it. handleHook reads through readBounded, with 413 on too large and 400 on bad JSON. Literal caps are now rows, and the UNCAPPED escape is gone. The probe test and the internal-listener test cover the behaviour. Non-blocking: handleHook accepts JSON null, and forwardedGitEnv(parsed.gitEnv) would then throw. Check that the parsed value is an object.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs