How to fix: document.write() in Page Scripts
document.write() was found in the page scripts. This function is dangerous because it can overwrite the entire page document, is a primary DOM XSS sink, and is deprecated in modern browsers.
Why it matters
If any attacker-controlled data reaches document.write(), it results in DOM XSS. Additionally, document.write() called after page load rewrites the entire document, destroying all event listeners and breaking React/Vue/Angular hydration.
document.write() is a legacy API from the early web. Modern JavaScript has safe alternatives for all of its use cases. Browsers have started restricting it (Chrome warns in console), and it's likely to be removed eventually.
How to fix it
- 1Replace document.write('<script>...') with dynamic script element creation: const s = document.createElement('script'); s.src = url; document.head.appendChild(s).
- 2Replace document.write('<div>text</div>') with element.textContent = 'text' or element.appendChild(...).
- 3Audit all uses of document.writeln() as well, same issue.
Code examples
Safe script injection alternative
// Bad
document.write('<script src="' + url + '"><\/script>');
// Good
const script = document.createElement('script');
script.src = url;
script.async = true;
document.head.appendChild(script);References
Detected automatically by VulnRadar. The check id is cs-document-write-usage, stable across every scan so you can filter or diff on it.
More client-side checks
- HighCSP Allows 'unsafe-inline' Scripts
- MediumCSP Allows 'unsafe-eval'
- HighpostMessage Without Origin Validation
- HighSensitive Data Stored in localStorage
- HighDOM XSS via location.hash Assignment
- Higheval() in Client-Side JavaScript
- MediumExternal Script Without Subresource Integrity
- MediumJavaScript Source Map Exposed in Production
Questions
- How severe is document.write() in Page Scripts?
- VulnRadar rates this high severity in the client-side category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
- Does VulnRadar check for this automatically?
- Yes. It runs as part of every scan under the check id cs-document-write-usage, which stays the same between runs so you can filter, diff, or gate CI on it.
- Which standards does document.write() in Page Scripts map to?
- It maps to CWE-79. In the OWASP Top 10 it falls under A03:2021.
Missing a check?
The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.