📖 Scenario: You are building a simple Flask web app that has a form where users can submit their favorite color. To keep the app safe from CSRF attacks, you will add CSRF protection step-by-step.
🎯 Goal: Build a Flask app with a form that uses CSRF protection to prevent unauthorized form submissions.
📋 What You'll Learn
Create a Flask app with a route for the form
Add a secret key configuration for CSRF
Use Flask-WTF to add CSRF protection to the form
Render the form with CSRF token included
💡 Why This Matters
🌍 Real World
CSRF protection is essential in web apps to stop attackers from tricking users into submitting unwanted requests.
💼 Career
Understanding CSRF and how to protect against it is a key skill for web developers working with Flask or any web framework.
Progress0 / 4 steps
1
Create a basic Flask app with a form route
Create a Flask app by importing Flask and instantiate it as app. Then create a route /color that returns a simple HTML form with a POST method and an input named color.
Flask
Hint
Start by importing Flask and creating the app instance. Then define a route '/color' that accepts GET and POST methods. Return a simple HTML form with an input named 'color'.
2
Add a secret key configuration for CSRF
Add a secret key to the Flask app by setting app.config['SECRET_KEY'] to the string 'mysecretkey'. This key is needed for CSRF protection.
Flask
Hint
Set the secret key on the app config to enable secure sessions and CSRF protection.
3
Use Flask-WTF to add CSRF protection to the form
Import FlaskForm and StringField from flask_wtf and wtforms. Create a form class called ColorForm that inherits from FlaskForm and has a color field of type StringField. Then import CSRFProtect from flask_wtf and initialize it with the Flask app.
Flask
Hint
Import FlaskForm and CSRFProtect. Create a ColorForm class with a color field. Initialize CSRFProtect with the app.
4
Render the form with CSRF token included
Modify the color route to create an instance of ColorForm. Render the form in the response using form.csrf_token inside the form HTML. Use form.color.label and form.color() to render the label and input. Return the form HTML as a string.
Flask
Hint
Create a ColorForm instance. Use f-string to include form.csrf_token, form.color.label, and form.color() inside the form HTML.
Practice
(1/5)
1. What is the main purpose of CSRF protection in a Flask web application?
easy
A. To speed up the loading time of web pages
B. To encrypt user passwords in the database
C. To prevent attackers from making users perform unwanted actions without their consent
D. To improve the visual design of forms
Solution
Step 1: Understand what CSRF stands for
CSRF means Cross-Site Request Forgery, which tricks users into submitting unwanted requests.
Step 2: Identify the purpose of CSRF protection
CSRF protection stops attackers from forcing users to perform actions they did not intend.
Final Answer:
To prevent attackers from making users perform unwanted actions without their consent -> Option C
Quick Check:
CSRF protection = prevent unwanted user actions [OK]
Hint: CSRF stops fake actions from attackers using your login [OK]
Common Mistakes:
Confusing CSRF with password encryption
Thinking CSRF improves page speed
Believing CSRF changes form appearance
2. Which of the following is the correct way to enable CSRF protection in a Flask app using Flask-WTF?
easy
A. from flask_wtf import CSRFProtect
app = Flask(__name__)
csrf = CSRFProtect(app)
csrf.init_app(app)
B. from flask_wtf import CSRFProtect
csrf = CSRFProtect()
app = Flask(__name__)
C. app = Flask(__name__)
app.csrf = True
D. from flask_wtf import CSRFProtect
csrf = CSRFProtect(app)
Solution
Step 1: Recall Flask-WTF CSRF setup
You import CSRFProtect and pass the Flask app instance to it to enable protection.
Step 2: Check the options for correct syntax
from flask_wtf import CSRFProtect
csrf = CSRFProtect(app) correctly imports CSRFProtect and initializes it with the app in one step.
Final Answer:
from flask_wtf import CSRFProtect
csrf = CSRFProtect(app) -> Option D
Quick Check:
CSRFProtect(app) initializes protection [OK]
Hint: Initialize CSRFProtect with app instance directly [OK]
Common Mistakes:
Calling CSRFProtect without passing app
Setting app.csrf = True (not valid)
Calling init_app after passing app (redundant)
3. Given this Flask route with CSRF protection enabled, what happens if the form is submitted without a CSRF token?