Skip to content

Load the duplicate attribute PFX under a lock - #130875

Merged
vcsjones merged 3 commits into
dotnet:mainfrom
vcsjones:dup-attribute-loader-lock
Jul 18, 2026
Merged

vcsjones merged 3 commits into
dotnet:mainfrom
vcsjones:dup-attribute-loader-lock

Conversation

@vcsjones

Copy link
Copy Markdown
Member

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

@vcsjones vcsjones self-assigned this Jul 16, 2026
Copilot AI review requested due to automatic review settings July 16, 2026 14:23
@vcsjones vcsjones added area-System.Security test-bug Problem in test source code (most likely) labels Jul 16, 2026
@azure-pipelines

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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

Copilot AI review requested due to automatic review settings July 16, 2026 14:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 5/5 changed files
  • Comments generated: 3

@vcsjones
vcsjones enabled auto-merge (squash) July 17, 2026 22:10
Copilot AI review requested due to automatic review settings July 17, 2026 22:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 5/5 changed files
  • Comments generated: 0 new

@vcsjones
vcsjones merged commit e7c3d65 into dotnet:main Jul 18, 2026
90 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 19, 2026
@vcsjones
vcsjones deleted the dup-attribute-loader-lock branch July 19, 2026 15:35
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Security test-bug Problem in test source code (most likely)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test Failure: System.Security.Cryptography.X509Certificates.Tests.X509CertificateLoaderPkcs12Tests_FromByteSpan.VerifyNamesWithDuplicateAttributes

3 participants