aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/http/headers/content-security-policy/connect-src/index.html
blob: 163006b4d003095eed46d3af94b5c06c28cfb400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
---
title: 'CSP: connect-src'
slug: Web/HTTP/Headers/Content-Security-Policy/connect-src
translation_of: Web/HTTP/Headers/Content-Security-Policy/connect-src
---
<div>{{HTTPSidebar}}</div>

<p>HTTP协议头部{{HTTPHeader("Content-Security-Policy")}} (CSP)的<code><strong>connect</strong></code><strong><code>-src</code></strong> 指令用于控制允许通过脚本接口加载的链接地址。其中受到影响的API如下: </p>

<ul>
 <li>{{HTMLElement("a")}} {{htmlattrxref("ping", "a")}},</li>
 <li>{{domxref("Fetch")}},</li>
 <li>{{domxref("XMLHttpRequest")}},</li>
 <li>{{domxref("WebSocket")}}, and</li>
 <li>{{domxref("EventSource")}}.</li>
</ul>

<table class="properties">
 <tbody>
  <tr>
   <th scope="row">CSP version</th>
   <td>1</td>
  </tr>
  <tr>
   <th scope="row">Directive type</th>
   <td>{{Glossary("Fetch directive")}}</td>
  </tr>
  <tr>
   <th scope="row">{{CSP("default-src")}} fallback</th>
   <td>Yes. If this directive is absent, the user agent will look for the <code>default-src</code> directive.</td>
  </tr>
 </tbody>
</table>

<h2 id="Syntax">Syntax</h2>

<p>connect-src 可以设置一个或者多个源地址: </p>

<pre class="syntaxbox">Content-Security-Policy: connect-src &lt;source&gt;;
Content-Security-Policy: connect-src &lt;source&gt; &lt;source&gt;;
</pre>

<h3 id="Sources">Sources</h3>

<p>{{page("/Web/HTTP/Headers/Content-Security-Policy/default-src", "Sources")}}</p>

<h2 id="Examples">Examples</h2>

<h3 id="Violation_cases">Violation cases</h3>

<p>给定如下CSP头部: </p>

<pre class="brush: bash">Content-Security-Policy: connect-src https://example.com/</pre>

<p>如下的连接请求会被阻塞且不会加载: </p>

<pre class="brush: html">&lt;a ping="https://not-example.com"&gt;

&lt;script&gt;
  var xhr = new XMLHttpRequest();
  xhr.open('GET', 'https://not-example.com/');
  xhr.send();

  var ws = new WebSocket("https://not-example.com/");

  var es = new EventSource("https://not-example.com/");

  navigator.sendBeacon("https://not-example.com/", { ... });
&lt;/script&gt;</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{specName("CSP 3.0", "#directive-connect-src", "connect-src")}}</td>
   <td>{{Spec2('CSP 3.0')}}</td>
   <td>No changes.</td>
  </tr>
  <tr>
   <td>{{specName("CSP 1.1", "#directive-connect-src", "connect-src")}}</td>
   <td>{{Spec2('CSP 1.1')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{Compat("http.headers.csp.connect-src")}}</p>

<h2 id="Compatibility_notes">Compatibility notes</h2>

<ul>
 <li>Prior to Firefox 23, <code>xhr-src</code> was used in place of the <code>connect-src</code> directive and only restricted the use of {{domxref("XMLHttpRequest")}}.</li>
</ul>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{HTTPHeader("Content-Security-Policy")}}</li>
 <li>{{HTMLElement("a")}} {{htmlattrxref("ping", "a")}}</li>
 <li>{{domxref("Fetch")}}</li>
 <li>{{domxref("XMLHttpRequest")}}</li>
 <li>{{domxref("WebSocket")}}</li>
 <li>{{domxref("EventSource")}}</li>
</ul>