Form validation fails due missing CSRF

前端 未结 4 707
忘了有多久
忘了有多久 2020-11-29 07:11

A few days ago I have reset my local flask environment without having captured the dependencies via a pip freeze before I deleted it. Hence I had to re-install

相关标签:
4条回答
  • 2020-11-29 07:55

    The Flask-WTF CSRF infrastructure rejects a token if:

    • the token is missing. Not the case here, you can see the token in the form.

    • it is too old (default expiration is set to 3600 seconds, or an hour). Set the TIME_LIMIT attribute on forms to override this. Probably not the case here.

    • if no 'csrf_token' key is found in the current session. You can apparently see the session token, so that's out too.

    • If the HMAC signature doesn't match; the signature is based on the random value set in the session under the 'csrf_token' key, the server-side secret, and the expiry timestamp in the token.

    Having eliminated the first three possibilities, you need to verify why the 4th step fails. You can debug the validation in flask_wtf/csrf.py file, in the validate_csrf() function.

    For your setup, you need to verify that the session setup is correct (especially if you don't use the default session configuration), and that you are using the correct server-side secret. The form itself could have a SECRET_KEY attribute set but is not stable across requests, or the app WTF_CSRF_SECRET_KEY key has changed (the latter defaults to the app.secret_key value).

    The CSRF support was added in version 0.9.0, do check out the specific CSRF protection documentation if you upgraded. The standard Flask-WTF Form class includes the CSRF token as a hidden field, rendering the hidden fields is enough to include it:

    {{ form.hidden_tag() }}
    
    0 讨论(0)
  • For me, the problem was not coming from Flask-WTF being badly configured, or a missing token. It was coming from the environment variables.

    If your Flask server is not running on localhost then in order to get Flask to work properly, you need to set a SERVER_NAME environment variable. You’ve likely forgotten to modify the SERVER_NAME value somewhere.

    For example, you could have something like this in config/settings.py:

    SERVER_NAME = 'my-domain.com'
    

    For more information, check out this great resource

    0 讨论(0)
  • 2020-11-29 08:06

    At the time of creating the app:

    from flask_wtf.csrf import CsrfProtect
    
    csrf = CsrfProtect()
    
    app = Flask(__name__)   
    
    ...
    
    csrf.init_app(app)
    
    ...
    
    0 讨论(0)
  • 2020-11-29 08:07

    I finally found the problem after nearly a day working on it. :( Big thanks to Martijn though for his help.

    The actual problem lies in the way the latest flask_wtf.csrf is working. The makers have overhauled it completely.

    You have to replace all {{form.hidden_tag()}} in your templates with <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>.

    And you have now to enable CSRF protection explicitly by adding CsrfProtect(app).

    The documentation is now obviously reflecting that, but I didn't know this has changed and was chasing ghosts.

    Its a big problem with deprecated functionality without notifying the developer somehow. Anyone that upgrades now to the latest version, will be chasing ghosts like I did. But its also my fault not having taken a snapshot of my dependencies. Lesson learned the hard way.

    0 讨论(0)
提交回复
热议问题