Same-Origin Policy
The Same-Origin Policy is a security rule built into web browsers that prevents a website from freely reading or manipulating data belonging to a different website. An "origin" is defined by the combination of the protocol (such as HTTPS), the hostname, and the port number, and the policy blocks scripts on one origin from accessing resources loaded from another origin. This helps protect users from malicious websites that might try to steal sensitive information from other sites the user is logged into.
The Same-Origin Policy (SOP) is a fundamental browser-enforced security mechanism that restricts how documents or scripts loaded from one origin can interact with resources from a different origin, where an origin is defined by the tuple of scheme, host, and port. SOP primarily governs DOM access (preventing cross-origin scripts from reading or manipulating another page's Document Object Model), restricts cross-origin network reads for responses obtained via APIs such as XMLHttpRequest and Fetch, and limits certain forms of cross-origin embedding and data extraction. It is important to note that cookie access is governed by its own model based on domain and path attributes rather than the full scheme/host/port origin tuple, so SOP's protections and cookie scoping rules, while related, are not identical. SOP can be selectively relaxed through mechanisms such as Cross-Origin Resource Sharing (CORS), the document.domain property (now deprecated in some browsers), and postMessage. SOP does not typically block cross-origin resource embedding (e.g., images, scripts, stylesheets loaded via HTML tags), which means it does not prevent all classes of cross-origin data leakage without complementary defenses.
Why it matters
The Same-Origin Policy is one of the most foundational security boundaries in the web platform. Without it, any website a user visits could freely read data from other sites the user is simultaneously logged into, such as banking portals, email accounts, or corporate applications. SOP prevents a malicious page from using JavaScript to silently extract sensitive information from another origin's DOM or from reading the responses to cross-origin network requests. This baseline isolation is what makes it possible for users to have multiple tabs open to different sites without those sites being able to interfere with or spy on each other.
However, SOP alone does not constitute a complete defense against all cross-origin attacks. It does not typically block cross-origin resource embedding (for example, images, scripts, and stylesheets loaded via HTML tags), which means that certain classes of data leakage or injection attacks remain possible without complementary controls. Additionally, cookie access follows its own scoping model based on domain and path attributes rather than the full scheme, host, and port tuple that defines an origin under SOP. This distinction means that assumptions about SOP protections do not directly translate to cookie isolation, and developers must apply additional measures such as cookie flags (Secure, HttpOnly, SameSite) and proper CORS configuration to achieve robust cross-origin security.
Misconfigurations in mechanisms that relax SOP, particularly overly permissive CORS policies, are a recurring source of real-world vulnerabilities. When applications reflect arbitrary origins in Access-Control-Allow-Origin headers or set Access-Control-Allow-Credentials without strict origin validation, attackers can leverage trusted user sessions to exfiltrate sensitive data cross-origin. Understanding how SOP works, where its boundaries lie, and how its relaxation mechanisms can be abused is essential for building secure web applications.
Who it's relevant to
Inside SOP
Common questions
Answers to the questions practitioners most commonly ask about SOP.