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

Refactor DataProtection trimming #41650

Merged
merged 4 commits into from
May 13, 2022
Merged

Conversation

JamesNK
Copy link
Member

@JamesNK JamesNK commented May 12, 2022

Fixes #41411
Fixes #24705

Result of the discussion with Levi and Hao about how DataProtection is used:

  • Custom type names are rare
  • Registry policy is also rare. Mostly used by administrators who want to override crypto settings machine-wide. Adding a config setting to opt into registry policy isn't ideal for this situation because now the machine admin also needs to know that all apps have the setting enabled.

Since custom type names are rare, this PR removes build warnings. It's possible to get runtime errors, but this change tries to make them useful.

This PR:

  • Removes trimming warnings from public APIs
  • Suppresses warnings at a lower level
  • Provides friendly error message if a custom type name is used and trimmed
  • Statically references any built-in types, e.g. deserializers, encryptors

@JamesNK JamesNK added the area-dataprotection Includes: DataProtection label May 12, 2022
@JamesNK JamesNK requested a review from Pilchie as a code owner May 12, 2022 05:34
@HaoK
Copy link
Member

HaoK commented May 12, 2022

Can we add a test somewhere to verify the warning experience, i.e. a trimmed app test that references a type that fails to deserialize?

@JamesNK
Copy link
Member Author

JamesNK commented May 13, 2022

Testing trimming is difficult because you need to build a new app, publish it, then run it.

However, I can add a test that verifies a type that can't be found throws the friendly error message.

Comment on lines 480 to 484
else if (type == typeof(CngCbcAuthenticatedEncryptorDescriptorDeserializer))
else if (type == typeof(CngCbcAuthenticatedEncryptorDescriptorDeserializer) && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return _activator.CreateInstance<CngCbcAuthenticatedEncryptorDescriptorDeserializer>(descriptorDeserializerTypeName);
}
else if (type == typeof(CngGcmAuthenticatedEncryptorDescriptorDeserializer))
else if (type == typeof(CngGcmAuthenticatedEncryptorDescriptorDeserializer) && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Copy link
Member

Choose a reason for hiding this comment

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

Is this netstandard 2.0? These APIs aren't trim friendly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it targets netstandard2.0.

What specifically isn't trim-friendly? No warnings are coming from the linker.

Copy link
Member

Choose a reason for hiding this comment

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

My bad, I meant that RuntimeInformation.IsOSPlatform(OSPlatform.Windows) isn't recognized by the trimmer. Those branches will exist when this is trimmed and we're targeting linux.

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it. I added IsOSPlatform to suppress errors from build, not for trimming.

A couple of types won't matter. I don't think introducing a net6-window target is worth it.

@davidfowl
Copy link
Member

davidfowl commented May 13, 2022

This might still fail in weird ways. Even if the external type is loaded, it would be missing things that it depends on that weren't preserved. We're going to let that fail randomly in that case right? (TBH, nothing can be done about that...)

@JamesNK
Copy link
Member Author

JamesNK commented May 13, 2022

Agreed.

Nice thing is these changes are internal and conservative. If people run into problems we can't revisit them in the future.

@JamesNK JamesNK merged commit 2c2074b into main May 13, 2022
@JamesNK JamesNK deleted the jamesnk/dataprotection-trimming-redux branch May 13, 2022 09:31
@ghost ghost added this to the 7.0-preview5 milestone May 13, 2022
@khellang
Copy link
Member

Hey @JamesNK! 👋🏻

I'm pretty sure these changes broke cross-version forwarding when using Azure.Extensions.AspNetCore.DataProtection.Keys. See Azure/azure-sdk-for-net#42223.

The code in XmlEncryptionExtensions used to call IActivator.CreateInstance directly. Since the KeyVault extensions register their own IActivator, which handles their own forwarding, it worked across versions, but after this change, it tries to call Type.GetType first, which results in the following System.IO.FileLoadException:

at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
   at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
   at System.Type.GetType(String typeName, Boolean throwOnError)
   at Microsoft.AspNetCore.DataProtection.XmlEncryption.XmlEncryptionExtensions.CreateDecryptor(IActivator activator, String decryptorTypeName)
   at Microsoft.AspNetCore.DataProtection.XmlEncryption.XmlEncryptionExtensions.DecryptElement(XElement element, IActivator activator)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager.Microsoft.AspNetCore.DataProtection.KeyManagement.Internal.IInternalXmlKeyManager.DeserializeDescriptorFromKeyElement(XElement keyElement)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.DeferredKey.<>c__DisplayClass1_0.<GetLazyDescriptorDelegate>g__GetLazyDescriptorDelegate|0()
   at System.Lazy`1.CreateValue()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Lazy`1.get_Value()
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyBase.get_Descriptor()
   at Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.CngGcmAuthenticatedEncryptorFactory.CreateEncryptorInstance(IKey key)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyBase.CreateEncryptor()
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRing.KeyHolder.GetEncryptorInstance(Boolean& isRevoked)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRing.GetAuthenticatedEncryptorByKeyId(Guid keyId, Boolean& isRevoked)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)

Since the old version of the decryptor cannot be found.

I'd send a PR to fix this, but I'm not 100% sure what the intention of the change in this PR was and how to work around it.

@JamesNK
Copy link
Member Author

JamesNK commented Feb 28, 2024

Hi

Create an issue with what you just said. PR welcome. @amcasey is working on data protection recently. Include both of us on the issue and PR.

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

Successfully merging this pull request may close these issues.

Improving DataProtection linkability DataProtection isn't linker friendly
4 participants