简体   繁体   English

如何允许使用“不安全哈希”在 CSP 中执行内联事件处理程序

[英]How to allow the execution of inline event handler in CSP with the 'unsafe-hashes'

We are trying to enable Content Security Policy to prevent XSS, so we put the script-src 'self' in order to avoid inline scripts.我们正在尝试启用内容安全策略来防止 XSS,因此我们将 script-src 设置为“self”以避免内联脚本。 However our application is using a third party javascript library that has inline event handler.但是,我们的应用程序正在使用具有内联事件处理程序的第三方 javascript 库。

We read that adding the unsafe-hashes attribute with a sha256 hash in order to allow this specific library to execute inline events:我们读到添加带有 sha256 hash 的 unsafe-hashes 属性,以允许该特定库执行内联事件:

We added the following policy in the web.cofig:我们在 web.cofig 中添加了以下策略:

However is giving us the following error in Google Chrome browser console:但是在 Google Chrome 浏览器控制台中给我们以下错误:

maskedinput.js:7 Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src-attr 'unsafe-hashes' 'sha256-TS+Ok6crNR4qdKYT6T5jwMHjsPdCzbg7dmT0eWCSN20='". maskedinput.js:7 拒绝执行内联事件处理程序,因为它违反了以下内容安全策略指令:“script-src-attr 'unsafe-hashes' 'sha256-TS+Ok6crNR4qdKYT6T5jwMHjsPdCzbg7dmT0eWCSN20='”。 Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.启用内联执行需要“不安全内联”关键字、hash(“sha256-...”)或随机数(“nonce-...”)。

Any idea how to avoid inline script, but at the same time allowing this specific library to run inline events.任何想法如何避免内联脚本,但同时允许这个特定的库运行内联事件。

You can use the unsafe-hashes like so.您可以像这样使用unsafe-hashes Let's say you have:假设您有:

<button type="button" onclick="alert('Hello World')">Show Alert</button>

then you can use this command to generate your hash:然后您可以使用此命令生成您的 hash:

echo -n "alert('Hello World')" | openssl dgst -sha256 -binary | openssl base64

and then you should use it like:然后你应该像这样使用它:

Content-Security-Policy: script-src 'unsafe-hashes' 'sha256-YOUR_HASH_HERE';

in this case the hash YOUR_HASH_HERE would be: ztv5rbjekwogmr7erW0rKA6H3gY4jEFwwdz2cl8bk1I=在这种情况下,hash YOUR_HASH_HERE将是: ztv5rbjekwogmr7erW0rKA6H3gY4jEFwwdz2cl8bk1I=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使Umbraco与NWebSec内置的CSP Report事件处理程序配合使用? - How do I make Umbraco play nice with NWebSec's built in CSP Report event handler? 我如何允许传递包含不安全单词的密码 - How i can allow passing passwords that contain unsafe words 如何在遵守不允许使用内联脚本的CSP的情况下动态设置Google Universal Analytics的跟踪代码? - How can I dynamically set the tracking code for Google Universal Analytics while adhering to a CSP that disallows inline scripts? 内容安全策略-不安全的内联和不安全的评估在Moz和Chrome中不起作用 - content security policy - unsafe inline and unsafe eval not working in moz and chrome 如何在MVC中为DropDownList添加事件处理程序 - How to add event handler for DropDownList in MVC 如何将JavaScript事件处理程序附加到对话框弹出窗口? - How to attach a JavaScript event handler to a dialog popup? 如何在.NET 4上允许带有数据URL的嵌入式图像而不触发请求验证? - How do I allow inline images with data urls on .NET 4 without triggering request validation? 如何从DataSource事件处理程序获取kendoGrid ID? - How to get kendoGrid id from DataSource event handler? 引发附加到元素的事件处理程序 - raise event handler attached to element ObjectContext.SaveChanges事件的事件处理程序 - Event handler for ObjectContext.SaveChanges event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM