Repository navigation
Build the DataProtection AAD template lazily - #70192
Merged
Merged
Conversation
Intermediate protectors in a purpose chain are only used to create child protectors, so defer building their additional authenticated data template until first Protect/Unprotect. The newest purpose is still validated eagerly. Co-authored-by: Copilot <[email protected]>
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
The review lacks a frozen evidence bundle with pinned head/base revisions and existing feedback, preventing independent verification of revision matching and feedback deduplication.
No open issues 🎉
What changed in this PR
This PR reduces DataProtection purpose-chain construction work by deferring additional authenticated data (AAD) template creation until the first protect or unprotect operation.
Changes:
- Lazily builds and atomically publishes each protector’s AAD template, avoiding template allocations for unused intermediate protectors.
- Preserves eager validation of newly added purposes and existing per-key caching behavior.
- Adds tests for invalid-purpose exceptions, AAD byte compatibility, cache reuse, and concurrent first use.
| File | Description |
|---|---|
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/AdditionalAuthenticatedDataTemplateTests.cs | Adds validation, compatibility, caching, and concurrency tests. |
| src/DataProtection/DataProtection/src/KeyManagement/KeyRingBasedDataProtector.cs | Defers AAD template construction while preserving eager purpose validation. |
Review effort: Balanced
Member
|
/review |
DeagleGross
approved these changes
Nov 5, 2026
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 16:50 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 16:50 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 16:51 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 16:53 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 16:55 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 16:55 — with
GitHub Actions
Inactive
Contributor
|
✅ No documentation PR was created. Confidence that documentation is needed: 5% No docs needed: internal refactoring (lazy AAD template construction) with unchanged behavior. Documentation surfaces considered
|
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 16:56 — with
GitHub Actions
Inactive
This branch was previously deployed
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.
Build the DataProtection AAD template lazily
Description
Every
KeyRingBasedDataProtectorbuilt its additional authenticated data (AAD) template (magic || keyId || purposeCount || purposes…, UTF-8 encoded) in its constructor. In a purpose chain such asprovider.CreateProtector("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware", "Cookies", "v2"), plus the application discriminator thatAddDataProtectionadds, every intermediate protector built and encoded a template for its prefix of the chain. That is O(n²) UTF-8 work, but only the leaf protector ever protects or unprotects.The template is now built on the first
Protect/Unprotect(GetAadForKey) and published withInterlocked.CompareExchange. The cache works as before: the template is cached per protector instance and updated for the default key on protect. To keepCreateProtectorbehavior unchanged, the constructor still eagerly validates the newly added purpose with the sameSecureUtf8Encoding.GetByteCount. An invalid purpose, such as a lone surrogate, still throwsEncoderFallbackExceptionfromCreateProtector. Earlier purposes in the chain were already validated when their protectors were created (the only constructor callers areKeyRingBasedDataProtectionProvider.CreateProtectorwith no prior purposes, andKeyRingBasedDataProtector.CreateProtectorwith its own validatedPurposes).Found by profiling (EventPipe allocation sampling):
BuildAadTemplateBytesaccounted for about 60% of allocations inCreateProtectorchains.Benchmarks
Linux x64, 2 cores, BenchmarkDotNet in-process, 3 interleaved rounds vs main (median). Provider from
AddDataProtection(includes the app discriminator), default algorithms.CreateProtector(cookie chain)onlyCreateProtector(cookie chain)+ Protect 100 BCreateProtector(antiforgery, single purpose)+ Protect 100 BThe single-purpose case has no intermediate protectors to save. It pays 8 bytes for the extra reference kept in the template struct.
Security reasoning
BuildAadTemplateBytesbuilds the template from the same purposes, and the key id is written exactly as before. A new test checks that the lazily built AAD equals the eagerly built template. Existing tests cover byte-level compatibility ofBuildAadTemplateBytes(ReturnsSameResultAsPreviousImplementation) and protect/unprotect round-trips.Interlocked.CompareExchange, so concurrent first callers agree on one immutable template (new concurrency test). The existingVolatileread/write logic for key changes is unchanged.CreateProtector(new test). No public API change, no PublicAPI.*.txt changes, no unsafe code added. No crypto, comparison or validation logic is touched, and the key ring is still not resolved byCreateProtector.Overlap
No overlap with #70146 (managed encryptor internals), #70187 (string overloads) or #70191 (TimeLimited).
Tests
Microsoft.AspNetCore.DataProtection.Tests: 340 passed, including 3 new tests.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)