Cybernetics

Diagnostic playbook

DOM-based XSS via document.write sink reading URL fragment

Web / Cross-Site Scripting

Knowledge identity

Definition and taxonomy

Summary

Client-side JavaScript reads a value from the URL and writes it into the page with document.write() without encoding, creating a DOM-based XSS sink. Server-side filtering may be bypassable.

Severity rationale

Demonstrated client-side sink: document.write() echoes an unencoded URL-derived value into the DOM. Server-side filters can be bypassed with event-handler payloads. Execution is browser-side; verified by source sink analysis plus server-side filter behavior in the authorized lab.

Aliases

DOM XSS, document.write sink

Standards

CWE-79 CAPEC-588 A03:2021-Injection

CVSS vector

CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N

1. First Signal

What should make an agent suspicious

Symptoms

The page contains document.write() echoing a URL-derived string; a payload containing <script causes a 302 redirect (blocked), while an event-handler payload is served 200 with no server interference.

Prerequisites

Authorized lab session and a browser-context analysis (curl can verify server filter behavior; sink presence from source).

2. Recognition Logic

How to recognize this next time

If JS derives a value from the URL and passes it to document.write/innerHTML/eval, identify the sink. Test server filters by watching status codes: 302 for script payloads and 200 for event-handler payloads indicates a bypassable client-side sink.

3. Diagnostic Procedure

Safe checks, in order

  1. Confirm authorized scope. 2. Fetch the page and confirm the sink in JS source. 3. Send a <script> payload and note the server response. 4. Send an event-handler payload and note the response. 5. Reason about what the browser would execute at the sink. 6. Record status codes and filter behavior as evidence.

4. Proof

Evidence

DVWA xss_d: sink 'var lang = document.location.href.substring(...indexOf(default=)+8); document.write(...lang...)' present. low=200 no filter; medium=200 for img onerror payload, 302 for <script> payload; high=302 for all non-whitelisted payloads (mitigated).

5. Controls

Negative checks

high level whitelist redirects all non-language values, so the sink is unreachable (mitigated control). At impossible level the sink is removed.

6. Avoid Bad Memory

False positives and traps

A document.write of a static value is not XSS. Server 200 for a payload does not by itself execute XSS; the sink must receive attacker data in a browser.

7. Why It Matters

Impact

Attacker-controlled HTML/JS executed in the victim's browser via a crafted link, since the sink runs on page load.

8. Why It Happened

Root cause

Client-side code trusts URL input and passes it to document.write without encoding; server filters (when present) only block specific patterns and can be bypassed with event-handler payloads.

9. Fix

Remediation and prevention

Avoid document.write sinks for user data; use textContent or encoded DOM APIs. Validate/allowlist the parameter server-side (as high does) to make the sink unreachable. Add CSP.

10. Re-test

Verification steps

  1. After fix, confirm the sink no longer receives attacker data. 2. Send all payload variants and confirm they are ignored/encoded. 3. Confirm no client-side document.write of URL data remains.

Reference

Tools and sources

Tools used

curl for server behavior, source review (docker exec) for sink analysis

References

OWASP DOM XSS Prevention Cheat Sheet; CWE-79; PortSwigger DOM-based XSS

Intelligence

Products and history

Technologies

JavaScript, browser DOM, PHP (server side)

Affected products / versions

DVWA (Damn Vulnerable Web Application) current master

History

First documented in DVWA xss_d module in the 2026-08 benchmark run.

Exploit knowledge

Variants and references

Attack surface

default query parameter read client-side via document.location.href and written to a <select> with document.write

Exploit maturity

proof-of-concept

Known variants

document.write sink with URL-derived value, innerHTML sinks, location.hash-based sinks, server-filtered DOM XSS where only event-handler payloads survive

Defensive signatures

Page JS using document.write/substring on location.href, server 302 redirects on payloads containing <script, 200 responses for event-handler payloads that a browser would execute

Reproduction commands

Low (no filter): curl -s -b $JAR "http://127.0.0.1:4280/vulnerabilities/xss_d/?default=English%3C/option%3E%3C/select%3E%3Cimg%20src=x%20onerror=alert(1)%3E" # page renders, sink writes payload Medium (blocks '<script' only, 302 otherwise): curl -s -b $JAR "http://127.0.0.1:4280/vulnerabilities/xss_d/?default=English%3C/option%3E%3C/select%3E%3Cimg%20src=x%20onerror=alert(1)%3E" # 200, payload reaches sink High (whitelist, mitigates): curl -s -b $JAR "http://127.0.0.1:4280/vulnerabilities/xss_d/?default=English%3C/option%3E%3C/select%3E%3Cimg%20src=x%20onerror=alert(1)%3E" # 302 -> ?default=English

Exploit references

No external references stored; all evidence from the authorized local lab.

Case provenance

Linked findings