Repository navigation
feat(codec): MaxDepth on DecodeOptions & more bounds testing - #625
Merged
Merged
Conversation
Adds a MaxDepth field to dagcbor.DecodeOptions and dagjson.DecodeOptions that caps the nesting depth of decoded structures. Exceeding the limit returns a new ErrDecodeDepthExceeded error. Defaults to 1024.
willscott
approved these changes
Apr 23, 2026
willscott
left a comment
Member
There was a problem hiding this comment.
useful to be able to trace / prevent loops with this
There was a problem hiding this comment.
Pull request overview
Adds configurable maximum decode nesting depth to DAG-JSON and DAG-CBOR decoders, plus expanded boundary/bounds tests.
Changes:
- Add
MaxDepthtodagjson.DecodeOptionsand enforce it during decode with a newErrDecodeDepthExceededsentinel. - Add
MaxDepthtodagcbor.DecodeOptionsand enforce it in the recursive unmarshal logic. - Add new tests for MaxDepth and additional DAG-CBOR decoder/tokenizer boundary conditions.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
codec/dagjson/unmarshal.go |
Introduces MaxDepth support and depth tracking in DAG-JSON unmarshal recursion. |
codec/dagjson/options_test.go |
Adds DAG-JSON tests covering default/custom MaxDepth and lookahead interactions. |
codec/dagcbor/unmarshal.go |
Adds MaxDepth option and threads depth through CBOR unmarshal functions with checks at collection opens. |
codec/dagcbor/unmarshal_test.go |
Adds MaxDepth tests and new boundary tests for CBOR decoding/tokenization behavior. |
Comments suppressed due to low confidence (1)
codec/dagjson/unmarshal.go:333
- The MaxDepth check runs immediately on
tok.TMapOpen, before the ParseLinks/ParseBytes lookahead can collapse DAG-JSON link/bytes objects into leaf nodes. This means a deeply nested structure whose leaf is a link/bytes object can be rejected even though the decoded structure doesn’t actually add another map level. Consider moving the depth check to apply only when you actuallyBeginMap(i.e., after lookahead determines it’s not a link/bytes), or otherwise excluding the link/bytes special cases from depth accounting to match the comment’s “decoded structures” semantics.
case tok.TMapOpen:
if depth >= st.options.maxDepth() {
return ErrDecodeDepthExceeded
}
// dag-json has special needs: we pump a few tokens ahead to look for dag-json's "link" pattern.
// We can't actually call BeginMap until we're sure it's not gonna turn out to be a link.
if st.options.ParseLinks {
gotLink, err := st.linkLookahead(na, tokSrc)
if err != nil { // return in error if any token peeks failed or if structure looked like a link but failed to parse as CID.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
rvagg
force-pushed
the
rvagg/decode-depth-limit
branch
from
April 23, 2026 03:11
f09fe09 to
4790928
Compare
rvagg
added a commit
to rvagg/cborg
that referenced
this pull request
Oct 28, 2026
Nesting of arrays, maps and tags beyond maxDepth throws a decode error instead of overflowing the stack. Set to Infinity to disable. Input nested more than 256 levels now throws, where it previously decoded on runtimes with enough stack. Ref: ipld/go-ipld-prime#625 (1024 cap) Ref: ipld/go-ipld-prime#611 Ref: ipld/serde_ipld_dagcbor#25 (256 cap)
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.
MaxDepthfield todagcbor.DecodeOptionsanddagjson.DecodeOptionsthat caps the nesting depth of decoded structures. Exceeding the limit returns a newErrDecodeDepthExceedederror. Defaults to 1024.