CIDADAXSEC
← All topics
Expert

DOM-based vulnerabilities

DOM-based flaws occur when client-side scripts process attacker-controlled input and write it into dangerous sinks such as innerHTML, with no server compromise required.

Material

DOM-based flaws run entirely inside the browser. The server only ships the page and its scripts, so the vulnerability is created and executed client-side while the request itself stays completely normal.

How it works

Every DOM flaw has three parts: a source, a transformation, and a sink. Common sources are the URL and its fragment, form fields, local storage, postMessage payloads, and data fetched from another origin.

The source-to-sink path is what you hunt for. Follow the value: where it enters the page, which function reshapes it, and where it finally lands as markup or as a navigable link.

javascript
const params = new URLSearchParams(location.search);
const name = params.get('name');

// name dikendalikan penyerang dan mendarat di sink markup
document.getElementById('greeting').innerHTML = 'Halo, ' + name;

Attack shapes

  • Script execution through innerHTML, outerHTML, insertAdjacentHTML, or document.write with values taken from the URL.
  • Open redirect through a link built from a query parameter that the page navigates to without user interaction.
  • Cookie and token theft when injected script reads document.cookie or localStorage and sends it out.
  • DOM clobbering when an id or name attribute overwrites a built-in property of window and changes script behaviour.
  • Interface spoofing where injected markup renders a fake login form on top of the real one.

Preventing it

  • Never write raw input into innerHTML, outerHTML, insertAdjacentHTML, or document.write.
  • Use textContent or value for plain text, and a strict allowlist sanitizer when real markup is required.
  • Encode with encodeURIComponent before putting values into href or src, and validate the origin before redirecting.
  • Strip inline event handlers and keep third-party scripts away from DOM writing helpers.
  • Ship a strict Content Security Policy that blocks inline script and eval so injected payloads have no execution path.
javascript
const el = document.createElement('div');
el.textContent = name;                       // teks, bukan markup
document.querySelector('#greeting').replaceWith(el);

const target = new URL(value, location.origin);
if (target.origin === location.origin) {
  location.assign(target.href);               // redirect hanya ke origin yang sama
}

A practical test loop: drive the page through a recording proxy, change one URL parameter, then watch for DOM changes, new background requests, or a navigation that fires with no click.

Note: a CSP is a safety net, not the fix. Browsers already block javascript: navigations in many cases, yet DOM XSS still runs because the payload never needed a navigation to succeed.

Labs in this topic

Labs run on a domain separate from this site, so a payload cannot reach your account session.

Labs for this category land in Phase 2. The material is readable now.

Challenges in this category

Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.