How do XSS and CSRF differ, and how does CSP help against XSS?
Short answer
XSS is foreign script running inside your page, able to do anything the user can; CSRF is another site tricking the browser into sending a request with the cookie, without the attacker reading the response. Fight XSS with output encoding and CSP; fight CSRF with CSRF tokens and SameSite.
3 points to say:
- Against XSS: auto-escaping templates (
th:text, notth:utext), whitelist sanitizing for HTML, a CSP with a nonce or hash. - A CSP with
'unsafe-inline'/'unsafe-eval'is nearly useless; runReport-Onlyfirst, then enforce. - XSS beats every CSRF defence: a same-origin script can read the CSRF token.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.