-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update FluentAssertions * Split out EndpointSecurityHeadersMiddleware from SecurityHeadersMiddleware
- Loading branch information
1 parent
7949a01
commit d1b0a6d
Showing
10 changed files
with
250 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/NetEscapades.AspNetCore.SecurityHeaders/EndpointSecurityHeadersMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using NetEscapades.AspNetCore.SecurityHeaders.Headers; | ||
using NetEscapades.AspNetCore.SecurityHeaders.Infrastructure; | ||
|
||
namespace NetEscapades.AspNetCore.SecurityHeaders; | ||
|
||
/// <summary> | ||
/// An ASP.NET Core middleware for adding security headers. | ||
/// </summary> | ||
internal class EndpointSecurityHeadersMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly ILogger<EndpointSecurityHeadersMiddleware> _logger; | ||
private readonly CustomHeaderOptions _options; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EndpointSecurityHeadersMiddleware"/> class. | ||
/// </summary> | ||
/// <param name="next">The next middleware in the pipeline.</param> | ||
/// <param name="logger">A logger for recording errors.</param> | ||
/// <param name="options">Options on how to control the settings that are applied</param> | ||
public EndpointSecurityHeadersMiddleware(RequestDelegate next, ILogger<EndpointSecurityHeadersMiddleware> logger, CustomHeaderOptions options) | ||
{ | ||
_next = next ?? throw new ArgumentNullException(nameof(next)); | ||
_logger = logger; | ||
_options = options; | ||
} | ||
|
||
/// <summary> | ||
/// Invoke the middleware | ||
/// </summary> | ||
/// <param name="context">The current context</param> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||
public Task Invoke(HttpContext context) | ||
{ | ||
// Policy resolution rules: | ||
// | ||
// 1. If there is an endpoint with a named policy, then fetch that policy | ||
// 2. Use the provided default policy | ||
var endpoint = context.GetEndpoint(); | ||
var metadata = endpoint?.Metadata.GetMetadata<ISecurityHeadersPolicyMetadata>(); | ||
|
||
if (!string.IsNullOrEmpty(metadata?.PolicyName)) | ||
{ | ||
if (_options.GetPolicy(metadata.PolicyName) is { } namedPolicy) | ||
{ | ||
context.Items[SecurityHeadersMiddleware.HttpContextKey] = namedPolicy; | ||
} | ||
else | ||
{ | ||
// log that we couldn't find the policy | ||
_logger.LogWarning( | ||
"Error configuring security headers middleware: policy '{PolicyName}' could not be found. " | ||
+ "Configure the policies for your application by calling AddSecurityHeaderPolicies() on IServiceCollection " | ||
+ "and adding a policy with the required name.", | ||
metadata.PolicyName); | ||
} | ||
} | ||
|
||
return _next(context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.