Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zh-cn(update): sync connet-src for the English #10712

Merged
merged 15 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ slug: Web/HTTP/Headers/Content-Security-Policy/base-uri

{{HTTPSidebar}}

HTTP 协议 {{HTTPHeader("Content-Security-Policy")}} 首部字段中的 **`base-uri`** 指令限制了可以应用于一个文档的 {{HTMLElement("base")}} 元素的 URL。假如指令值为空,那么任何 URL 都是允许的。如果指令不存在,那么用户代理会使用 {{HTMLElement("base")}} 元素中的值。

| CSP version | 2 |
| ------------------------------------- | -------------------------------------------- |
| Directive type | {{Glossary("Document directive")}} |
| {{CSP("default-src")}} fallback | No. Not setting this allows anything. |
HTTP {{HTTPHeader("Content-Security-Policy")}}(内容安全策略)中的 **`base-uri`** 指令限制了可以应用于一个文档的 {{HTMLElement("base")}} 元素的 URL。假如指令值为空,那么任何 URL 都是允许的。如果指令不存在,那么用户代理会使用 {{HTMLElement("base")}} 元素中的值。

<table class="properties">
<tbody>
<tr>
<th scope="row">CSP 版本</th>
<td>2</td>
</tr>
<tr>
<th scope="row">指令类型</th>
<td>{{Glossary("Document directive","文档指令")}}</td>
</tr>
<tr>
<th scope="row">{{CSP("default-src")}} 回落</th>
<td>无。没有设置允许任何 URL。</td>
fwqaaq marked this conversation as resolved.
Show resolved Hide resolved
</tr>
</tbody>
</table>

## 语法

Expand All @@ -23,7 +35,9 @@ Content-Security-Policy: base-uri <source> <source>;

### 源

