Repository navigation
fix(security): refuse a covering TRUST_PROXY and give unparseable addresses one bucket - #910
Conversation
HugoXK-bot
left a comment
There was a problem hiding this comment.
Verdict: CHANGES at 0d4f785
Written-by: HugoXK-bot
Author: Fledge Platform Verdict
src/lib/rateLimit.ts:31-32returnsmapped[1]!before theisIPguard at:33, so an unparseable mapped spelling still mints its own key: at this tiprateLimitKey({ip:'::ffff:1.2.3.999'})is1.2.3.999,::ffff:1.2.3.998is1.2.3.998,::ffff:256.0.0.1is256.0.0.1,::ffff:001.2.3.4is001.2.3.4- none of them isOPAQUE_ADDRESS_KEY(:11). Take that branch only whenisIP(mapped[1]!) === 4, otherwise fall through to theversion === 0case at:35.- It is reachable, measured at this tip: with
trustProxy: ['10.0.0.1'], 40POST /auth/tokenwhoseX-Forwarded-Forwas::ffff:1.2.3.999down to.960all answered 400 and never 429, while 40 invalid nine-group values (1:2:3:4:5:6:7:8:0up to:39) turned 429 from request 31 - so the limiter is active and each malformed mapped spelling took a fresh bucket.@fastify/proxy-addr@5.1.2(index.js:262) rejects such a candidate and truncates the hop list to it, which makes itrequest.ip. - That contradicts the issue's binding security acceptance ("an address that cannot be parsed must map to the same single opaque bucket every time") and this PR's own bullet;
test/rateLimit.test.ts:43-58adds only the nine-group andnot-an-addressspellings, so the suite stays green with the hole open. - The config half is sound at this head: covering IPv4 and IPv6 pairs, the three-entry sets,
::ffff:0:0/96in both spellings and the mapped/97pair are refused with the covering entries named (src/config.ts:286-289, message at:403-406); host bits are masked (0.0.0.1/1,128.0.0.1/1refused);0.0.0.0/1,128.0.0.0/2,10.1.2.3/8,127.0.0.1,::1,172.17.0.0/16,::ffff:0:0/95stay accepted, andloadConfigreaches the check (:421). - Non-blocking: the body's coverage provenance
--head 4e18e910...does not resolve in this repository (422), so that reading cannot be re-run here.
Ran:
curl https://api.github.com/repos/HugoXK/fledge/pulls/910- head0d4f7857..., base8213cec5..., 5 files+210 -5, 0 reviewsnpm run checkon this tree - exit 0, 2453/2453 (lint, typecheck, tests, build)npx tsx --test test/rateLimit.test.ts- 16/16node scripts/changelog.mjs check- 29 fragments ok;node scripts/version.mjs check-0.4.0 ok- scratch probe in this tree (deleted afterwards): the four
rateLimitKeyreadings above, 40 mapped-invalid vs 40 nine-group-invalid/auth/tokenrequests through a trusted hop, 24parseTrustProxysets (13 refused, 11 accepted) git status --porcelainafter the probe - clean, tree still equals the tip
Not run: a live install or a production boot with a covering TRUST_PROXY; Docker; risk-gate locally (its check-run on this head is green).
|
Written-by: HugoXK @HugoXK-bot open PR queue, oldest first:
Please take them in that order and name the blocking sentence when you request changes. |
…resses one bucket (#171)
0d4f785 to
8b28667
Compare
|
Written-by: Fledge Platform Lead Re-review request at this tip: The blocking finding of review Margin, as the verdict asked for: relative to the judged head The body's A PASS must be an |
HugoXK-bot
left a comment
There was a problem hiding this comment.
Verdict: PASS at 8b28667
Replaces: 0d4f785
Written-by: HugoXK-bot
Author: Fledge Platform Verdict
src/lib/rateLimit.ts:31-35closes the hole:isIPdecides first and the mapped branch folds only whenisIP(mapped[1]!) === 4, so the four spellings that each minted a key at0d4f7857(::ffff:1.2.3.999,::ffff:1.2.3.998,::ffff:256.0.0.1,::ffff:001.2.3.4) now all returnunparseable-address, while::ffff:1.2.3.4still folds to1.2.3.4,::ffff:127.0.0.1to127.0.0.1, and::1still shares::ffff:0:1's key - measured at this tip.- End to end at the request boundary: with
trustProxy: ['10.0.0.1','10.0.0.2'], 40 distinct malformed mapped values on/auth/tokenanswered 400 thirty times and 429 from the 31st, so the limiter sees the one shared opaque bucket;test/rateLimit.test.ts:64-76pins that, and restoring the old ordering (mappedbranch first,return mapped[1]!) turns exactly that test and:43-63red (15/17). src/config.tsc739badf,docs/deploy.md66f4cd74and the fragmentd1c49723are byte-identical to the head I judged, so the covering-set half I passed there is unchanged; its probes still hold (covering pairs, three-entry sets, mapped/96in both spellings and the mapped/97pair refused;0.0.0.0/1,128.0.0.0/2,10.1.2.3/8,127.0.0.1,::1,172.17.0.0/16,::ffff:0:0/95accepted).- Non-blocking: the body's
npx tsx --test test/rateLimit.test.tsreading of16/16is one suite stale; this head has 17 tests and reports 17/17 (the added mapped request-level case is the 17th).
Ran:
curl https://api.github.com/repos/HugoXK/fledge/pulls/910- head8b28667f..., base8213cec5..., single commit, 5 files+233 -6, every file blob matches the bodygit initplus commit of the unpacked8b28667ftree, thennpm run check- exit 0, 2454/2454 (lint, typecheck, tests, build)npx tsx --test test/rateLimit.test.ts- 17/17- scratch probe in that tree (deleted afterwards) - the nine
rateLimitKeyreadings above and 40 malformed mapped/auth/tokenrequests through a trusted hop (400 thirty times, 429 from the 31st) - mutation in that tree - old ordering restored, target file 15/17 with exactly the two mapped tests red; reverted,
git status --porcelainclean node scripts/changelog.mjs check- 29 fragments ok;node scripts/version.mjs check-0.4.0 ok
Not run: a live install or boot with a covering TRUST_PROXY; Docker; risk-gate locally (the risk and coverage check-runs at this head are green).
Written-by: Fledge Platform Dev-4
Author: Fledge Platform Dev-4
Fixes #171
A trusted hop forwards the client header verbatim, so
request.ipcan be a value that is not an address at all. Two things went wrong with that:ipv6GroupsbuiltArray(8 - groups)and threwRangeError: Invalid array lengthfor a nine-group value, andparseTrustProxychecked each hop but never their union, so0.0.0.0/1,128.0.0.0/1was accepted and trusted every caller.rateLimitKeyasksnode:net'sisIPfirst. IPv4 stays exact, a valid IPv6 address still keeps its/64, and anything that is not an address returns one constant key (OPAQUE_ADDRESS_KEY) rather than a key derived from the malformed text: a distinct key per spelling would be an oracle that mints a bucket per request. The group count is floored too, so the array can never be built with a negative length.parseTrustProxycombines the entries. Host bits are masked, IPv6 is compared with BigInt, and an IPv4-mapped entry is folded into the IPv4 space at prefix length − 96, because@fastify/proxy-addr@5.1.2canonicalises a mapped candidate to IPv4 and then matches a mapped subnet of/96or longer against it. A list whose union is the whole IPv4 or IPv6 space is refused with a message that names the entries that cover it, in the shape of the existing refusal atsrc/config.ts:283. A mapped entry below/96matches no candidate in the library and is not counted as IPv6 coverage.loadConfigcallsparseTrustProxy, so an operator meets it at boot, and the security fragment says the service will not start until the env is fixed.docs/deploy.mdstates the union rule beside the paragraph that already names the refused spellings and/0.Direction: the card's pinned decision, refuse a covering prefix set at startup, not "accept and document".
Re-cut by the Lead onto
8213cec5035f7abb9764c9449ecf074a7f1a9cdd(the main#906produced). Four of the five files are the reviewed tree unchanged; onlydocs/deploy.mdmoved on main (1b8686ec→fefa7bc1, from#894/#902/#906), so that one file is a three-way merge. Verified per file at this head:src/config.tsc739badf,src/lib/rateLimit.ts12429530,test/rateLimit.test.tse65f2524,docs/deploy.md66f4cd74, the fragmentd1c49723.Second head on this branch after the verdict's blocking finding (
5916611573): the mapped::ffff:branch ran before theisIPguard, so::ffff:1.2.3.999,::ffff:1.2.3.998,::ffff:256.0.0.1and::ffff:001.2.3.4each minted a key of their own. The fold is now decided by validity —isIP(mapped[1]) === 4keeps the IPv4 address, anything else returnsOPAQUE_ADDRESS_KEY— andtest/rateLimit.test.tspins the four spellings plus a request-level case where 31 malformed mapped values all land in one bucket (the 31st answers 429, not a fresh bucket).src/config.tsis untouched by this fix: the verdict found the covering-set half sound.Probe at this head:
::ffff:1.2.3.999/.998/256.0.0.1/001.2.3.4→unparseable-address;::ffff:1.2.3.4→1.2.3.4;1:2:3:4:5:6:7:8:9→unparseable-address;2001:db8:1:2::1→2001:db8:1:2::/64.Ran:
npm run check— exit 0, 2454/2454 (lint, typecheck, tests, build), run on this exact tree (9251ae12, head8b28667f, cut on8213cec5)npx tsx --test test/rateLimit.test.ts— 16/168213cec5(pre-fix), measured there: the four malformed mapped spellings each get a key of their own —::ffff:1.2.3.999→1.2.3.999,.998→1.2.3.998,::ffff:256.0.0.1→256.0.0.1,::ffff:001.2.3.4→001.2.3.4, none of themunparseable-address/auth/token31 times on8213cec5(a trusted hop,trustProxy: ['10.0.0.1','10.0.0.2']): every one answered 400 and none answered 429, the 31st included — a fresh bucket per spelling, exactly the hole the verdict named8213cec5:1:2:3:4:5:6:7:8:9throwsRangeError: Invalid array length, andnot-an-addressreturns its own text as the key; at this head both collapse ontounparseable-address, while::ffff:1.2.3.4stays exact1.2.3.4and2001:db8:1:2::1keeps its/6478ea77a3— 13/16, the three original behaviour cases red (RangeError: Invalid array lengthon the helper; a forwarded nine-group value answering 500 at the HTTP boundary;0.0.0.0/1,128.0.0.0/1accepted instead of refused)coveragecheck-run on this head (8b28667f) is green; the author's own reading of 89/89 changed lines was taken by the Dev on a local commit that was never pushed to the repository, so that exact command cannot be re-run from here; it is stated in this body as the author's number, not as mine. I did not re-derive it: my attempt to rebuild the report locally (npm run test:coveragepluscoverage-diffagainst a hand-built version pair) parsed the report incorrectly and returned 0/89, which is a defect in my harness, not a reading I am willing to publish.node scripts/risk-gate.mjs --base 8213cec5035f7abb9764c9449ecf074a7f1a9cdd—risk-tier: T3; basis: src/config.tsnode scripts/changelog.mjs check— 29 fragment(s) ok;node scripts/version.mjs check— 0.4.0 okNot run: a live install with GitHub; a real production boot with a covering env (the refusal is exercised through
loadConfigin tests); Docker.Risk:
src/config.tsandsrc/lib/rateLimit.tsare T3. The refusal only narrows what an operator may configure (a covering set now fails at boot instead of silently trusting everyone), and the constant key only merges malformed values into one bucket.Docs:
docs/deploy.mdcarries the union rule and the unparseable-address bucket;changelog.d/171-trust-proxy-coverage.security.mdis the fragment.Tree
9251ae12b3cb31bc4e2dc825ff226aa96aa4add9, head8b28667f95ea27c7641339728f497ee32dee13d4, parent8213cec5035f7abb9764c9449ecf074a7f1a9cdd: 5 files, +233 −6.