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

Every /api/v1 and /mcp answer, including errors and 404s, carries the security floor headers: one test sweeps them #14715

Merged opened this

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

Merged

This adds test/api-security-floor.test.ts, a sweep of every /api/v1 and /mcp answer for Content-Security-Policy, X-Content-Type-Options and Referrer-Policy. The sweep found no route without the floor, so src/http/respond.ts is unchanged. What it asks.

Notes

shipped production is serving it deploy 404563e

opened this

This adds test/api-security-floor.test.ts, a sweep of every /api/v1 and /mcp answer for Content-Security-Policy, X-Content-Type-Options and Referrer-Policy. The sweep found no route without the floor, so src/http/respond.ts is unchanged.

What it asks. The routes come from GET /contract/openapi.json, read as a site admin so the document holds every role's routes: about 470 operations on 381 paths. A route added to the contract is swept without editing the test. Each operation is asked:

  • GETs: signed out and signed in, plus a HEAD of each (200, 401, 404).
  • Every mutation: signed out, with a malformed body (401).
  • Every operation that declares a body: signed in as the admin, with malformed JSON (400).
  • Each path under a method it does not declare (405).
  • An unknown path and an unknown repository (404).
  • One 429 from the sign-in window.

MCP. The /mcp side is built from tools/list the same way:

  • every tool called by a stranger with empty arguments;
  • a malformed body, from a stranger and from a token;
  • an unknown method and an unknown tool;
  • a notification's bodiless 202;
  • GET, DELETE and PUT, which /mcp refuses (405).

Every request really reaches a handler. Each sweep fails if it sees a 429, so a pass can't be a sweep of rate-limiter refusals. The first run hit two limiters this way (the anonymous read limit of 300 a minute and the 600 a minute for an address without a token), so setup raises both: api.anonymous_per_source and setStrangerLimits. Each sweep also checks it reached the statuses listed above. With the floor's referrer-policy line removed from securityFloor, the file fails.

Streams and HEAD are not exempt. A stream's headers are judged and its body is cancelled, because the floor is set before the first byte. HEAD has no body and is judged the same way. A path parameter with no seeded value gets a placeholder; its not-found answer must carry the floor too.

Choices:

  • MCP tools are called only by a stranger, not with the admin token: empty arguments as the admin could act on the seeded forge.
  • Signed in, malformed JSON goes only to operations that declare a body, which read it before acting; the bodiless DELETEs and POSTs are only asked signed out.

Checks run: bun scripts/gate.ts --fix (it rewrapped one comment, included in the commit), bun x tsc --noEmit, bun test test/api-security-floor.test.ts (7 pass, about 4 s) and bun test test/mcp-size.test.ts all pass. The keep_plan and estimate tools were not available in this run, so the issue has no plan and no estimate.

Try it: bun test test/api-security-floor.test.ts

🤖 Generated with Claude Code

Closes #14701

1 earlier comment · 1 event
commented

This adds test/api-security-floor.test.ts, a sweep of every /api/v1 and /mcp answer for Content-Security-Policy, X-Content-Type-Options and Referrer-Policy. The sweep found no route without the floor, so src/http/respond.ts is unchanged.

What it asks. The routes come from GET /contract/openapi.json, read as a site admin so the document holds every role's routes: about 470 operations on 381 paths. A route added to the contract is swept without editing the test. Each operation is asked:

  • GETs: signed out and signed in, plus a HEAD of each (200, 401, 404).
  • Every mutation: signed out, with a malformed body (401).
  • Every operation that declares a body: signed in as the admin, with malformed JSON (400).
  • Each path under a method it does not declare (405).
  • An unknown path and an unknown repository (404).
  • One 429 from the sign-in window.

MCP. The /mcp side is built from tools/list the same way:

  • every tool called by a stranger with empty arguments;
  • a malformed body, from a stranger and from a token;
  • an unknown method and an unknown tool;
  • a notification's bodiless 202;
  • GET, DELETE and PUT, which /mcp refuses (405).

