document.domain Relaxation and Risks
You'll learn how document.domain once allowed subdomains to bypass Same-Origin Policy restrictions—and why browsers are removing this dangerous feature.
The Subdomain Collaboration Problem
Imagine you have mail.example.com and calendar.example.com. They're both part of your company's suite, but the Same-Origin Policy treats them as completely separate origins. Your JavaScript on mail.example.com can't access the DOM or cookies of calendar.example.com, even though they're siblings under the same parent domain.
How document.domain Worked
The document.domain property was a controlled way to relax the Same-Origin Policy for subdomains sharing a common parent. Both pages could set:
document.domain = "example.com";
Once both subdomains set their document.domain to the same value, they were treated as the same origin and could interact freely—sharing DOM access, cookies, and localStorage.
Think of it like this: It's like two branches of a family business agreeing to share their resources by both declaring loyalty to the parent company.
The Security Risks
This feature created serious vulnerabilities:
- Unintended exposure: If any subdomain had an XSS vulnerability, attackers could set
document.domainand access all other subdomains that also used this feature - Cookie theft: Setting
document.domainexposed all cookies scoped to the parent domain - Difficult to secure: Organizations had to ensure every single subdomain was secure, or none were safe
Why It's Deprecated
Modern browsers are disabling document.domain by default because it weakens security boundaries. Safer alternatives exist: postMessage() for cross-origin communication and careful cookie scoping with the Domain attribute.
Key Takeaway: document.domain allowed subdomains to share the same origin by setting a common parent domain, but this feature is deprecated because it created a shared security boundary where one vulnerable subdomain compromised all others.