我已经构建了一个谷歌云函数,它接受输入并将其发送到 Firestore。我需要在 Firefox WebExtension 中调用它,我将在 Twitter 上使用它。我不是第一次做这种类型的项目,但这次我遇到了关于“内容安全策略”的问题。
我尝试使用 GC 测试工具调用我的 GC 函数,然后使用 Postman,然后在 Python 脚本中,最后在新打开的选项卡的控制台中。所有这些都奏效了! 但它在我的 WebExtension 中不起作用。因此,我尝试在 twitter 上调用控制台(Firefox)中的函数。它也不起作用。我收到以下错误消息:
error TypeError: NetworkError when attempting to fetch resource. <anonymous> debugger eval code:9
接下来是:
Content Security Policy: The page’s settings blocked the loading of a resource at
https://...cloudfunctions.net/...?...=... (“connect-src”).
在 Chrome 上尝试时,我收到此错误消息:
error TypeError: Failed to fetch
at <anonymous>:9:1
Refused to connect to 'https://....cloudfunctions.net/...?...=...'
because it violates the following Content Security Policy directive: "connect-src 'self' blob:
我可以提供的另一个有趣的信息是,在访问 Twitter 页面时打开控制台(在 Firefox 中)时,我收到以下警告消息:
Content Security Policy: Ignoring “'unsafe-inline'” within script-src or style-src: nonce-source or hash-source specified
以及以下错误消息:
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“script-src”).
仅供参考:在 GC 函数内部,我已按以下方式设置标题:
response.headers.set("Access-Control-Allow-Origin", "*")
headers.set("Access-Control-Allow-Methods", "GET, POST")
我也尝试添加以下行,但不起作用:
response.headers.set("Content-Security-Policy", "default-src * self blob: data: gap:; style-src * self 'unsafe-inline' blob: data: gap:; script-src * 'self' 'unsafe-eval' 'unsafe-inline' blob: data: gap:; object-src * 'self' blob: data: gap:; img-src * self 'unsafe-inline' blob: data: gap:; connect-src self * 'unsafe-inline' blob: data: gap:; frame-src * self blob: data: gap:;")
你们能帮我吗?我真的被困住了。谢谢!!
答案 0 :(得分:2)
我可以提供的另一个有趣的信息是,当打开 控制台(在 Firefox 中)访问 Twitter 页面时,我得到 以下警告消息:
Content Security Policy: Ignoring “'unsafe-inline'” within script-src or style-src: nonce-source or hash-source specified
这不是 WebExtension 错误,而是 Twitter 的 CSP 警告,因为它支持 backward browsers compatibility mode。忽略它,你无法影响 Twitter 的 CSP。
<块引用>我也尝试添加以下行,但不起作用:
response.headers.set("Content-Security-Policy", "...")
WebExtension 无法以这种方式发布 CSP,需要使用 content_security_policy key in the app's manifest(manifest.json
文件)。在CSP in WebExtension查看详情。
扩展的默认内容安全策略是:script-src 'self'; object-src 'self';
这就是 https://...cloudfunctions.net/
源被阻止的原因。看起来您在 WexExtension 中也使用了某种内联脚本,因此您必须将 'hash-value'
添加到 script-src
键中的 content_security_policy
。
*
不能在 'unsafe-inline'
键中使用 content_security_policy
令牌。
请注意,带有 'unsafe-eval'
、'unsafe-inline'
、远程脚本、blob
的扩展,或在其 CSP are not allowed 中的远程源,用于 addons.mozilla.org 上列出的扩展主要安全问题。