我有一个网页(比如 origin=A),其中嵌入了一个从不同域(比如 B)加载的 iframe。 B 从不同域(各种 CDN)加载一堆脚本。我的网页 A 设置了非常严格的 CSP,例如:
default-src 'none'; script-src 'self'; frame-src B
B 没有设置任何 CSP header 。
现在我希望子框架 B 继承 A 的 CSP 规则,并且由于 script-src 'self' 而尝试访问各种 CDN 应该违反其 CSP,但令我惊讶的是,它运行顺利。
所以我的问题是: CSP 如何被子 iframe 继承?如果未提及 iframe 的 CSP,是否取决于其父框架的 CSP?如果是,如何?是否有任何关于它的文档,我找不到任何可以解释上述情况的具体内容。
有没有办法调试子 iframe 继承的 CSP?从 Chrome 的调试器或 FF 的调试器 - 通过选择 iframe 然后显示 iframe 的 CSP?
最佳答案
How CSP is inherited by child iframes?
不是——不是在常见情况下(问题中的“从不同域加载”的情况)。
但是还有其他方法可以填充 iframe
,而 CSP 的工作方式不同(见下文)。
Does it depend on its parent frame's CSP if CSP for iframe is not mentioned?
不,它不适用于常见情况(问题中的“从不同域加载”的情况)。
Is there any documentation somewhere about it
是的,请参阅 Policy applicability section of the CSP2 specification ,上面写着:
Embedded Contexts: Any resource included via
iframe
,object
, orembed
.Unless the embedded resource is a globally unique identifier (or a
srcdoc
iframe), the embedded resource is controlled by the policy delivered with the resource. If the embedded resource is a globally unique identifier orsrcdoc
iframe, it inherits the policy of the context creating it.
“全局唯一标识符”是带有 data:
URL 或其他类型的非分层 URL 的 URL,例如 https
/http
网址。
因此,常见的情况(问题中的“从不同的域加载”)是“嵌入式资源由资源随附的策略控制”的情况——也就是说,它不继承。
相比之下,如果 iframe
是一个 srcdoc
iframe,情况就非常不同了 the spec says :
Whenever a user agent creates an
iframe srcdoc
document in a browsing context nested in the protected resource, if the user agent is enforcing any policies for the protected resource, the user agent MUST enforce those policies on theiframe srcdoc
document as well.
这是一个 srcdoc
iframe 确实继承了其父级的 CSP 策略。
关于javascript - 什么 CSP 子 iframe 从其父级继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43236626/