[release/8.0-staging] Fix erroneous success in AsnDecoder.ReadSequence #109596
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.
Backport of #109586 to release/8.0-staging
/cc @bartonjs
Customer Impact
Calls to AsnReader.ReadSequence, (internal) AsnValueReader.ReadSequence, or any method that indirectly calls them, will get an ArgumentOutOfRangeException if processing specific data, which stems from AsnDecoder.ReadSequence erroneously succeeding on particular malformed data.
This was originally reported as an unexpected ArgumentOutOfRangeException from a constructor of X509Certificate2, which can be reproduced on non-Windows systems when checking if the contents are PKCS#12/PFX. Particular malformed data may therefore provide unexpected exception flow to applications.
Regression
This incorrect behavior has always been present in AsnDecoder.ReadSequence, and has probably been present for reading certificates on Linux since before AsnDecoder was public API. In modern versions of .NET it appears as an OS-specific bug, so users may experience it as a "regression from Windows" or "regression from .NET Framework".
Testing
Additional tests are added in this change that will prevent a regression in this method, or related methods.
Risk
Low. Copious tests, both direct and indirect, ensure that well-formed data continues to function. The new tests introduced in this change add coverage for this particular sort of malformed data.