{{page("Web/HTTP/Headers/Content-Security-Policy/default-src", "Sources")}}
此指令与其它 CSP 指令一样,使用大部分与参数相同的源值:[CSP 源值](/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/Sources#sources)。

然而,请注意,一些值对 `base-uri` 没有意义,例如关键字 `unsafe-inline` 和 `strict-dynamic`。
fwqaaq marked this conversation as resolved.
Show resolved Hide resolved

## 示例

Expand All @@ -47,9 +61,9 @@ Header set Content-Security-Policy "base-uri 'self';
add_header Content-Security-Policy "base-uri 'self';"
```

### _违犯策略的案例_
### 违规的案列

假如你的域名不是 example.com,那么将 {{HTMLElement("base")}} 元素的 href 属性值设置成 example.com 会违犯 CSP 策略。
假如你的域名不是 `example.com`,那么将 {{HTMLElement("base")}} 元素的 `href` 属性值设置成 `https://example.com` 会违犯 CSP 策略。
fwqaaq marked this conversation as resolved.
Show resolved Hide resolved

```html example-bad
<meta http-equiv="Content-Security-Policy" content="base-uri 'self'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,97 @@ slug: Web/HTTP/Headers/Content-Security-Policy/connect-src

{{HTTPSidebar}}

HTTP 协议头部{{HTTPHeader("Content-Security-Policy")}} (CSP) 的**`connect-src`** 指令用于控制允许通过脚本接口加载的链接地址。其中受到影响的 API 如下:
HTTP {{HTTPHeader("Content-Security-Policy")}}(内容安全策略,CSP)中的 **`connect-src`** 指令用于限制通过使用脚本接口加载的 URL。其中受限制的 API 如下:

- {{HTMLElement("a")}} {{htmlattrxref("ping", "a")}},
- {{domxref("Fetch")}},
- {{domxref("fetch()")}},
- {{domxref("XMLHttpRequest")}},
- {{domxref("WebSocket")}}, and
- {{domxref("EventSource")}}.

| CSP version | 1 |
| ------------------------------------- | ------------------------------------------------------------------------------------------- |
| Directive type | {{Glossary("Fetch directive")}} |
| {{CSP("default-src")}} fallback | Yes. If this directive is absent, the user agent will look for the `default-src` directive. |

## Syntax

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

```plain
- {{domxref("WebSocket")}},
- {{domxref("EventSource")}}, and
- {{domxref("Navigator.sendBeacon()")}}.
fwqaaq marked this conversation as resolved.
Show resolved Hide resolved

> **备注:** `connect-src 'self'` 并不是所有浏览器都能解析为 websocket 协议,更多信息,请查看这个 [issue](https://github.com/w3c/webappsec-csp/issues/7)。
fwqaaq marked this conversation as resolved.
Show resolved Hide resolved

<table class="properties">
<tbody>
<tr>
<th scope="row">CSP 版本</th>
<td>1</td>
</tr>
<tr>
<th scope="row"> 指令类型</th>
<td>{{Glossary("Fetch directive","Fetch 指令")}}</td>
</tr>
<tr>
<th scope="row">{{CSP("default-src")}} 回落</th>
<td>
是的。如果没有此指令,用户代理将查找 <code>default-src</code> 指令。
fwqaaq marked this conversation as resolved.
Show resolved Hide resolved
</td>
</tr>
</tbody>
</table>

## 语法

connect-src 策略可以允许一个或多个源:

```http
Content-Security-Policy: connect-src <source>;
Content-Security-Policy: connect-src <source> <source>;
```

### Sources
### 源

`<source>` 可以是 [CSP 源值](/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/Sources#sources)列出来的任意一个。

{{page("/Web/HTTP/Headers/Content-Security-Policy/default-src", "Sources")}}
注意,这套相同的值可以用于所有 {{Glossary("fetch directive", "fetch 指令")}}(以及[许多其他指令](/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/Sources#relevant_directives))。

## Examples
## 示例

### Violation cases
### 违规的案例

给定如下 CSP 头部
给定此 CSP 标头

```bash
```http
Content-Security-Policy: connect-src https://example.com/
```

如下的连接请求会被阻塞且不会加载
一下连接被禁止并且将不会夹在
fwqaaq marked this conversation as resolved.
Show resolved Hide resolved

```html
<a ping="https://not-example.com">
<script>
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://not-example.com/");
xhr.send();

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

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

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

navigator.sendBeacon("https://not-example.com/", { ... });
</script>
navigator.sendBeacon("https://not-example.com/", {
/* … */
});
</script></a>
```

## Specifications
## 规范

{{Specifications}}

## Browser compatibility
## 浏览器兼容性

{{Compat}}

## Compatibility notes
### 兼容性注意
fwqaaq marked this conversation as resolved.
Show resolved Hide resolved

- Prior to Firefox 23, `xhr-src` was used in place of the `connect-src` directive and only restricted the use of {{domxref("XMLHttpRequest")}}.
- Firefox 23 之前,`xhr-src` 被用来代替 `connect-src` 指令,并且只用于限制 {{domxref("XMLHttpRequest")}} 的使用。

## See also
## 参见

- {{HTTPHeader("Content-Security-Policy")}}
- {{HTMLElement("a")}} {{htmlattrxref("ping", "a")}}
- {{domxref("Fetch")}}
- {{domxref("fetch()")}}
- {{domxref("XMLHttpRequest")}}
- {{domxref("WebSocket")}}
- {{domxref("EventSource")}}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ slug: Web/HTTP/Headers/Content-Security-Policy/font-src

HTTP {{HTTPHeader("Content-Security-Policy")}} (CSP 内容安全策略) 中 **`font-src`** 指令定义了 {{cssxref("@font-face")}} 加载字体的有效源规则。

| CSP 版本 | 1 |
| --------------------------------- | ----------------------------------------------------------------------- |
| 指令类型 | {{Glossary("Fetch directive")}} |
| {{CSP("default-src")}} 替代 | 是。如果 **`font-src`** 没有指定,则使用 `default-src` 指令。 |
| CSP 版本 | 1 |
| --------------------------- | ------------------------------------------------------------- |
| 指令类型 | {{Glossary("Fetch directive")}} |
| {{CSP("default-src")}} 回落 | 是。如果 **`font-src`** 没有指定,则使用 `default-src` 指令。 |

## 语法

Expand All @@ -23,13 +23,15 @@ Content-Security-Policy: font-src <source> <source>;

### 源代码

{{page("Web/HTTP/Headers/Content-Security-Policy/default-src", "Sources")}}
`<source>` 可以是 [CSP 源值](/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/Sources#sources)列出来的任意一个。

注意,这套相同的值可以用于所有 {{Glossary("fetch directive", "fetch 指令")}}(以及[许多其他指令](/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/Sources#relevant_directives))。

## 示例

### 反例
### 违规的案例

给出这个 CSP
给定此 CSP 标头

```bash
Content-Security-Policy: font-src https://example.com/
Expand Down Expand Up @@ -57,7 +59,7 @@ Content-Security-Policy: font-src https://example.com/

{{Compat}}

## See also
## 参见

- {{HTTPHeader("Content-Security-Policy")}}
- {{HTTPHeader("Content-Security-Policy")}}(内容安全性策略)
fwqaaq marked this conversation as resolved.
Show resolved Hide resolved
- {{cssxref("@font-face")}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ slug: Web/HTTP/Headers/Content-Security-Policy/worker-src

{{HTTPSidebar}}

HTTP {{HTTPHeader("Content-Security-Policy")}}(内容安全策略,CSP)**`worker-src`** 指令指定了 {{domxref("Worker")}}、{{domxref("SharedWorker")}} 或 {{domxref("ServiceWorker")}} 脚本的有效来源。
HTTP {{HTTPHeader("Content-Security-Policy")}}(内容安全策略,CSP)中的 **`worker-src`** 指令指定了 {{domxref("Worker")}}、{{domxref("SharedWorker")}} 或 {{domxref("ServiceWorker")}} 脚本的有效来源。

<table class="properties">
<tbody>
Expand Down