0%
Security Engineering for Software Teams
Thinking About Security
Cryptography You Will Actually Use
Identity
Infrastructure and Cloud
Detection and Response
XSS, CSRF and the Browser Security Model
Web applications run inside browsers that hold the user cookies, sessions and data for many sites at once. The browser security model is the set of rules that keeps those sites apart, so a page from one site cannot read another site data or act as the user there. Cross site scripting and cross site request forgery are the two classic ways attackers work around those rules. To defend against them, engineers first need a clear picture of what the browser does and does not protect. This section explains origins, the same origin policy, cookies and the attributes that control them and how cross origin requests are governed.
Origins
An origin is the combination of scheme, host and port, such as https, app.example.com and 443. Two URLs share an origin only if all three match exactly. A page at https://app.example.com and a page at https://api.example.com are different origins, as are http and https versions of the same host or the same host on a different port. Paths do not matter, so every page on one host and port shares an origin. Almost every browser security decision is made in terms of origins, which is why understanding them precisely matters.
The same origin policy
The same origin policy lets a page send many kinds of requests to other origins, such as loading images, scripts and stylesheets or submitting forms, but stops it from reading the responses. A script on one origin cannot read the DOM of a page from another origin, cannot read the body of a cross origin response without permission and cannot access another origin storage. This asymmetry, where writing across origins is often allowed but reading is restricted, explains both why CSRF is possible and why XSS is so powerful.
Sites versus origins
A site is a broader grouping: the registrable domain plus scheme, such as https and example.com. All subdomains of example.com are the same site even though they are different origins. Cookies and the SameSite attribute work in terms of sites, while the same origin policy works in terms of origins. This means a vulnerable or attacker controlled subdomain can sometimes set cookies for, or make same site requests to, the main application, which is why subdomain hygiene matters for security.
Cookies and ambient authority
Most web sessions use cookies, which the browser attaches automatically to requests to the site that set them. This automatic attachment is called ambient authority: the request carries the user credentials no matter which page triggered it. Ambient authority is convenient, because users stay logged in across tabs, but it is the root of CSRF, since a page on another site can trigger a request that carries the user session cookie. Token based authentication that requires script to attach a header does not have this property.
Cookie attributes
Several attributes change how cookies behave. Secure sends the cookie only over HTTPS. HttpOnly hides it from scripts, so XSS cannot read it directly. SameSite controls whether it is sent on requests initiated by other sites: Strict never sends it cross site, Lax sends it only on top level navigations with safe methods such as following a link, and None sends it everywhere but requires Secure. The Domain and Path attributes control scope, and the host prefix forces a cookie to be locked to one exact host.
Browser defaults
Modern browsers treat cookies without an explicit SameSite attribute as Lax by default, which blocks many classic CSRF attacks automatically. The default is not universal across all browsers and versions, does not protect same site attacks and does not cover GET requests that change state. Applications should set SameSite explicitly on every cookie rather than relying on defaults, and should not treat the browser default as a complete CSRF defence.
Cross origin resource sharing
Cross origin resource sharing, or CORS, lets a server relax the same origin policy for specific origins. When a script on one origin requests data from another, the browser checks response headers to decide whether the script may read the response. Servers list allowed origins and say whether credentials such as cookies may be included. CORS is a permission system for reading across origins, not a defence against requests being sent, and misconfigured CORS is a common way to undo the same origin policy.
Preflight requests
For requests that could have side effects or use unusual headers or content types, the browser first sends a preflight request asking the server whether the real request is allowed. Simple requests, such as form posts with standard content types, skip the preflight and are sent directly. This distinction matters for CSRF: a JSON API that requires a custom header or a JSON content type forces a preflight, which a cross site page cannot pass unless CORS allows it.
Storage and isolation
Browsers keep local storage, session storage, IndexedDB and caches separate per origin, and increasingly partition third party cookies and storage by the top level site as well. Script running in an origin can read all of that origin storage. This is why storing tokens in local storage is risky when XSS is possible: any injected script can read them, whereas HttpOnly cookies at least hide the token value from script.
Frames and windows
Pages can embed other pages in frames and open new windows. The same origin policy blocks reading across origins in both cases, but embedding creates other risks. A site can frame another and overlay it to trick users into clicking, which is clickjacking. Windows opened with links can sometimes navigate the opener page. Headers such as frame ancestors in a content security policy and the cross origin opener policy control these interactions.
What the browser protects
Taken together, the browser stops one origin from reading another origin data, isolates storage per origin, controls when cookies are attached and lets servers opt into limited sharing. It does not stop cross site requests from being sent, does not stop script that runs inside your origin from doing anything your own code can do and cannot tell a malicious script from a legitimate one if both come from your pages. XSS and CSRF exploit exactly these gaps.
Threat model for web applications
A useful threat model for browser attacks assumes the attacker controls some website the victim visits, can send the victim links, may control content stored in your application, such as comments or profile fields, and may control a subdomain or a third party script your pages load. Each assumption maps to a class of attack: hostile sites to CSRF and clickjacking, links and stored content to XSS, subdomains to cookie tossing and third party scripts to supply chain compromise.
Common misconceptions
Several beliefs about the browser model cause real bugs. HTTPS does not stop XSS or CSRF; it protects data in transit, not what pages do with it. CORS does not protect an API from being called by scripts or tools outside a browser, because only browsers enforce it. A request coming from your own domain is not proof it came from your code, since injected script and compromised subdomains share that domain. And hiding a button in the interface does not stop anyone from sending the request it would have made. Each of these misunderstandings appears regularly in security reviews and incident reports.
An origin is scheme, host and port. The browser lets pages send requests across origins but blocks reading the responses, attaches cookies automatically as ambient authority and lets servers relax reading with CORS. Set Secure, HttpOnly and SameSite explicitly, and remember the browser cannot tell your scripts from injected ones.