I'm currently using Flask-WTF v0.13.1, i have a few forms on my website, all created including the CSRF token.
For some reasons i have to set a different expiration on each form, so far i could set manually the time_limit
value upon creating the csrf token.
I would like to update to the v0.14, according to the changelog time_limit
is gone and there is no reference in the docs on how to change it anymore.
Looking in the source code i saw that the form has a csrf_time_limit
meta parameter.
I tried to set that parameter on my form:
from flask_wtf import FlaskFormclass myForm(FlaskForm):class Meta:csrf_time_limit = 7200content = TextAreaField('content')
Although debugging the csrf.py
module i see that the validate_csrf_token
of _FlaskFormCSRF
is actually never called.
The method validate_csrf
is called within the method protect()
instead, in this case the meta parameter is never considered.
I don't understand if this is a bug of the package or if i'm missing something.
UPDATE:
Example code:
app.py
from flask import Flask, render_template, request
from flask_wtf.csrf import CSRFProtect
from flask_wtf import FlaskForm
from wtforms import IntegerFieldcsrf = CSRFProtect()
app = Flask(__name__)
app.config.update(dict(SECRET_KEY="super secret key"
))csrf.init_app(app)class MyForm(FlaskForm):class Meta:csrf_time_limit = 1id = IntegerField('id')@app.route("/", methods=['GET', 'POST'])
def test_form_csrf():if request.method == 'POST':myForm = MyForm(request.form)print(myForm.id.data)return render_template('test_form.html', myForm= MyForm())
templates/test_form.html
<form method="post" action="/"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />{{ myForm.id }}<input type="submit" value="test" />
</form>
requirements.txt
click==6.7
Flask==0.12.2
Flask-WTF==0.14.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
Werkzeug==0.14.1
WTForms==2.1
project structure
app.py
templates
|
--- test_form.html
To run the code,
FLASK_APP=app.py flask run
I also put a debug breakpoint on this line to check the actual value of time_limit
, the value is always 3600
.