[release/7.0] Base64.Decode: fixed latent bug for non-ASCII inputs #76812
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 #76795 to release/7.0
/cc @adamsitnik @gfoidl
Customer Impact
.NET 6 and earlier didn't have this bug. Without that fix in .NET 7 Base64.Decode has a bug in the recognization of non-ASCII / non-Base64 characters that leads to decoding of invalid data -- especially in the case of decoding via buffer-chains where up to consumed / written it should be assumed the data is valid. With the bug, this constraint is violated, meaning that customers could operate on invalid data.
As Base64 is used for e.g. security tokens too, that could be also a security hole.
Testing
Unit tests got added to repro the faulty behavior, and to ensure that the fix works.
Also manual debugging in a console app, to see where the bug originated.
The input values, which are added in the unit tests, are obtained by a fuzz-test run by me via sharpfuzz.
Risk
Low. The bug got introduced in the .NET 7 timeframe and wasn't there in .NET 6 and previous versions.
The additional bitwise and, that fixes the bug, is negligible perf-wise.
IMPORTANT: Is this backport for a servicing release? If so and this change touches code that ships in a NuGet package, please make certain that you have added any necessary package authoring and gotten it explicitly reviewed.