内容安全策略阻止插件的 Google API

问题描述 投票:0回答:1

我正在为 Firefox 编写一个插件,但内容安全策略有问题。当我按下按钮时,它需要访问 Google API,所以我在 popup.html 中添加了以下脚本标签

<script src="https://apis.google.com/js/api.js"></script>

但是我遇到了内容安全策略错误。经过多次尝试,我通过将 api.js 下载到插件文件夹并更改为:

来修复它
<script src="api.js"></script>

但是,addon 仍然与 Google 连接并被 Firefox 阻止。以下是来自开发控制台的错误:

Loading failed for the <script> with source “https://apis.google.com/_/scs/abc-static/_/js/k=gapi.lb[....]/cb=gapi.loaded_0?le=scs”. popup.html:1:1

Content Security Policy: The page’s settings blocked the loading of a resource at https://apis.google.com/_/scs/abc-static/_/js/k=gapi.lb[....]/cb=gapi.loaded_0?le=scs (“script-src”).

我尝试在清单文件中添加必要的权限:

"permissions": [
  "...",
  "https://apis.google.com/"
],

或者尝试在 popup.html 中添加元数据,例如:

<meta http-equiv="Content-Security-Policy" content="
"content_security_policy": "default-src 'self'; script-src 'self' https://apis.google.com 'unsafe-eval';">

或许多其他可能性,例如:

script-src 'unsafe-inline';
default-src 'self';

还尝试将“nonce”属性添加到“脚本”标签中:

<script src="https://apis.google.com/js/api.js" nonce="random_value"></script>

然后

script-src 'self' https://apis.google.com 'nonce-random_value';

或者使用像

*.google.com

这样的通配符

但我一直有同样的问题。我知道这个问题在 StackOverflow 上被问过很多次,但没有一个解决方案适合我。我怎样才能让它工作?谢谢。

javascript firefox-addon content-security-policy firefox-addon-webextensions firefox-developer-tools
1个回答
0
投票

您的站点上有一个默认的内容安全策略,很可能用作响应标头。您需要确定此策略的设置位置和方式,并调整指令以支持您正在加载的内容。您可能需要经历多次迭代,将 google 源代码添加到指令中,例如 script-src、style-src、connect-src、frame-src 和 img-src。

© www.soinside.com 2019 - 2024. All rights reserved.