Repository navigation
Description
Description
A tag (major type 6) must be followed by a data item. CborReader enforces this everywhere except at the end of a root-level value sequence: with AllowMultipleRootLevelValues = true, when the data ends immediately after a root-level tag header, PeekState() returns CborReaderState.Finished instead of reporting the malformed content, even though the document is not well-formed.
CborWriter cannot produce such encoding, so this only arises with untrusted or corrupt input.
Inside nested contexts the same condition is detected correctly (PopDataItem and the break-byte path in PeekStateCore both throw CborContentException with "A CBOR tag should always be followed by a data item."). The root-level end-of-data path is the only place a dangling tag escapes detection, because the Finished branch in PeekStateCore only checks _currentMajorType is null && _definiteLength is null and ignores _isTagContext.
A secondary inconsistency follows from the same root cause in the incremental reading mode added by #133602: the end-of-sequence guard in PeekInitialByte (_offset > 0 || _itemsRead > 0) does not account for a pending tag being the only thing consumed, so equivalent reader states produce different exception types. #133602 deliberately matched the shipped Finished behavior for consistency rather than changing it and this issue tracks fixing it for both modes.
Reproduction Steps
// Lax is used only so the same options work for the incremental case below
// (non-final blocks require Lax); the dangling-tag behavior itself is
// independent of the conformance mode.
var options = new CborReaderOptions
{
ConformanceMode = CborConformanceMode.Lax,
AllowMultipleRootLevelValues = true
};
// 1. Single buffer: dangling root-level tag reported as Finished
var reader = new CborReader(new byte[] { 0x01, 0xC1 }, options); // 1, then tag(1) with no value
reader.ReadInt32();
reader.ReadTag();
Console.WriteLine(reader.PeekState()); // Finished (expected: malformed-content error)
reader.ReadInt32(); // throws InvalidOperationException
// "No more CBOR data items to read in the current context."
// 2. Incremental, tag is the only consumed token: same state, different exception
reader = new CborReader(new byte[] { 0xC1 }, options, isFinalBlock: false);
reader.ReadTag();
reader.SlideData(ReadOnlyMemory<byte>.Empty, isFinalBlock: true);
Console.WriteLine(reader.PeekState()); // Finished
reader.ReadInt32(); // throws CborContentException
// "Unexpected end of CBOR encoding data."
reader.TrySkipValue(); // throws InvalidOperationExceptionExpected behavior
A root-level value sequence ending in a dangling tag is not well-formed, so PeekState() should throw CborContentException ("A CBOR tag should always be followed by a data item."), consistent with how the same condition is reported inside nested contexts.
Actual behavior
| Scenario (after consuming the dangling tag) | PeekState() |
ReadInt32() |
|---|---|---|
Single buffer 01 C1 (or C1), final block |
Finished |
InvalidOperationException |
Incremental 01 + C1, then empty final block |
Finished |
InvalidOperationException |
Incremental C1 only, then empty final block |
Finished |
CborContentException |
Regression?
No. Reporting Finished for a dangling root-level tag has been the behavior since System.Formats.Cbor shipped in .NET 5.
Known Workarounds
Callers can treat PeekState() == Finished observed immediately after ReadTag() as malformed data.
Configuration
No response
Other information
Relevant code (src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/):
CborReader.PeekState.cs,PeekStateCoreend-of-buffer branch: returnsFinishedwhen_currentMajorType is null && _definiteLength is null, without checking_isTagContext.CborReader.cs,PeekInitialByte: the end-of-sequence guard(_offset > 0 || _itemsRead > 0)misses the consumed-tag-only case (ReadTagadvances the offset but not_itemsRead, andSlideDatarebases the offset to 0); including_isTagContextwould make the incremental and single-buffer states indistinguishable.
Fixing PeekState to throw is a behavioral change for the shipped final-block reader, which is why it was kept out of #133602 (see "Notes for reviewers" there).
Activity
dotnet-policy-service commentedon Oct 4, 2026
Tagging subscribers to this area: @dotnet/area-system-formats-cbor, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.