Course contentsShow
← Computer Science
Lesson 2457 of 2,87255. Web SecurityFree lesson

DOM-Based XSS Attacks

Exploring client-side XSS where the vulnerability exists in JavaScript code that processes user input in the DOM.

DOM-Based XSS Attacks

What you'll learn: You'll understand how XSS vulnerabilities can hide entirely in client-side JavaScript, exploiting the way scripts manipulate the DOM without ever sending malicious payloads to the server.

The Client-Side Trap

Unlike Reflected and Stored XSS (which involve server-side processing), DOM-Based XSS occurs purely in the browser. The vulnerability exists in JavaScript code that reads user-controlled input from the DOM and writes it back unsafely—all without the malicious data ever touching the server.

Think of it like a self-inflicted wound: your own JavaScript code becomes the weapon that executes the attack, processing dangerous input right in the user's browser.

How It Works

DOM-Based XSS exploits JavaScript that reads from sources (user-controllable data) and writes to sinks (dangerous execution contexts) without proper validation.

Common sources:

  • window.location.hash (URL fragment after #)
  • document.URL
  • document.referrer

Dangerous sinks:

  • document.write()
  • innerHTML
  • eval()
  • setTimeout() with string arguments

For example, JavaScript that reads a name from the URL hash and displays it: the code grabs window.location.hash, extracts a value, and inserts it directly into the page using innerHTML. An attacker crafts a URL with a malicious hash containing script tags, and when the victim's browser runs that JavaScript, it executes the injected code—no server involvement required.

Why It's Different

The server never sees the attack payload (it's often in the URL fragment), making traditional server-side defenses ineffective. The vulnerability lives in your client-side code logic.

Key Takeaway: DOM-Based XSS exploits insecure JavaScript that processes user input entirely in the browser, requiring developers to sanitize data within client-side code before inserting it into dangerous DOM operations.