【问题标题】:Content-Security-Policy problems Google Cloud FunctionContent-Security-Policy 问题 Google Cloud Function
【发布时间】:2021-04-20 09:52:17
【问题描述】:

我已经构建了一个谷歌云函数,它接受输入并将其发送到 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:;")

你们能帮我吗?我真的被困住了。谢谢!!

【问题讨论】:

    标签: javascript python google-cloud-platform google-cloud-functions content-security-policy


    【解决方案1】:

    我可以提供的另一个有趣的信息是,当打开 控制台(在 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 manifestmanifest.json文件)。详情见CSP in WebExtension
    扩展的默认内容安全策略是:script-src 'self'; object-src 'self'; 这就是 https://...cloudfunctions.net/ 源被阻止的原因。看起来您在 WexExtension 中使用了某种内联脚本,因此您必须在 content_security_policy 键中将 'hash-value' 添加到 script-src
    * 'unsafe-inline' 令牌不能使用在content_security_policy 键中。

    请注意,由于重大安全问题,对于 addons.mozilla.org 上列出的扩展,在其 CSP are not allowed 中带有 'unsafe-eval''unsafe-inline'、远程脚本、blob 或远程源的扩展。

    【讨论】:

    • 感谢@granty,我已经清楚了。因此,当我在复制/粘贴到控制台时遇到此错误:“内容安全策略:页面的设置阻止在us-central1-twitter-fake-accounts.cloudfunctions.net/…(“connect-src”)加载资源。”,我试图添加这个manifest.json 的参数“content_security_policy”:“script-src 'self' 'hash-value' us-central1-twitter-fake-accounts.cloudfunctions.net; object-src 'self'”。但我仍然遇到同样的网络错误。我做错了什么?
    • "content_security_policy": "script-src 'self' 'hash-value'; object-src 'self'; connect-src us-central1-twitter-fake-accounts.cloudfunctions.net" 因为主机源在connect-src 指令中被阻止。
    • 好的,这意味着我不能用任何参数覆盖它?
    • 我不明白你想用参数覆盖什么?为什么您的源策略 "content_security_policy": "script-src 'self' 'hash-value' us-central1-twitter-fake-accounts.cloudfunctions.net; object-src 'self';" 可以被覆盖,但 "content_security_policy": "script-src 'self' 'hash-value'; object-src 'self'; connect-src us-central1-twitter-fake-accounts.cloudfunctions.net" - 不能?只添加了一个指令,情况发生了变化?
    • 我的意思是,即使使用"content_security_policy": "script-src 'self' 'hash-value' us-central1-twitter-fake-accounts.cloudfunctions.net; object-src 'self';",我仍然会得到这个error TypeError: NetworkError when attempting to fetch resource.
    猜你喜欢
    • 2013-01-13
    • 2021-04-13
    • 2020-12-09
    • 2017-04-19
    • 2021-07-09
    • 2017-05-25
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多