Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure CSP in appsettings.json but want to set OnSendingHeader #76

Open
litera opened this issue Mar 18, 2024 · 1 comment
Open

Configure CSP in appsettings.json but want to set OnSendingHeader #76

litera opened this issue Mar 18, 2024 · 1 comment

Comments

@litera
Copy link

litera commented Mar 18, 2024

I would like to configure CSP in the appsettings.json but as the title suggests also prevent adding the header for some requests that don't make sense (similar to how it's described in the docs to omit the response header on API requests).

I set the configuration using

services.Configure<CspOptions>(configuration.GetSection("Csp"));

and in order to configure OnSendingHeader in the UseCsp:

app.UseCsp(cspBuilder =>
{
    cspBuilder.OnSendingHeader = ctx =>
    {
        ctx.ShouldNotSend = /* boolean condition */
        return Task.Completed;
    }
});

which overrides the CSP options defined in the appsettings.json which IMO is a bug. If the options are already configured, the UseCsp call with the builder parameter should not generate empty CSP which it does.

@litera litera changed the title Configure CSP in appsettings.json but want to use OnSendingHeader Configure CSP in appsettings.json but want to set OnSendingHeader Mar 18, 2024
@litera
Copy link
Author

litera commented Mar 18, 2024

Mitigation until this is resolved

Assuming CSP options are singleton

app.UseCsp();
var options = app.ApplicationServices.GetRequiredService<IOptions<CspOptions>>();
options.Value.OnSendingHeader = context =>
{
    context.ShouldNotSend = context.HttpContext.Request.Path.StartWithSegment("/api");
    return Task.CompletedTask;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant