HTTP Content-Security-Policy(内容安全策略,CSP)中的 connect-src 指令用于限制通过使用脚本接口加载的 URL。其中受限制的 API 如下:
- a
ping - fetch()
- XMLHttpRequest
- WebSocket
- EventSource
- Navigator.sendBeacon()
[!NOTE] 并不是所有浏览器都能将
connect-src 'self'解析为 websocket 协议,更多信息,请查看这个 issue。
<table class="properties"> <tbody> <tr> <th scope="row">CSP 版本</th> <td>1</td> </tr> <tr> <th scope="row"> 指令类型</th> <td>Fetch directive</td> </tr> <tr> <th scope="row">default-src 回落</th> <td> 是。如果没有此指令,用户代理将查找 <code>default-src</code> 指令。 </td> </tr> </tbody> </table>
语法
connect-src 策略可以允许一个或多个源:
Content-Security-Policy: connect-src <source>;
Content-Security-Policy: connect-src <source> <source>;
源
<source> 可以是 CSP 源值列出来的任意一个。
注意,这套相同的值可以用于所有 fetch directive(以及许多其他指令)。
示例
违规的案例
给定此 CSP 标头:
Content-Security-Policy: connect-src https://example.com/
以下连接被禁止并且将不会加载:
<a ping="https://not-example.com">
<script>
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://not-example.com/");
xhr.send();
const ws = new WebSocket("https://not-example.com/");
const es = new EventSource("https://not-example.com/");
navigator.sendBeacon("https://not-example.com/", {/* … */});
</script></a
>
规范
浏览器兼容性
兼容性备注
- Firefox 23 之前,
xhr-src被用来代替connect-src指令,并且只用于限制 XMLHttpRequest 的使用。
参见
- Content-Security-Policy
- a
ping - fetch()
- XMLHttpRequest
- WebSocket
- EventSource
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
