I need to support an older website running on Asp.net, C# and .NET 4.6.1. Security Audit has informed us that we need to enforce Content Security Policy. I have searched google but found no concrete answers to if there is support for webforms (not MVC) in NWebSec.
https://docs.nwebsec.com/en/latest/ does mention support for Asp.net 4 as follows:
NWebsec for ASP.NET 4
Historically, NWebsec has been targeting ASP.NET 4. The following packages target ASP.NET 4:
NWebsec
NWebsec.Mvc
NWebsec.Owin
To test the NWebSec support for asp.net webforms, I followed these steps:
Created a new Asp.net webforms project in VS 2017 using .net 4.6.1. Tested it as working.
Added a NuGet package as specified here - https://www.nuget.org/packages/NWebsec/: Install-Package NWebsec -Version 6.0.0
Changed the Web.config file to include the following:
<nwebsec>
<httpHeaderSecurityModule xmlns="http://nwebsec.com/HttpHeaderSecurityModuleConfig.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="NWebsecConfig/HttpHeaderSecurityModuleConfig.xsd">
<securityHttpHeaders>
<content-Security-Policy enabled="true">
<default-src self="true"/>
<script-src self="true" enabled="true">
<add source="maxcdn.bootstrapcdn.com" />
<add source="code.jquery.com" />
<add source="ajax.googleapis.com" />
<!--<add source ="localhost:60252"/>-->
</script-src>
<style-src self="true" />
<report-uri enableBuiltinHandler="true"/>
</content-Security-Policy>
</securityHttpHeaders>
</httpHeaderSecurityModule>
</nwebsec>
4.Saved and Ran the project.
5.I got the following errors in Google Chrome Console
`modernizr-2.8.3.js:134 Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-CwE3Bg0VYQOIdNAkbB/Btdkhul49qZuwgNCMPgNY5zw='), or a nonce ('nonce-...') is required to enable inline execution.
localhost/:20 Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' maxcdn.bootstrapcdn.com code.jquery.com ajax.googleapis.com". Either the 'unsafe-inline' keyword, a hash ('sha256-uYoAmCrBFM4tx/Ww+6eFuIJxuwZ3YFRT7fWUTlgnPuE='), or a nonce ('nonce-...') is required to enable inline execution.
localhost/:39 Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' maxcdn.bootstrapcdn.com code.jquery.com ajax.googleapis.com". Either the 'unsafe-inline' keyword, a hash ('sha256-2vr5KMButMK7a+bOf/ned/cPnF2yNooMulXA8E65wGw='), or a nonce ('nonce-...') is required to enable inline execution.
localhost/:52 Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' maxcdn.bootstrapcdn.com code.jquery.com ajax.googleapis.com". Either the 'unsafe-inline' keyword, a hash ('sha256-AJipRK0+ga273yKzZZX3BqTHwvwc1v3R9erdu31Wh6I='), or a nonce ('nonce-...') is required to enable inline execution.`
Clicking on the error in bold in the Chrome Console for example leads to the following block of JavaScript which is being injected into the aspx page by the Asp.net framework
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['ctl01'];
if (!theForm) {
theForm = document.ctl01;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
I would like to see these Google console errors go away, however, I do not want to allow the 'unsafe-inline' keyword or use the hash ('sha256-uYoAmCrBFM4tx/Ww+6eFuIJxuwZ3YFRT7fWUTlgnPuE='), or a nonce ('nonce-...').
Any help/pointers/support is appreciated.