Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does CSRF stand for and what problem does it solve?
CSRF stands for Cross-Site Request Forgery. It stops attackers from tricking users into making unwanted actions on websites where they are logged in.
Click to reveal answer
beginner
How does a CSRF token help protect a web app?
A CSRF token is a secret value sent with forms or requests. The server checks this token to make sure the request is from the real user, not a fake source.
Click to reveal answer
intermediate
Which Express middleware is commonly used for CSRF protection?
The 'csurf' middleware is commonly used in Express apps to add CSRF protection by generating and validating tokens.
Click to reveal answer
intermediate
What must you do to use 'csurf' middleware correctly in Express?
You must use cookie-parser or session middleware first, then add 'csurf'. Also, include the CSRF token in your forms or AJAX requests for validation.
Click to reveal answer
intermediate
Why should CSRF tokens be unique per user session?
Unique tokens prevent attackers from guessing or reusing tokens. This ensures only the real user’s requests are accepted.
Click to reveal answer
What is the main purpose of CSRF protection in Express apps?
APrevent unauthorized commands from trusted users
BEncrypt user passwords
CImprove page load speed
DValidate email addresses
✗ Incorrect
CSRF protection stops attackers from making unwanted actions on behalf of logged-in users.
Which middleware must be used before 'csurf' in Express for it to work properly?
Abody-parser only
Bcookie-parser or session middleware
Chelmet
Dcors
✗ Incorrect
'csurf' requires cookie-parser or session middleware to read tokens from cookies or sessions.
How is the CSRF token usually sent back to the server in a form submission?
AAs a hidden form field
BIn the URL query string
CIn the HTTP headers only
DAs a cookie only
✗ Incorrect
CSRF tokens are commonly included as hidden fields inside forms to be submitted with user data.
What happens if a request does not have a valid CSRF token when using 'csurf'?
AThe request is accepted anyway
BThe token is automatically generated
CThe server logs the request but processes it
DThe request is rejected with an error
✗ Incorrect
'csurf' middleware rejects requests missing or having invalid tokens to protect against CSRF attacks.
Why is CSRF protection important even if you use HTTPS?
ABecause HTTPS blocks all cookies
BBecause HTTPS slows down the site
CBecause HTTPS only encrypts data, it does not stop forged requests
DBecause HTTPS disables JavaScript
✗ Incorrect
HTTPS encrypts data but does not prevent attackers from tricking users into sending forged requests.
Explain how CSRF tokens work in an Express app to protect user actions.
Think about how the server knows a request is genuine.
You got /4 concepts.
Describe the steps to add CSRF protection using 'csurf' middleware in an Express application.
Consider middleware order and token usage.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of CSRF protection in an Express app?
easy
A. To prevent unauthorized commands from being sent from other websites
B. To speed up the server response time
C. To encrypt user passwords
D. To log user activity on the server
Solution
Step 1: Understand CSRF meaning
CSRF stands for Cross-Site Request Forgery, which tricks users into submitting unwanted actions.
Step 2: Identify CSRF protection goal
Protection stops other sites from sending commands on behalf of a user without permission.
Final Answer:
To prevent unauthorized commands from being sent from other websites -> Option A
B. The server responds with 'Form submitted' anyway
C. The server throws a 403 Forbidden error
D. The server crashes with an uncaught exception
Solution
Step 1: Understand csurf error handling
If the CSRF token is missing or invalid, csurf middleware triggers an error with status 403 Forbidden.
Step 2: Check route behavior
The route handler is not called; instead, Express sends a 403 error response.
Final Answer:
The server throws a 403 Forbidden error -> Option C
Quick Check:
Invalid CSRF token = 403 Forbidden error [OK]
Hint: Missing token causes 403 error, not success [OK]
Common Mistakes:
Assuming form submits anyway
Thinking server redirects automatically
Believing server crashes
4. You added csurf middleware but your form keeps failing CSRF validation. Which of these is the most likely cause?
medium
A. You did not install the cookie-parser package
B. You used app.use(express.json()) before csurf()
C. You set cookie: false in csurf options
D. You forgot to include the CSRF token in the form as a hidden input
Solution
Step 1: Check form token inclusion
CSRF protection requires the token to be sent with the form, usually as a hidden input field.
Step 2: Evaluate other options
While cookie-parser is needed if using cookies, the most common cause is missing token in the form.
Final Answer:
You forgot to include the CSRF token in the form as a hidden input -> Option D
Quick Check:
Missing token in form = validation fails [OK]
Hint: Always add CSRF token hidden input in forms [OK]
Common Mistakes:
Ignoring token in form fields
Misordering middleware without reason
Assuming cookie-parser always required
5. You want to protect an Express app using csurf with cookie-based tokens and render the token in a form. Which code snippet correctly sets up the middleware and passes the token to the template?