在我的网站上实现 CSP 标头时,我遇到了 webforms 添加到页面的自动生成的回发 JavaScript 的问题:
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
Run Code Online (Sandbox Code Playgroud)
为了支持其他一些内联脚本标签,我已成功添加了该nonce属性;但是我找不到修改这段生成的代码来做同样的事情的方法。我已经探索过ClientScript.GetPostBackEventReference,但这似乎控制了其中的 JavaScript,与<script>标签本身的呈现无关。
解决方案不一定需要涉及添加nonce属性——任何符合的都可以。例如,如果有一个 ASP.NET 设置可以配置为将此脚本作为文件加载(我可以将其列入白名单),那就没问题了。