---
schema_version: "1"
id: b6bec11a165e4c5cac15070397b295453d306144c6b4b5a4311e56af8b64840f
title: 内容安全策略
collection: tableau
version: current
locale: zh-cn
portal_url: https://docs.salesforce.com/tableau/current/zh-cn/server/security_csp.md
canonical_url: https://help.tableau.com/current/server/zh-cn/security_csp.htm
source_site: help.tableau.com
content_type: documentation
content_hash: b9231cc0abadbeb1059abbf73d57fbb5248d8caa1ffdcbd1cde6062870a4e806
description: Configure and enable Content Security Policy on Tableau Server.
---
[跳转到主要内容](#main-content)

# 内容安全策略

Tableau Server 支持内容安全策略 (CSP) 标准。CSP 旨在为抵御跨站点脚本攻击和其他基于 Web 的恶意攻击提供一层额外的安全保护。CSP 以 HTTP 响应头的形式实现，它允许您指定可从何处安全地加载诸如脚本和图像等外部资源。

有关 CSP 的详细信息，请参见 [Mozilla 网站](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CSP)。

## 配置和启用 CSP

使用 [tsm configuration set 选项](https://help.tableau.com/current/server/zh-cn/cli_configuration-set_tsm.htm) 命令来配置和启用 CSP。如果在分布式部署中运行 Tableau Server，请在群集中的初始节点上运行这些命令。配置将在您运行 [tsm pending-changes apply](https://help.tableau.com/current/server/zh-cn/cli_pending-changes.htm#pending-changes-apply) 之后在整个群集中应用。

### 步骤 1：设置默认指令

Tableau Server 包括下表中的一组默认指令。

若要设置指令，请使用以下 tsm 语法：

`tsm configuration set -k content_security_policy.directive.<directive_name> -v "<value>"`

例如，若要设置 `connect_src` 指令，请运行以下命令：

`tsm configuration set -k content_security_policy.directive.connect_src -v "* unsafe-inline"`

<table style="width: 100%;" class="simple-table"><colgroup><col style="width: 45%;"><col style="width: 20%;"><col style="width: 25%;"></colgroup><tbody><tr><th>选项</th><th>默认值</th><th><p>描述</p></th></tr><tr><td>content_security_policy.directive.default_src</td><td>“none”</td><td><p>充当其他拉取指令的后备指令。</p><p><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy__by_cnvoid/default-src" target="_blank"><code>default_src</code> 的有效值</a>。</p></td></tr><tr><td>content_security_policy.directive.connect_src</td><td>*</td><td><p>限制可使用脚本接口加载的 URL。</p><p><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/connect-src" target="_blank"><code>connect_src</code> 的有效值</a>。</p></td></tr><tr><td>content_security_policy.directive.script_src</td><td>*</td><td><p>指定 JavaScript 的有效来源。</p><p><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/script-src" target="_blank"><code>script_src</code> 的有效值</a>。</p></td></tr><tr><td>content_security_policy.directive.style_src</td><td>*“unsafe-inline”</td><td><p>指定样式表的有效来源。</p><p><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/style-src" target="_blank"><code>style_src</code> 的有效值</a>。</p></td></tr><tr><td>content_security_policy.directive.img_src</td><td>* 数据：</td><td><p>指定图像和网站图标的有效来源。</p><p><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/img-src" target="_blank"><code>img_src</code> 的有效值</a>。</p></td></tr><tr><td>content_security_policy.directive.font_src</td><td>* 数据：</td><td><p>指定使用 @font-face 加载的字体的有效来源。</p><p><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/font-src" target="_blank"><code>font_src</code> 的有效值</a>。</p></td></tr><tr><td>content_security_policy.directive.frame_src</td><td>* 数据：</td><td><p>指定使用诸如 &lt;frame&gt; 和 &lt;iframe&gt; 等元素加载的嵌套浏览上下文的有效来源。</p><p><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src" target="_blank"><code>frame_src</code> 的有效值</a>。</p></td></tr><tr><td>content_security_policy.directive.object_src</td><td>数据：</td><td><p>指定 &lt;object&gt;、&lt;embed&gt; 和 &lt;applet&gt; 元素的有效来源。</p><p><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/object-src" target="_blank"><code>object_src</code> 的有效值</a>。</p></td></tr><tr><td>content_security_policy.directive.report_uri</td><td>/vizql/csp-report</td><td><p>指示用户代理报告尝试违反 CSP 的次数。这些违例报告由通过 HTTP POST 请求发送到指定 URI 的 JSON 文档组成。</p><p><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/report-uri" target="_blank"><code>report_uri</code> 的有效值</a>。</p></td></tr></tbody></table>

### 步骤 2：添加其他指令（可选）

Tableau Server 附带的默认指令是 CSP 支持的指令的子集。

有关支持的 CSP 指令的完整列表，请转到 [https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy)。

通过在 content\_security\_policy.directive 命名空间中添加新指令，您可以向现有默认指令集中添加指令。添加新指令时，您必须包括 `--force-keys` 参数。语法如下：

`tsm configuration set -k content_security_policy.directive.<new_directive_name> -v "<value>" --force-keys`

例如，若要添加 [`worker-src`](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/worker-src) 指令，请运行以下命令：

`tsm configuration set -k content_security_policy.directive.worker-src -v "none" --force-keys`

### 步骤 3：指定仅报告的指令（可选）

您可以将 CPS 配置为报告某些指令和执行其他指令。将 content\_security\_policy.enforce\_enabled 设置为 true 时，则执行所有指令（即使 content\_security\_policy.report\_only\_enable 也设置为 true）。

若要将指令指定为“仅报告”而不执行，请将指令添加到 report\_only\_directive 命名空间。添加新指令时，您必须包括 `--force-keys` 参数。语法如下：

`tsm configuration set -k content_security_policy.report_only_directive.<directive_name> -v "<value>" --force-keys`

例如，若要仅报告 `script_src` 指令，请运行以下命令：

`tsm configuration set -k content_security_policy.report_only_directive.script_src -v " http://*.example.com" --force-keys`

### 步骤 4：在 Tableau Server 上启用 CSP

配置了指令之后，在 Tableau Server 上启用 CSP。

以下选项用于为您设置的指令启用执行或仅报告模式。

<table style="width: 100%;" class="simple-table"><colgroup><col style="width: 45%;"><col style="width: 20%;"><col style="width: 25%;"></colgroup><tbody><tr><th>选项</th><th>默认值</th><th><p>描述</p></th></tr><tr><td>content_security_policy.enforce_enabled</td><td>false</td><td><p>向所有请求中添加 CSP 标头，以便浏览器将实施任何违例。</p></td></tr><tr><td>content_security_policy.report_only_enabled</td><td>true</td><td>向所有请求中添加 CSP 标头，以便在 vizql-client 日志中记录任何违例，但浏览器不实施这些违例。</td></tr></tbody></table>

若要执行您已指定的 CSP 指令，请运行以下命令

`tsm configuration set -k content_security_policy.enforce_enabled -v true`

### 步骤 5：运行 tsm pending-changes apply

配置完 CSP 后，运行 [tsm pending-changes apply](https://help.tableau.com/current/server/zh-cn/cli_pending-changes.htm#pending-changes-apply)。

如果待定更改需要重新启动服务器，`pending-changes apply` 命令将显示一个提示，告知您将进行重新启动。即使服务器已停止，此提示也会显示，但在这种情况下不会重新启动。您可以使用 `--ignore-prompt` 选项隐藏提示，但这样做不会改变重新启动行为。如果更改不需要重新启动，则会在不提示的情况下应用更改。有关详细信息，请参见[tsm pending-changes apply](https://help.tableau.com/current/server/zh-cn/cli_pending-changes.htm#pending-changes-apply)。

## 查看 CSP 报告

若要查看给定可视化项的 CSP 违例，请在包括开发者工具的浏览器中加载该可视化项。此示例使用 Chrome 浏览器。

1. 加载一个包含违例情况的测试可视化项，该可视化项托管在您在其中配置了 CSP 的 Tableau Server 部署上。
2. 输入 `CTRL+Shift+I`，在 Chrome 中打开开发者工具。
3. 单击“**Network**”（网络）选项卡。
4. 在“**Filter**”（筛选器）字段中，输入 `csp-report`，然后单击“**Find All**”（查找全部）。
    
    - 如果没有违例，搜索将不会返回任何 CSP 报告。
    - 如果有违例，请单击结果窗格的“Headers”（标题）选项卡，并滚动到底部来查看“**Request Payload**”（请求有效负载）。