Study Vault Search notes, aliases and full text
Graph
ConceptDBISAI-written · not yet proofread

Same-Origin Policy

Also SOP · CORS · Cross-Origin Resource Sharing

The same-origin policy is the browser rule that a script may use results only from a server with the same domain and port as the page it runs on, unless the other server explicitly allows it via CORS headers.

Properties#

  • Why. Suppose you are redirected to an evil site that looks like your bank’s site (e.g. http://www.comdirekt.de). Without the SOP, its script could talk to the bank’s server just like the real site; with a stolen session cookie it could act in your name without you even participating.
  • Enforced by the browser. The request may well reach the other server; the browser refuses to hand the result to the script.

CORS#

Cross-Origin Resource Sharing relaxes the SOP on the server’s initiative. When JavaScript requests a resource from a different host+port, the browser adds the request header

Origin: http://<host name>:<port>

The result (think: JSON) is accepted only if the response contains a matching header

Access-Control-Allow-Origin: http://<host name>:<port>

otherwise the browser reports “Cross-Origin Request Blocked: …”. Access-Control-Allow-Origin: * allows any requesting site to use the result.

Exceptions#

  • Scripts can be loaded from anywhere, important for libraries: <script src="http://code.jquery.com/jquery-1.10.2.js"></script>. This seemed reasonable because images and other objects can be loaded from anywhere, too.
  • This enabled JSONP, formerly the standard way to send JSON cross-origin: dynamically add <script>callback("…")</script> to the DOM, which executes callback with the data. No longer allowed, but similar exploits still exist.

The SOP addresses the third kind of web-app vulnerability, “communication of trusted information to an untrusted site”; the other two are access to private data (HTTP Server) and executing injected code (Code Injection Protection).

Example#

Sources#

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only