Challenge - 5 Problems
CSRF Protection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β component_behavior
intermediate2:00remaining
How does CSRF token validation work in Express?
Consider an Express app using the csurf middleware. What happens when a POST request is sent without the CSRF token?
Express
const express = require('express'); const csurf = require('csurf'); const cookieParser = require('cookie-parser'); const app = express(); app.use(cookieParser()); app.use(csurf({ cookie: true })); app.post('/submit', (req, res) => { res.send('Form submitted'); });
Attempts:
2 left
π‘ Hint
Think about what the csurf middleware does when the token is missing or wrong.
β Incorrect
The csurf middleware checks for a valid CSRF token in the request. If the token is missing or invalid, it blocks the request by sending a 403 Forbidden error to prevent CSRF attacks.
π Syntax
intermediate1:30remaining
Which code snippet correctly sets up CSRF protection with cookies in Express?
Choose the correct code to enable CSRF protection using the csurf middleware with cookie support.
Attempts:
2 left
π‘ Hint
Look for the option that enables cookie-based CSRF tokens.
β Incorrect
To use cookie-based CSRF tokens, the csurf middleware must be configured with { cookie: true }. This tells csurf to store the token in a cookie instead of the session.
π§ Debug
advanced2:30remaining
Why does the CSRF token validation fail in this Express app?
Given the code below, why does the server always respond with a 403 error on POST requests?
Express
const express = require('express'); const csurf = require('csurf'); const cookieParser = require('cookie-parser'); const app = express(); app.use(cookieParser()); app.use(csurf({ cookie: true })); app.get('/form', (req, res) => { res.send(`<form method='POST' action='/submit'> <input type='hidden' name='_csrf' value='${req.csrfToken()}' /> <button type='submit'>Submit</button> </form>`); }); app.post('/submit', (req, res) => { res.send('Success'); });
Attempts:
2 left
π‘ Hint
Check the order of middleware and what csurf needs to read the token from the body.
β Incorrect
The csurf middleware requires the request body to be parsed before it can find the CSRF token. Without express.urlencoded middleware before csurf, the token in the form body is not accessible, causing validation to fail.
π§ Conceptual
advanced1:30remaining
What is the main purpose of CSRF protection in web frameworks like Express?
Why do we add CSRF protection middleware in web applications?
Attempts:
2 left
π‘ Hint
Think about what CSRF attacks do to users and servers.
β Incorrect
CSRF protection stops attackers from making users unknowingly send harmful requests, like changing passwords or making purchases, by requiring a secret token that only the userβs browser knows.
β state_output
expert2:00remaining
What is the value of the CSRF token after this Express route runs?
Given the code below, what will be the output of the console.log statement?
Express
const express = require('express'); const csurf = require('csurf'); const cookieParser = require('cookie-parser'); const app = express(); app.use(cookieParser()); app.use(csurf({ cookie: true })); app.get('/token', (req, res) => { const token = req.csrfToken(); console.log(token); res.send('Token sent'); });
Attempts:
2 left
π‘ Hint
What does req.csrfToken() return when csurf middleware is used correctly?
β Incorrect
The csurf middleware adds the req.csrfToken() function which returns a unique token string for the current user session or cookie. This token is used to validate requests.