pallets-eco / flask-debugtoolbar

A toolbar overlay for debugging Flask applications
https://flask-debugtoolbar.readthedocs.io
BSD 3-Clause "New" or "Revised" License
948 stars 144 forks source link

Use CSRF header on ajax calls, if available #156

Open luismanson opened 3 years ago

luismanson commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

@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 commented 1 year ago

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

caffeinatedMike commented 1 year ago

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

macnewbold commented 11 months ago

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 commented 8 months ago

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 commented 3 months ago

The same issue.