首页 > 解决方案 > Google AppEngine 上的内容安全政策问题

问题描述

我无法让我的应用程序在 Google AppEngine 上正常运行。我有一个 Flask 应用程序,其内容安全策略 (CSP) 由flask-talisman管理

我正在阻止对http://my-project.appspot.com/api/foo的 API 调用,因为它们违反了“connect-src”政策。对该 URL 的请求属于xhr. 实际消息如下:

Refused to connect to 'http://review-dot-my-project.appspot.com/api/foo' because it violates the following Content Security Policy directive: "connect-src 'self' *.appspot.com".

在生产中,我的 URL 是https://my-project.appspot.com

对于评论应用,我的网址是http://review-dot-my-project.appspot.com

http 协议在我的评论应用程序中使用,在生产中它是 https。

我的 CSP 的相关部分如下:

...
Talisman(app, content_security_policy={
        'default-src': ["'self'", "*.google.com"],
        ...
        'connect-src': ["'self'", "*.appspot.com"]
    })

connect-src我已经尝试了我的 CSP 部分的多次迭代

'connect-src': ["'self'", "*://*.appspot.com"]

'connect-src': ["'self'", "*://*my-project.appspot.com"]

'connect-src': ["'self'", "*.appspot.com"]

'connect-src': ["'self'", "*.appspot.com*"]

我似乎找不到关于通配符如何工作的明确文档。由于 Flask Talisman 来自 Google Cloud Platform 组织,我希望能够找到更多示例

标签: pythongoogle-app-engineflaskgoogle-cloud-platform

解决方案


我建议试试这个:

'connect-src': ["'self'", 
                "http://review-dot-my-project.appspot.com", 
                "https://my-project.appspot.com"]

看起来您不需要通配符,并且您的 CSP 越具体越好。

你也可以试试这个:

'connect-src': ["'self'", 
                "http://*.appspot.com", 
                "https://my-project.appspot.com"]

如果您有其他非生产版本的应用。


推荐阅读