Allow specifying different properties for endpoints #172
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Initial attempt at having different policies for multiple endpoints.
I'm not entirely sure about this at the moment, I feel like it's a bit confusing having to call
UseSecurityHeaders()
multiple times (to make sure you have the "default" headers set).Another option would be to use an IStartupFilter to auto-add the default middleware to the start of the pipeline using the services, but then that could be confusing, as it would differ from the "simple" approach...
Would be interested in feedback/suggestions...
Fixes #55
Fixes #87
The Readme looks something like this:
Applying different headers to different endpoints
In some situations, you may need to apply different security headers to different endpoints. For example, you may want to have a very restrictive Content-Security-Policy by default, but then have a more relaxed on specific endpoints that require it. This is supported, but requires more configuration.
1. Configure your policies using
AddSecurityHeaderPolicies()
You can configure named and default policies by calling
AddSecurityHeaderPolicies()
onIServiceCollection
. You can configure the default policy to use, as well as any named policies. For example, the following configures the default policy (used whenUseSecurityHeaders()
is called without any arguments), and a named policy:2. Add the default middleware early to the pipeline
The security headers middleware can only add headers to all requests if it is early in the middleware pipeline, so it's important to add the headders middleware at the start of your middleware pipeline. However, if you want to have endpoint-specific policies, then you also need to place the middleware after the call to
UseRouting()
, as that is the point at which the endpoint that will be executed is selected.Note that if you pass a policy to any call to
UseSecurityHeaders()
it will override the "default" policy used at that point.3. Apply custom policies to endpoints
To apply a non-default policy to an endpoint, use the
WithSecurityHeadersPolicy(policy)
endpoint extension method, and pass in the name of the policy to apply:If you're using MVC controllers or Razor Pages, you can apply the
[SecurityHeadersPolicy(policyName)]
attribute to your endpoints:Each call to
UseSecurityHeaders()
will re-evaluate the applicable policies; the headers are applied just before the response is sent. The policy to apply is determined as follows, with the first applicable policy selected.SecurityHeadersMiddleware
, use that.SetDefaultPolicy()
, use that.AddDefaultSecurityHeaders()
)