Every request really reaches a handler. Each sweep fails if it sees a 429, so a pass can't be a sweep of rate-limiter refusals. The first run hit two limiters this way (the anonymous read limit of 300 a minute and the 600 a minute for an address without a token), so setup raises both: api.anonymous_per_source and setStrangerLimits. Each sweep also checks it reached the statuses listed above. With the floor's referrer-policy line removed from securityFloor, the file fails.

Streams and HEAD are not exempt. A stream's headers are judged and its body is cancelled, because the floor is set before the first byte. HEAD has no body and is judged the same way. A path parameter with no seeded value gets a placeholder; its not-found answer must carry the floor too.

Choices:

  • MCP tools are called only by a stranger, not with the admin token: empty arguments as the admin could act on the seeded forge.
  • Signed in, malformed JSON goes only to operations that declare a body, which read it before acting; the bodiless DELETEs and POSTs are only asked signed out.

Checks run: bun scripts/gate.ts --fix (it rewrapped one comment, included in the commit), bun x tsc --noEmit, bun test test/api-security-floor.test.ts (7 pass, about 4 s) and bun test test/mcp-size.test.ts all pass. The keep_plan and estimate tools were not available in this run, so the issue has no plan and no estimate.

Try it: bun test test/api-security-floor.test.ts

🤖 Generated with Claude Code

Changes requested — before the latest push

The sweep is sound: routes come from the contract, every sweep asserts it reached 200/401/404/405/400/429 and fails on a limiter 429, and no route needed a respond.ts change. One change to make, in test/api-security-floor.test.ts. The Ask type has a want?: number field that is set on three asks (the unknown API path, the unknown repository and the sign-in 429) but never read anywhere. That is dead code. Either assert it in ask/sweep (return it in Seen and expect status === want when set, which would also make the 404/429 cases check themselves instead of relying on the joined-string expectation), or delete the field and its three uses. Also check the floor on the first sign-in ask in the 429 test, whose result is currently discarded, or say in a comment that it is only there to fill the window. Two things for next: restore signin.per_forge after the 429 test so a later test in the file cannot inherit it, and add the docs note on how to extend the exempt list if a route ever must be skipped. Boxes: 1 and 2 are met; 3 is met vacuously because nothing was found missing.

commented

Every response from /api/v1 and /mcp now has a test proving it sends the three security floor headers (Content-Security-Policy, X-Content-Type-Options, Referrer-Policy). That includes errors, 404s, 405s and the 429 from the rate limiter. test/api-security-floor.test.ts reads the routes from the served contract (/contract/openapi.json) and sends each one a request signed out, signed in, under a method it doesn't declare, and with a malformed body. It also covers /mcp and every tool it lists. No route was missing a header, so src/http/respond.ts is unchanged.

This push makes the changes asked for in review, all in the test file:

  • want is now checked. An ask's expected status is passed through to Seen, and misses reports any answer whose status differs, as "answered X, wanted Y". The two 404 cases and the 429 now check their own status, so the test no longer compares a joined list of names and statuses. Temporarily changing the expected 404 to 403 made the test fail with that message, so the check is live.
  • The first sign-in request is checked too. It fills the one-attempt window, is added to the results so its headers are checked, and the test asserts it was not itself a 429. A comment says it is there to fill the window.
  • signin.per_forge is put back. The test saves the value before setting it to 1 and restores it in a finally, so no later test in the file inherits the lowered limit.
  • How to skip a route is documented. The header comment now says to skip a route by name, never by kind: filter it out of ops in beforeAll with a comment saying why, and list it in the exemptions paragraph.

bun scripts/gate.ts --fix and bun x tsc --noEmit are clean, and bun test test/api-security-floor.test.ts passes 7 of 7. To try it, run bun test test/api-security-floor.test.ts.

🤖 Generated with Claude Code

Approved

I opened the preview. / answers 200, and an unknown /api/v1 path answers 404 with content-security-policy, x-content-type-options: nosniff and referrer-policy. There are no pictures in this change, because it only adds a test. I read the diff and did not run the test. All four earlier asks are met: the expected status is now checked, the first sign-in request is judged, signin.per_forge is restored in a finally, and the comment says to skip a route by name, never by kind. Next, both non-blocking: (1) call the MCP tools once as the admin against a throwaway repository, so the signed-in path is swept; (2) send malformed JSON signed in to the bodiless POSTs and DELETEs.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs