ProNotion
11/20/2023, 3:52 PMContent-Security-Policy
currently set in the web.config. The value in the web.config is similar to the following:
script-src 'self' 'nonce-{0}'
What I cannot seem to do is find a point in the pipeline where this header exists so that I can replace the placeholder with the server generated nonce.
I have a component composer as follows:
public class HttpApplicationEventsComposer : ComponentComposer<HttpApplicationEventsComponent>
{}
private void UmbracoApplicationBase_ApplicationInit(object sender, System.EventArgs e)
{
if (!(sender is HttpApplication application))
{
return;
}
var app = application;
app.PreSendRequestHeaders += AppOnPreSendRequestHeaders;
}
private void AppOnPreSendRequestHeaders(object sender, EventArgs e)
{
if (!HttpContext.Current.Response.Headers.AllKeys.Contains("Content-Security-Policy-Report-Only"))
{
return;
}
var cspReportOnlyHeader = HttpContext.Current.Response.Headers["Content-Security-Policy-Report-Only"];
var scriptNonce = SecurityHelper.GenerateNonce(32);
HttpContext.Current.Response.Headers["Content-Security-Policy-Report-Only"] =
string.Format(cspReportOnlyHeader, scriptNonce);
HttpContext.Current.Items[Constants.Keys.ContentSecurityPolicyScriptNonce] = scriptNonce;
}
At no point can I find the header present in order to modify it before the response is returned to the client. I may well have been looking at this for too long and I am missing something obvious but I can't at the moment see how I can achieve this.
Any ideas?Sebastiaan
11/21/2023, 2:28 PMProNotion
11/27/2023, 1:13 PMProNotion
11/27/2023, 3:10 PMProNotion
11/28/2023, 12:02 PMIComponent
and using that to hook into the BeginRequest
and EndRequest
events in the request pipeline.
I am however now curious if what I was trying to do is actually possible and if not, why not. If I can find some time I will try and do some research into where in the pipeline the headers are added that are defined in the customHeaders
section of the web.config.kdx-perbol
11/28/2023, 1:11 PM