How the Web's Trust Model Works
A web application has three parties: the user, the user's browser, and the server. The server sees only HTTP requests. It can't see a person, a keyboard or which page a click came from; it sees bytes that claim to come from somewhere. The browser, meanwhile, runs code from many servers at once, in tabs and frames, and has to keep them from interfering with each other while still letting pages link, embed and submit to one another. Nearly every web vulnerability is a mistake about who trusts whom in this triangle. This lesson draws the triangle and names the rules that later lessons break and repair.
Definitions
- Origin: the triple (scheme, host, port) of a URL.
https://shop.lab.testandhttps://shop.lab.test:443/cartshare an origin;http://shop.lab.test(other scheme),https://api.shop.lab.test(other host) andhttps://shop.lab.test:8443(other port) do not. The browser isolates code by origin. - Site: the scheme plus the registrable domain (
lab.testforapi.shop.lab.test), looked up in the Public Suffix List. Cookie scoping (a cookie ignores port, and withDomaincovers subdomains) andSameSiteare coarser than origins:SameSitecompares sites. - Ambient authority: credentials the browser attaches automatically, whatever page caused the request: cookies, HTTP Basic credentials, TLS client certificates. Convenient for users, dangerous because a request "with the user's cookies" does not mean "the user wanted it".
- Trust boundary: a line where data passes from a less trusted to a more trusted component, for example from the browser to the server, or from the server to its database. Validation belongs at the boundary, on the trusted side.
The triangle
Three facts follow from the picture, and each is the root of a family of bugs:
- The server must not trust anything the browser sends. Hidden form fields, disabled buttons, JavaScript checks, a
rolein a JSON body, theRefererheader: all are typed by whoever controls the client, which may be a script,curlor an intercepting proxy. Client-side checks are for usability; the server re-checks. Breaking this gives mass assignment, IDOR, business-logic flaws. - The browser attaches credentials to requests other sites cause. Origin B can make your browser send a form or load an image from origin A, with A's cookies. B can't read the answer (the same-origin policy forbids it), but the request still happens. Breaking this gives CSRF;
SameSitecookies and tokens repair it. - Code that runs in origin A has all of A's power. If attacker text becomes script inside A's pages, the same-origin policy no longer protects anything, because the attacker is origin A. This is XSS, and it is why output encoding and CSP matter so much.
Origins in code
Browsers compute origins with the URL standard's parser. Python's urllib.parse is close enough to see the rule, including the default ports that make :443 disappear:
from urllib.parse import urlsplit
DEFAULT_PORT = {"http": 80, "https": 443}
def origin(url):
u = urlsplit(url)
return (u.scheme, u.hostname, u.port or DEFAULT_PORT.get(u.scheme))
base = "https://shop.lab.test/cart"
for other in ["https://shop.lab.test:443/account",
"http://shop.lab.test/cart",
"https://api.shop.lab.test/v1/orders",
"https://shop.lab.test:8443/",
"https://SHOP.lab.test/help"]:
same = origin(base) == origin(other)
print(f"{other:40} {'same origin' if same else 'cross-origin'} {origin(other)}")https://shop.lab.test:443/account same origin ('https', 'shop.lab.test', 443)
http://shop.lab.test/cart cross-origin ('http', 'shop.lab.test', 80)
https://api.shop.lab.test/v1/orders cross-origin ('https', 'api.shop.lab.test', 443)
https://shop.lab.test:8443/ cross-origin ('https', 'shop.lab.test', 8443)
https://SHOP.lab.test/help same origin ('https', 'shop.lab.test', 443)
Host names are case-insensitive, so the last URL is the same origin. api.shop.lab.test is a different origin but the same site (lab.test), so a cookie set with Domain=lab.test travels to both, and SameSite treats requests between them as same-site. Keep that difference in mind for the cookie and CSRF lessons.
Who decides what
| Question | Decided by | Lesson |
|---|---|---|
| Who is this request from? | server: session cookie or token, checked on every request | sessions, JWT, OAuth/OIDC |
| May this user do this to this object? | server: authorization on every endpoint and object | IDOR, function-level |
| Did the user mean to send it? | server, with browser help: SameSite, CSRF tokens, Origin header |
CSRF |
| May page B read A's response? | browser: same-origin policy, relaxed only by A's CORS headers | SOP, CORS |
| May this script run in A's page? | browser, as instructed by A: CSP, Trusted Types | CSP, XSS defences |
| Is this text data or code? | server and page code: parameters, encoding, safe sinks | SQLi, XSS |
How trust mistakes are detected
- Code review signal: a security decision made from client-supplied data (
if request.json["is_admin"]), or made only in JavaScript. Search for authorization checks and see which endpoints lack them. - Testing: replay requests with an intercepting proxy, changing ids, roles, prices and hidden fields; remove the cookie; send the request from another origin. If the server's answer doesn't change, it trusted the client.
- Logs: requests that no UI could produce (a
rolefield on a profile update, an order with a negative quantity, an object id from another tenant) are a strong signal of probing.
How they are fixed
- Decide on the server, from server-side state: the session says who the user is, the database says who owns the object, a policy says what the role may do.
- Treat every input as data at every boundary: parameterised queries, output encoding for the exact context, allow-lists for identifiers, URLs and file names.
- Make ambient authority explicit:
SameSitecookies, CSRF tokens, checkingOriginon state-changing requests, tokens sent in headers rather than cookies where it fits. - Tell the browser your rules: CSP,
frame-ancestors, HSTS, strict CORS. The browser enforces what you declare and nothing more.
Pitfalls
- "Same site" is not "same origin". A sibling subdomain run by another team, or user-hosted content on
files.lab.test, is same-site and can set cookies for the whole site. - "Only our app calls this API" is never true: anyone can call it with
curl. - The
Refererheader is not authentication: it can be stripped by policy and is set by the client.
Practice
1. Are https://lab.test and https://www.lab.test the same origin? The same site?
Different origins (different host), same site (registrable domain lab.test, same scheme).
2. A shop disables the "price" input with JavaScript so users can't edit it, and the server reads the price from the form. Which fact of the triangle does this break, and what is the fix?
Fact 1: the server trusts the client. Anyone can send the form with another price from a proxy or curl. The server must look the price up from its own catalogue by product id.
3. Page B submits a hidden form to https://bank.lab.test/transfer. Can B read the bank's response? Does the transfer happen?
B can't read the response (same-origin policy), but the request is sent, with the bank's cookies unless SameSite stops them, so the transfer may happen. That is CSRF.
Test yourself
Which part of a URL is NOT part of its origin?
- Scheme
- Host
- Port
- Path
What is ambient authority?
- Credentials the browser attaches automatically, such as cookies
- A server's admin password
- The CSP header
- A user's explicit consent
Why does XSS defeat the same-origin policy?
- It disables JavaScript
- The injected script runs inside the victim origin, so it has that origin's full access
- It changes the port
- It uses a different browser
Where must authorization decisions be made?
- In the page's JavaScript
- In hidden form fields
- On the server, from server-side state, on every request
- In the Referer header