Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CSRF header on ajax calls, if available #156

Open
luismanson opened this issue Mar 21, 2021 · 7 comments
Open

Use CSRF header on ajax calls, if available #156

luismanson opened this issue Mar 21, 2021 · 7 comments

Comments

@luismanson
Copy link

Hello, I just discovered template editor, but was unable to preview and save template edits.

After further inspection, it seems to be that in template_editor.html' all ajax calls don't use the CSRF header provided by Flask-WTF.

The code suggested in their site regarding javascript requests has no effect.

$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
            xhr.setRequestHeader("X-CSRFToken", csrf_token);
        }
    }
});

I hope it can be fixed, thanks for this great tool.

@jeffwidman
Copy link
Member

Happy to merge a PR if you or anyone else wants to dig into it.

My current work is unrelated to Flask, so won't have time to look into it myself.

@caffeinatedMike
Copy link
Contributor

@jeffwideman @luismanson I also face this issue with the SQLAlchemy panel when clicking SELECT and EXPLAIN SELECT next to the queries.

I will look into correcting this and opening a PR in the next day or so.

@rimvislt
Copy link

rimvislt commented Dec 5, 2022

Any update on this? Still facing this issue on master branch, so I guess it was never merged/created ?

@caffeinatedMike
Copy link
Contributor

@rimvislt I haven't had the time to invest in contributing this change.

@macnewbold
Copy link
Contributor

I have run into this issue as well I think, but didn't dive into debugging it yet. I'm in favor of getting a solution in place when someone has time to continue down this path.

@taoky
Copy link

taoky commented Feb 6, 2024

A workaround in flask app init script:

# ...
app.csrf = CSRFProtect(app)
# replace the view name by the view you need -- this could be printed in flask_wtf/csrf.py `csrf_protect()` func.
app.csrf.exempt('flask_debugtoolbar.panels.sqlalchemy.sql_select')

@princerb

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

7 participants