-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix Base64{Url} incorrectly handling small destinations when inputs contain whitespace #123223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…contains whitespace When ignoreWhiteSpace is true, the DestinationTooSmallExit path now falls through to InvalidDataFallback, which properly handles whitespace and determines if the actual content fits in the destination. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…cumentation Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
Tagging subscribers to this area: @dotnet/area-system-buffers |
src/libraries/System.Memory/tests/Base64Url/Base64UrlDecoderUnitTests.cs
Show resolved
Hide resolved
src/libraries/System.Memory/tests/Base64Url/Base64UrlDecoderUnitTests.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request fixes a bug in Base64{Url}.DecodeFromUtf8 and DecodeFromChars where they incorrectly return DestinationTooSmall when the input contains whitespace and the destination is small but sufficient for the actual data.
Changes:
- Fixed the core decoder logic to fall through to whitespace-handling fallback when
ignoreWhiteSpaceis true at theDestinationTooSmallExitlabel - Added comprehensive regression tests for the bug scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64DecoderHelper.cs | Added fallthrough to InvalidDataExit when ignoreWhiteSpace is true at DestinationTooSmallExit, allowing whitespace to be stripped and destination size to be re-evaluated |
| src/libraries/System.Memory/tests/Base64Url/Base64UrlDecoderUnitTests.cs | Added three test methods covering: decoding with whitespace into small destination for UTF8 and chars, and verifying genuine DestinationTooSmall errors are still reported correctly |
src/libraries/System.Memory/tests/Base64Url/Base64UrlDecoderUnitTests.cs
Show resolved
Hide resolved
Tests embedded whitespace scenarios (e.g., "z A==") which trigger the block-wise decoding fallback path for both DecodeFromUtf8 and DecodeFromChars. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
src/libraries/System.Memory/tests/Base64Url/Base64UrlDecoderUnitTests.cs
Show resolved
Hide resolved
Added similar tests for the Base64 decoder that were already present for Base64Url, since both share the same underlying implementation. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Tests that whitespace-only inputs work correctly with an empty (size 0) destination buffer, since decoding produces 0 output bytes. Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Description
Base64{Url}.DecodeFromUtf8andDecodeFromCharsincorrectly returnDestinationTooSmallwhen input contains whitespace and destination is small but sufficient for actual data.Root cause:
DecodeFromcalculatesmaxSrcLengthfrom total source length (including whitespace). WhendestLength < decodedLength - 2, it jumps toDestinationTooSmallExitbefore the whitespace-handling fallback can strip whitespace and re-evaluate.Fix: At
DestinationTooSmallExit, whenignoreWhiteSpaceis true, fall through toInvalidDataFallbackwhich properly handles whitespace and determines if the actual content fits.Base64DecoderHelper.cs: Added fallthrough toInvalidDataExitwhenignoreWhiteSpaceis trueBase64UrlDecoderUnitTests.cs: Added tests for leading/trailing whitespace scenarios that reproduce the issueBase64UrlDecoderUnitTests.cs: Added tests for embedded whitespace scenarios (e.g., "z A==") that trigger theDecodeWithWhiteSpaceBlockwiseWrapperpathBase64DecoderUnitTests.cs: Added similar tests for the Base64 decoder since both Base64 and Base64Url share the same underlying implementationOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.