-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix FromBase64Transform buffer sizing and output buffer handling #122805
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
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones |
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 PR fixes a buffer sizing bug in FromBase64Transform where partial blocks followed by power-of-two sized inputs could cause an ArgumentException due to undersized buffer rental from CryptoPool.
Key Changes:
- Fixed
CryptoPool.Rent()calls to usebytesToTransform(retained + new bytes) instead ofinputCount(new bytes only) - Added comprehensive tests to prevent regression of the specific bug scenario
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Base64Transforms.cs |
Fixed buffer sizing in TransformBlock and TransformFinalBlock by changing CryptoPool.Rent(inputCount) to CryptoPool.Rent(bytesToTransform) in both methods |
src/libraries/System.Security.Cryptography/tests/Base64TransformsTests.cs |
Added two regression tests covering TransformBlock and TransformFinalBlock with partial blocks followed by power-of-two inputs |
…rsized output buffer Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
Fixed in e0622e7. Added handling for Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
src/libraries/System.Security.Cryptography/tests/Base64TransformsTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
main PR
Description
This PR fixes two bugs in
FromBase64Transform:1. CryptoPool rental sizing bug:
TransformBlockandTransformFinalBlockrent fromCryptoPoolusinginputCountinstead ofbytesToTransform. When retained bytes exist from a previous partial block (_inputIndex > 0) and the new input size is near a power of two, the rented buffer is undersized.2. Output buffer too small handling (#122807): When
Base64.DecodeFromUtf8returnsOperationStatus.DestinationTooSmall, the code was hitting aDebug.Assert(fails in debug builds) and then throwingFormatException(incorrect). The fix properly handles this case by throwingArgumentOutOfRangeException("outputBuffer")instead.Customer Impact
ArgumentException: Destination is too shortwhen usingFromBase64Transformwith partial blocks followed by inputs near power-of-two sizes.FormatException(instead ofArgumentOutOfRangeException) when the output buffer is too small.Regression
Unknown. Code has been unchanged since 2023.
Testing
TransformBlock_PartialBlockFollowedByPowerOfTwoInput_DoesNotThrowTransformFinalBlock_PartialBlockFollowedByPowerOfTwoInput_DoesNotThrowTransformBlock_OutputBufferTooSmall_ThrowsArgumentOutOfRangeExceptionRisk
Low. The changes are minimal:
inputCounttobytesToTransformin theCryptoPool.Rentcalls (both variables are already computed and used in the same methods)OperationStatus.DestinationTooSmallexplicitly before the existingInvalidDatahandlingPackage authoring no longer needed in .NET 9
IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.