Bird
0
0

Given this Flask form code snippet, what happens if the CSRF token is missing or invalid when submitting the form?

medium📝 component behavior Q13 of 15
Flask - Security Best Practices
Given this Flask form code snippet, what happens if the CSRF token is missing or invalid when submitting the form?
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField

class MyForm(FlaskForm):
    name = StringField('Name')
    submit = SubmitField('Send')
AThe form submission is accepted without any error
BThe form automatically regenerates a new CSRF token and retries
CA CSRFError is raised and the request is rejected
DThe form data is saved but a warning is logged
Step-by-Step Solution
Solution:
  1. Step 1: Understand FlaskForm CSRF behavior

    FlaskForm automatically checks for a valid CSRF token on submission.
  2. Step 2: Effect of missing/invalid token

    If the token is missing or wrong, Flask-WTF raises a CSRFError and rejects the request.
  3. Final Answer:

    A CSRFError is raised and the request is rejected -> Option C
  4. Quick Check:

    Invalid CSRF token = CSRFError raised [OK]
Quick Trick: Missing CSRF token causes error and blocks form [OK]
Common Mistakes:
MISTAKES
  • Assuming form submits anyway without token
  • Thinking token regenerates automatically on failure
  • Believing data saves with just a warning

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes