Repository navigation
DRAFT security: harden merchant key lookup (do not merge until J5 PASS and owner approval) - #85
Merged
Conversation
…in counters The merchant key from X-Merchant-Key / the verify body was pasted unescaped into a Stripe Search query. Allowlist the exact key shape first, require stored metadata to equal the presented key, and use a one-way reference instead of the raw key (or its first 16 chars) in the verify rate limit name and toll payer id. Adds an offline selftest with fake keys.
ceedot-rock
marked this pull request as ready for review
October 21, 2026 23:15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Status: DRAFT. Not for merge. Waits for J5 PASS and the owner's approval. Nothing has been deployed.
Problem
The merchant key from the X-Merchant-Key header (and the /api/verify body) was pasted unescaped into a Stripe Search query in
findSubscriptionByMerchantKey(src/lib/stripe.ts). A crafted value could widen the query and match another merchant's subscription. Callers: /api/rider/issue, /api/verify, /api/v0/citizen-receipts, toll billing (resolveTollPayer).Change
src/lib/merchant-key-core.mjs: strict allowlist^merchant_live_[0-9a-f]{48}$(exact length) checked before any query is built. A malformed key never reaches Stripe.findSubscriptionByMerchantKeynow goes through that module and, after the search, requires the storedmetadata.merchant_keyto equal the presented key (constant time). A search hit alone no longer grants access./api/verify: rejects malformed keys with 400 before lookup; the monthly usage counter name uses a one-way reference (verify:<sha256 prefix>) instead of the raw key.src/lib/merchant-key.selftest.mjs(npm run selftest:merchant-key). Offline, fake keys only (single repeated hex digit). 20 hostile headers including ones built to match an active subscription; asserts Stripe is never queried for them, the pre-fix lookup is shown to be widened by the same inputs in an emulated search, and the post-search equality check holds even if Stripe returns the wrong subscription.Test result
node lib/merchant-key.selftest.mjspasses on Node 20.19 and Node 24.22.tsc --noEmitreports no errors in the touched files.Known limits
/api/verifynow returns 400 for a malformed key where it used to returnvalid:false. Other routes return their existing 402..github/workflows/webpack.yml); editing a workflow is left to the owner.No real key appears in this PR.