-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow specifying different properties for endpoints (#172)
* nit: Remove dead code * Remove unused package references * Remove ICustomHeaderService and make header service static * Add support for adding multiple named policies, and referencing them in the middleware * Add support for specifying a different header policy for a given endpoint * Allow configuring the default policy in the services * Add ReadMe * Add support for MVC attributes with named policies
- Loading branch information
1 parent
bef205d
commit 6471076
Showing
23 changed files
with
676 additions
and
394 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
30 changes: 30 additions & 0 deletions
30
src/NetEscapades.AspNetCore.SecurityHeaders/EndpointConventionBuilderExtensions.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,30 @@ | ||
using System; | ||
using NetEscapades.AspNetCore.SecurityHeaders; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.AspNetCore.Builder; | ||
|
||
/// <summary> | ||
/// Header extension methods for <see cref="IEndpointConventionBuilder"/>. | ||
/// </summary> | ||
public static class EndpointConventionBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a security headers policy with the provided policy name to the endpoint(s). | ||
/// </summary> | ||
/// <param name="builder">The endpoint convention builder.</param> | ||
/// <param name="policyName">The security headers policy to use.</param> | ||
/// <returns>The original convention builder parameter.</returns> | ||
/// <typeparam name="TBuilder">The type of the endpoint convention builder</typeparam> | ||
public static TBuilder WithSecurityHeadersPolicy<TBuilder>(this TBuilder builder, string policyName) | ||
where TBuilder : IEndpointConventionBuilder | ||
{ | ||
if (builder is null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
builder.Add(endpointBuilder => { endpointBuilder.Metadata.Add(new SecurityHeadersPolicyMetadata(policyName)); }); | ||
return builder; | ||
} | ||
} |
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
26 changes: 0 additions & 26 deletions
26
src/NetEscapades.AspNetCore.SecurityHeaders/Infrastructure/ICustomHeaderService.cs
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/NetEscapades.AspNetCore.SecurityHeaders/Infrastructure/ISecurityHeadersPolicyMetadata.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,12 @@ | ||
namespace NetEscapades.AspNetCore.SecurityHeaders; | ||
|
||
/// <summary> | ||
/// Metadata about which policy to apply to and endpoint | ||
/// </summary> | ||
internal interface ISecurityHeadersPolicyMetadata | ||
{ | ||
/// <summary> | ||
/// The name of the policy to apply | ||
/// </summary> | ||
public string PolicyName { get; } | ||
} |
73 changes: 73 additions & 0 deletions
73
src/NetEscapades.AspNetCore.SecurityHeaders/Infrastructure/SecurityHeaderPolicyBuilder.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,73 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
namespace NetEscapades.AspNetCore.SecurityHeaders.Infrastructure; | ||
|
||
/// <summary> | ||
/// Used to configure <see cref="HeaderPolicyCollection"/> for security headers | ||
/// </summary> | ||
public class SecurityHeaderPolicyBuilder | ||
{ | ||
private readonly CustomHeaderOptions _options; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SecurityHeaderPolicyBuilder"/> class. | ||
/// </summary> | ||
/// <param name="options">The options where the configuration is stored</param> | ||
internal SecurityHeaderPolicyBuilder(CustomHeaderOptions options) | ||
{ | ||
_options = options; | ||
} | ||
|
||
/// <summary> | ||
/// Adds a security header policy to the application with the given name. | ||
/// This name can be used to refer to the policy in endpoints | ||
/// </summary> | ||
/// <param name="name">The name of the policy </param> | ||
/// <param name="configurePolicy">An <see cref="Action{T}"/>to configure the security headers for the policy</param> | ||
/// <returns>The <see cref="SecurityHeaderPolicyBuilder"/> for chaining</returns> | ||
public SecurityHeaderPolicyBuilder AddPolicy(string name, Action<HeaderPolicyCollection> configurePolicy) | ||
{ | ||
var policyCollection = new HeaderPolicyCollection(); | ||
configurePolicy(policyCollection); | ||
return AddPolicy(name, policyCollection); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a security header policy to the application with the given name. | ||
/// This name can be used to refer to the policy in endpoints | ||
/// </summary> | ||
/// <param name="name">The name of the policy </param> | ||
/// <param name="policyCollection">The security headers for the policy</param> | ||
/// <returns>The <see cref="SecurityHeaderPolicyBuilder"/> for chaining</returns> | ||
public SecurityHeaderPolicyBuilder AddPolicy(string name, HeaderPolicyCollection policyCollection) | ||
{ | ||
_options.NamedPolicyCollections[name] = policyCollection; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the default security header policy to use when no other named policy is provided | ||
/// This policy is used wherever a named policy does not apply | ||
/// </summary> | ||
/// <param name="configurePolicy">An <see cref="Action{T}"/>to configure the security headers for the policy</param> | ||
/// <returns>The <see cref="SecurityHeaderPolicyBuilder"/> for chaining</returns> | ||
public SecurityHeaderPolicyBuilder SetDefaultPolicy(Action<HeaderPolicyCollection> configurePolicy) | ||
{ | ||
var policyCollection = new HeaderPolicyCollection(); | ||
configurePolicy(policyCollection); | ||
return SetDefaultPolicy(policyCollection); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the default security header policy to the application. | ||
/// This policy is used wherever a named policy does not apply | ||
/// </summary> | ||
/// <param name="policyCollection">The security headers for the policy</param> | ||
/// <returns>The <see cref="SecurityHeaderPolicyBuilder"/> for chaining</returns> | ||
public SecurityHeaderPolicyBuilder SetDefaultPolicy(HeaderPolicyCollection policyCollection) | ||
{ | ||
_options.DefaultPolicy = policyCollection; | ||
return this; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/NetEscapades.AspNetCore.SecurityHeaders/Infrastructure/SecurityHeadersPolicyMetadata.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,11 @@ | ||
namespace NetEscapades.AspNetCore.SecurityHeaders; | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="SecurityHeadersPolicyMetadata"/> with the specified <paramref name="policyName"/> | ||
/// </summary> | ||
/// <param name="policyName">The name of the policy to apply</param> | ||
internal class SecurityHeadersPolicyMetadata(string policyName) : ISecurityHeadersPolicyMetadata | ||
{ | ||
/// <inheritdoc/> | ||
public string PolicyName { get; } = policyName; | ||
} |
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.