SteelReed

Search

Search posts, questions…

SecurityQuestion 7 / 24

Junior

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.

XSS: injected scriptRuns in your originReads DOM, tokens,acts as the userCSRF: evil.com pageBrowser auto-sends cookieRequest goes through,response unreadable

3 points to say:

  1. Against XSS: auto-escaping templates (th:text, not th:utext), whitelist sanitizing for HTML, a CSP with a nonce or hash.
  2. A CSP with 'unsafe-inline'/'unsafe-eval' is nearly useless; run Report-Only first, then enforce.
  3. 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.