Skip to content

feat(codec): MaxDepth on DecodeOptions & more bounds testing - #625

Merged
rvagg merged 2 commits into
masterfrom
rvagg/decode-depth-limit
Apr 23, 2026
Merged

rvagg merged 2 commits into
masterfrom
rvagg/decode-depth-limit

Conversation

@rvagg

@rvagg rvagg commented Apr 23, 2026

Copy link
Copy Markdown
Member

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 willscott left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useful to be able to trace / prevent loops with this

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds configurable maximum decode nesting depth to DAG-JSON and DAG-CBOR decoders, plus expanded boundary/bounds tests.

Changes:

  • Add MaxDepth to dagjson.DecodeOptions and enforce it during decode with a new ErrDecodeDepthExceeded sentinel.
  • Add MaxDepth to dagcbor.DecodeOptions and 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 actually BeginMap (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.

Comment thread codec/dagjson/options_test.go Resolved
Comment thread codec/dagcbor/unmarshal_test.go Resolved
@rvagg
rvagg force-pushed the rvagg/decode-depth-limit branch from f09fe09 to 4790928 Compare April 23, 2026 03:11
@rvagg
rvagg merged commit d5efcf7 into master Apr 23, 2026
13 checks passed
@rvagg
rvagg deleted the rvagg/decode-depth-limit branch April 23, 2026 03:29
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants