Skip to content

der: accept the documented maximum nesting depth - #2424

Open
zegroged wants to merge 1 commit into
RustCrypto:masterfrom
zegroged:der/nesting-depth-64
Open

der: accept the documented maximum nesting depth#2424
zegroged wants to merge 1 commit into
RustCrypto:masterfrom
zegroged:der/nesting-depth-64

Conversation

@zegroged

@zegroged zegroged commented Sep 6, 2026

Copy link
Copy Markdown

Fixes the off-by-one from #2392.

Position::split_nested increments a counter that starts at 0 and then checks depth < MAX_DEPTH, so the 64th nested SEQUENCE is rejected with NestingDepth while MAX_DEPTH is 64 and tests/nesting.rs calls 64 "the expected maximum depth". Measured against master (7637997):

depth=62 -> OK
depth=63 -> OK
depth=64 -> Err(NestingDepth)
depth=65 -> Err(NestingDepth)

Changes:

  • reader/position.rs: <<=, so exactly MAX_DEPTH levels decode and MAX_DEPTH + 1 still fails.
  • tests/nesting.rs: the message builder is now a shared helper, and there's a new test for the boundary itself. The old test only checked that 65 errors; nothing checked that 64 succeeds, which is how this slipped through. The new test fails on the old code and passes with this change — I ran it both ways.

I asked on the issue whether 64 is actually meant to be accepted (#2392 (comment)) and didn't want to sit on this waiting for an answer, so opening it now to cover both cases. If 63 is the intended ceiling, say so and I'll turn this into a comment/test fix instead of a code change.

Closes #2392

`Position::split_nested` checks `depth < MAX_DEPTH` after incrementing a
counter that starts at zero, so the 64th nested SEQUENCE is rejected with
`ErrorKind::NestingDepth` while `MAX_DEPTH` is 64 and the test module
describes 64 as the expected maximum. Against master this decodes 63
levels and fails on 64.

Comparing with `<=` makes exactly `MAX_DEPTH` levels decode, and
`MAX_DEPTH + 1` still fails.

The nesting test only asserted that `MAX_DEPTH + 1` errors; nothing
asserted that `MAX_DEPTH` itself succeeds, which is why this never
surfaced. The message builder is now a shared helper and there is a
second test for the boundary itself. That test fails on the previous
code and passes with this change -- checked by running it both ways.

Closes RustCrypto#2392
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.

der: nested reader depth limit may reject the documented maximum depth of 64

1 participant