Skip to content

Commit 3ff3207

Browse files
authored
Ensure DataProtection can be used in trimmed apps. (#48082)
* Add a DynamicDependency to ensure Aes decryption works in EncryptedXmlDecryptor * Suppress the warnings from EncryptedXml * Add trimming tests to ensure these scenarios work correctly. * Remove the RequiresUnreferencedCode attribute on AddAuthentication, since this is the only thing in that method that has warnings. Fix #47695 * Fix parallel build to not copy files to the same destination
1 parent 7db7dcd commit 3ff3207

File tree

71 files changed

+373
-34
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+373
-34
lines changed

AspNetCore.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Crypto
499499
EndProject
500500
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Cryptography.KeyDerivation.Tests", "src\DataProtection\Cryptography.KeyDerivation\test\Microsoft.AspNetCore.Cryptography.KeyDerivation.Tests.csproj", "{F421D0C4-6EF7-48B7-9213-AFD21322E08B}"
501501
EndProject
502-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.DataProtection.Tests", "src\DataProtection\DataProtection\test\Microsoft.AspNetCore.DataProtection.Tests.csproj", "{696BE515-B3AB-4925-969C-350F1BDA5C30}"
502+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.DataProtection.Tests", "src\DataProtection\DataProtection\test\Microsoft.AspNetCore.DataProtection.Tests\Microsoft.AspNetCore.DataProtection.Tests.csproj", "{696BE515-B3AB-4925-969C-350F1BDA5C30}"
503503
EndProject
504504
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.DataProtection.Extensions.Tests", "src\DataProtection\Extensions\test\Microsoft.AspNetCore.DataProtection.Extensions.Tests.csproj", "{3CB3CA43-6D65-4DDE-B5E3-A9E0DF957E38}"
505505
EndProject

eng/RequiresDelayedBuildProjects.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
-->
99
<Project>
1010
<ItemGroup>
11+
<RequiresDelayedBuild Include="$(RepoRoot)src\DataProtection\DataProtection\test\Microsoft.AspNetCore.DataProtection.TrimmingTests\Microsoft.AspNetCore.DataProtection.TrimmingTests.proj" />
12+
<RequiresDelayedBuild Include="$(RepoRoot)src\DefaultBuilder\test\Microsoft.AspNetCore.NativeAotTests\Microsoft.AspNetCore.NativeAotTests.proj" />
1113
<RequiresDelayedBuild Include="$(RepoRoot)src\Grpc\JsonTranscoding\perf\Microsoft.AspNetCore.Grpc.Microbenchmarks\Microsoft.AspNetCore.Grpc.Microbenchmarks.csproj" />
1214
<RequiresDelayedBuild Include="$(RepoRoot)src\Grpc\JsonTranscoding\src\Microsoft.AspNetCore.Grpc.JsonTranscoding\Microsoft.AspNetCore.Grpc.JsonTranscoding.csproj" />
1315
<RequiresDelayedBuild Include="$(RepoRoot)src\Grpc\JsonTranscoding\src\Microsoft.AspNetCore.Grpc.Swagger\Microsoft.AspNetCore.Grpc.Swagger.csproj" />
@@ -17,6 +19,5 @@
1719
<RequiresDelayedBuild Include="$(RepoRoot)src\Grpc\JsonTranscoding\test\testassets\IntegrationTestsWebsite\IntegrationTestsWebsite.csproj" />
1820
<RequiresDelayedBuild Include="$(RepoRoot)src\Grpc\JsonTranscoding\test\testassets\Sandbox\Sandbox.csproj" />
1921
<RequiresDelayedBuild Include="$(RepoRoot)src\ProjectTemplates\test\Templates.Blazor.Tests\Templates.Blazor.Tests.csproj" />
20-
<RequiresDelayedBuild Include="$(RepoRoot)src\DefaultBuilder\test\Microsoft.AspNetCore.NativeAotTests\Microsoft.AspNetCore.NativeAotTests.proj" />
2122
</ItemGroup>
2223
</Project>

eng/testing/linker/SupportFiles/Directory.Build.targets

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
2-
2+
33
<PropertyGroup>
44
<!-- Used to silence the warning caused by the workaround for https://github.com/dotnet/runtime/issues/81382 -->
55
<SuppressGenerateILCompilerExplicitPackageReferenceWarning>true</SuppressGenerateILCompilerExplicitPackageReferenceWarning>
@@ -20,6 +20,7 @@
2020
-->
2121
<ItemGroup>
2222
<ProjectReference Include="$(RepoRoot)src\DefaultBuilder\src\Microsoft.AspNetCore.csproj" />
23+
<ProjectReference Include="$(RepoRoot)src\DataProtection\Extensions\src\Microsoft.AspNetCore.DataProtection.Extensions.csproj" />
2324
</ItemGroup>
24-
25+
2526
</Project>

eng/testing/linker/trimmingTests.targets

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
<TestConsoleAppSourceFiles Condition="'@(TestConsoleAppSourceFiles)' == ''" Include="$(MSBuildProjectDirectory)\*.cs" />
66

77
<TestSupportFiles Include="$(MSBuildThisFileDirectory)SupportFiles\Directory.Build.*">
8-
<DestinationFolder>$(TrimmingTestDir)</DestinationFolder>
8+
<DestinationFolder>$([MSBuild]::NormalizeDirectory('$(TrimmingTestProjectsDir)', '$(MSBuildProjectName)'))</DestinationFolder>
99
</TestSupportFiles>
1010
</ItemGroup>
1111

1212
<Target Name="CreateTestDir"
1313
Inputs="@(TestSupportFiles)"
1414
Outputs="@(TestSupportFiles->'%(DestinationFolder)\%(FileName)%(Extension)')">
1515
<MakeDir Directories="%(TestSupportFiles.DestinationFolder)" />
16-
<Copy SourceFiles="@(TestSupportFiles)" DestinationFolder="%(TestSupportFiles.DestinationFolder)" />
16+
<Copy SourceFiles="@(TestSupportFiles)"
17+
DestinationFolder="%(TestSupportFiles.DestinationFolder)"
18+
SkipUnchangedFiles="true" />
1719
</Target>
1820

1921
<Target Name="GetTestConsoleApps">

src/DataProtection/DataProtection.slnf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"src\\DataProtection\\Cryptography.KeyDerivation\\src\\Microsoft.AspNetCore.Cryptography.KeyDerivation.csproj",
1010
"src\\DataProtection\\Cryptography.KeyDerivation\\test\\Microsoft.AspNetCore.Cryptography.KeyDerivation.Tests.csproj",
1111
"src\\DataProtection\\DataProtection\\src\\Microsoft.AspNetCore.DataProtection.csproj",
12-
"src\\DataProtection\\DataProtection\\test\\Microsoft.AspNetCore.DataProtection.Tests.csproj",
12+
"src\\DataProtection\\DataProtection\\test\\Microsoft.AspNetCore.DataProtection.Tests\\Microsoft.AspNetCore.DataProtection.Tests.csproj",
1313
"src\\DataProtection\\EntityFrameworkCore\\src\\Microsoft.AspNetCore.DataProtection.EntityFrameworkCore.csproj",
1414
"src\\DataProtection\\EntityFrameworkCore\\test\\Microsoft.AspNetCore.DataProtection.EntityFrameworkCore.Test.csproj",
1515
"src\\DataProtection\\Extensions\\src\\Microsoft.AspNetCore.DataProtection.Extensions.csproj",

src/DataProtection/DataProtection/src/Microsoft.AspNetCore.DataProtection.WarningSuppressions.xml

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/DataProtection/DataProtection/src/XmlEncryption/CertificateXmlEncryptor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ public EncryptedXmlInfo Encrypt(XElement plaintextElement)
7676
return new EncryptedXmlInfo(encryptedElement, typeof(EncryptedXmlDecryptor));
7777
}
7878

