Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should RegistryXmlRepository close the regkey between reads? #58224

Open
1 task done
debracey opened this issue Oct 3, 2024 · 5 comments
Open
1 task done

Should RegistryXmlRepository close the regkey between reads? #58224

debracey opened this issue Oct 3, 2024 · 5 comments
Labels
area-dataprotection Includes: DataProtection
Milestone

Comments

@debracey
Copy link

debracey commented Oct 3, 2024

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
@martincostello martincostello added the area-dataprotection Includes: DataProtection label Oct 3, 2024
@amcasey
Copy link
Member

amcasey commented Oct 3, 2024

I think the usual pattern is that the creator of the object disposes of it - normally, it's not a class or method that accepts it as an argument. I'm certainly open to examples or guidance showing evidence of a different approach.

In the meantime, it seems like we mostly want to update the doc comment on PersistKeysToRegistry to let people know they shouldn't close the key?

I could also imagine having the registry close and re-open the regkey on demand, but I haven't thought through all the consequences of that.

@debracey
Copy link
Author

debracey commented Oct 3, 2024

@amcasey Agree that normally the creator would dispose of the object. In this case it becomes a bit annoying for the consumer since they'd have to maintain a reference to the (opened) registry key for (potentially) the whole life of the application.

@amcasey
Copy link
Member

amcasey commented Oct 3, 2024

@debracey Can you elaborate? At that point, wouldn't you just declare it without a using? The finalizer should close the regkey.

@amcasey
Copy link
Member

amcasey commented Oct 3, 2024

Here's where DataProtection creates its own instance (as a fallback). It looks like it just doesn't use a using. Are you aware of a concrete problem that holding that key open causes?

@debracey
Copy link
Author

debracey commented Oct 4, 2024

@amcasey Yeah - what I meant to say was that, if you want to manually close/dispose the key, you'd have to maintain a reference to that key for the whole life of the application. Since the finalizer will take care of it, there's no practical reason to do this.

Yes - the workaround I plan to use is to just remove the using statement from the example code shared previously.

I am not aware of any problem with leaving the key open. We only found this issue after we were testing some stylecop/sonarqube suggestions and found that the suggestion(s) failed our pipelines with the exception mentioned above.

@amcasey amcasey changed the title RegistryXmlRepository Does Not Dispose Registry Key Should RegistryXmlRepository close the regkey between reads? Oct 4, 2024
@amcasey amcasey added this to the Backlog milestone Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dataprotection Includes: DataProtection
Projects
None yet
Development

No branches or pull requests

3 participants