Browser Security Model

Same-origin policy, CORS, HTTPS, cookies and security headers: the rules that keep one site from attacking another.

Intermediate · about 3 hours · difficulty 3/5

Learn first: HTTP and HTTPS, Working with REST APIs in Production

The browser runs code from millions of untrusted sites side by side. Its security model is what stops a malicious page from reading your bank account in another tab. Frontend developers need to understand it because many vulnerabilities come from working around it carelessly.

The same-origin policy

An origin is scheme + host + port (https://app.example.com:443). Scripts can freely read responses and DOM only from their own origin. Two consequences:

  • CORS is how a server opts in to let other origins read its responses (Access-Control-Allow-Origin). It is enforced by the browser, configured on the server, and it does not stop requests from being sent.
  • Iframes and windows from other origins are isolated; communicate deliberately with postMessage and always check event.origin.

HTTPS everywhere

TLS protects data in transit and is required for many modern APIs (service workers, geolocation, clipboard). Add HSTS so browsers never downgrade to HTTP, and avoid mixed content.

Cookies

Know the attributes: HttpOnly (JavaScript cannot read it), Secure (HTTPS only), SameSite=Lax|Strict|None (controls cross-site sending), plus Domain, Path and expiry.

Security headers

Content-Security-Policy, X-Content-Type-Options: nosniff, Referrer-Policy, Permissions-Policy and frame-ancestors (clickjacking protection). Check them with your browser's DevTools or an observatory scanner.

A mindset

Anything in the browser is visible and modifiable by the user: never put secrets in frontend code, and never trust client-side checks for authorisation. The server is the security boundary.

Good enough to move on

You can explain the same-origin policy and CORS precisely, set secure cookie attributes, list the key security headers and explain why frontend validation is not security.

Key concepts

  • Origins and the same-origin policy
  • CORS preflight and headers
  • HTTPS, HSTS and mixed content
  • Cookie attributes: HttpOnly, Secure, SameSite
  • Security headers
  • postMessage and origin checks
  • No secrets in frontend code

Mini project

Deploy a small app and harden it: secure session cookies, HSTS, nosniff, a strict referrer policy and frame-ancestors, then scan it with an online header checker and document the before and after results.

Check yourself

A request to another origin fails with a CORS error. Did the request reach the server? Where must the fix go?

Free resources