| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | using ProjectTemplate.Web.Options; |
| | | 3 | | |
| | | 4 | | namespace ProjectTemplate.Web.Middleware; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Middleware that applies a set of security-related HTTP headers to responses. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// The middleware can be configured via <see cref="ApplicationSecurityHeadersOptions"/> to enable/disable |
| | | 11 | | /// individual headers and to exclude certain request path prefixes. |
| | | 12 | | /// </remarks> |
| | 126 | 13 | | public sealed class SecurityHeadersMiddleware(RequestDelegate next, IOptions<ApplicationSecurityHeadersOptions> options) |
| | | 14 | | { |
| | 126 | 15 | | private readonly RequestDelegate _next = next ?? throw new ArgumentNullException(nameof(next)); |
| | 126 | 16 | | private readonly ApplicationSecurityHeadersOptions _options = options?.Value ?? throw new ArgumentNullException(name |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Invokes the middleware for the given <paramref name="context"/>, applying configured |
| | | 20 | | /// security headers to the response when appropriate. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="context">The current HTTP context.</param> |
| | | 23 | | /// <returns>A <see cref="Task"/> that completes when the middleware and the next delegate finish processing.</retur |
| | | 24 | | public async Task InvokeAsync(HttpContext context) |
| | | 25 | | { |
| | 120 | 26 | | if (!_options.Enabled || IsExcludedPath(context.Request.Path)) |
| | | 27 | | { |
| | 22 | 28 | | await _next(context); |
| | 22 | 29 | | return; |
| | | 30 | | } |
| | | 31 | | |
| | 98 | 32 | | context.Response.OnStarting(() => |
| | 98 | 33 | | { |
| | 98 | 34 | | IHeaderDictionary headers = context.Response.Headers; |
| | 98 | 35 | | |
| | 98 | 36 | | AddHeaderIfMissing(headers, "X-Content-Type-Options", "nosniff"); |
| | 98 | 37 | | AddHeaderIfMissing(headers, "X-Frame-Options", "DENY"); |
| | 98 | 38 | | AddHeaderIfMissing(headers, "Referrer-Policy", "strict-origin-when-cross-origin"); |
| | 98 | 39 | | AddHeaderIfMissing(headers, "X-Permitted-Cross-Domain-Policies", "none"); |
| | 98 | 40 | | |
| | 98 | 41 | | // Modern browser isolation / cross-origin protections. |
| | 98 | 42 | | if (_options.EnableCrossOriginHeaders) |
| | 98 | 43 | | { |
| | 96 | 44 | | AddHeaderIfMissing(headers, "Cross-Origin-Opener-Policy", "same-origin"); |
| | 96 | 45 | | AddHeaderIfMissing(headers, "Cross-Origin-Resource-Policy", "same-origin"); |
| | 98 | 46 | | } |
| | 98 | 47 | | |
| | 98 | 48 | | if (_options.EnablePermissionsPolicy && |
| | 98 | 49 | | !string.IsNullOrWhiteSpace(_options.PermissionsPolicy)) |
| | 98 | 50 | | { |
| | 96 | 51 | | AddHeaderIfMissing(headers, "Permissions-Policy", _options.PermissionsPolicy); |
| | 98 | 52 | | } |
| | 98 | 53 | | |
| | 98 | 54 | | if (_options.EnableContentSecurityPolicy && |
| | 98 | 55 | | !string.IsNullOrWhiteSpace(_options.ContentSecurityPolicy)) |
| | 98 | 56 | | { |
| | 96 | 57 | | AddHeaderIfMissing(headers, "Content-Security-Policy", _options.ContentSecurityPolicy); |
| | 98 | 58 | | } |
| | 98 | 59 | | |
| | 98 | 60 | | // Do not add X-XSS-Protection. It is obsolete and can cause problems in some browsers. |
| | 98 | 61 | | |
| | 98 | 62 | | return Task.CompletedTask; |
| | 98 | 63 | | }); |
| | | 64 | | |
| | 98 | 65 | | await _next(context); |
| | 120 | 66 | | } |
| | | 67 | | |
| | | 68 | | private bool IsExcludedPath(PathString path) |
| | | 69 | | { |
| | 118 | 70 | | return _options.ExcludedPathPrefixes.Any(prefix => |
| | 532 | 71 | | path.StartsWithSegments(prefix, StringComparison.OrdinalIgnoreCase)); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | private static void AddHeaderIfMissing( |
| | | 75 | | IHeaderDictionary headers, |
| | | 76 | | string name, |
| | | 77 | | string value) |
| | | 78 | | { |
| | 776 | 79 | | if (!headers.ContainsKey(name)) |
| | | 80 | | { |
| | 632 | 81 | | headers[name] = value; |
| | | 82 | | } |
| | 776 | 83 | | } |
| | | 84 | | } |
| | | 85 | | |