0
0
Flaskframework~10 mins

CSRF protection in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - CSRF protection
User loads form page
Server generates CSRF token
Token embedded in form as hidden field
User submits form with token
Server checks token validity
Process
form data
This flow shows how a CSRF token is created, sent with the form, and checked on submission to protect against unauthorized requests.
Execution Sample
Flask
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from flask_wtf.csrf import CSRFProtect
from flask import Flask

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'

csrf = CSRFProtect(app)

class MyForm(FlaskForm):
    name = StringField('Name')
    submit = SubmitField('Submit')
This code sets up CSRF protection in Flask and creates a form that automatically includes a CSRF token.
Execution Table
StepActionCSRF Token GeneratedToken Sent in FormToken ReceivedToken Valid?Result
1User requests form pageToken123Token123 embedded in formN/AN/AForm displayed with token
2User fills form and submitsToken123Token123 sent with form dataToken123YesForm data processed
3User submits form without tokenToken123No token sentNoneNoRequest rejected
4User submits form with wrong tokenToken123WrongToken456 sentWrongToken456NoRequest rejected
💡 Execution stops when form submission is accepted or rejected based on token validity.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
csrf_tokenNoneToken123Token123Token123Token123
form_token_receivedNoneNoneToken123NoneWrongToken456
token_validFalseN/ATrueFalseFalse
Key Moments - 2 Insights
Why does the server reject the form submission if the CSRF token is missing or wrong?
Because the server compares the received token with the one it generated (see execution_table rows 3 and 4). If they don't match, it means the request might be forged, so it rejects it to protect the user.
How does the CSRF token get into the form the user submits?
The token is generated by the server when the form page loads and embedded as a hidden field inside the form (see execution_table step 1). This way, when the user submits, the token is sent back automatically.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the token_valid value at Step 2?
ATrue
BFalse
CNone
DN/A
💡 Hint
Check the 'Token Valid?' column at Step 2 in the execution_table.
At which step does the server reject the request due to missing token?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the row where 'Token Received' is 'None' and 'Result' is 'Request rejected'.
If the token sent with the form was changed to 'Token123' at Step 4, what would happen?
ARequest rejected
BForm data processed
CForm redisplayed
DServer error
💡 Hint
Compare Step 2 and Step 4 token values and results in the execution_table.
Concept Snapshot
CSRF protection in Flask:
- Server generates a unique token per user session.
- Token is embedded in forms as hidden fields.
- On form submit, server checks token matches.
- If valid, process form; if not, reject request.
- Use Flask-WTF's CSRFProtect for easy setup.
Full Transcript
CSRF protection works by creating a secret token when the user loads a form. This token is hidden inside the form and sent back when the user submits. The server checks if the token matches what it created. If it matches, the form data is accepted. If not, the request is rejected to prevent unauthorized actions. Flask-WTF helps by automatically generating and checking these tokens in forms.