Fix OverflowException in COSE decoder for oversized integer header labels#126095
Fix OverflowException in COSE decoder for oversized integer header labels#126095
Conversation
…ypto Wrap CborReader.ReadInt32 calls in COSE decode paths to catch OverflowException and surface it as CryptographicException. - Create Helper.cs with ReadInt32ForCrypto method - Use helper in CoseMessage.DecodeBucket and MissingCriticalHeaders - Add OverflowException to CoseHeaderMap.ValidateInsertion catch filter - Add regression test for oversized header label input Fixes #126036 Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/f94f350b-5d2b-4098-9ccc-da39835cf4a4
Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/f94f350b-5d2b-4098-9ccc-da39835cf4a4
Rename Helper class to CborReaderExtensions and make ReadInt32ForCrypto an extension method so call sites read as reader.ReadInt32ForCrypto() instead of Helper.ReadInt32ForCrypto(reader). Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/6b8f5520-dcdf-4dad-81f4-6fb435f5deb1
Preserve the inner exception when lifting ArgumentException to CryptographicException in DecodeBucket, so the root cause (OverflowException) is available on InnerException. Add Assert.IsType<OverflowException> to the second test for consistency. Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/34df0375-6578-4ef5-824f-bc970b0f71b6
Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/34df0375-6578-4ef5-824f-bc970b0f71b6
|
Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security |
There was a problem hiding this comment.
Pull request overview
Fixes COSE decode behavior so oversized CBOR integer header labels / critical header entries no longer leak raw OverflowException and are surfaced as CryptographicException (with the original OverflowException preserved as InnerException), matching the library’s error-shaping expectations for malformed inputs.
Changes:
- Added
CborReader.ReadInt32ForCrypto()extension to wrapReadInt32()overflow intoCryptographicException. - Updated COSE decode paths (
DecodeBucket,MissingCriticalHeaders) to use the new helper and to preserve inner exceptions when liftingArgumentExceptiontoCryptographicException. - Updated header value validation (
CoseHeaderMap.ValidateInsertion) to treatOverflowExceptionas a CBOR/value validation failure and wrap it consistently. - Added regression tests for both overflow paths.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Security.Cryptography.Cose/tests/CoseMessageTests.DecodeSign1.cs | Adds regression coverage asserting CryptographicException with OverflowException inner for oversized label / crit element inputs. |
| src/libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/CoseMessage.cs | Uses overflow-wrapping int reads during header-label and crit-header processing; preserves inner exceptions when rethrowing. |
| src/libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/CoseHeaderMap.cs | Wraps OverflowException from CBOR validation as ArgumentException (consistent with other invalid CBOR/value errors). |
| src/libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/CborReaderExtensions.cs | Introduces ReadInt32ForCrypto() to convert OverflowException into CryptographicException with inner exception preserved. |
| src/libraries/System.Security.Cryptography.Cose/src/System.Security.Cryptography.Cose.csproj | Includes the new CborReaderExtensions.cs in the build. |
…lowException Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/75a1e4fe-b17e-442e-a227-95a1cc4a94c4
🤖 Copilot Code Review — PR #126095Note This review was generated by GitHub Copilot and should be treated as advisory. A human reviewer should make the final decision. Holistic AssessmentMotivation: The PR fixes a real bug where Approach: Wrapping Summary: Detailed Findings✅ Correctness — Decode path fix is soundThe three call sites in The remaining production ✅ InnerException preservation — Good improvementThe change from
|
Description
CoseMessage.DecodeSign1throws rawOverflowExceptioninstead ofCryptographicExceptionwhen CBOR header labels or critical header values contain integers that overflowInt32. This affects two decode paths: header map label parsing and critical header array element parsing.Changes
CborReaderExtensions.ReadInt32ForCrypto— Extension method onCborReaderthat wrapsReadInt32(), catchingOverflowExceptionand rethrowing asCryptographicExceptionwith the original asInnerExceptionCoseMessage.DecodeBucket— Usesreader.ReadInt32ForCrypto()for header label reads; preservesInnerExceptionwhen liftingArgumentExceptiontoCryptographicExceptionCoseMessage.MissingCriticalHeaders— Usesreader.ReadInt32ForCrypto()for crit header array elementsCoseHeaderMap.ValidateInsertion— Usesreader.ReadInt32ForCrypto()for crit header integer reads, consistent with all other CBOR integer decode pathsCryptographicExceptionwithOverflowExceptioninnerFixes #126036
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.