简体   繁体   English

如何允许内联 JS 脚本使用 Nonce 进行 CSP

[英]How to allow Inline JS Scripts using Nonces for CSP

I am trying to implement CSP on my site.我正在尝试在我的网站上实施 CSP。 I change my inline scripts a lot, so hashes are a bad idea for me for CSP我经常更改我的内联脚本,所以对于 CSP 来说,哈希对我来说是个坏主意

I read that Nonces which are random integers can be used to implement CSP.我读到可以使用随机整数的 Nonce 来实现 CSP。

I am using WordPress.我正在使用 WordPress。 And below is my CSP header.下面是我的 CSP 标头。

Header add Content-Security-Policy "default-src 'self'; script-src unsafe-hashes 'self' https://milyin.com https://cdnjs.cloudflare.com https://cdn.tiny.cloud  https:; object-src 'none';base-uri 'none';img-src https: data:;style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://www.google.com https:;report-uri https://milyin.com/?csp=true"

As you might be able to tell, inline scripts wont execute from it.正如您可能知道的那样,内联脚本不会从中执行。 So how to implement it.那么如何实现呢。

Initial research found me the idea of hashes, and the fact that I can get hash of scripts from chrome Dev Tools made it simple and obvious.最初的研究让我发现了哈希的想法,而且我可以从 chrome 开发工具中获取脚本的哈希这一事实使它变得简单明了。

However, the fact that slightest change to any script would require me to change the hash was a headache.但是,对任何脚本进行最轻微的更改都会要求我更改哈希,这一事实令人头疼。

So I heard of nonces.所以我听说了nonce。

Please suggest how to implement it, and (if possible) tell if their exists some easier way for wordpress user like me.请建议如何实现它,并(如果可能)告诉他们是否存在像我这样的 wordpress 用户更简单的方法。

So I have couple of doubts:所以我有几个疑问:

  1. What is Nonce?什么是随机数? As far as I understand it's a random integer of my liking.据我了解,这是我喜欢的随机整数。

  2. Can I use same nonce all scripts?我可以使用相同的 nonce 所有脚本吗? I mean can i put say Header add Content-Security-Policy "default-src 'self'; script-src 'self' 'nonce-69' and then put <script nonce="69" in each and every script of my site?我的意思是我可以说Header add Content-Security-Policy "default-src 'self'; script-src 'self' 'nonce-69'然后把<script nonce="69"放在我网站的每个脚本中?

  3. Do I need to change Nonce frequently?我需要经常更改 Nonce 吗? I mean if Nonce is constant then someone can simply put inject script with same nonce and get it working.我的意思是如果 Nonce 是恒定的,那么有人可以简单地使用相同的 nonce 放置注入脚本并让它工作。 How to prevent that.如何防止这种情况。

1.) What is Nonce? 1.) 什么是随机数? As far as I understand it's a random integer of my liking.据我了解,这是我喜欢的随机整数。

Nonce is base64 encoded value: Nonce 是 base64 编码值:

; Nonces: 'nonce-[nonce goes here]'
nonce-source  = "'nonce-" base64-value "'"
base64-value  = 1*( ALPHA / DIGIT / "+" / "/" / "-" / "_" )*2( "=" )

2.) Can I use same nonce all scripts? 2.) 我可以使用相同的 nonce 所有脚本吗? I mean can i put say Header add Content-Security-Policy "default-src 'self'; script-src 'self' 'nonce-69' and then put <script nonce="69" in each and every script of my site?我的意思是我可以说 Header add Content-Security-Policy "default-src 'self'; script-src 'self' 'nonce-69' 然后把 <script nonce="69" 放在我网站的每个脚本中?

Yes, you can use once generated nonce and do not need to generate a new nonce for each script on the page.是的,您可以使用一次生成的随机数,而无需为页面上的每个脚本生成一个新的随机数。
To generate nonce you can use:要生成随机数,您可以使用:

3.) Do I need to change Nonce frequently? 3.) 我需要经常更改 Nonce 吗? I mean if Nonce is constant then someone can simply put inject script with same nonce and get it working.我的意思是如果 Nonce 是恒定的,那么有人可以简单地使用相同的 nonce 放置注入脚本并让它工作。 How to prevent that.如何防止这种情况。

According to CSP spec The server MUST generate a unique nonce value each time it transmits a policy.根据CSP 规范,服务器每次传输策略时都必须生成一个唯一的 nonce 值。 The generated value SHOULD be at least 128 bits long (before encoding), and SHOULD be generated via a cryptographically secure random number generator in order to ensure that the value is difficult for an attacker to predict.生成的值应至少为 128 位长(编码前),并且应通过密码安全的随机数生成器生成,以确保攻击者难以预测该值。
It means you must regenerate a nonce on each page loading.这意味着您必须在每个页面加载时重新生成一个随机数。

BTW, you have an error in the ... script-src unsafe-hashes 'self' ... - the unsafe-hashes token should be a single-quoted: 'unsafe-hashes' .顺便说一句,你有一个错误... script-src unsafe-hashes 'self' ... - unsafe-hashes标记应该是一个单引号: 'unsafe-hashes' But anyway it's not useful since it's not supported by Safari.但无论如何它没有用,因为 Safari 不支持它。

A unique nonce has to be generated for every pages load The architecture to roll-out a nonce-based CSP is generally used in custom web applications - and would be very complex for a Wordpress site, as I imagine that you may be using Caching / CDN.必须为每个页面加载生成一个唯一的 nonce推出基于 nonce 的 CSP 的架构通常用于自定义 Web 应用程序 - 对于 Wordpress 网站来说会非常复杂,因为我想您可能正在使用 Caching / CDN。

I would suggest remaining with the 'unsafe-inline' for the Wordpress site.我建议保留 Wordpress 网站的'unsafe-inline' If you have other more important pages (like a checkout page / admin page), you can create a separate CSP.如果您有其他更重要的页面(如结帐页面/管理页面),您可以创建一个单独的 CSP。

With that said, if you do go ahead and implement a CSP with nonces, you will probably need to add the 'strict-dynamic' and 'unsafe-eval' permissions.话虽如此,如果您继续使用 nonces 实现 CSP,您可能需要添加'strict-dynamic''unsafe-eval'权限。

I wrote a small class to generated the header and nonce to be used later.我写了一个小类来生成标题和随机数以供以后使用。 Every construct of the CspBuilder class will create a unique nonce which can be used later. CspBuilder类的每个构造都将创建一个唯一的随机数,供以后使用。 As long as the scripts are referenced in a .php file this will work:只要脚本在 .php 文件中被引用,这将起作用:

$Csp = (new \CspBuilder(true))
    -> addCspPolicyNonce(CspDirective::Script)
    -> setCspHeader();

The side effect of setCspHeader() is the generation of a complete Content-Security-Policy . setCspHeader() 的副作用是生成完整的Content-Security-Policy Wherever including a script in a PHP file use:在 PHP 文件中包含脚本的任何地方都使用:

<script nonce="<?=$Csp->getNonce?>">

There are many more options in this small, fully type-checked class.在这个经过全面类型检查的小型类中还有更多选项。 Read the description or the code.阅读说明或代码。 Link here: https://github.com/theking2/php-csp-builder链接在这里: https ://github.com/theking2/php-csp-builder

暂无
暂无

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

相关问题 使用带有 nonces 脚本的 CSP 时,是否有办法在非异步资产上记录资产加载错误? - Is there a way to log asset load errors on non-async assets when using a CSP with nonces for scripts? 多个内联脚本的CSP解决方案 - CSP solution for multiple inline scripts 如何在没有内联脚本的情况下异步加载 CSS(符合 CSP) - How to load CSS Asynchronously without Inline Scripts (comply with CSP) 如何在遵守不允许使用内联脚本的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? 如何使用 AWS Lambda + CloudFront + S3 设置为内联脚本/样式动态添加 CSP 随机数属性? - How to dynamically adds CSP nonce attributes for inline scripts/styles with AWS Lambda + CloudFront + S3 setup? 属性内内联 JS 的 CSP 哈希或随机数 - CSP hash or nonce for inline JS within attribute 如何使用 Accept.js 获得两个单独的随机数? - How To Get Two Separate Nonces with Accept.js? CSP和SSL内联Javascript以及如何在外部引用 - CSP & SSL inline Javascript & how to reference externally Shopify - 在内联部分/片段脚本中使用您的 JS 库等 - Shopify - using your JS libraries etc in inline section / snippet scripts 如何在Chrome打包应用中使用KnockoutJs进行MVVM? 它不允许html中的内联JS - How to do MVVM using KnockoutJs in Chrome Packaged App? it doesntt allow inline JS in html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM