|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.AspNetCore.Http; |
| 5 | +using Microsoft.AspNetCore.Http.Abstractions.Metadata; |
| 6 | +using Microsoft.AspNetCore.Mvc; |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.Antiforgery; |
| 10 | + |
| 11 | +internal sealed partial class AntiforgeryMiddleware |
| 12 | +{ |
| 13 | + private readonly IAntiforgery _antiforgery; |
| 14 | + private readonly RequestDelegate _next; |
| 15 | + private readonly ILogger<AntiforgeryMiddleware> _logger; |
| 16 | + |
| 17 | + public AntiforgeryMiddleware(IAntiforgery antiforgery, RequestDelegate next, ILogger<AntiforgeryMiddleware> logger) |
| 18 | + { |
| 19 | + _antiforgery = antiforgery; |
| 20 | + _next = next; |
| 21 | + _logger = logger; |
| 22 | + } |
| 23 | + |
| 24 | + public Task Invoke(HttpContext context) |
| 25 | + { |
| 26 | + var endpoint = context.GetEndpoint(); |
| 27 | + if (endpoint is null) |
| 28 | + { |
| 29 | + return _next(context); |
| 30 | + } |
| 31 | + |
| 32 | + var antiforgeryMetadata = endpoint.Metadata.GetMetadata<IAntiforgeryMetadata>(); |
| 33 | + if (antiforgeryMetadata is null) |
| 34 | + { |
| 35 | + Log.NoAntiforgeryMetadataFound(_logger); |
| 36 | + return _next(context); |
| 37 | + } |
| 38 | + |
| 39 | + if (antiforgeryMetadata is not IValidateAntiforgeryMetadata validateAntiforgeryMetadata) |
| 40 | + { |
| 41 | + Log.IgnoreAntiforgeryMetadataFound(_logger); |
| 42 | + return _next(context); |
| 43 | + } |
| 44 | + |
| 45 | + if (_antiforgery is DefaultAntiforgery defaultAntiforgery) |
| 46 | + { |
| 47 | + var valueTask = defaultAntiforgery.TryValidateAsync(context, validateAntiforgeryMetadata.ValidateIdempotentRequests); |
| 48 | + if (valueTask.IsCompletedSuccessfully) |
| 49 | + { |
| 50 | + var (success, message) = valueTask.GetAwaiter().GetResult(); |
| 51 | + if (success) |
| 52 | + { |
| 53 | + Log.AntiforgeryValidationSucceeded(_logger); |
| 54 | + return _next(context); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + Log.AntiforgeryValidationFailed(_logger, message); |
| 59 | + return WriteAntiforgeryInvalidResponseAsync(context, message); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return TryValidateAsyncAwaited(context, valueTask); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + return ValidateNonDefaultAntiforgery(context); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private async Task TryValidateAsyncAwaited(HttpContext context, ValueTask<(bool success, string? message)> tryValidateTask) |
| 72 | + { |
| 73 | + var (success, message) = await tryValidateTask; |
| 74 | + if (success) |
| 75 | + { |
| 76 | + Log.AntiforgeryValidationSucceeded(_logger); |
| 77 | + await _next(context); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + Log.AntiforgeryValidationFailed(_logger, message); |
| 82 | + await context.Response.WriteAsJsonAsync(new ProblemDetails |
| 83 | + { |
| 84 | + Status = StatusCodes.Status400BadRequest, |
| 85 | + Title = "Antiforgery validation failed", |
| 86 | + Detail = message, |
| 87 | + }); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private async Task ValidateNonDefaultAntiforgery(HttpContext context) |
| 92 | + { |
| 93 | + if (await _antiforgery.IsRequestValidAsync(context)) |
| 94 | + { |
| 95 | + Log.AntiforgeryValidationSucceeded(_logger); |
| 96 | + await _next(context); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + Log.AntiforgeryValidationFailed(_logger, message: null); |
| 101 | + await WriteAntiforgeryInvalidResponseAsync(context, message: null); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private static Task WriteAntiforgeryInvalidResponseAsync(HttpContext context, string? message) |
| 106 | + { |
| 107 | + context.Response.StatusCode = StatusCodes.Status400BadRequest; |
| 108 | + return context.Response.WriteAsJsonAsync(new ProblemDetails |
| 109 | + { |
| 110 | + Status = StatusCodes.Status400BadRequest, |
| 111 | + Title = "Antiforgery validation failed", |
| 112 | + Detail = message, |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + private static partial class Log |
| 117 | + { |
| 118 | + [LoggerMessage(1, LogLevel.Debug, "No antiforgery metadata found on the endpoint.", EventName = "NoAntiforgeryMetadataFound")] |
| 119 | + public static partial void NoAntiforgeryMetadataFound(ILogger logger); |
| 120 | + |
| 121 | + [LoggerMessage(2, LogLevel.Debug, $"Antiforgery validation suppressed on endpoint because {nameof(IValidateAntiforgeryMetadata)} was not found.", EventName = "IgnoreAntiforgeryMetadataFound")] |
| 122 | + public static partial void IgnoreAntiforgeryMetadataFound(ILogger logger); |
| 123 | + |
| 124 | + [LoggerMessage(3, LogLevel.Debug, "Antiforgery validation completed successfully.", EventName = "AntiforgeryValidationSucceeded")] |
| 125 | + public static partial void AntiforgeryValidationSucceeded(ILogger logger); |
| 126 | + |
| 127 | + [LoggerMessage(4, LogLevel.Debug, "Antiforgery validation failed with message '{message}'.", EventName = "AntiforgeryValidationFailed")] |
| 128 | + public static partial void AntiforgeryValidationFailed(ILogger logger, string? message); |
| 129 | + } |
| 130 | +} |
0 commit comments