CSP:不推荐使用 child-src 和 frame-src

CSP: child-src and frame-src deprecated

  1. 在 CSP v2 中 frame-src 已弃用。 child-src建议改用
  2. 在 CSP v3 frame-src 中未弃用并且 child-src 已弃用。
  3. 目前(2017 年 9 月)Chrome:

The 'child-src' directive is deprecated and will be removed in M60, around August 2017. Please use the 'script-src' directive for Workers instead.

那么在现代(负 2 个版本)浏览器中工作的正确指令集合是什么?看起来 frame-src + script-src 就够了吗?但是 script-src 中应该有什么?

PS:"undeprecate" 东西合法吗?

2018-12-20更新

child-src 同时有 been un-deprecated... 所以现在 frame-srcchild-src 都不再被弃用了。但是此处原始答案的指导仍然适用:

So what's correct collection of directives to work in modern (minus 2 versions) browsers?

这个问题的答案似乎取决于您要为其指定策略的具体内容。

  1. 如果您的需求很简单,所以您不想对 iframe 元素和脚本使用不同的策略,那么只需使用 default-src 为两者指定相同的策略。
  2. 如果您的需求更复杂,并且您想要针对 iframe 元素的策略不同于针对其他资源的策略,那么请使用 frame-src。同样,如果您想要的脚本策略不同于其他资源的策略,请使用 script-src.
  3. 如果您不希望工作脚本的策略不同于其他脚本的策略,那么您只需提供 script-src 策略就可以了,并且可以到此为止。
  4. 如果您确实想要工作脚本的策略不同于其他脚本的策略,那么在提供 script-src 策略的同时,提供 [=20= 】 政策太。 worker-src 还不会影响浏览器,但会在浏览器添加支持时面向未来。

如果您对 child-src 未被弃用的理由感到好奇,请参阅 https://github.com/w3c/webappsec-csp/issues/239#issuecomment-336135344

I was hoping other vendors would implement worker-src so we could drive down usage of child-src and remove it from the platform, but it doesn't look like that's happening quickly enough (removing Chrome's weird fallbacks would break a worker load on 0.006% of page views, which is not huge, but not nothing).


2017-09-04回答

它比问题中描述的还要复杂一点,因为 CSP3 规范还引入了 the worker-src directive. But the spec gives the following guidance:

The child-src model has been substantially altered:

  1. The frame-src directive, which was deprecated in CSP Level 2, has been undeprecated, but continues to defer to child-src if not present (which defers to default-src in turn).
  2. A worker-src directive has been added, deferring to script-src if not present (which likewise defers to default-src in turn).
  3. child-src is now deprecated.

那是在 https://w3c.github.io/webappsec-csp/, which is an editor’s draft but is what you should always consult for current spec requirements. Reason: You can’t trust https://www.w3.org/TR/CSP/ to be up to date (and in general you can’t trust anything under https://www.w3.org/TR to be up to date), and editor’s drafts are what browser implementors actually implement from (they don’t wait to implement until something’s published under https://www.w3.org/TR).

无论如何,child-src 指令被弃用的原因是 its effect is specified as:

The child-src directive governs the creation of nested browsing contexts (e.g. <iframe> and <frame> navigations) and Worker execution contexts.

实践中的问题是:iframe 与工作脚本非常不同。所以这就是添加 worker-src 和不推荐使用 frame-src 的原因(因为您确实需要一个单独的指令来为 iframe 元素指定策略),以及为什么 child-src已弃用(因为您真的不想将一种策略应用于 iframe 元素和工作脚本)。

So what's correct collection of directives to work in modern (minus 2 versions) browsers?

这个问题的答案似乎取决于您要为其指定策略的具体内容。

  1. 如果您的需求很简单,所以您不想对 iframe 元素和脚本使用不同的策略,那么只需使用 default-src 为两者指定相同的策略。
  2. 如果您的需求更复杂,并且您想要针对 iframe 元素的策略不同于针对其他资源的策略,那么请使用 frame-src。同样,如果您想要的脚本策略不同于其他资源的策略,请使用 script-src.
  3. 如果您不希望工作脚本的策略不同于其他脚本的策略,那么您只需提供 script-src 策略就可以了,并且可以到此为止。
  4. 如果您确实想要工作脚本的策略不同于其他脚本的策略,那么在提供 script-src 策略的同时,提供 [=20= 】 政策太。 worker-src 还不会影响浏览器,但会在浏览器添加支持时面向未来。

PS: is it even legal to "undeprecate" stuff?

是的。虽然我不记得曾经见过任何其他规范或工作组这样做过,但在这种情况下这是正确的做法——因为 CSP 规范作者和工作组意识到 child-src 是一个错误,并且 frame-src 实际上是必要的,弃用它是错误的。

因此他们解决了这些错误——而且相对较快。它在这种情况下起作用的部分原因是: frame-src 的弃用时间还不够长,以至于浏览器无法放弃对它的支持,而且许多 Web 开发人员也从未开始使用 child-src 开头。