Skip to content

Fixup a few dataprotection and antiforgery edge cases#66508

Merged
BrennanConroy merged 3 commits into
mainfrom
brecon/fixup
Apr 29, 2026
Merged

Fixup a few dataprotection and antiforgery edge cases#66508
BrennanConroy merged 3 commits into
mainfrom
brecon/fixup

Conversation

@BrennanConroy
Copy link
Copy Markdown
Member

@BrennanConroy BrennanConroy commented Apr 28, 2026

With the recent dataprotection issue, I had AI look at the dataprotection code a bit more closely and found a few edge cases we can handle better.

  1. DefaultAntiforgeryTokenGenerator should be checking the ClaimUidBytes regardless of whether the original token has any, this lets us validate that if a different user connects with a previous token (or the same user but they have new claims and so token doesn't match)
  2. DefaultAntiforgeryTokenGenerator was using SequenceEqual when it should be using a constant-time comparison
  3. Read7BitEncodedInt didn't handle int32 overflow (it just returned 0). FormatterBinaryReader.Read7BitEncodedInt throws FormatException in this case, so updated to have the same behavior
  4. The span-based protect/unprotect methods assumed a span-based encryptor from the key ring. While that should be the case 99.99% of the time, it's technically not required to be true, so added a fallback path
  5. There were some stackalloc byte[255] which bothered me a bit, 256 is the normal value we use 😆

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Handles several edge cases across antiforgery and data protection, aligning behavior with framework expectations and tightening validation logic.

Changes:

  • Antiforgery: always attempt claim UID extraction during validation and use constant-time claim UID comparison.
  • Shared encoding: make Read7BitEncodedInt throw FormatException on int32 overflow (including invalid 5th byte).
  • DataProtection: add fallback paths when span-based protect/unprotect is used with a non-span encryptor; normalize small stackalloc buffers to 256 bytes.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/Shared/test/Shared.Tests/Encoding/Int7BitEncodingUtilsTests.cs Adds regression test for 5th-byte overflow in 7-bit int decoding.
src/Shared/Encoding/Int7BitEncodingUtils.cs Adds explicit overflow detection for invalid 5th byte in 7-bit int decoding.
src/DataProtection/benchmarks/Microsoft.AspNetCore.DataProtection.MicroBenchmarks/Benchmarks/SpanDataProtectorComparison.cs Updates benchmark stackalloc size from 255 to 256.
src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Managed/ManagedAuthenticatedEncryptorTests.cs Updates test comment to reflect 256-byte initial stackalloc size.
src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/KeyRingBasedDataProtectorTests.cs Adds regression tests for span protector fallback when encryptor isn’t span-capable.
src/DataProtection/DataProtection/src/Managed/ManagedAuthenticatedEncryptor.cs Uses stackalloc[256] for output sizes up to and including 256.
src/DataProtection/DataProtection/src/Managed/AesGcmAuthenticatedEncryptor.cs Uses stackalloc[256] for output sizes up to and including 256.
src/DataProtection/DataProtection/src/KeyManagement/KeyRingBasedSpanDataProtector.cs Adds fallback to byte[] path when span encryptor is unexpectedly unavailable.
src/DataProtection/DataProtection/src/Cng/CngGcmAuthenticatedEncryptor.cs Uses stackalloc[256] for output sizes up to and including 256.
src/DataProtection/DataProtection/src/Cng/CbcAuthenticatedEncryptor.cs Uses stackalloc[256] for output sizes up to and including 256.
src/Antiforgery/test/DefaultAntiforgeryTokenGeneratorTest.cs Adds regression test for username token validation when user now has extractable claims; updates one existing test setup.
src/Antiforgery/src/Internal/DefaultAntiforgeryTokenGenerator.cs Always extracts claim UID bytes for authenticated users and switches to constant-time UID comparison.

Comment thread src/Antiforgery/test/DefaultAntiforgeryTokenGeneratorTest.cs Outdated
Comment on lines 201 to 205
return false;
}

return token.ClaimUid.GetData().SequenceEqual(claimUidBytes);
return CryptographicOperations.FixedTimeEquals(token.ClaimUid.GetData(), claimUidBytes);
}
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AreIdenticalClaimUids can reach FixedTimeEquals(token.ClaimUid.GetData(), claimUidBytes) even when claimUidBytesExtracted is false. Since claimUidBytes is stackalloc'd and not initialized when extraction fails, this risks nondeterministic behavior (and, in theory, a false positive). Consider returning false immediately when token.ClaimUid is non-null but claimUidBytesExtracted is false, before the length check / compare.

Copilot uses AI. Check for mistakes.
Comment thread src/Shared/test/Shared.Tests/Encoding/Int7BitEncodingUtilsTests.cs Outdated
@BrennanConroy BrennanConroy merged commit 5a0718f into main Apr 29, 2026
25 checks passed
@BrennanConroy BrennanConroy deleted the brecon/fixup branch April 29, 2026 02:07
@dotnet-policy-service dotnet-policy-service Bot added this to the 11.0-preview5 milestone Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-dataprotection Includes: DataProtection

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants