--- title: "Dom Clobbering" section: "Web Pentesting" sectionSlug: "pentesting-web" sourcePath: "src/pentesting-web/xss-cross-site-scripting/dom-clobbering.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/pentesting-web/xss-cross-site-scripting/dom-clobbering.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0" --- # Dom Clobbering ## **Basics** It's possible to generate **global variables inside the JS context** with the attributes **`id`** and **`name`** in HTML tags.[[2]](#references) ```html
``` **Only** certain elements can use the **name attribute** to clobber globals, they are: `embed`, `form`, `iframe`, `image`, `img` and `object`. Interestingly, when you use a **form element** to **clobber** a variable, you will get the **`toString`** value of the element itself: `[object HTMLFormElement]` but with **anchor** the **`toString`** will be the anchor **`href`**. Therefore, if you clobber using the **`a`** tag, you can **control** the **value** when it's **treated as a string**: ```html ``` ### Arrays & Attributes It's also possible to **clobber an array** and **object attributes**: ```html ``` To clobber **a 3rd attribute** (e.g. x.y.z), you need to use a **`form`**: ```html
``` Clobbering more attributes is **more complicated but still possible**, using iframes: ```html ``` > [!WARNING] > The style tag is used to **give enough time to the iframe to render**. Without it you will find an alert of **undefined**. To clobber deeper attributes, you can use **iframes with html encoding** this way: ```html ``` ### **Filter Bypassing** If a filter is **looping** through the **properties** of a node using something like `document.getElementByID('x').attributes` you could **clobber** the attribute **`.attributes`** and **break the filter**. Other DOM properties like **`tagName`** , **`nodeName`** or **`parentNode`** and more are also **clobberable**.[[2]](#references) ```html
``` ## **Clobbering `window.someObject`** In JavaScript it's common to find: ```javascript var someObject = window.someObject || {} ``` Manipulating HTML on the page allows overriding `someObject` with a DOM node, potentially introducing security vulnerabilities. For example, you can replace `someObject` with an anchor element pointing to a malicious script: ```html ``` In a vulnerable code such as: ```html ``` This method exploits the script source to execute unwanted code.[[2]](#references) **Trick**: **`DOMPurify`** allows you to use the **`cid:`** protocol, which **does not URL-encode double-quotes**. This means you can **inject an encoded double-quote that will be decoded at runtime**. Therefore, injecting something like **``** will make the HTML encoded `"` to be **decoded on runtime** and **escape** from the attribute value to **create** the **`onerror`** event.[[4]](#references) Another technique uses a **`form`** element. Certain client-side libraries inspect the attributes of a newly created form element to clean them. However, by adding an `input` with `id=attributes` inside the form, you effectively overwrite the attributes property, preventing the sanitizer from accessing the actual attributes. You can [**find an example of this type of clobbering in this CTF writeup**](/hacktricks/pentesting-web/xss-cross-site-scripting/iframes-in-xss-and-csp#iframes-in-sop-2). ## Clobbering document object According to the documentation it's possible to overwrite attributes of the document object using DOM Clobbering: > The [Document](https://html.spec.whatwg.org/multipage/dom.html#document) interface [supports named properties](https://webidl.spec.whatwg.org/#dfn-support-named-properties). The [supported property names](https://webidl.spec.whatwg.org/#dfn-supported-property-names) of a [Document](https://html.spec.whatwg.org/multipage/dom.html#document) object document at any moment consist of the following, in [tree order](https://dom.spec.whatwg.org/#concept-tree-order) according to the element that contributed them, ignoring later duplicates, and with values from [id](https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute) attributes coming before values from name attributes when the same element contributes both: > > \- The value of the name content attribute for all [exposed](https://html.spec.whatwg.org/multipage/dom.html#exposed) [embed](https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-embed-element), [form](https://html.spec.whatwg.org/multipage/forms.html#the-form-element), [iframe](https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element), [img](https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element), and [exposed](https://html.spec.whatwg.org/multipage/dom.html#exposed) [object](https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-object-element) elements that have a non-empty name content attribute and are [in a document tree](https://dom.spec.whatwg.org/#in-a-document-tree) with document as their [root](https://dom.spec.whatwg.org/#concept-tree-root);\ > \ > \- The value of the [id](https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute) content attribute for all [exposed](https://html.spec.whatwg.org/multipage/dom.html#exposed) [object](https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-object-element) elements that have a non-empty [id](https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute) content attribute and are [in a document tree](https://dom.spec.whatwg.org/#in-a-document-tree) with document as their [root](https://dom.spec.whatwg.org/#concept-tree-root);\ > \ > \- The value of the [id](https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute) content attribute for all [img](https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element) elements that have both a non-empty [id](https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute) content attribute and a non-empty name content attribute, and are [in a document tree](https://dom.spec.whatwg.org/#in-a-document-tree) with document as their [root](https://dom.spec.whatwg.org/#concept-tree-root). Using this technique you can overwrite commonly used **values such as `document.cookie`, `document.body`, `document.children`**, and even methods in the Document interface like `document.querySelector`. ```javascript document.write("") document.cookie typeof(document.cookie) 'object' //Something more sanitize friendly than a img tag document.write("
") document.cookie HTMLCollection(2) [img, form, cookie: img] typeof(document.cookie) 'object ``` ## Writing after the element clobbered The results of calls to **`document.getElementById()`** and **`document.querySelector()`** can be altered by injecting a `` or `` tag with an identical id attribute. Here's how it can be done:[[1]](#references) ```html

