使用护身符从单路进入Flask

2024-05-12 16:52:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我用的是带瓶护身符的烧瓶。我的CSP当前配置为,所有路由为:

SELF = '\'self\''
csp = {
    'default-src': [SELF, '*.gstatic.com'],
    'connect-src': [SELF, 'https://fonts.googleapis.com', 'https://cdnjs.cloudflare.com'],
    'frame-src': [SELF, 'https://js.stripe.com'],
    'script-src': [SELF, 'https://cdnjs.cloudflare.com', 'https://js.stripe.com', 'https://www.googletagmanager.com'],
    'style-src': [SELF, 'https://cdnjs.cloudflare.com', 'https://fonts.googleapis.com', '\'unsafe-inline\''],
    'img-src': [SELF, '*', 'blob:', 'data:']
}
talisman.init_app(app, content_security_policy=csp, content_security_policy_nonce_in=['script-src'])

每当外部站点试图通过iframe加载我的页面时,它们会收到错误X-Frame-Options is SAMEORIGIN,这通常是正常的。在

但是,我希望加载外部iframe可以访问单个路由。为了实现这一点,我遵循以下建议:

^{pr2}$

在我的具体路线之前。在

但是Chrome不允许这样做,并报告错误。我认为应该设置CSP。我应该如何重新编写或重新配置路由以允许外部iframe在所有浏览器中访问它?在


Tags: httpsselfsrccom路由jsfontsscript
2条回答

解决方案是直接在路由上增加一个frame-ancestors头,而不是在某些浏览器(inc chrome)中优先使用。在

# assume a csp dict exists
@talisman(frame_options=ALLOW_FROM,
          frame_options_allow_from='*',
          content_security_policy={**csp, 'frame-ancestors': ['*']})
def flask_route():
    # individualised route

烧瓶护身符路线示例:

# Example of a route-specific talisman configuration

@app.route('/embeddable')
@talisman(

    frame_options='ALLOW-FROM',
    frame_options_allow_from='https://example.com/',

)

def embeddable():
    return "<html>I can be embedded.</html>"

https://github.com/GoogleCloudPlatform/flask-talisman/blob/master/example_app/main.py

相关问题 更多 >