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 <[email protected]>
…cumentation Co-authored-by: stephentoub <[email protected]>
Copilot
AI
changed the title
[WIP] Fix Base64Url decoding for whitespace in small destinations
Fix Base64{Url} incorrectly handling small destinations when inputs contain whitespace
Jan 15, 2026
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-buffers |
stephentoub
reviewed
Jan 15, 2026
src/libraries/System.Memory/tests/Base64Url/Base64UrlDecoderUnitTests.cs
Show resolved
Hide resolved
stephentoub
approved these changes
Jan 15, 2026
stephentoub
reviewed
Jan 15, 2026
src/libraries/System.Memory/tests/Base64Url/Base64UrlDecoderUnitTests.cs
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment.
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 <[email protected]>
Co-authored-by: stephentoub <[email protected]>
MihaZupan
reviewed
Jan 15, 2026
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 <[email protected]>
Tests that whitespace-only inputs work correctly with an empty (size 0) destination buffer, since decoding produces 0 output bytes. Co-authored-by: MihaZupan <[email protected]>
MihaZupan
approved these changes
Jan 15, 2026
This was referenced Jan 16, 2026
2 tasks
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.
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.