Skip to content

Commit 33ea420

Browse files
pranavkmJamesNK
andauthored
Enable trimming for data protection (#41118)
Co-authored-by: James Newton-King <[email protected]>
1 parent 0ab246d commit 33ea420

File tree

39 files changed

+395
-58
lines changed

39 files changed

+395
-58
lines changed

eng/TrimmableProjects.props

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
-->
88
<Project>
99
<ItemGroup>
10+
<TrimmableProject Include="Microsoft.AspNetCore.DataProtection.Abstractions" />
11+
<TrimmableProject Include="Microsoft.AspNetCore.Cryptography.Internal" />
12+
<TrimmableProject Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" />
13+
<TrimmableProject Include="Microsoft.AspNetCore.DataProtection" />
14+
<TrimmableProject Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" />
15+
<TrimmableProject Include="Microsoft.AspNetCore.DataProtection.Extensions" />
16+
<TrimmableProject Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" />
1017
<TrimmableProject Include="Microsoft.AspNetCore.Hosting.Abstractions" />
1118
<TrimmableProject Include="Microsoft.AspNetCore.Hosting" />
1219
<TrimmableProject Include="Microsoft.AspNetCore.Hosting.Server.Abstractions" />

src/DataProtection/Abstractions/src/DataProtectionCommonExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.Diagnostics.CodeAnalysis;
78
using Microsoft.AspNetCore.DataProtection.Abstractions;
89
using Microsoft.Extensions.Internal;
910

@@ -182,6 +183,7 @@ public static IDataProtector GetDataProtector(this IServiceProvider services, st
182183
/// <param name="protector">The data protector to use for this operation.</param>
183184
/// <param name="plaintext">The plaintext data to protect.</param>
184185
/// <returns>The protected form of the plaintext data.</returns>
186+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
185187
public static string Protect(this IDataProtector protector, string plaintext)
186188
{
187189
if (protector == null)
@@ -216,6 +218,7 @@ public static string Protect(this IDataProtector protector, string plaintext)
216218
/// <exception cref="System.Security.Cryptography.CryptographicException">
217219
/// Thrown if <paramref name="protectedData"/> is invalid or malformed.
218220
/// </exception>
221+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
219222
public static string Unprotect(this IDataProtector protector, string protectedData)
220223
{
221224
if (protector == null)

src/DataProtection/Abstractions/src/IDataProtector.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace Microsoft.AspNetCore.DataProtection;
57

68
/// <summary>
@@ -13,6 +15,7 @@ public interface IDataProtector : IDataProtectionProvider
1315
/// </summary>
1416
/// <param name="plaintext">The plaintext data to protect.</param>
1517
/// <returns>The protected form of the plaintext data.</returns>
18+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
1619
byte[] Protect(byte[] plaintext);
1720

1821
/// <summary>
@@ -23,5 +26,6 @@ public interface IDataProtector : IDataProtectionProvider
2326
/// <exception cref="System.Security.Cryptography.CryptographicException">
2427
/// Thrown if the protected data is invalid or malformed.
2528
/// </exception>
29+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
2630
byte[] Unprotect(byte[] protectedData);
2731
}

src/DataProtection/Abstractions/src/Microsoft.AspNetCore.DataProtection.Abstractions.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>ASP.NET Core data protection abstractions.
@@ -10,12 +10,14 @@ Microsoft.AspNetCore.DataProtection.IDataProtector</Description>
1010
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1111
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1212
<PackageTags>aspnetcore;dataprotection</PackageTags>
13-
<Nullable>enable</Nullable>
13+
<IsTrimmable>true</IsTrimmable>
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<Compile Include="..\..\shared\src\*.cs" />
17+
<Compile Include="..\..\shared\src\*.cs" LinkBase="Shared" />
1818
<Compile Include="$(SharedSourceRoot)WebEncoders\**\*.cs" />
19+
<Compile Include="$(SharedSourceRoot)TrimmingAttributes.cs" LinkBase="Shared"
20+
Condition="'$(TargetFramework)' != '$(DefaultNetCoreTargetFramework)'" />
1921
</ItemGroup>
2022

2123
</Project>

src/DataProtection/Cryptography.Internal/src/Microsoft.AspNetCore.Cryptography.Internal.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<PackageTags>aspnetcore;dataprotection</PackageTags>
10-
<Nullable>enable</Nullable>
10+
<IsTrimmable>true</IsTrimmable>
1111
</PropertyGroup>
1212

1313
</Project>

src/DataProtection/Cryptography.KeyDerivation/src/Microsoft.AspNetCore.Cryptography.KeyDerivation.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<PackageTags>aspnetcore;dataprotection</PackageTags>
10-
<Nullable>enable</Nullable>
10+
<IsTrimmable>true</IsTrimmable>
1111
<Nullable Condition="'$(TargetFramework)' == 'netstandard2.0'">annotations</Nullable>
1212
</PropertyGroup>
1313

src/DataProtection/DataProtection/src/ActivatorExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using Microsoft.AspNetCore.Cryptography;
67
using Microsoft.AspNetCore.DataProtection.Internal;
78
using Microsoft.Extensions.DependencyInjection;
@@ -17,6 +18,7 @@ internal static class ActivatorExtensions
1718
/// Creates an instance of <paramref name="implementationTypeName"/> and ensures
1819
/// that it is assignable to <typeparamref name="T"/>.
1920
/// </summary>
21+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
2022
public static T CreateInstance<T>(this IActivator activator, string implementationTypeName)
2123
where T : class
2224
{

src/DataProtection/DataProtection/src/AuthenticatedEncryption/ConfigurationModel/AuthenticatedEncryptorDescriptorDeserializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Linq;
67
using System.Xml.Linq;
78

@@ -16,6 +17,7 @@ public sealed class AuthenticatedEncryptorDescriptorDeserializer : IAuthenticate
1617
/// <summary>
1718
/// Imports the <see cref="AuthenticatedEncryptorDescriptor"/> from serialized XML.
1819
/// </summary>
20+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
1921
public IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element)
2022
{
2123
if (element == null)

src/DataProtection/DataProtection/src/AuthenticatedEncryption/ConfigurationModel/CngCbcAuthenticatedEncryptorDescriptorDeserializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Runtime.Versioning;
67
using System.Xml.Linq;
78

@@ -17,6 +18,7 @@ public sealed class CngCbcAuthenticatedEncryptorDescriptorDeserializer : IAuthen
1718
/// <summary>
1819
/// Imports the <see cref="CngCbcAuthenticatedEncryptorDescriptor"/> from serialized XML.
1920
/// </summary>
21+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
2022
public IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element)
2123
{
2224
if (element == null)

src/DataProtection/DataProtection/src/AuthenticatedEncryption/ConfigurationModel/CngGcmAuthenticatedEncryptorDescriptorDeserializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Runtime.Versioning;
67
using System.Xml.Linq;
78

@@ -17,6 +18,7 @@ public sealed class CngGcmAuthenticatedEncryptorDescriptorDeserializer : IAuthen
1718
/// <summary>
1819
/// Imports the <see cref="CngCbcAuthenticatedEncryptorDescriptor"/> from serialized XML.
1920
/// </summary>
21+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
2022
public IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element)
2123
{
2224
if (element == null)

src/DataProtection/DataProtection/src/AuthenticatedEncryption/ConfigurationModel/IAuthenticatedEncryptorDescriptorDeserializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using System.Xml.Linq;
56

67
namespace Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
@@ -15,5 +16,6 @@ public interface IAuthenticatedEncryptorDescriptorDeserializer
1516
/// </summary>
1617
/// <param name="element">The element to deserialize.</param>
1718
/// <returns>The <see cref="IAuthenticatedEncryptorDescriptor"/> represented by <paramref name="element"/>.</returns>
19+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
1820
IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element);
1921
}

src/DataProtection/DataProtection/src/AuthenticatedEncryption/ConfigurationModel/ManagedAuthenticatedEncryptorDescriptorDeserializer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Security.Cryptography;
67
using System.Xml.Linq;
78

@@ -16,6 +17,7 @@ public sealed class ManagedAuthenticatedEncryptorDescriptorDeserializer : IAuthe
1617
/// <summary>
1718
/// Imports the <see cref="ManagedAuthenticatedEncryptorDescriptor"/> from serialized XML.
1819
/// </summary>
20+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
1921
public IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element)
2022
{
2123
if (element == null)
@@ -46,6 +48,7 @@ public IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element)
4648

4749
// Any changes to this method should also be be reflected
4850
// in ManagedAuthenticatedEncryptorDescriptor.TypeToFriendlyName.
51+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
4952
private static Type FriendlyNameToType(string typeName)
5053
{
5154
if (typeName == nameof(Aes))

src/DataProtection/DataProtection/src/EphemeralDataProtectionProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Runtime.InteropServices;
78
using Microsoft.AspNetCore.Cryptography.Cng;
89
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
@@ -94,6 +95,7 @@ public EphemeralKeyRing(ILoggerFactory loggerFactory)
9495
return (keyId == default(Guid)) ? DefaultAuthenticatedEncryptor : null;
9596
}
9697

98+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
9799
public IKeyRing GetCurrentKeyRing()
98100
{
99101
return this;

src/DataProtection/DataProtection/src/IPersistedDataProtector.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace Microsoft.AspNetCore.DataProtection;
57

68
/// <summary>
@@ -29,5 +31,6 @@ public interface IPersistedDataProtector : IDataProtector
2931
/// Implementations should throw CryptographicException if the protected data is
3032
/// invalid or malformed.
3133
/// </remarks>
34+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
3235
byte[] DangerousUnprotect(byte[] protectedData, bool ignoreRevocationErrors, out bool requiresMigration, out bool wasRevoked);
3336
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace Microsoft.AspNetCore.DataProtection;
57

68
// Single implementation of this interface is conditionally added to DI on Windows
79
// We have to use interface because some DI implementations would try to activate class
810
// even if it was not registered causing problems crossplat
911
internal interface IRegistryPolicyResolver
1012
{
13+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
1114
RegistryPolicy? ResolvePolicy();
1215
}

src/DataProtection/DataProtection/src/Internal/IActivator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56

67
namespace Microsoft.AspNetCore.DataProtection.Internal;
78

@@ -15,5 +16,6 @@ public interface IActivator
1516
/// Creates an instance of <paramref name="implementationTypeName"/> and ensures
1617
/// that it is assignable to <paramref name="expectedBaseType"/>.
1718
/// </summary>
19+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
1820
object CreateInstance(Type expectedBaseType, string implementationTypeName);
1921
}

src/DataProtection/DataProtection/src/KeyManagement/DeferredKey.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Xml.Linq;
78
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
89
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
@@ -17,6 +18,7 @@ namespace Microsoft.AspNetCore.DataProtection.KeyManagement;
1718
/// </summary>
1819
internal sealed class DeferredKey : KeyBase
1920
{
21+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
2022
public DeferredKey(
2123
Guid keyId,
2224
DateTimeOffset creationDate,
@@ -34,6 +36,7 @@ public DeferredKey(
3436
{
3537
}
3638

39+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
3740
private static Func<IAuthenticatedEncryptorDescriptor> GetLazyDescriptorDelegate(IInternalXmlKeyManager keyManager, XElement keyElement)
3841
{
3942
// The <key> element will be held around in memory for a potentially lengthy period
@@ -42,13 +45,19 @@ private static Func<IAuthenticatedEncryptorDescriptor> GetLazyDescriptorDelegate
4245

4346
try
4447
{
45-
return () => keyManager.DeserializeDescriptorFromKeyElement(encryptedKeyElement.ToXElement());
48+
return GetLazyDescriptorDelegate;
4649
}
4750
finally
4851
{
4952
// It's important that the lambda above doesn't capture 'descriptorElement'. Clearing the reference here
5053
// helps us detect if we've done this by causing a null ref at runtime.
5154
keyElement = null!;
5255
}
56+
57+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
58+
IAuthenticatedEncryptorDescriptor GetLazyDescriptorDelegate()
59+
{
60+
return keyManager.DeserializeDescriptorFromKeyElement(encryptedKeyElement.ToXElement());
61+
}
5362
}
5463
}

src/DataProtection/DataProtection/src/KeyManagement/IKeyManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Threading;
78

89
namespace Microsoft.AspNetCore.DataProtection.KeyManagement;
@@ -28,6 +29,7 @@ public interface IKeyManager
2829
/// Fetches all keys from the underlying repository.
2930
/// </summary>
3031
/// <returns>The collection of all keys.</returns>
32+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
3133
IReadOnlyCollection<IKey> GetAllKeys();
3234

3335
/// <summary>

src/DataProtection/DataProtection/src/KeyManagement/Internal/ICacheableKeyRingProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56

67
namespace Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;
78

@@ -15,5 +16,6 @@ public interface ICacheableKeyRingProvider
1516
/// This API supports infrastructure and is not intended to be used
1617
/// directly from your code. This API may change or be removed in future releases.
1718
/// </summary>
19+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
1820
CacheableKeyRing GetCacheableKeyRing(DateTimeOffset now);
1921
}

src/DataProtection/DataProtection/src/KeyManagement/Internal/IInternalXmlKeyManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Xml.Linq;
67
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
78

@@ -23,6 +24,7 @@ public interface IInternalXmlKeyManager
2324
/// This API supports infrastructure and is not intended to be used
2425
/// directly from your code. This API may change or be removed in future releases.
2526
/// </summary>
27+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
2628
IAuthenticatedEncryptorDescriptor DeserializeDescriptorFromKeyElement(XElement keyElement);
2729

2830
/// <summary>

src/DataProtection/DataProtection/src/KeyManagement/Internal/IKeyRingProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;
57

68
/// <summary>
@@ -13,5 +15,6 @@ public interface IKeyRingProvider
1315
/// This API supports infrastructure and is not intended to be used
1416
/// directly from your code. This API may change or be removed in future releases.
1517
/// </summary>
18+
[RequiresUnreferencedCode(TrimmerWarning.Message)]
1619
IKeyRing GetCurrentKeyRing();
1720
}

0 commit comments

Comments
 (0)