repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
shadow-dom.mdMarkdown133 lines5.7 KiBsource raw download

title: "Shadow DOM" section: "Web Pentesting" sectionSlug: "pentesting-web" sourcePath: "src/pentesting-web/xss-cross-site-scripting/shadow-dom.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/pentesting-web/xss-cross-site-scripting/shadow-dom.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0"#

Shadow DOM#

Shadow DOM encapsulates DOM and CSS implementation details, but it is not a security boundary for script already executing in the same origin/realm. It creates useful assessment surfaces when applications mix closed roots, Declarative Shadow DOM (DSD), and HTML parsing/sanitizing APIs.[3]

Why attackers care#

Enumerating shadow roots#

Open roots are trivial to enumerate:

for (const el of document.querySelectorAll('*')) {
  if (el.shadowRoot) console.log('shadow host:', el, el.shadowRoot);
}

When testing large applications, recursively walk every discovered shadowRoot because Web Components are often nested several levels deep.

Intercepting closed roots#

If you can run JavaScript before the component is initialized, hook attachShadow() and keep the returned reference:

(() => {
  const orig = Element.prototype.attachShadow;
  window.__shadowRoots = [];
  Element.prototype.attachShadow = function (init) {
    const root = orig.call(this, init);
    window.__shadowRoots.push({ host: this, mode: init.mode, root });
    return root;
  };
})();

This is one of the most important takeaways when auditing apps or browser extensions that wrongly assume closed protects secrets, CSRF tokens, anti-clickjacking UI, or privileged controls.

Declarative Shadow DOM as an injection surface#

DSD creates a shadow root directly from markup:

<div id="host">
  <template shadowrootmode="open">
    <img src=x onerror=alert(document.domain)>
  </template>
</div>

Important parsing behavior for exploitation:[2][4]

Therefore, look for applications that:

Also remember that classic <script> tags inserted from HTML strings stay inert, so prefer event handlers, javascript: URLs, or event gadgets inside the shadow tree.

A useful gadget is slotchange:

x<template shadowrootmode=open><slot onslotchange=alert(1)>

The leading text node (x) becomes assigned content for the default slot, which fires slotchange.

Sanitizer and mXSS footguns#

Recent sanitizer bypasses made DSD more relevant for XSS research:

Typical risky pattern:

const frag = sanitize(userHTML, { returnDOM: true });
target.appendChild(frag);

If frag still contains a declarative shadow root, the append operation can reintroduce attacker-controlled handlers inside the page.

Dumping serializable / clonable shadow roots#

Newer Shadow DOM features are useful post-XSS recon primitives:

Examples:

host.getHTML({ serializableShadowRoots: true });
const copy = host.cloneNode(true);

If the application opted in, this can expose shadow DOM content that would otherwise stay out of normal innerHTML output. This is especially interesting when developers use Shadow DOM to hide tokens, comments, feature flags, or internal UI state.

Practice#

A good lab for this topic is the DiceCTF shadow challenge, which chained DOM XSS with closed-shadow-root abuse and CSS-based extraction ideas.[5]

References#