使用 Dash 的 Flask 框架中的 CSRF 保护
CSRF Protection in a Flask Framework that uses Dash
这个问题建立在我 关于破折号集成的基础上。
问题:
当使用 flask_wtf
模块激活 CSRF 时,您如何集成 Dash 模块而不因缺少 csrf 令牌而阻止 Dash 帖子?
MWE:
from flask import Flask, request, render template
from flask_wtf.csrf import CSRFProtect
from dash import Dash
from dash.dependencies import Input, Output
app = Flask(__name__)
csrf = CSRFProtect(app)
app.config['SECRET_KEY'] = 'somethignrandom'
dapp = Dash(__name__, server=app, routes_pathname_prefix='/dash/')
dapp.layout = layoutfunction # this is left for your imagination
@app.route('/', methods=['GET','POST'])
def helloworld():
return render_template('index.html')
@app.route('/dash')
def dashing():
dapp.layout = layoutfunction
if __name__ == '__main__':
app.run(debug=True)
每当加载 /dash
时,这 returns 就会出现 404 错误。
发件人:https://github.com/plotly/dash/issues/308
解决方案
添加以下行以从 csrf 令牌要求中豁免破折号:
from flask import Flask, request, render template
from flask_wtf.csrf import CSRFProtect
from dash import Dash
from dash.dependencies import Input, Output
app = Flask(__name__)
csrf = CSRFProtect(app)
app.config['SECRET_KEY'] = 'somethignrandom'
########## ADD THIS LINE
csrf._exempt_views.add('dash.dash.dispatch')
##########
dapp = Dash(__name__, server=app, routes_pathname_prefix='/dash/')
dapp.layout = layoutfunction # this is left for your imagination
@app.route('/', methods=['GET','POST'])
def helloworld():
return render_template('index.html')
@app.route('/dash')
def dashing():
dapp.layout = layoutfunction
if __name__ == '__main__':
app.run(debug=True)
评论
- 这是否是一个可以接受的解决方案还有待商榷。我不确定这是否会打开 Dash 进行注入。
- 我不知道有一种方法可以将 csrf 令牌添加到 dash,但如果有,我会更新我的答案。
这个问题建立在我
问题:
当使用 flask_wtf
模块激活 CSRF 时,您如何集成 Dash 模块而不因缺少 csrf 令牌而阻止 Dash 帖子?
MWE:
from flask import Flask, request, render template
from flask_wtf.csrf import CSRFProtect
from dash import Dash
from dash.dependencies import Input, Output
app = Flask(__name__)
csrf = CSRFProtect(app)
app.config['SECRET_KEY'] = 'somethignrandom'
dapp = Dash(__name__, server=app, routes_pathname_prefix='/dash/')
dapp.layout = layoutfunction # this is left for your imagination
@app.route('/', methods=['GET','POST'])
def helloworld():
return render_template('index.html')
@app.route('/dash')
def dashing():
dapp.layout = layoutfunction
if __name__ == '__main__':
app.run(debug=True)
每当加载 /dash
时,这 returns 就会出现 404 错误。
发件人:https://github.com/plotly/dash/issues/308
解决方案
添加以下行以从 csrf 令牌要求中豁免破折号:
from flask import Flask, request, render template
from flask_wtf.csrf import CSRFProtect
from dash import Dash
from dash.dependencies import Input, Output
app = Flask(__name__)
csrf = CSRFProtect(app)
app.config['SECRET_KEY'] = 'somethignrandom'
########## ADD THIS LINE
csrf._exempt_views.add('dash.dash.dispatch')
##########
dapp = Dash(__name__, server=app, routes_pathname_prefix='/dash/')
dapp.layout = layoutfunction # this is left for your imagination
@app.route('/', methods=['GET','POST'])
def helloworld():
return render_template('index.html')
@app.route('/dash')
def dashing():
dapp.layout = layoutfunction
if __name__ == '__main__':
app.run(debug=True)
评论
- 这是否是一个可以接受的解决方案还有待商榷。我不确定这是否会打开 Dash 进行注入。
- 我不知道有一种方法可以将 csrf 令牌添加到 dash,但如果有,我会更新我的答案。