HTTP Content-Security-Policy(内容安全策略,CSP)中的 worker-src 指令指定了 Worker、SharedWorker 或 ServiceWorker 脚本的有效来源。
<table class="properties"> <tbody> <tr> <th scope="row">CSP 版本</th> <td>3</td> </tr> <tr> <th scope="row">指令类型</th> <td>Fetch directive</td> </tr> <tr> <th scope="row">回落</th> <td> <p> 如果这个指令不存在,则用户代理在管理 worker 执行时,将首先查找 child-src 指令,然后查找 script-src 指令,最后再查找 default-src 指令。 </p> </td> </tr> </tbody> </table>
语法
worker-src 策略可以允许一个或者多个源:
Content-Security-Policy: worker-src <source>;
Content-Security-Policy: worker-src <source> <source>;
源
<source> 可以是 CSP 源值中的任意一个。
请注意,这套相同的值可以用于所有 fetch directive(以及许多其他的指令)。
示例
违规的案例
给定此 CSP 标头:
Content-Security-Policy: worker-src https://example.com/
Worker、SharedWorker、ServiceWorker 被阻止,无法加载。
<script>
let blockedWorker = new Worker("data:application/javascript,…");
blockedWorker = new SharedWorker("https://not-example.com/");
navigator.serviceWorker.register("https://not-example.com/sw.js");
</script>
规范
浏览器兼容性
参见
- Content-Security-Policy
- Web Worker 的内容安全策略(CSP)
- Worker、SharedWorker、ServiceWorker
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
