在 iframe 中启用 CSP 的不安全评估
Enabling CSP's unsafe-eval in an iframe
我需要将 Vega-js 库(new Function()
evals 的重度用户)嵌入到具有锁定 CSP(不允许 eval)的站点中。我一直在探索两种选择,希望得到有关最佳选择的反馈:
nonce
- 通过 nonce
指令提供使用 eval 的脚本,希望不会触发 eval() 错误。这个假设可能不正确 - 我仍在努力理解 nonce
限制。
- 将 Vega 图放在
<iframe sandbox='allow-scripts'>...</>
中,并通过消息与其通信。我不知道锁定站点上的 <iframe>
是否有可能比其父站点具有更宽松的安全设置(允许评估)。
I don't know if it is possible for an <iframe>
on a locked down site to have a more lax security settings (allow evals) than its parent.
<iframe>
实际上有可能拥有比其父级更宽松的 CSP 政策。除非它是 <iframe srcdoc=…>
,否则它不会从父级的 CSP 策略继承任何内容,并且与父级 CSP 策略完全没有关系。
请参阅 and the section of the CSP spec at https://w3c.github.io/webappsec-csp/2/#which-policy-applies 的答案,其中涵盖 “通过 iframe
、object
或 embed
包含的任何资源” .
nonce
- serve the eval-using script via the nonce
directive, hoping that eval() error will not be triggered. This assumption might not be correct - I'm still trying to comprehend nonce
limitations.
请参阅 中的答案,了解有关 nonce
工作原理的详细说明。
但即使您使用 nonce
将特定脚本“列入白名单”,也不会允许该脚本使用 eval
。有了 CSP 策略,允许使用 eval
的唯一方法是指定 'unsafe-eval'
。如果您指定它,它将允许文档嵌入的所有脚本使用 eval
— 没有办法只允许特定脚本使用 eval
而不允许其他脚本使用
我需要将 Vega-js 库(new Function()
evals 的重度用户)嵌入到具有锁定 CSP(不允许 eval)的站点中。我一直在探索两种选择,希望得到有关最佳选择的反馈:
nonce
- 通过nonce
指令提供使用 eval 的脚本,希望不会触发 eval() 错误。这个假设可能不正确 - 我仍在努力理解nonce
限制。- 将 Vega 图放在
<iframe sandbox='allow-scripts'>...</>
中,并通过消息与其通信。我不知道锁定站点上的<iframe>
是否有可能比其父站点具有更宽松的安全设置(允许评估)。
I don't know if it is possible for an
<iframe>
on a locked down site to have a more lax security settings (allow evals) than its parent.
<iframe>
实际上有可能拥有比其父级更宽松的 CSP 政策。除非它是 <iframe srcdoc=…>
,否则它不会从父级的 CSP 策略继承任何内容,并且与父级 CSP 策略完全没有关系。
请参阅 iframe
、object
或 embed
包含的任何资源” .
nonce
- serve the eval-using script via thenonce
directive, hoping that eval() error will not be triggered. This assumption might not be correct - I'm still trying to comprehendnonce
limitations.
请参阅 nonce
工作原理的详细说明。
但即使您使用 nonce
将特定脚本“列入白名单”,也不会允许该脚本使用 eval
。有了 CSP 策略,允许使用 eval
的唯一方法是指定 'unsafe-eval'
。如果您指定它,它将允许文档嵌入的所有脚本使用 eval
— 没有办法只允许特定脚本使用 eval
而不允许其他脚本使用