我需要支持在 Asp.net、C# 和 .NET 4.6.1 上运行的旧网站。安全审计通知我们,我们需要执行内容安全政策。我搜索了谷歌,但没有找到关于 NWebSec 中是否支持网络表单(不是 MVC)的具体答案。 https://docs.nwebsec.com/en/latest/确实提到了对 Asp.net 4 的支持,如下所示:
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
为了测试对 asp.net webforms 的 NWebSec 支持,我按照以下步骤操作:
使用 .net 4.6.1 在 VS 2017 中创建了一个新的 Asp.net webforms 项目。测试它工作正常。
添加了此处指定的 NuGet 包 - https://www.nuget.org/packages/NWebsec/:Install-Package NWebsec -Version 6.0.0
更改了 Web.config 文件以包含以下内容:
<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.保存并运行项目。
5.Google Chrome 控制台出现以下错误
`modernizr-2.8.3.js:134 拒绝应用内联样式,因为它违反了以下内容安全策略指令:“style-src 'self'”。启用内联执行需要“unsafe-inline”关键字、哈希(“sha256-CwE3Bg0VYQOIdNAkbB/Btdkhul49qZuwgNCMPgNY5zw=”)或随机数(“nonce-...”)。
localhost/:20 拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src 'self' maxcdn.bootstrapcdn.com code.jquery.com ajax.googleapis.com”。启用内联执行需要“unsafe-inline”关键字、哈希(“sha256-uYoAmCrBFM4tx/Ww+6eFuIJxuwZ3YFRT7fWUTlgnPuE=”)或随机数(“nonce-...”)。
localhost/:39 拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src 'self' maxcdn.bootstrapcdn.com code.jquery.com ajax.googleapis.com”。启用内联执行需要“unsafe-inline”关键字、哈希(“sha256-2vr5KMButMK7a+bOf/ned/cPnF2yNooMulXA8E65wGw=”)或随机数(“nonce-...”)。
localhost/:52 拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src 'self' maxcdn.bootstrapcdn.com code.jquery.com ajax.googleapis.com”。启用内联执行需要“unsafe-inline”关键字、哈希(“sha256-AJipRK0+ga273yKzZZX3BqTHwvwc1v3R9erdu31Wh6I=”)或随机数(“nonce-...”)。
例如,单击 Chrome 控制台中的粗体错误会导致以下 JavaScript 块被 Asp.net 框架注入到 aspx 页面中
<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>
我希望看到这些 Google 控制台错误消失,但是,我不想允许使用 'unsafe-inline' 关键字或使用哈希('sha256-uYoAmCrBFM4tx/Ww+6eFuIJxuwZ3YFRT7fWUTlgnPuE=')或随机数('nonce -...')。任何帮助/指针/支持表示赞赏。