clobbered

``` Furthermore, by employing styles to hide these injected HTML/body tags, interference from other text in the `innerText` can be prevented, thus enhancing the efficacy of the attack: ```html

existing text

clobbered ``` Investigations into SVG revealed that a `` tag can also be utilized effectively: ```html clobbered ``` For the HTML tag to function within SVG in browsers like Chrome and Firefox, a `` tag is necessary: ```html clobbered ``` ## Clobbering Forms It's possible to add **new entries inside a form** just by **specifying the `form` attribute** inside some tags. You can use this to **add new values inside a form** and to even add a new **button** to **send it** (clickjacking or abusing some `.click()` JS code): ```html ``` - For more form attributes in [**button check this**](https://www.w3schools.com/tags/tag_button.asp)**.** ## HTMLCollection-backed config object clobbering A very practical pattern is when the application reads a config object from `window` and later uses one of its properties in a sink such as `script.src`:[[3]](#references)[[5]](#references) ```javascript let config = window.PixelAnalyticsConfig || { enabled: false, scriptUrl: '/js/mock-tracker.js' }; if (config.enabled) { let s = document.createElement('script'); s.src = config.scriptUrl; document.body.appendChild(s); } ``` If you can inject HTML and the sanitizer keeps `id` / `name`, duplicate IDs can create an `HTMLCollection` on `window`, and named members of that collection become properties of the clobbered object: ```html
``` Then the browser resolves something similar to: ```javascript window.PixelAnalyticsConfig // HTMLCollection (truthy) window.PixelAnalyticsConfig.enabled // (truthy) window.PixelAnalyticsConfig.scriptUrl // ``` This is powerful because the code often only checks **truthiness**, not types. When `script.src = config.scriptUrl` executes, the browser coerces the anchor element to a string, and anchors stringify to their `href`. Therefore a DOM clobbering primitive becomes a **remote script loader**.[[3]](#references) ### Why this is frequently exploitable 1. The app treats `window.someConfig || {}` as trusted. 2. Duplicate IDs produce an `HTMLCollection` instead of a single node. 3. `name` attributes become object-like properties on that collection. 4. URL-valued sinks (`script.src`, `iframe.src`, `fetch()`, redirects, etc.) coerce DOM nodes to strings. 5. If CSP allows the target origin, or no restrictive `script-src` exists, attacker JS executes.[[6]](#references) ### Chaining note: converting self-XSS into higher-impact XSS If the HTML injection is only rendered inside the attacker's own storage bucket (profile, draft, testimonial list, etc.), DOM clobbering may initially look like self-XSS. Check whether predictable session cookies, IDOR/BOLA, shared objects, or authorization flaws let you: 1. **Store** the payload in another user's scope. 2. **Render** that same stored payload while authenticated as the victim/admin. This is especially relevant when the sink is only reached after loading user-specific content.[[3]](#references) ## References - [1] [PortSwigger Research: Hijacking service workers via DOM clobbering](https://portswigger.net/research/hijacking-service-workers-via-dom-clobbering) - [2] [PortSwigger Web Security Academy: DOM clobbering](https://portswigger.net/web-security/dom-based/dom-clobbering) - [3] [How I Chained Three Bugs to XSS an Intigriti CTF — IDOR + DOM Clobbering + DOMPurify 3.0.9 Bypass](https://prateekpulastya.medium.com/how-i-chained-three-bugs-to-xss-an-intigriti-ctf-idor-dom-clobbering-dompurify-3-0-9-bypass-25b74fc7afc7) - [4] [DOMPurify repository / configuration examples](https://github.com/cure53/DOMPurify) - [5] [Lab: Exploiting DOM clobbering to enable XSS](https://portswigger.net/web-security/dom-based/dom-clobbering/lab-dom-xss-exploiting-dom-clobbering) - [6] [Bypassing CSP via DOM clobbering](https://portswigger.net/research/bypassing-csp-via-dom-clobbering)