Add MaxDepth property to CborReader and CborWriter#129245
Closed
eiriktsarpalis with Copilot wants to merge 3 commits into
Closed
Add MaxDepth property to CborReader and CborWriter#129245eiriktsarpalis with Copilot wants to merge 3 commits into
eiriktsarpalis with Copilot wants to merge 3 commits into
Conversation
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add MaxDepth property to CborReader
Add MaxDepth property to CborReader and CborWriter
Jun 10, 2026
| public static void MaxDepth_DefaultValue_ShouldBe64() | ||
| { | ||
| var writer = new CborWriter(); | ||
| Assert.Equal(64, writer.MaxDepth); |
eiriktsarpalis
approved these changes
Jun 10, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends System.Formats.Cbor with a configurable maximum nesting depth for both CborReader and CborWriter, exposed via new nullable-options constructors plus MaxDepth properties. The change adds enforcement points on container-opening operations (arrays, maps, and indefinite-length strings) and introduces new CborReaderOptions / CborWriterOptions public types.
Changes:
- Added new public options types (
CborReaderOptions,CborWriterOptions) and new nullable-options constructors, withMaxDepthexposed on reader/writer instances. - Implemented MaxDepth enforcement in reader/writer container entry points via
EnsureMaxDepthNotExceeded(), with reader throwingCborContentExceptionand writer throwingInvalidOperationException. - Added resource strings, updated ref surface + csproj compile items, and added/updated tests to validate defaulting and enforcement behavior.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.cs | Adds DefaultMaxDepth, MaxDepth property, nullable-options ctor, and depth enforcement helper. |
| src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Array.cs | Enforces MaxDepth on ReadStartArray(). |
| src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Map.cs | Enforces MaxDepth on ReadStartMap(). |
| src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.String.cs | Enforces MaxDepth on indefinite-length string start APIs. |
| src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReaderOptions.cs | New public options class for reader configuration, including MaxDepth. |
| src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Writer/CborWriter.cs | Adds DefaultMaxDepth, MaxDepth property, nullable-options ctor, and depth enforcement helper. |
| src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Writer/CborWriter.Array.cs | Enforces MaxDepth on WriteStartArray*. |
| src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Writer/CborWriter.Map.cs | Enforces MaxDepth on WriteStartMap*. |
| src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Writer/CborWriter.String.cs | Enforces MaxDepth on indefinite-length string start APIs. |
| src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Writer/CborWriterOptions.cs | New public options class for writer configuration, including MaxDepth and InitialCapacity. |
| src/libraries/System.Formats.Cbor/src/Resources/Strings.resx | Adds reader/writer MaximumDepthExceeded resource strings. |
| src/libraries/System.Formats.Cbor/src/System.Formats.Cbor.csproj | Includes new options source files in compilation list. |
| src/libraries/System.Formats.Cbor/ref/System.Formats.Cbor.cs | Updates reference surface with new public APIs/types. |
| src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.cs | Adds tests for reader MaxDepth reporting/defaulting/enforcement. |
| src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.SkipValue.cs | Updates deep-nesting SkipValue test to opt into a matching MaxDepth. |
| src/libraries/System.Formats.Cbor/tests/Writer/CborWriterTests.cs | Adds tests for writer MaxDepth reporting/defaulting/enforcement and options flow-through. |
Member
|
This PR is unwelcome, as I've already written it. |
This was referenced Jun 10, 2026
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.
Adds a configurable nesting-depth limit to
System.Formats.Cbor, guarding the reader against excessive work/stack pressure from deeply nested untrusted input and the writer against runaway/cyclic encoders. IntroducesCborReaderOptions/CborWriterOptions(per the approved API in issue #128087) rather than stacking another[EditorBrowsable(Never)]ctor overload.New public API
Changes
CborReaderOptions/CborWriterOptionswith eager setter validation (ConformanceModeand negativeMaxDepth/InitialCapacitythrowArgumentOutOfRangeException). DefaultConformanceModeisStrictvia backing field initializer.nulloptions yields legacy defaults.MaxDepthis 64;options.MaxDepth == 0means "use library default". TheMaxDepthproperty reports the effective limit, not the raw option value.EnsureMaxDepthNotExceeded()is invoked after the non-consumingPeekInitialBytebut beforeAdvanceBuffer/PushDataItem, so a rejected frame doesn't moveBytesConsumedand the reader locks onto the error state correctly (supports checkpoint rollback). Applies toReadStartArray/ReadStartMap/indefinite-length string starts, and transitively toSkipValue/ReadEncodedValue; the writer mirrors this on theWriteStart*paths. Tags do not push a frame and do not count toward depth.CborContentException(depth is a property of untrusted content); writer throwsInvalidOperationException(programming error, matchingUtf8JsonWriter).CborWriterOptions.MaxDepthdocuments the narrow intent (detect runaway/cyclic encoders) and explicitly disclaims being a defense against already-hydrated hostile object graphs.SkipValuestack-overflow test now opts into a matchingMaxDepth.Usage
Open question
DefaultMaxDepthis set to 64 (matchingSystem.Text.Jsonand the reference branch). An earlier proposal draft mentioned 1000 — worth a reviewer confirmation.