Skip to content

Enable trimming on remaining middleware and security projects #41688

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 4 commits into from
May 16, 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
16 changes: 16 additions & 0 deletions eng/TrimmableProjects.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<TrimmableProject Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" />
<TrimmableProject Include="Microsoft.AspNetCore.DataProtection.Extensions" />
<TrimmableProject Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" />
<TrimmableProject Include="Microsoft.AspNetCore.Antiforgery" />
<TrimmableProject Include="Microsoft.AspNetCore.Hosting.Abstractions" />
<TrimmableProject Include="Microsoft.AspNetCore.Hosting" />
<TrimmableProject Include="Microsoft.AspNetCore.Hosting.Server.Abstractions" />
Expand All @@ -37,7 +38,21 @@
<TrimmableProject Include="Microsoft.AspNetCore.Server.Kestrel" />
<TrimmableProject Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Quic" />
<TrimmableProject Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets" />
<TrimmableProject Include="Microsoft.AspNetCore.Authentication.Certificate" />
<TrimmableProject Include="Microsoft.AspNetCore.Authentication.Cookies" />
<TrimmableProject Include="Microsoft.AspNetCore.Authentication" />
<TrimmableProject Include="Microsoft.AspNetCore.Authentication.Facebook" />
<TrimmableProject Include="Microsoft.AspNetCore.Authentication.Google" />
<TrimmableProject Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<TrimmableProject Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" />
<TrimmableProject Include="Microsoft.AspNetCore.Authentication.Negotiate" />
<TrimmableProject Include="Microsoft.AspNetCore.Authentication.OAuth" />
<TrimmableProject Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" />
<TrimmableProject Include="Microsoft.AspNetCore.Authentication.Twitter" />
<TrimmableProject Include="Microsoft.AspNetCore.Authentication.WsFederation" />
<TrimmableProject Include="Microsoft.AspNetCore.Authorization" />
<TrimmableProject Include="Microsoft.AspNetCore.Authorization.Policy" />
<TrimmableProject Include="Microsoft.AspNetCore.CookiePolicy" />
<TrimmableProject Include="Microsoft.AspNetCore.Cors" />
<TrimmableProject Include="Microsoft.AspNetCore.Diagnostics.Abstractions" />
<TrimmableProject Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" />
Expand All @@ -56,6 +71,7 @@
<TrimmableProject Include="Microsoft.AspNetCore.ResponseCaching" />
<TrimmableProject Include="Microsoft.AspNetCore.ResponseCompression" />
<TrimmableProject Include="Microsoft.AspNetCore.Rewrite" />
<TrimmableProject Include="Microsoft.AspNetCore.Session" />
<TrimmableProject Include="Microsoft.AspNetCore.SpaProxy" />
<TrimmableProject Include="Microsoft.AspNetCore.SpaServices.Extensions" />
<TrimmableProject Include="Microsoft.AspNetCore.StaticFiles" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;antiforgery</PackageTags>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;session;sessionstate</PackageTags>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<DefineConstants>$(DefineConstants);SECURITY</DefineConstants>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;authentication;security;x509;certificate</PackageTags>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;authentication;security</PackageTags>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
15 changes: 14 additions & 1 deletion src/Security/Authentication/Core/src/AuthenticationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public AuthenticationBuilder(IServiceCollection services)
where TOptions : AuthenticationSchemeOptions, new()
where THandler : class, IAuthenticationHandler
{
var state = new AddSchemeHelperState(typeof(THandler));
Services.Configure<AuthenticationOptions>(o =>
{
o.AddScheme(authenticationScheme, scheme =>
{
scheme.HandlerType = typeof(THandler);
scheme.HandlerType = state.HandlerType;
scheme.DisplayName = displayName;
});
});
Expand All @@ -50,6 +51,18 @@ public AuthenticationBuilder(IServiceCollection services)
return this;
}

