Skip to content

Fix eager load of currentKeyRing on resolving IDataProtector - #67465

Merged
DeagleGross merged 3 commits into
dotnet:mainfrom
DeagleGross:deaglegross-dp-eager-key-load-fix
Jul 2, 2026
Merged

DeagleGross merged 3 commits into
dotnet:mainfrom
DeagleGross:deaglegross-dp-eager-key-load-fix

Conversation

@DeagleGross

Copy link
Copy Markdown
Member

The reported bug eagerly resolves encryptor in order to determine whether IAuthenticatedEncryptor supports Span-based APIs (in other words is ISpanAuthenticatedEncryptor):

        var currentKeyRing = _keyRingProvider.GetCurrentKeyRing();
        var encryptor = currentKeyRing.DefaultAuthenticatedEncryptor;

However, calling _keyRingProvider.GetCurrentKeyRing() is not free, and in case keys are persisted to an external store, we now load them (which may be problematic in startup scenarios).


The easy fix is to always return KeyRingBasedSpanDataProtector which is ISpanDataProtector - even if underlying IAuthenticatedEncryptor is not ISpanAuthenticatedEncryptor it simply fallbacks to allocatey byte[] APIs.

However that is a bit misleading in the usage scenarios:

class MyService
{
   private IDataProtector _dataProtector;
   
   public void DoDataProtection(Span<byte> input)
   {
      if (_dataProtector is ISpanDataProtector spanDataProtector)
      {
         spanDataProtector.TryProtect(input...); // ALSO calls byte[] under the hood - undiscoverable
      }
      else
      {
         _dataProtector.Protect(input); // byte[] API
      }
   }
}

So we can probably work on introducing new ISpanDataProtectorProvider:

namespace Microsoft.AspNetCore.DataProtection;

+ public interface ISpanDataProtectionProvider : IDataProtectionProvider
{
+  ISpanDataProtector CreateSpanDataProtector(string purpose);
}

which will match the type with the expectations. But I think fallback is anyway a good choice, so we can simply keep it.

Fixes #67447

@DeagleGross DeagleGross self-assigned this Jun 29, 2026
Copilot AI review requested due to automatic review settings June 29, 2026 15:39
@DeagleGross DeagleGross added bug This issue describes a behavior which is not expected - a bug. area-dataprotection Includes: DataProtection labels Jun 29, 2026

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 pull request fixes a startup-time regression in Data Protection where resolving an IDataProtector could eagerly load the current key ring (and therefore hit external key storage) by changing KeyRingBasedDataProtectionProvider.CreateProtector to avoid calling IKeyRingProvider.GetCurrentKeyRing() during protector creation.

Changes:

  • Update KeyRingBasedDataProtectionProvider.CreateProtector to always return KeyRingBasedSpanDataProtector on .NET, deferring key-ring resolution until Protect/Unprotect.
  • Adjust KeyRingBasedSpanDataProtector commentary around the non-span-encryptor fallback path.
  • Add a regression test ensuring CreateProtector does not call GetCurrentKeyRing.
Show a summary per file
File Description
src/DataProtection/DataProtection/src/KeyManagement/KeyRingBasedDataProtectionProvider.cs Removes eager key-ring resolution by returning the span-capable protector on .NET without probing the encryptor up front.
src/DataProtection/DataProtection/src/KeyManagement/KeyRingBasedSpanDataProtector.cs Updates the fallback-path commentary when the encryptor doesn’t implement span-based APIs.
src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/KeyRingBasedDataProtectorTests.cs Adds a regression test asserting CreateProtector does not resolve the key ring.

Copilot's findings

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

@DeagleGross
DeagleGross merged commit 8ef91a5 into dotnet:main Jul 2, 2026
25 checks passed
@DeagleGross
DeagleGross deleted the deaglegross-dp-eager-key-load-fix branch July 2, 2026 18:20
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-dataprotection Includes: DataProtection bug This issue describes a behavior which is not expected - a bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data Protection: CreateProtector reads the key store eagerly on .NET 11 (was lazy until first Protect/Unprotect on .NET 10)

4 participants