flask-debugtoolbar icon indicating copy to clipboard operation
flask-debugtoolbar copied to clipboard

Use CSRF header on ajax calls, if available

Open luismanson opened this issue 3 years ago • 7 comments

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.

luismanson avatar Mar 21 '21 14:03 luismanson

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.

jeffwidman avatar Mar 26 '22 07:03 jeffwidman

@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.

caffeinatedMike avatar Aug 18 '22 18:08 caffeinatedMike

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

rimvislt avatar Dec 05 '22 12:12 rimvislt

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

caffeinatedMike avatar Dec 05 '22 13:12 caffeinatedMike

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.

macnewbold avatar Nov 16 '23 00:11 macnewbold

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')

taoky avatar Feb 06 '24 14:02 taoky

The same issue.

princerb avatar Jul 24 '24 13:07 princerb