OOmnistu

Blog / Engineering Playbooks

Engineering Playbooks

DOM-based XSS: when your own JavaScript runs the attack

Understand how client-side code can turn untrusted data into executable content and why safe browser APIs matter.

OOmnistu
Reviewed 2026-11-16 · 3 min read

Stored and reflected XSS describe how a payload reaches a victim. Server-side and client-side describe where untrusted data is handled unsafely. OWASP's DOM based XSS Prevention Cheat Sheet covers the client-side case: your page's JavaScript takes untrusted data and puts it somewhere the browser treats as code or markup. We read the sheet on 15 November 2026 except its last part, on reducing mutation of encoded data. It extends OWASP's general XSS sheet, which our XSS prevention post summarises. Everything attributed here is OWASP's.

Why it is hard

OWASP says DOM-based XSS is extremely difficult to mitigate because of its large attack surface and lack of standardisation across browsers. In JavaScript, the main context is script, but the code can also reach HTML, attribute, URL and CSS contexts through DOM methods, and each parser treats encoding differently. It adds that JavaScript escapes are valid code: an escaped string can still execute, while an HTML-encoded tag is disarmed. So JavaScript encoding is a weaker defence than people expect.

The fix OWASP prefers

Use the right sink. To put user input in a page element, do not use innerHTML; use textContent (or innerText). The sheet calls this the right way to fix DOM-based XSS and says encoding the output correctly "is a hassle and can easily go wrong". Its example swaps an innerHTML write of the page's URL for textContent. It also says passing user-controlled input to eval is, in its words, 99% of the time a sign of bad practice, so do not try to sanitise it; avoid it.

Developer guidelines from the sheet

  1. Treat untrusted data as displayable text only, never code or markup.
  2. When templating JavaScript, encode untrusted data and put it in a quoted string.
  3. Build dynamic interfaces with createElement, setAttribute and appendChild. But setAttribute is safe only for a limited set of attributes; event attributes such as onclick and onblur execute code, because setAttribute converts the string to the attribute's type. Assigning a plain string to the onclick property sets nothing; assign a trusted function and treat untrusted values as data inside it.
  4. Avoid untrusted data in innerHTML, outerHTML, document.write and document.writeln.
  5. Avoid methods that implicitly evaluate strings, such as setTimeout and setInterval with a string, and Function. If you cannot, the sheet describes a closure approach and layered encoding, which are fiddly: the number of layers must match the number of times the data is evaluated.

If you must write into other subcontexts

The sheet documents different rules for nested HTML, JavaScript, attribute, CSS and URL contexts, with older library-specific examples. Do not treat those as a universal sequence of escaping steps. Prefer textContent for plain text, trusted DOM APIs with fixed safe attributes, and a maintained sanitizer when HTML is genuinely required. URL destinations also need allowed-scheme and destination checks. Have a developer review the exact sink and parser chain against current framework guidance; this overview is not a drop-in encoding recipe.

Questions for your team

Related: XSS prevention for app teams, content security policy basics, input validation basics.

Limits

One OWASP page, mostly read; its last part and links were not read. The sheet is partly dated in tone (it references older practice) and its encoder examples depend on a particular library. We give no statistics, no code and no tested claims. Use your framework's current security documentation.

By Omnistu.

Sources

Previous
Docker container security: a checklist for small teams