Repository navigation
[WIP] feat: Sec-Fetch-* based Antiforgery #65887
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
Changes from all commits
4618e25
54be7b1
b4e2283
83ef3dd
5a8923f
f86262a
31e0fb5
90e561a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||||
|
|
||||||||
| using Microsoft.AspNetCore.Antiforgery; | ||||||||
| using Microsoft.AspNetCore.Antiforgery.CrossOrigin; | ||||||||
| using Microsoft.AspNetCore.Antiforgery.Internal; | ||||||||
| using Microsoft.Extensions.DependencyInjection.Extensions; | ||||||||
| using Microsoft.Extensions.Options; | ||||||||
|
|
||||||||
|
|
@@ -21,12 +23,50 @@ public static IServiceCollection AddAntiforgery(this IServiceCollection services | |||||||
| { | ||||||||
| ArgumentNullException.ThrowIfNull(services); | ||||||||
|
|
||||||||
| services.AddAntiforgery((AntiforgeryOptions _) => { }, (CrossOriginAntiforgeryOptions _) => { }); | ||||||||
|
|
||||||||
| return services; | ||||||||
| } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// Adds antiforgery services to the specified <see cref="IServiceCollection" />. | ||||||||
| /// </summary> | ||||||||
| /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> | ||||||||
| /// <param name="setupAction">An <see cref="Action{AntiforgeryOptions}"/> to configure the provided <see cref="AntiforgeryOptions"/>.</param> | ||||||||
| /// <param name="crossOriginSetupAction">An <see cref="Action{CrossOriginAntiforgeryOptions}"/> to configure the provided <see cref="CrossOriginAntiforgeryOptions"/>.</param> | ||||||||
| /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> | ||||||||
| public static IServiceCollection AddAntiforgery(this IServiceCollection services, Action<AntiforgeryOptions> setupAction, Action<CrossOriginAntiforgeryOptions> crossOriginSetupAction) | ||||||||
| { | ||||||||
| ArgumentNullException.ThrowIfNull(services); | ||||||||
| ArgumentNullException.ThrowIfNull(setupAction); | ||||||||
| ArgumentNullException.ThrowIfNull(crossOriginSetupAction); | ||||||||
|
|
||||||||
| services.AddAntiforgery((AntiforgeryOptions _) => { }); // token-based | ||||||||
| services.AddCrossOriginAntiforgery((CrossOriginAntiforgeryOptions _) => { }); // cross-origin | ||||||||
|
|
||||||||
| services.Configure(setupAction); | ||||||||
| services.Configure(crossOriginSetupAction); | ||||||||
| return services; | ||||||||
| } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// Adds antiforgery services to the specified <see cref="IServiceCollection" />. | ||||||||
| /// </summary> | ||||||||
| /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> | ||||||||
| /// <param name="setupAction">An <see cref="Action{AntiforgeryOptions}"/> to configure the provided <see cref="AntiforgeryOptions"/>.</param> | ||||||||
| /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> | ||||||||
| public static IServiceCollection AddAntiforgery(this IServiceCollection services, Action<AntiforgeryOptions> setupAction) | ||||||||
| { | ||||||||
| ArgumentNullException.ThrowIfNull(services); | ||||||||
| ArgumentNullException.ThrowIfNull(setupAction); | ||||||||
|
|
||||||||
| services.AddDataProtection(); | ||||||||
|
|
||||||||
| // Don't overwrite any options setups that a user may have added. | ||||||||
| services.TryAddEnumerable( | ||||||||
|
Comment on lines
+58
to
66
|
||||||||
| ServiceDescriptor.Transient<IConfigureOptions<AntiforgeryOptions>, AntiforgeryOptionsSetup>()); | ||||||||
|
|
||||||||
| // Token-based antiforgery | ||||||||
| services.TryAddSingleton<IAntiforgery, DefaultAntiforgery>(); | ||||||||
| services.TryAddSingleton<IAntiforgeryTokenGenerator, DefaultAntiforgeryTokenGenerator>(); | ||||||||
| services.TryAddSingleton<IAntiforgeryTokenSerializer, DefaultAntiforgeryTokenSerializer>(); | ||||||||
|
|
@@ -38,17 +78,33 @@ public static IServiceCollection AddAntiforgery(this IServiceCollection services | |||||||
| } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// Adds antiforgery services to the specified <see cref="IServiceCollection" />. | ||||||||
| /// Adds cross-origin antiforgery services to the specified <see cref="IServiceCollection" />. | ||||||||
| /// Only registers cross-origin validation; does not register token-based antiforgery or data protection services. | ||||||||
| /// </summary> | ||||||||
| /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> | ||||||||
| /// <param name="setupAction">An <see cref="Action{AntiforgeryOptions}"/> to configure the provided <see cref="AntiforgeryOptions"/>.</param> | ||||||||
| /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> | ||||||||
| public static IServiceCollection AddAntiforgery(this IServiceCollection services, Action<AntiforgeryOptions> setupAction) | ||||||||
| public static IServiceCollection AddCrossOriginAntiforgery(this IServiceCollection services) | ||||||||
| { | ||||||||
| ArgumentNullException.ThrowIfNull(services); | ||||||||
|
|
||||||||
|
||||||||
| services.AddOptions(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.AspNetCore.Antiforgery.CrossOrigin; | ||
|
|
||
| /// <summary> | ||
| /// Provides programmatic configuration for the cross-origin antiforgery system. | ||
| /// </summary> | ||
| public class CrossOriginAntiforgeryOptions | ||
| { | ||
| /// <summary> | ||
| /// Gets the collection of trusted origins that are allowed for cross-origin requests. | ||
| /// Origins should be in the format "scheme://host[:port]" (e.g., "https://example.com"). | ||
| /// </summary> | ||
| public IList<string> TrustedOrigins { get; } = new List<string>(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.AspNetCore.Antiforgery.CrossOrigin; | ||
|
|
||
| /// <summary> | ||
| /// Result of cross-origin request validation. | ||
| /// </summary> | ||
| public enum CrossOriginValidationResult | ||
| { | ||
| /// <summary> | ||
| /// Request is safe (same-origin, trusted, safe method). | ||
| /// </summary> | ||
| Allowed, | ||
|
|
||
| /// <summary> | ||
| /// Request is a cross-origin attack. | ||
| /// </summary> | ||
| Denied, | ||
|
|
||
| /// <summary> | ||
| /// Not enough information to determine the outcome. | ||
| /// </summary> | ||
| Unknown | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.AspNetCore.Http; | ||
|
|
||
| namespace Microsoft.AspNetCore.Antiforgery.CrossOrigin; | ||
|
|
||
| /// <summary> | ||
| /// Provides cross-origin request validation using Fetch Metadata headers. | ||
| /// </summary> | ||
| public interface ICrossOriginAntiforgery | ||
| { | ||
| /// <summary> | ||
| /// Validates the incoming HTTP request against cross-origin antiforgery requirements. | ||
| /// </summary> | ||
| /// <param name="context">The HTTP context representing the current request to validate.</param> | ||
| /// <returns>A <see cref="CrossOriginValidationResult"/> indicating whether the request is allowed, denied, or unknown.</returns> | ||
| CrossOriginValidationResult Validate(HttpContext context); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.AspNetCore.Antiforgery.CrossOrigin; | ||
| using Microsoft.AspNetCore.Http; | ||
|
|
||
| namespace Microsoft.AspNetCore.Antiforgery.Internal; | ||
|
|
||
| /// <summary> | ||
| /// Middleware that performs cross-origin-only antiforgery validation. | ||
| /// Unknown results are treated as denied (strict mode). | ||
| /// </summary> | ||
| internal sealed class CrossOriginAntiforgeryMiddleware(ICrossOriginAntiforgery crossOriginAntiforgery, RequestDelegate next) | ||
| { | ||
| private readonly RequestDelegate _next = next; | ||
|
|
||
| private const string AntiforgeryMiddlewareWithEndpointInvokedKey = "__AntiforgeryMiddlewareWithEndpointInvoked"; | ||
| private static readonly object AntiforgeryMiddlewareWithEndpointInvokedValue = new object(); | ||
|
|
||
| public Task Invoke(HttpContext context) | ||
| { | ||
| var endpoint = context.GetEndpoint(); | ||
|
|
||
| if (endpoint is not null) | ||
| { | ||
| context.Items[AntiforgeryMiddlewareWithEndpointInvokedKey] = AntiforgeryMiddlewareWithEndpointInvokedValue; | ||
| } | ||
|
|
||
| var method = context.Request.Method; | ||
| if (!HttpExtensions.IsValidHttpMethodForForm(method)) | ||
| { | ||
| return _next(context); | ||
| } | ||
|
|
||
| if (endpoint?.Metadata.GetMetadata<IAntiforgeryMetadata>() is { RequiresValidation: true }) | ||
| { | ||
| var result = crossOriginAntiforgery.Validate(context); | ||
|
|
||
| return result switch | ||
| { | ||
| CrossOriginValidationResult.Allowed => _next(context), | ||
| _ => HandleDenied(context) | ||
| }; | ||
| } | ||
|
|
||
| return _next(context); | ||
| } | ||
|
|
||
| private async Task HandleDenied(HttpContext context) | ||
| { | ||
| context.Features.Set<IAntiforgeryValidationFeature>(new AntiforgeryValidationFeature(isValid: false, exception: null)); | ||
| await _next(context); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,99 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Licensed to the .NET Foundation under one or more agreements. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using Microsoft.AspNetCore.Antiforgery.CrossOrigin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using Microsoft.AspNetCore.Http; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using Microsoft.Extensions.Options; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace Microsoft.AspNetCore.Antiforgery.Internal; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Validates cross-origin requests using Fetch Metadata headers (Sec-Fetch-Site) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// and Origin header as a fallback. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| internal sealed class CrossOriginRequestValidator : ICrossOriginAntiforgery | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private const string SecFetchSiteHeaderName = "Sec-Fetch-Site"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private const string SecFetchSiteSameOrigin = "same-origin"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private const string SecFetchSiteNone = "none"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private readonly CrossOriginAntiforgeryOptions _options; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public CrossOriginRequestValidator(IOptions<CrossOriginAntiforgeryOptions> options) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _options = options.Value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public CrossOriginValidationResult Validate(HttpContext context) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ArgumentNullException.ThrowIfNull(context); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var request = context.Request; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+31
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public CrossOriginValidationResult Validate(HttpContext context) | |
| { | |
| ArgumentNullException.ThrowIfNull(context); | |
| var request = context.Request; | |
| /// <summary> | |
| /// Validates the specified HTTP context for potential cross-origin attacks. | |
| /// </summary> | |
| /// <param name="context">The <see cref="HttpContext"/> associated with the current request.</param> | |
| /// <returns> | |
| /// A <see cref="CrossOriginValidationResult"/> value indicating whether the request is allowed, | |
| /// denied, or requires additional validation. | |
| /// </returns> | |
| /// <remarks> | |
| /// This method always treats HTTP safe methods (<c>GET</c>, <c>HEAD</c>, <c>OPTIONS</c>, and <c>TRACE</c>) | |
| /// as allowed and does not apply cross-origin antiforgery checks to them. | |
| /// </remarks> | |
| /// <example> | |
| /// <code> | |
| /// var result = crossOriginAntiforgery.Validate(httpContext); | |
| /// if (result == CrossOriginValidationResult.Denied) | |
| /// { | |
| /// // Handle denied cross-origin request | |
| /// } | |
| /// </code> | |
| /// </example> | |
| public CrossOriginValidationResult Validate(HttpContext context) | |
| { | |
| ArgumentNullException.ThrowIfNull(context); | |
| var request = context.Request; | |
| if (HttpMethods.IsGet(request.Method) || | |
| HttpMethods.IsHead(request.Method) || | |
| HttpMethods.IsOptions(request.Method) || | |
| HttpMethods.IsTrace(request.Method)) | |
| { | |
| return CrossOriginValidationResult.Allowed; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UseAntiforgery() still only checks for IAntiforgery, but AntiforgeryMiddleware now also depends on ICrossOriginAntiforgery. Consider updating the UseAntiforgery() verification logic (similar to VerifyCrossOriginAntiforgeryServicesAreRegistered) so the error is thrown at pipeline setup time with an actionable message.