DOM-based vulnerabilities

DOM Based XSS via Misconfigured postMessage() Function

The application insecurely injects user-supplied data (e.data) into the DOM using innerHTML without validation or sanitization, leading to DOM-based XSS. The attacker uses postMessage to inject an image tag with an onerror handler that triggers JavaScript execution.

Vulnerablae Function

<script>
   window.addEventListener('message', function(e) {
      document.getElementById('ads').innerHTML = e.data;
   })
</script>

Sending malicious payload to the target

<iframe src="https://0abe000f03ab5cf1843519c100490025.web-security-academy.net/" onload="this.contentWindow.postMessage('<img src=x onerror=print()>','*') "></iframe>

DOM XSS using web messages and a JavaScript URL

The script checks for the presence of http: or https: in the message string, but doesn’t validate the full protocol. By sending javascript:print()//http:, the attacker bypasses the check and causes a redirect to a javascript: URI, triggering code execution.

Vulnerable Function

<script>
                        window.addEventListener('message', function(e) {
                            var url = e.data;
                            if (url.indexOf('http:') > -1 || url.indexOf('https:') > -1) {
                                location.href = url;
                            }
                        }, false);
</script>

Sending malicious payload to the target

DOM XSS using web messages and JSON-parse

The application accepts JSON via postMessage and sets the src attribute of a dynamically created iframe without validating the URL. Although javascript: URIs are typically blocked in iframes, some older browsers or misconfigurations might still execute the JavaScript.

Vulnerable function:

Sending malicious Payload:

DOM-Based Open Redirection Vulnerability

Vulnerable function

  • This code extracts a url parameter from the current URL using a regular expression.

  • If a match is found, it redirects the user to that external URL using location.href.

  • If not, it redirects to the homepage (/).

Payload

Vulnerable Function

  • The script sets a cookie using the entire window.location as its value.

  • If the attacker injects a payload into the URL, it becomes part of the HTML DOM when the page renders.

  • This cookie value is later embedded into an anchor tag (<a>) directly in the HTML, without escaping it properly.

Injected via query param:

Which becomes:

This breaks the tag and injects the script.

Final Exploit PoC:

DOM-Based XSS via DOM Clobbering

Vulnerable Code

The application checks if window.defaultAvatar exists. If not, it falls back to a default object containing an image path.

However, the script does not validate whether window.defaultAvatar is a safe object. An attacker can take advantage of this by injecting a DOM element like:

This is a technique known as DOM Clobbering, where DOM elements with specific id or name attributes can overwrite global JavaScript variables.

As a result:

  • window.defaultAvatar now points to the <a> element

  • window.defaultAvatar.avatar becomes the name="avatar" value, which is "x"

Last updated