Skip to content

Should RegistryXmlRepository close the regkey between reads? #58224

Open
@debracey

Description

@debracey

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

RegistryXmlRepository takes a RegistryKey as a parameter in its constructor (code ref), but that key is never disposed. Furthermore, if you construct this class inside a using block (see example), the class could attempt to access a closed registry key.

Expected Behavior

  • Class should be disposable and dispose/close the key correctly.
  • Possibly the class should reopen the key? Not sure.

Steps To Reproduce

  1. Setup a standard ASP.NET core web app
  2. Enable data protection for the app, persisting the data to the registry
  3. Ensure that the registry key is disposed in the setup method
  4. Notice that, when the protection information is accessed, the code will fail with an exception
using var registryKey = RegistryKey
      .OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
      .OpenSubKey(some_registry_path, true);

dataProtectionBuilder.ProtectKeysWithDpapi(true).PersistKeysToRegistry(registryKey);                

Exceptions (if any)

"Type":"System.ObjectDisposedException", 
"Message":"Cannot access a closed registry key."
"Object name: 'key path set in code'."

.NET Version

8.0.400

Anything else?

  • Is the key even supposed to be closed/disposed?
  • If not, maybe the documentation for these extension methods/classes just needs updating

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions