Course contentsShow
← Security
Lesson 1060 of 3,05222. Web Security - Client-Side and SPA SecurityFree lesson

document.domain Relaxation and Risks

Learn how document.domain allows controlled SOP relaxation for subdomains and why this feature is deprecated.

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:

  1. Unintended exposure: If any subdomain had an XSS vulnerability, attackers could set document.domain and access all other subdomains that also used this feature
  2. Cookie theft: Setting document.domain exposed all cookies scoped to the parent domain
  3. 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.