Repository navigation
Load the duplicate attribute PFX under a lock - #130875
Merged
Merged
Conversation
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates X509/PFX unit tests to prevent concurrent imports of the “duplicate attribute” PKCS#12 payload, by routing access through a helper that serializes PFX loading. This aims to reduce Windows-only flakiness where concurrent imports with the same named key can end up sharing key containers, leading to “keyset not found” failures after disposal.
Changes:
- Make the duplicate-attributes PFX bytes private and expose access via a
TestData.WithDuplicateAttributesPfx(...)callback helper. - Update the affected tests to import the duplicate-attributes PFX through that callback instead of directly referencing the raw byte array.
- Minor refactoring of the test call sites to use the new callback-based access pattern.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Security.Cryptography/tests/X509Certificates/PfxTests.cs | Updates the legacy X509Certificate2 ctor-based test to import the duplicate-attributes PFX via the new callback helper. |
| src/libraries/Common/tests/System/Security/Cryptography/X509Certificates/X509CertificateLoaderPkcs12Tests.WindowsAttributes.cs | Routes duplicate-attributes PFX import through the new helper to ensure serialized loading. |
| src/libraries/Common/tests/System/Security/Cryptography/X509Certificates/X509CertificateLoaderPkcs12Tests.cs | Updates the duplicate-attributes loader test to use the new helper. |
| src/libraries/Common/tests/System/Security/Cryptography/X509Certificates/X509CertificateLoaderPkcs12CollectionTests.cs | Updates the collection loader test to use the new helper. |
| src/libraries/Common/tests/System/Security/Cryptography/X509Certificates/TestData.cs | Makes DuplicateAttributesPfx private and adds WithDuplicateAttributesPfx helper for lock-serialized access. |
Copilot's findings
- Files reviewed: 5/5 changed files
- Comments generated: 4
bartonjs
approved these changes
Jul 17, 2026
vcsjones
enabled auto-merge (squash)
July 17, 2026 22:10
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
The "duplicate attribute" PFX we use in unit tests is notable because it has named keys, keyname000 and keyname001. Windows's PFX loader has a race where if two PFXs with the same named key are loaded concurrently, they may end up sharing the same private key instances. Disposing one of the certificates will remove the keys out from under the other certificate, resulting in a "keyset not found" error.
To address this, we make sure the duplicate attribute PFX has to be loaded under a lock. It's only the loading that needs to be locked, not the whole lifetime of the certificate. When the load happens serially, the Windows PFX loader is aware that a key with that name already exists, so it appropriately generates a random name instead. Our tests don't actually care what the imported name ends up being, just what it doesn't end up being.
This callback mechanism is not foolproof - someone could just return the raw bytes from the callback and use it anyway. But this is a unit test, not product code, so it's fine.
Fixes #128047