// Workaround for linker bug: https://github.com/dotnet/linker/issues/1981
private readonly struct AddSchemeHelperState
{
public AddSchemeHelperState([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type handlerType)
{
HandlerType = handlerType;
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
public Type HandlerType { get; }
}

/// <summary>
/// Adds a <see cref="AuthenticationScheme"/> which can be used by <see cref="IAuthenticationService"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;authentication;security</PackageTags>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;authentication;security</PackageTags>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;authentication;security</PackageTags>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;authentication;security</PackageTags>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;authentication;security</PackageTags>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// 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.Authentication.Negotiate;

// For testing
internal interface INegotiateStateFactory
{
[RequiresUnreferencedCode("Negotiate authentication uses types that cannot be statically analyzed.")]
INegotiateState CreateInstance();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma warning disable CA1810 // Initialize all static fields inline.

using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Reflection;
Expand All @@ -12,6 +13,7 @@

namespace Microsoft.AspNetCore.Authentication.Negotiate;

[RequiresUnreferencedCode("Negotiate authentication uses types that cannot be statically analyzed.")]
internal sealed class ReflectedNegotiateState : INegotiateState
{
// https://www.gnu.org/software/gss/reference/gss.pdf
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// 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.Authentication.Negotiate;

internal sealed class ReflectedNegotiateStateFactory : INegotiateStateFactory
{
[RequiresUnreferencedCode("Negotiate authentication uses types that cannot be statically analyzed.")]
public INegotiateState CreateInstance()
{
return new ReflectedNegotiateState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;authentication;security</PackageTags>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
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 Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Negotiate;
using Microsoft.AspNetCore.Authentication.Negotiate.Internal;
Expand All @@ -24,6 +25,7 @@ public static class NegotiateExtensions
/// </summary>
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
/// <returns>The original builder.</returns>
[RequiresUnreferencedCode("Negotiate authentication uses types that cannot be statically analyzed.")]
public static AuthenticationBuilder AddNegotiate(this AuthenticationBuilder builder)
=> builder.AddNegotiate(NegotiateDefaults.AuthenticationScheme, _ => { });

Expand All @@ -37,6 +39,7 @@ public static AuthenticationBuilder AddNegotiate(this AuthenticationBuilder buil
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
/// <param name="configureOptions">Allows for configuring the authentication handler.</param>
/// <returns>The original builder.</returns>
[RequiresUnreferencedCode("Negotiate authentication uses types that cannot be statically analyzed.")]
public static AuthenticationBuilder AddNegotiate(this AuthenticationBuilder builder, Action<NegotiateOptions> configureOptions)
=> builder.AddNegotiate(NegotiateDefaults.AuthenticationScheme, configureOptions);

Expand All @@ -51,6 +54,7 @@ public static AuthenticationBuilder AddNegotiate(this AuthenticationBuilder buil
/// <param name="authenticationScheme">The scheme name used to identify the authentication handler internally.</param>
/// <param name="configureOptions">Allows for configuring the authentication handler.</param>
/// <returns>The original builder.</returns>
[RequiresUnreferencedCode("Negotiate authentication uses types that cannot be statically analyzed.")]
public static AuthenticationBuilder AddNegotiate(this AuthenticationBuilder builder, string authenticationScheme, Action<NegotiateOptions> configureOptions)
=> builder.AddNegotiate(authenticationScheme, displayName: null, configureOptions: configureOptions);

Expand All @@ -66,6 +70,7 @@ public static AuthenticationBuilder AddNegotiate(this AuthenticationBuilder buil
/// <param name="displayName">The name displayed to users when selecting an authentication handler. The default is null to prevent this from displaying.</param>
/// <param name="configureOptions">Allows for configuring the authentication handler.</param>
/// <returns>The original builder.</returns>
[RequiresUnreferencedCode("Negotiate authentication uses types that cannot be statically analyzed.")]
public static AuthenticationBuilder AddNegotiate(this AuthenticationBuilder builder, string authenticationScheme, string? displayName, Action<NegotiateOptions> configureOptions)
{
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<NegotiateOptions>, PostConfigureNegotiateOptions>());
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.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
Expand All @@ -18,6 +19,7 @@ namespace Microsoft.AspNetCore.Authentication.Negotiate;
/// <summary>
/// Authenticates requests using Negotiate, Kerberos, or NTLM.
/// </summary>
[RequiresUnreferencedCode("Negotiate authentication uses types that cannot be statically analyzed.")]
public class NegotiateHandler : AuthenticationHandler<NegotiateOptions>, IAuthenticationRequestHandler
{
private const string AuthPersistenceKey = nameof(AuthPersistence);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;authentication;security</PackageTags>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;authentication;security</PackageTags>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;authentication;security</PackageTags>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
19 changes: 12 additions & 7 deletions src/Security/Authentication/Twitter/src/TwitterHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
Expand All @@ -19,13 +20,8 @@ namespace Microsoft.AspNetCore.Authentication.Twitter;
/// <summary>
/// Authentication handler for Twitter's OAuth based authentication.
/// </summary>
public class TwitterHandler : RemoteAuthenticationHandler<TwitterOptions>
public partial class TwitterHandler : RemoteAuthenticationHandler<TwitterOptions>
{
private static readonly JsonSerializerOptions ErrorSerializerOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};

private HttpClient Backchannel => Options.Backchannel;

/// <summary>
Expand Down Expand Up @@ -361,7 +357,7 @@ private async Task EnsureTwitterRequestSuccess(HttpResponseMessage response)
{
// Failure, attempt to parse Twitters error message
var errorContentStream = await response.Content.ReadAsStreamAsync(Context.RequestAborted);
errorResponse = await JsonSerializer.DeserializeAsync<TwitterErrorResponse>(errorContentStream, ErrorSerializerOptions);
errorResponse = await JsonSerializer.DeserializeAsync(errorContentStream, TwitterJsonContext.DefaultWithOptions.TwitterErrorResponse);
}
catch
{
Expand Down Expand Up @@ -390,4 +386,13 @@ private async Task EnsureTwitterRequestSuccess(HttpResponseMessage response)

throw new InvalidOperationException(errorMessageStringBuilder.ToString());
}

[JsonSerializable(typeof(TwitterErrorResponse))]
internal sealed partial class TwitterJsonContext : JsonSerializerContext
{
public static readonly TwitterJsonContext DefaultWithOptions = new TwitterJsonContext(new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;authentication;security</PackageTags>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;authorization</PackageTags>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore</PackageTags>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions src/Tools/Tools.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,21 @@
"src\\Middleware\\StaticFiles\\src\\Microsoft.AspNetCore.StaticFiles.csproj",
"src\\Middleware\\WebSockets\\src\\Microsoft.AspNetCore.WebSockets.csproj",
"src\\ObjectPool\\src\\Microsoft.Extensions.ObjectPool.csproj",
"src\\Security\\Authentication\\Certificate\\src\\Microsoft.AspNetCore.Authentication.Certificate.csproj",
"src\\Security\\Authentication\\Cookies\\src\\Microsoft.AspNetCore.Authentication.Cookies.csproj",
"src\\Security\\Authentication\\Core\\src\\Microsoft.AspNetCore.Authentication.csproj",
"src\\Security\\Authentication\\Facebook\\src\\Microsoft.AspNetCore.Authentication.Facebook.csproj",
"src\\Security\\Authentication\\Google\\src\\Microsoft.AspNetCore.Authentication.Google.csproj",
"src\\Security\\Authentication\\JwtBearer\\src\\Microsoft.AspNetCore.Authentication.JwtBearer.csproj",
"src\\Security\\Authentication\\MicrosoftAccount\\src\\Microsoft.AspNetCore.Authentication.MicrosoftAccount.csproj",
"src\\Security\\Authentication\\Negotiate\\src\\Microsoft.AspNetCore.Authentication.Negotiate.csproj",
"src\\Security\\Authentication\\OAuth\\src\\Microsoft.AspNetCore.Authentication.OAuth.csproj",
"src\\Security\\Authentication\\OpenIdConnect\\src\\Microsoft.AspNetCore.Authentication.OpenIdConnect.csproj",
"src\\Security\\Authentication\\Twitter\\src\\Microsoft.AspNetCore.Authentication.Twitter.csproj",
"src\\Security\\Authentication\\WsFederation\\src\\Microsoft.AspNetCore.Authentication.WsFederation.csproj",
"src\\Security\\Authorization\\Core\\src\\Microsoft.AspNetCore.Authorization.csproj",
"src\\Security\\Authorization\\Policy\\src\\Microsoft.AspNetCore.Authorization.Policy.csproj",
"src\\Security\\CookiePolicy\\src\\Microsoft.AspNetCore.CookiePolicy.csproj",
"src\\Servers\\Connections.Abstractions\\src\\Microsoft.AspNetCore.Connections.Abstractions.csproj",
"src\\Servers\\HttpSys\\src\\Microsoft.AspNetCore.Server.HttpSys.csproj",
"src\\Servers\\IIS\\IISIntegration\\src\\Microsoft.AspNetCore.Server.IISIntegration.csproj",
Expand Down