I have a very simple app that has no user management or any Flask-Login auth needs. It has forms, WTForms. All I want to do is collect some data submitted by the form. I could technically disable CSRF validation but Flask WTForms really urges me not to.
I'd like to disable flask session cookie in the browser because it seems unnecessary and I would need to put a cookie banner for GDPR compliance. So to avoid all that, I thought of disabling flask session cookie as follows:
class CustomSessionInterface(SecureCookieSessionInterface):
""" Disable session cookies """
def should_set_cookie(self, app: "Flask", session: SessionMixin) -> bool:
return False
# App initialization
app = Flask(__name__)
app.session_interface = CustomSessionInterface()
But doing so leads to a 500
error: "The CSRF session token is missing". However, looking at the HTML that was rendered has the following csrf token rendered properly:
<input id="csrf_token" name="csrf_token" type="hidden" value="ImI2ZDIwMDUxMDNmOGM3ZDFlMTI4ZTIzODE4ODBmNDUwNWU3ZmMzM2Ui.YhA2kQ.UnIHwlR1qLL61N9_30lDKngxLlM">
Questions:
- What is the relationship between CSRF token validation and session cookie? Why is a cookie necessary to validated the CSRF token?
- I tried enabling session cookies again, deleting the cookie in Chrome developer tools leads to the same error. So, indeed, session cookie seems to be absolutely necessary to validate CSRF token.
- How can I use CSRF form validation without a session cookie?
Thank you so much.
CSRF token
from database and compare withCSRF token
from your form. OR it may use session cookie with some secret value to generateCSRF token
and compare it withCSRF token
from your form.?sessionID=...
if they couldn't use cookies. But this way everyone can see these informations and Google's scraping bots can get them and keep in database and someone could get them usingGoogle Search
and reuse if they are valid long time.session['csrf_token']
in theself.protect()
method in the CSRF class.self.protect()
is called before every request: github.com/wtforms/flask-wtf/blob/… So I guess that answers it.