小编ref*_*ome的帖子

Flask CSP(Content-Security-Policy)针对跨站脚本等攻击的最佳实践

我是 CSP 新手。我试图通过设置 resp_header 在我的 Flask 应用程序的所有模板中实现 CSP,以保护我的网站免受跨站点脚本攻击。我修改了我的渲染模板:

return render_template('addvideos.html'  form=form, legend = 'Update video : '+ videos.video_name)
Run Code Online (Sandbox Code Playgroud)

    resp = make_response(render_template('addvideos.html', legend = 'Update video : '+ videos.video_name)
)
    resp.headers['Content-Security-Policy'] = "default-src 'self';"
    return resp
Run Code Online (Sandbox Code Playgroud)

我的应用程序中有超过 50 个“render_template”,我必须为每个添加一个响应标头。经过研究,我发现 after_request 可以实现如下所示的效果:

@app.after_request
def add_security_headers(resp):
    resp.headers['Content-Security-Policy']='default-src \'self\''
    return resp
Run Code Online (Sandbox Code Playgroud)

这很可靠吗?如果我直接在 HTML 模板 ( jinja2 ) 中应用 CSP,可能吗?哪个更可靠?

jinja2 flask content-security-policy

8
推荐指数
1
解决办法
9139
查看次数

if 语句确定在 jinja 模板中使用哪个路由,flask

我有两个使用一个模板的视图。

@users.route("/one")
@login_required
def one(username):
     return render_template('follower.html') 
Run Code Online (Sandbox Code Playgroud)

@users.route("/two")
@login_required
def two(username):
     return render_template('follower.html') 
Run Code Online (Sandbox Code Playgroud)

在 jinja 模板( follower.html )中,如果像这样使用路由一,我正在尝试执行一个程序:

{% if url_for('users.one')  %}
    execute program
{% endif %}
Run Code Online (Sandbox Code Playgroud)

但似乎我做错了。请问确定使用哪条路线的正确方法是什么?

templates jinja2 flask

2
推荐指数
1
解决办法
278
查看次数