CSRF stands for Cross-Site Request Forgery. It is a type of attack where a bad website tricks a user's browser into making unwanted actions on a trusted site where the user is logged in.
Click to reveal answer
beginner
How does Flask help protect against CSRF attacks?
Flask uses a CSRF token, a secret value sent with forms. The server checks this token on form submission to make sure the request is from the real user, not a fake site.
Click to reveal answer
beginner
What is a CSRF token and why is it important?
A CSRF token is a unique, secret value generated by the server and included in forms. It helps confirm that the form submission is genuine and not forged by another site.
Click to reveal answer
intermediate
Which Flask extension is commonly used to add CSRF protection?
Flask-WTF is a popular extension that adds CSRF protection automatically by generating and validating CSRF tokens in forms.
Click to reveal answer
beginner
Why is it important to include CSRF protection in web forms?
Without CSRF protection, attackers can trick users into submitting forms unknowingly, causing unwanted actions like changing passwords or making purchases.
Click to reveal answer
What does a CSRF token do in a Flask app?
AVerifies that form submissions come from the real user
BEncrypts user passwords
CStores user session data
DPrevents SQL injection attacks
✗ Incorrect
A CSRF token confirms that the form submission is genuine and not forged by another site.
Which Flask extension helps with CSRF protection?
AFlask-Login
BFlask-SQLAlchemy
CFlask-WTF
DFlask-Migrate
✗ Incorrect
Flask-WTF automatically adds CSRF tokens to forms and validates them.
CSRF attacks happen because:
AForms use GET instead of POST
BUsers forget their passwords
CServers do not encrypt data
DBrowsers trust requests from logged-in users without extra checks
✗ Incorrect
Browsers send cookies automatically, so attackers can trick browsers into sending unwanted requests if no CSRF protection exists.
What must a server check to prevent CSRF?
AUser's IP address
BPresence and validity of CSRF token
CUser's browser type
DForm field lengths
✗ Incorrect
The server checks the CSRF token to confirm the request is legitimate.
Which HTTP method is most commonly protected by CSRF tokens?
APOST
BGET
CHEAD
DOPTIONS
✗ Incorrect
POST requests change data and are protected by CSRF tokens to prevent forged submissions.
Explain in simple terms what CSRF protection is and why it matters in Flask web apps.
Think about how a bad website might trick a logged-in user to do something unwanted.
You got /3 concepts.
Describe how to add CSRF protection to a Flask form using Flask-WTF.
Focus on the steps to set up and use the extension.
You got /4 concepts.
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?