# 由于内容安全策略的限制，我无法在着陆页插件中让脚本标签正常工作

**URL:** https://meta.discourse.org/t/cant-get-script-tag-to-work-in-landing-pages-plugin-due-to-content-security-policy/372200
**Category:** Support
**Created:** [2025年六月30日 00:54 UTC](https://meta.discourse.org/t/cant-get-script-tag-to-work-in-landing-pages-plugin-due-to-content-security-policy/372200 "2025-06-30T00:54:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![TheNab](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thenab/32/510092_2.png) [@TheNab](https://meta.discourse.org/u/TheNab)
#### Post date: [2025年六月30日 00:54 UTC](https://meta.discourse.org/t/cant-get-script-tag-to-work-in-landing-pages-plugin-due-to-content-security-policy/372200/1 "2025-06-30T00:54:47Z")

</div>

由於 CSP 限制，內嵌的 JS 腳本標籤無法加載，我不知道如何修復。

---

<div class="post-metadata">

### Author: ![awesomerobot](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/awesomerobot/32/142900_2.png) [@awesomerobot](https://meta.discourse.org/u/awesomerobot)
#### Post date: [2025年六月30日 13:12 UTC](https://meta.discourse.org/t/cant-get-script-tag-to-work-in-landing-pages-plugin-due-to-content-security-policy/372200/2 "2025-06-30T13:12:38Z")

</div>

此帖子中的一些信息可能有所帮助：[Mitigate XSS Attacks with Content Security Policy](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243#p-510350-csp-and-third-party-integrations-8)

> [@Discourse](#):
>
> ## CSP 和第三方集成
> 
> 在使用 Google Tag Manager、Google Analytics 或广告服务等第三方服务时，您可能需要调整 CSP 设置。在大多数情况下，对于 Discourse 3.3.0.beta1 或更高版本，由于实现了“strict-dynamic”CSP，外部脚本应无需额外配置即可正常工作。
> 
> 如果遇到问题，您可能需要：
> 
> 1. 通过监控浏览器控制台来识别所需的脚本来源
> 2. 将必要的来源添加到 `content_security_policy_script_src` 设置中
> 3. 对于加载外部资源的复杂集成（如广告服务），您可能需要启用跨域渲染（[discourse-adplugin 的示例 PR](https://github.com/discourse/discourse-adplugin/pull/201) 进行了此操作）。
> 
> ## 最佳实践
> 
> 1. 从 CSP Report-Only 模式开始，以识别潜在问题
> 2. 在解决合法的违规行为时，逐步收紧您的 CSP
> 3. 定期审查您的 CSP 设置并根据需要进行调整
> 4. 添加 `'unsafe-eval'` 或 `'wasm-unsafe-eval'` 等宽松指令时要小心
> 5. 保持您的 Discourse 实例更新，以受益于最新的 CSP 改进

---

<div class="post-metadata">

### Author: ![TheNab](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thenab/32/510092_2.png) [@TheNab](https://meta.discourse.org/u/TheNab)
#### Post date: [2025年六月30日 17:14 UTC](https://meta.discourse.org/t/cant-get-script-tag-to-work-in-landing-pages-plugin-due-to-content-security-policy/372200/3 "2025-06-30T17:14:46Z")

</div>

我已经阅读了这些和其他内容，但仍然无法理清如何实际将例外添加到 content\_security\_policy\_script\_src。

---

<div class="post-metadata">

### Author: ![awesomerobot](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/awesomerobot/32/142900_2.png) [@awesomerobot](https://meta.discourse.org/u/awesomerobot)
#### Post date: [2025年七月1日 15:46 UTC](https://meta.discourse.org/t/cant-get-script-tag-to-work-in-landing-pages-plugin-due-to-content-security-policy/372200/4 "2025-07-01T15:46:57Z")

</div>

您的浏览器控制台中是否出现有关脚本的错误？类似这样？

 ![浏览器中红色的错误消息，详细说明了阻止内联脚本执行的安全策略违规，并引用了“unsafe-inline”等指令和必需的关键字，或者用于启用脚本的 nonce。（由 AI 标注）](https://global.discourse-cdn.com/meta/original/4X/1/6/f/16f0b2e6e7c2be9466c46d9e8618a95d45807c33.png)

您需要将提供的哈希值（`'sha256-xxxxx'`）添加到管理员 \> 所有站点设置中找到的“内容安全策略脚本源”设置中。

---

<div class="post-metadata">

### Author: ![TheNab](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thenab/32/510092_2.png) [@TheNab](https://meta.discourse.org/u/TheNab)
#### Post date: [2025年七月1日 15:57 UTC](https://meta.discourse.org/t/cant-get-script-tag-to-work-in-landing-pages-plugin-due-to-content-security-policy/372200/5 "2025-07-01T15:57:04Z")

</div>

我在 Firefox 浏览器中遇到的错误是 nonce-s0m3h4sh，而我没有遇到 sha256-s0m3h4sh。但当我刚才在 Chrome 中查看时，它却是 sha256 的那个。

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2025年七月31日 15:58 UTC](https://meta.discourse.org/t/cant-get-script-tag-to-work-in-landing-pages-plugin-due-to-content-security-policy/372200/6 "2025-07-31T15:58:00Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
