Performance: CSRF protection
CSRF protection affects the server response time and client-side interaction by adding security tokens to requests, which can slightly increase payload size and processing.
Jump into concepts and practice - no test required
import csurf from 'csurf'; const csrfProtection = csurf({ cookie: true }); app.get('/form', csrfProtection, (req, res) => { res.render('form', { csrfToken: req.csrfToken() }); }); app.post('/submit', csrfProtection, (req, res) => { processForm(req.body); res.send('Form submitted'); });
app.post('/submit', (req, res) => { // No CSRF token validation processForm(req.body); res.send('Form submitted'); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No CSRF Protection | No extra DOM nodes | 0 | 0 | [X] Bad - insecure, no protection |
| CSRF Token in Form (cookie-based) | Adds hidden input for token | 0 | Minimal | [OK] Good - secure with minimal overhead |
csurf package?csurf middleware is used as csurf({ cookie: true }) to enable cookie-based CSRF tokens.app.post('/submit', csurf({ cookie: true }), (req, res) => {
res.send('Form submitted');
});csurf middleware but your form keeps failing CSRF validation. Which of these is the most likely cause?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?csurf({ cookie: true }) to enable cookie-based CSRF tokens.req.csrfToken() as a functionreq.csrfToken(), not just reference the function.csrfToken in the render call for the form to use.