简体   繁体   English

Flask-Talisman 打破了 flask-restplus 的 swagger 文档

[英]Flask-Talisman breaks flask-restplus' swagger documentation

I recently installed flask-talisman and after defining the default Content Security Policy, I realized my Swagger documentation page did not load.我最近安装了烧瓶护身符,在定义了默认的内容安全策略后,我意识到我的 Swagger 文档页面没有加载。

The Swagger documentation page is automatically generated by flask-restplus and it simply stopped loading. Swagger 文档页面由 flask-restplus 自动生成,它只是停止加载。

The Content Security Policy (CSP) I defined was like this:我定义的内容安全策略(CSP)是这样的:

csp = {
    'default-src': '\'self\''
}
talisman = Talisman(app, content_security_policy=csp)

Could this be simply solved by adding swagger to the trusted domains in the CSP, like so?这可以通过将 swagger 添加到 CSP 中的受信任域来简单地解决,就像这样?

csp = {
    'default-src': ["'self'", "*.swagger.com"]
}
talisman = Talisman(app, content_security_policy=csp)

Or do I need to define other parameters?还是我需要定义其他参数?

No, it's because swagger-ui uses inline scripts and styles -- here's the open issue tracker for swagger-ui and here's the open issue for flask-restplus .不,这是因为 swagger-ui 使用内联脚本styles -这是 swagger-ui 的未解决问题跟踪器,这是flask-restplus的未解决问题。

flask-talisman allows control on a 'per-view' basis , so you could add the 'unsafe-inline' keyword to your CSP for that endpoint. flask-talisman 允许在“per-view”基础上进行控制,因此您可以将“unsafe-inline”关键字添加到该端点的 CSP。 If that's not possible with flask-restplus, you could also modify your CSP using before_request for whatever the swagger route prefix is, as outlinedhere .如果使用 flask-restplus 无法做到这一点,您还可以使用 before_request 修改您的 CSP,无论 swagger 路由前缀是什么,如此所述。

Edit:编辑:

A workaround is to use a different CSP for the Swagger view that allows inline scripts and styles:一种解决方法是对允许内联脚本和 styles 的 Swagger 视图使用不同的 CSP:

# Swagger CSP needs to have 'unsafe-inline' in the script-src and style-src fields
SWAGGER_CSP = {
    "script-src": ["'self'", "'unsafe-inline'"],
    "style-src": ["'self'", "'unsafe-inline'"]
}

# update the CSP for the Swagger view function
app.view_functions["swagger_ui.show"].talisman_view_options = {
    "content_security_policy": SWAGGER_CSP
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM