Repository navigation
fix(security): bin owner tokens and reject framing headers in /response-headers - #275
Conversation
POST /bins returns a random owner token one time. The store keeps only the SHA-256 hash. Send the token as Authorization: Bearer on management routes. A missing token returns HTTP 401. A wrong token returns HTTP 404, the same as a missing bin. GET /bins returns only the bin for that token. The capture route stays open. Co-authored-by: Jared Wray <me@jaredwray.com>
The route accepts only an RFC 7230 header name. It rejects framing names and a name that starts with a colon. An invalid header returns HTTP 400. The process stays up. Co-authored-by: Jared Wray <me@jaredwray.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
| const matches = ownerHashMatches(doc?.tokenHash, token); | ||
| if (!doc || !matches) { |
There was a problem hiding this comment.
🟡 Medium - Existing persisted bins become inaccessible after upgrade
When a deployment upgrades while its Keyv store still contains bins created before owner-token support, those documents have no tokenHash and every HTTP management request is denied as unauthorized to the bin. Because captures and ordinary updates preserve the legacy document without assigning a token, existing captured requests cannot be read, cleared, or deleted through the documented management API until the bins expire, with persistent Worker/Mongo bins affected across deployments.
Show fix
Provide a migration or compatibility path for legacy documents before enforcing ownership, such as assigning and exposing a new owner token for each legacy bin during upgrade, or explicitly migrate/expire and communicate the affected records. Ensure the migration persists the token hash so subsequent captures retain manageable ownership.
More info - Reply on this comment to give feedback or ignore the issue.
There was a problem hiding this comment.
This behavior is intentional. Do not add a migration that issues a new token.
A bin saved before this change has no owner. ownerHashMatches returns false when tokenHash is missing (src/mock-http-bins.ts, lines 33-44). authorizeOwner then returns { ok: false } (lines 149-151). The management route sends HTTP 404 when the caller presents a token, and HTTP 401 when the caller sends no token. The two results do not show that the bin exists.
There is no previous owner who can receive a new token. If the first caller receives a token, anonymous access returns. That is the defect this change closes.
The capture route stays open and does not mint a token. A capture must not become the owner.
The default bin lifetime is 24 hours. Keyv drops the old bin at the end of that lifetime. The pull request states this break at the top. In-process mock.bins methods can still read a bin in this process. They do not use the HTTP token.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #275 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 44 44
Lines 1865 2012 +147
Branches 409 434 +25
==========================================
+ Hits 1865 2012 +147 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The echo object uses a null prototype, so the key stays an own field. Rollback removes each header that the route already set. A strict 400 schema no longer turns other 400 responses into HTTP 500. Co-authored-by: Jared Wray <me@jaredwray.com>
createBin does not replace an id that is already in use. A read, clear, or delete checks the token on that same document. A stale token cannot act on a replacement bin. Co-authored-by: Jared Wray <me@jaredwray.com>
A bin id of length 4 or less walks the whole space. Create fails only when every id is already in use. A long id still uses a small number of random attempts. Co-authored-by: Jared Wray <me@jaredwray.com>
Two stores that share one Keyv wait on the same bin id. A second create does not replace the first bin. idLength must be a positive integer. A fraction throws at construction. Co-authored-by: Jared Wray <me@jaredwray.com>
Keep main's header error text. Reject content-length and host as well as the names from main. A bad header value returns HTTP 400. Co-authored-by: Jared Wray <me@jaredwray.com>
Breaking change. Management routes for bins now require an owner token.
Migration
POST /binsreturnstokenone time. The server stores only the SHA-256 hash.Send the token on later management calls:
Authorization: Bearer <token>This header is required for these routes:
GET /binsGET /bins/:idGET /bins/:id/requestsGET /bins/:id/requests/:reqIdDELETE /bins/:id/requestsDELETE /bins/:idSend no token and the route returns HTTP 401. Send a token that does not match and the route returns HTTP 404. A missing bin returns the same HTTP 404. The body does not show that the bin exists.
GET /binsreturns only the bin for the presented token. It does not return other bins.The capture route
/b/:idstays open. Do not send the owner token there.In-process
mock.binsmethods do not require the token.createBin()also returnstoken.Please check if the PR fulfills these requirements
What kind of change does this PR introduce?
Bug fix. This is a breaking API change.
Findings fixed
HIGH 1. Bins had no access control. The audit commit is
6abbcb83. Any caller could list, read, clear, and delete any bin, including capturedAuthorization,Cookie, andstripe-signatureheaders.POST /binsnow returns a 32-byte owner token in base64url. The store keeps the SHA-256 hash. Management routes compare that hash withcrypto.timingSafeEqual.HIGH 2.
GET /response-headers?trailer=x, or a POST body with atrailerkey, crashed the process withERR_HTTP_TRAILER_INVALIDwhen helmet is on. With http2 on,transfer-encoding,keep-alive, andupgradealso crashed it. The route now accepts only an RFC 7230 header name. It rejectstrailer,transfer-encoding,content-length,connection,keep-alive,upgrade,te,proxy-connection,host, and a name that starts with:. The response is HTTP 400 JSON.reply.headeris wrapped, so a remaining invalid header does not abort the process.Out of scope
These findings stay out of scope: bin size limits,
/rangeamplification, the/healthzleak, the TLS listener, and PEM echo.Test evidence
pnpm testpassed. 588 tests passed. 1 test skipped. Line coverage is 100%.Test audit
The new tests protect caller-visible contracts. The old tests do not cover those contracts.
Rewrote
GET /bins lists created bins. The old test protected public enumeration. That contract is the vulnerability. The new test checks that the list contains only the bin for the presented token.Rewrote the unknown-bin 404 cases so they send a bearer token. A request with no token now returns 401. The 404 contract remains when a token does not match.
Updated the worker tests that read, list, or delete a bin. They now send the owner token. They still protect storage behavior.
Changed the worker runtime list check from HTTP 200 with an empty list to HTTP 401.
Added tests for a prototype response key, invalid JSON on
/response-headers, a full one-character id space, a stale token on a replacement bin, two stores that share one Keyv, and a rejected fractionalidLength.No test was deleted.