79+
[UnconditionalSuppressMessage("AOT", "IL2026:RequiresUnreferencedCode",
80+
Justification = "This usage of EncryptedXml to encrypt an XElement using a X509Certificate2 does not use reflection.")]
7981
[UnconditionalSuppressMessage("AOT", "IL3050:RequiresDynamicCode",
80-
Justification = "Only XSLTs require dynamic code. The usage of EncryptedXml doesn't use XSLTs.")]
82+
Justification = "This usage of EncryptedXml to encrypt an XElement using a X509Certificate2 does not use XSLTs.")]
8183
private XElement EncryptElement(XElement plaintextElement)
8284
{
8385
// EncryptedXml works with XmlDocument, not XLinq. When we perform the conversion
@@ -88,9 +90,7 @@ private XElement EncryptElement(XElement plaintextElement)
8890
var elementToEncrypt = (XmlElement)xmlDocument.DocumentElement!.FirstChild!;
8991

9092
// Perform the encryption and update the document in-place.
91-
#pragma warning disable IL2026 // TODO: https://github.com/dotnet/aspnetcore/issues/47695
9293
var encryptedXml = new EncryptedXml(xmlDocument);
93-
#pragma warning restore IL2026
9494
var encryptedData = _encryptor.PerformEncryption(encryptedXml, elementToEncrypt);
9595
EncryptedXml.ReplaceElement(elementToEncrypt, encryptedData, content: false);
9696

src/DataProtection/DataProtection/src/XmlEncryption/EncryptedXmlDecryptor.cs

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

44
using System;
55
using System.Diagnostics.CodeAnalysis;
6+
using System.Security.Cryptography;
67
using System.Security.Cryptography.X509Certificates;
78
using System.Security.Cryptography.Xml;
89
using System.Xml;
@@ -44,6 +45,13 @@ public EncryptedXmlDecryptor(IServiceProvider? services)
4445
/// </summary>
4546
/// <param name="encryptedElement">An encrypted XML element.</param>
4647
/// <returns>The decrypted form of <paramref name="encryptedElement"/>.</returns>
48+
#pragma warning disable SYSLIB0022 // Rijndael types are obsolete
49+
// RijndaelManaged (aka AES) is used by default. If we find another important algorithm, we should add it here as well.
50+
// In the meantime, a useful exception will be thrown in a trimmed app if the algorithm can't be found.
51+
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof(RijndaelManaged))]
52+
#pragma warning restore SYSLIB0022
53+
[UnconditionalSuppressMessage("AOT", "IL2026:RequiresUnreferencedCode",
54+
Justification = "The common algorithms are being preserved by the above DynamicDependency attributes.")]
4755
[UnconditionalSuppressMessage("AOT", "IL3050:RequiresDynamicCode",
4856
Justification = "Only XSLTs require dynamic code. The usage of EncryptedXml doesn't use XSLTs.")]
4957
public XElement Decrypt(XElement encryptedElement)
@@ -83,10 +91,9 @@ private sealed class EncryptedXmlWithCertificateKeys : EncryptedXml
8391
private readonly XmlKeyDecryptionOptions? _options;
8492

8593
[RequiresDynamicCode("XmlDsigXsltTransform uses XslCompiledTransform which requires dynamic code.")]
94+
[RequiresUnreferencedCode("The algorithm implementations referenced in the XML payload might be removed.")]
8695
public EncryptedXmlWithCertificateKeys(XmlKeyDecryptionOptions? options, XmlDocument document)
87-
#pragma warning disable IL2026 // TODO: https://github.com/dotnet/aspnetcore/issues/47695
8896
: base(document)
89-
#pragma warning restore IL2026
9097
{
9198
_options = options;
9299
}

0 commit comments

Comments
 (0)