security(MEDIUM): bound canonicalJson recursion (DoS via deep JSON + Idempotency-Key)#365
Open
CryptoJones wants to merge 1 commit into
Open
Conversation
…ware
`canonicalJson()` recurses once per nesting level. A POST that
includes an Idempotency-Key header reaches `hashBody(req.body) ->
canonicalJson(...)` synchronously inside the middleware. With Node's
default ~10000-frame call stack, depth ~5000 blows the stack with
RangeError — which Express's async error path surfaces as a 500.
Reach: any POST endpoint mounted under /v1 (every CREATE handler and
every /bulk handler). Body limit is 100KB (env-tunable), allowing up
to ~20000 nesting levels at 5 chars per level (`{"a":`), so a
single 25KB payload reliably reproduces the 500.
The rate limiter caps abuse at ~100 requests / 15min, so this isn't a
sustained-DoS vector — but it IS a reliable cheap 500 trigger, and a
caught DoS is better than a swallowed one.
Verified with a one-liner before the fix:
$ node -e "const {canonicalJson}=require('./app/middleware/idempotency.js');
function n(d){let o=0;for(let i=0;i<d;i++)o={a:o};return o}
try{canonicalJson(n(5000))}catch(e){console.log(e.name)}"
RangeError
Fix:
- canonicalJson takes a `depth` arg (default 0) and throws a tagged
CanonicalJsonDepthError when depth exceeds MAX_CANONICAL_DEPTH=64.
64 is well above any plausible API body's nesting; the limit is
purely a DoS guard, not a correctness boundary.
- The middleware wraps hashBody() in a typed catch. On
CanonicalJsonDepthError it returns a clean 400
`{ message, code: 'body_too_deep' }` and short-circuits. The
message is hardcoded (not echoed from `error.message`) so it
passes the controller-error-shape policy that bans
`message: error.message` in middleware/controllers.
- MAX_CANONICAL_DEPTH and CanonicalJsonDepthError are added to the
module exports so unit tests can pin them.
Tests added:
tests/unit/idempotency.test.js (6 new tests under "bounded recursion"):
- shallow nesting succeeds
- depth at the limit succeeds
- depth + 5 throws CanonicalJsonDepthError (NOT RangeError)
- arrays bounded by the same depth
- the previously-DoS-able depth (5000) is rejected cleanly
- hashBody surfaces the same error so the middleware can catch
tests/api/idempotency.test.js (2 new tests under "DoS defense"):
- depth-5000 raw-string body returns 400 (NOT 500)
- normal-depth body with Idempotency-Key still passes through
Note on test construction: the API test builds the 5000-deep payload
as a raw JSON STRING and uses .send(payload) with explicit Content-
Type, because JSON.stringify itself is recursive and would overflow
before supertest could transmit the body. express.json's parser is
iterative (V8 >= 9) so the body reaches req.body intact.
804 tests pass (was 800); lint clean.
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.
canonicalJson()recurses once per nesting level. A POST thatincludes an Idempotency-Key header reaches
hashBody(req.body) -> canonicalJson(...)synchronously inside the middleware. With Node'sdefault ~10000-frame call stack, depth ~5000 blows the stack with
RangeError — which Express's async error path surfaces as a 500.
Reach: any POST endpoint mounted under /v1 (every CREATE handler and
every /bulk handler). Body limit is 100KB (env-tunable), allowing up
to ~20000 nesting levels at 5 chars per level (
{"a":), so asingle 25KB payload reliably reproduces the 500.
The rate limiter caps abuse at ~100 requests / 15min, so this isn't a
sustained-DoS vector — but it IS a reliable cheap 500 trigger, and a
caught DoS is better than a swallowed one.
Verified with a one-liner before the fix:
$ node -e "const {canonicalJson}=require('./app/middleware/idempotency.js');
function n(d){let o=0;for(let i=0;i<d;i++)o={a:o};return o}
try{canonicalJson(n(5000))}catch(e){console.log(e.name)}"
RangeError
Fix:
deptharg (default 0) and throws a taggedCanonicalJsonDepthError when depth exceeds MAX_CANONICAL_DEPTH=64.
64 is well above any plausible API body's nesting; the limit is
purely a DoS guard, not a correctness boundary.
CanonicalJsonDepthError it returns a clean 400
{ message, code: 'body_too_deep' }and short-circuits. Themessage is hardcoded (not echoed from
error.message) so itpasses the controller-error-shape policy that bans
message: error.messagein middleware/controllers.module exports so unit tests can pin them.
Tests added:
tests/unit/idempotency.test.js (6 new tests under "bounded recursion"):
- shallow nesting succeeds
- depth at the limit succeeds
- depth + 5 throws CanonicalJsonDepthError (NOT RangeError)
- arrays bounded by the same depth
- the previously-DoS-able depth (5000) is rejected cleanly
- hashBody surfaces the same error so the middleware can catch
tests/api/idempotency.test.js (2 new tests under "DoS defense"):
- depth-5000 raw-string body returns 400 (NOT 500)
- normal-depth body with Idempotency-Key still passes through
Note on test construction: the API test builds the 5000-deep payload
as a raw JSON STRING and uses .send(payload) with explicit Content-
Type, because JSON.stringify itself is recursive and would overflow
before supertest could transmit the body. express.json's parser is
iterative (V8 >= 9) so the body reaches req.body intact.
804 tests pass (was 800); lint clean.
Self-review caveats:
{"a":...}is paid regardless. Tightening further would require an express.json depth option (not in the stable API today) or a custom body-parser.