Skip to content

Enable trimming for data protection #41118

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

Merged
merged 2 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions eng/TrimmableProjects.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
-->
<Project>
<ItemGroup>
<TrimmableProject Include="Microsoft.AspNetCore.DataProtection.Abstractions" />
<TrimmableProject Include="Microsoft.AspNetCore.Cryptography.Internal" />
<TrimmableProject Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" />
<TrimmableProject Include="Microsoft.AspNetCore.DataProtection" />
<TrimmableProject Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" />
<TrimmableProject Include="Microsoft.AspNetCore.DataProtection.Extensions" />
<TrimmableProject Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" />
<TrimmableProject Include="Microsoft.AspNetCore.Hosting.Abstractions" />
<TrimmableProject Include="Microsoft.AspNetCore.Hosting" />
<TrimmableProject Include="Microsoft.AspNetCore.Hosting.Server.Abstractions" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.DataProtection.Abstractions;
using Microsoft.Extensions.Internal;

Expand Down Expand Up @@ -182,6 +183,7 @@ public static IDataProtector GetDataProtector(this IServiceProvider services, st
/// <param name="protector">The data protector to use for this operation.</param>
/// <param name="plaintext">The plaintext data to protect.</param>
/// <returns>The protected form of the plaintext data.</returns>
[RequiresUnreferencedCode(TrimmerWarning.Message)]
public static string Protect(this IDataProtector protector, string plaintext)
{
if (protector == null)
Expand Down Expand Up @@ -216,6 +218,7 @@ public static string Protect(this IDataProtector protector, string plaintext)
/// <exception cref="System.Security.Cryptography.CryptographicException">
/// Thrown if <paramref name="protectedData"/> is invalid or malformed.
/// </exception>
[RequiresUnreferencedCode(TrimmerWarning.Message)]
public static string Unprotect(this IDataProtector protector, string protectedData)
{
if (protector == null)
Expand Down
4 changes: 4 additions & 0 deletions src/DataProtection/Abstractions/src/IDataProtector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;

namespace Microsoft.AspNetCore.DataProtection;

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

/// <summary>
Expand All @@ -23,5 +26,6 @@ public interface IDataProtector : IDataProtectionProvider
/// <exception cref="System.Security.Cryptography.CryptographicException">
/// Thrown if the protected data is invalid or malformed.
/// </exception>
[RequiresUnreferencedCode(TrimmerWarning.Message)]
byte[] Unprotect(byte[] protectedData);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>ASP.NET Core data protection abstractions.
Expand All @@ -10,12 +10,14 @@ Microsoft.AspNetCore.DataProtection.IDataProtector</Description>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;dataprotection</PackageTags>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\shared\src\*.cs" />
<Compile Include="..\..\shared\src\*.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)WebEncoders\**\*.cs" />
<Compile Include="$(SharedSourceRoot)TrimmingAttributes.cs" LinkBase="Shared"
Condition="'$(TargetFramework)' != '$(DefaultNetCoreTargetFramework)'" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;dataprotection</PackageTags>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;dataprotection</PackageTags>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
<Nullable Condition="'$(TargetFramework)' == 'netstandard2.0'">annotations</Nullable>
</PropertyGroup>

Expand Down
2 changes: 2 additions & 0 deletions src/DataProtection/DataProtection/src/ActivatorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Cryptography;
using Microsoft.AspNetCore.DataProtection.Internal;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -17,6 +18,7 @@ internal static class ActivatorExtensions
/// Creates an instance of <paramref name="implementationTypeName"/> and ensures
/// that it is assignable to <typeparamref name="T"/>.
/// </summary>
[RequiresUnreferencedCode(TrimmerWarning.Message)]
public static T CreateInstance<T>(this IActivator activator, string implementationTypeName)
where T : class
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Xml.Linq;

Expand All @@ -16,6 +17,7 @@ public sealed class AuthenticatedEncryptorDescriptorDeserializer : IAuthenticate
/// <summary>
/// Imports the <see cref="AuthenticatedEncryptorDescriptor"/> from serialized XML.
/// </summary>
[RequiresUnreferencedCode(TrimmerWarning.Message)]
public IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element)
{
if (element == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Versioning;
using System.Xml.Linq;

Expand All @@ -17,6 +18,7 @@ public sealed class CngCbcAuthenticatedEncryptorDescriptorDeserializer : IAuthen
/// <summary>
/// Imports the <see cref="CngCbcAuthenticatedEncryptorDescriptor"/> from serialized XML.
/// </summary>
[RequiresUnreferencedCode(TrimmerWarning.Message)]
public IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element)
{
if (element == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Versioning;
using System.Xml.Linq;

Expand All @@ -17,6 +18,7 @@ public sealed class CngGcmAuthenticatedEncryptorDescriptorDeserializer : IAuthen
/// <summary>
/// Imports the <see cref="CngCbcAuthenticatedEncryptorDescriptor"/> from serialized XML.
/// </summary>
[RequiresUnreferencedCode(TrimmerWarning.Message)]
public IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element)
{
if (element == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Xml.Linq;

namespace Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
Expand All @@ -15,5 +16,6 @@ public interface IAuthenticatedEncryptorDescriptorDeserializer
/// </summary>
/// <param name="element">The element to deserialize.</param>
/// <returns>The <see cref="IAuthenticatedEncryptorDescriptor"/> represented by <paramref name="element"/>.</returns>
[RequiresUnreferencedCode(TrimmerWarning.Message)]
IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using System.Xml.Linq;

Expand All @@ -16,6 +17,7 @@ public sealed class ManagedAuthenticatedEncryptorDescriptorDeserializer : IAuthe
/// <summary>
/// Imports the <see cref="ManagedAuthenticatedEncryptorDescriptor"/> from serialized XML.
/// </summary>
[RequiresUnreferencedCode(TrimmerWarning.Message)]
public IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element)
{
if (element == null)
Expand Down Expand Up @@ -46,6 +48,7 @@ public IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element)

// Any changes to this method should also be be reflected
// in ManagedAuthenticatedEncryptorDescriptor.TypeToFriendlyName.
[RequiresUnreferencedCode(TrimmerWarning.Message)]
private static Type FriendlyNameToType(string typeName)
{
if (typeName == nameof(Aes))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Cryptography.Cng;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
Expand Down Expand Up @@ -94,6 +95,7 @@ public EphemeralKeyRing(ILoggerFactory loggerFactory)
return (keyId == default(Guid)) ? DefaultAuthenticatedEncryptor : null;
}

[RequiresUnreferencedCode(TrimmerWarning.Message)]
public IKeyRing GetCurrentKeyRing()
{
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;

namespace Microsoft.AspNetCore.DataProtection;

/// <summary>
Expand Down Expand Up @@ -29,5 +31,6 @@ public interface IPersistedDataProtector : IDataProtector
/// Implementations should throw CryptographicException if the protected data is
/// invalid or malformed.
/// </remarks>
[RequiresUnreferencedCode(TrimmerWarning.Message)]
byte[] DangerousUnprotect(byte[] protectedData, bool ignoreRevocationErrors, out bool requiresMigration, out bool wasRevoked);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;

namespace Microsoft.AspNetCore.DataProtection;

// Single implementation of this interface is conditionally added to DI on Windows
// We have to use interface because some DI implementations would try to activate class
// even if it was not registered causing problems crossplat
internal interface IRegistryPolicyResolver
{
[RequiresUnreferencedCode(TrimmerWarning.Message)]
RegistryPolicy? ResolvePolicy();
}
2 changes: 2 additions & 0 deletions src/DataProtection/DataProtection/src/Internal/IActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.AspNetCore.DataProtection.Internal;

Expand All @@ -15,5 +16,6 @@ public interface IActivator
/// Creates an instance of <paramref name="implementationTypeName"/> and ensures
/// that it is assignable to <paramref name="expectedBaseType"/>.
/// </summary>
[RequiresUnreferencedCode(TrimmerWarning.Message)]
object CreateInstance(Type expectedBaseType, string implementationTypeName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Xml.Linq;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
Expand All @@ -17,6 +18,7 @@ namespace Microsoft.AspNetCore.DataProtection.KeyManagement;
/// </summary>
internal sealed class DeferredKey : KeyBase
{
[RequiresUnreferencedCode(TrimmerWarning.Message)]
public DeferredKey(
Guid keyId,
DateTimeOffset creationDate,
Expand All @@ -34,6 +36,7 @@ public DeferredKey(
{
}

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

try
{
return () => keyManager.DeserializeDescriptorFromKeyElement(encryptedKeyElement.ToXElement());
return GetLazyDescriptorDelegate;
}
finally
{
// It's important that the lambda above doesn't capture 'descriptorElement'. Clearing the reference here
// helps us detect if we've done this by causing a null ref at runtime.
keyElement = null!;
}

[RequiresUnreferencedCode(TrimmerWarning.Message)]
IAuthenticatedEncryptorDescriptor GetLazyDescriptorDelegate()
{
return keyManager.DeserializeDescriptorFromKeyElement(encryptedKeyElement.ToXElement());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

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

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;

Expand All @@ -15,5 +16,6 @@ public interface ICacheableKeyRingProvider
/// This API supports infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
[RequiresUnreferencedCode(TrimmerWarning.Message)]
CacheableKeyRing GetCacheableKeyRing(DateTimeOffset now);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Xml.Linq;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;

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

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;

namespace Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;

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