python auth_error ai_generated true

BadRequest: The CSRF token is missing.

ID: python/flask-csrf-token-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-03-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Flask-WTF or similar CSRF protection requires a token in the form, but it is not included in the request.

generic

中文

Flask-WTF或类似的CSRF保护要求表单中包含令牌,但请求中未包含。

Workarounds

  1. 95% success
    Include the CSRF token in the form: {{ form.csrf_token }} or <input type='hidden' name='csrf_token' value='{{ csrf_token() }}'>
  2. 90% success
    For AJAX requests, set the X-CSRFToken header using the token from the cookie.

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Disabling CSRF protection globally exposes the app to CSRF attacks.

  2. 70% fail

    Adding a hidden field manually without the correct token value does not work.