| | | 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> |
| | 98 | 13 | | public sealed class SecurityHeadersMiddleware(RequestDelegate next, IOptions<ApplicationSecurityHeadersOptions> options) |
| | | 14 | | { |
| | 98 | 15 | | private readonly RequestDelegate _next = next ?? throw new ArgumentNullException(nameof(next)); |
| | 98 | 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 | | { |
| | 115 | 26 | | if (!_options.Enabled) |
| | | 27 | | { |
| | 1 | 28 | | await _next(context); |
| | 1 | 29 | | return; |
| | | 30 | | } |
| | | 31 | | |
| | 114 | 32 | | if (IsExcludedPath(context.Request.Path)) |
| | | 33 | | { |
| | | 34 | | // Excluded endpoints (health, metrics) skip the document-oriented headers, but MIME sniffing |
| | | 35 | | // protection costs nothing and applies to every response. |
| | 16 | 36 | | context.Response.OnStarting(() => |
| | 16 | 37 | | { |
| | 16 | 38 | | AddHeaderIfMissing(context.Response.Headers, "X-Content-Type-Options", "nosniff"); |
| | 16 | 39 | | return Task.CompletedTask; |
| | 16 | 40 | | }); |
| | | 41 | | |
| | 16 | 42 | | await _next(context); |
| | 16 | 43 | | return; |
| | | 44 | | } |
| | | 45 | | |
| | 98 | 46 | | context.Response.OnStarting(() => |
| | 98 | 47 | | { |
| | 98 | 48 | | IHeaderDictionary headers = context.Response.Headers; |
| | 98 | 49 | | |
| | 98 | 50 | | AddHeaderIfMissing(headers, "X-Content-Type-Options", "nosniff"); |
| | 98 | 51 | | AddHeaderIfMissing(headers, "X-Frame-Options", "DENY"); |
| | 98 | 52 | | AddHeaderIfMissing(headers, "Referrer-Policy", "strict-origin-when-cross-origin"); |
| | 98 | 53 | | AddHeaderIfMissing(headers, "X-Permitted-Cross-Domain-Policies", "none"); |
| | 98 | 54 | | |
| | 98 | 55 | | // Modern browser isolation / cross-origin protections. |
| | 98 | 56 | | if (_options.EnableCrossOriginHeaders) |
| | 98 | 57 | | { |
| | 98 | 58 | | AddHeaderIfMissing(headers, "Cross-Origin-Opener-Policy", "same-origin"); |
| | 98 | 59 | | AddHeaderIfMissing(headers, "Cross-Origin-Resource-Policy", "same-origin"); |
| | 98 | 60 | | } |
| | 98 | 61 | | |
| | 98 | 62 | | if (_options.EnablePermissionsPolicy && |
| | 98 | 63 | | !string.IsNullOrWhiteSpace(_options.PermissionsPolicy)) |
| | 98 | 64 | | { |
| | 98 | 65 | | AddHeaderIfMissing(headers, "Permissions-Policy", _options.PermissionsPolicy); |
| | 98 | 66 | | } |
| | 98 | 67 | | |
| | 98 | 68 | | if (_options.EnableContentSecurityPolicy && |
| | 98 | 69 | | !string.IsNullOrWhiteSpace(_options.ContentSecurityPolicy)) |
| | 98 | 70 | | { |
| | 98 | 71 | | AddHeaderIfMissing(headers, "Content-Security-Policy", _options.ContentSecurityPolicy); |
| | 98 | 72 | | } |
| | 98 | 73 | | |
| | 98 | 74 | | // Do not add X-XSS-Protection. It is obsolete and can cause problems in some browsers. |
| | 98 | 75 | | |
| | 98 | 76 | | return Task.CompletedTask; |
| | 98 | 77 | | }); |
| | | 78 | | |
| | 98 | 79 | | await _next(context); |
| | 112 | 80 | | } |
| | | 81 | | |
| | | 82 | | private bool IsExcludedPath(PathString path) |
| | | 83 | | { |
| | 114 | 84 | | return _options.ExcludedPathPrefixes.Any(prefix => |
| | 114 | 85 | | path.StartsWithSegments(prefix, StringComparison.OrdinalIgnoreCase)); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | private static void AddHeaderIfMissing( |
| | | 89 | | IHeaderDictionary headers, |
| | | 90 | | string name, |
| | | 91 | | string value) |
| | | 92 | | { |
| | 796 | 93 | | if (!headers.ContainsKey(name)) |
| | | 94 | | { |
| | 610 | 95 | | headers[name] = value; |
| | | 96 | | } |
| | 796 | 97 | | } |
| | | 98 | | } |
| | | 99 | | |