# Browser Security Model

> Same-origin policy, CORS, HTTPS, cookies and security headers: the rules that keep one site from attacking another. Free notes, key concepts, resources and a...

Canonical: https://www.internhack.in/roadmaps/tracks/frontend/web-security-basics
Last updated: 2026-11-28

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](https://www.internhack.in/roadmaps/tracks/frontend/http), [Working with REST APIs in Production](https://www.internhack.in/roadmaps/tracks/frontend/api-integration)

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

- [Same-origin policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) (MDN)
- [Web security](https://developer.mozilla.org/en-US/docs/Web/Security) (MDN)
- [OWASP Top Ten](https://owasp.org/www-project-top-ten/) (OWASP)

Previous: [Profiling with Lighthouse and DevTools](https://www.internhack.in/roadmaps/tracks/frontend/performance-tooling) · Next: [Cross-Site Scripting (XSS) Prevention](https://www.internhack.in/roadmaps/tracks/frontend/xss)

## More in Frontend Security

- [Browser Security Model](https://www.internhack.in/roadmaps/tracks/frontend/web-security-basics)
- [Cross-Site Scripting (XSS) Prevention](https://www.internhack.in/roadmaps/tracks/frontend/xss)
- [Authentication, Token Storage and CSRF](https://www.internhack.in/roadmaps/tracks/frontend/auth-security)
- [Content Security Policy and Supply-Chain Safety](https://www.internhack.in/roadmaps/tracks/frontend/csp)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Frontend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/frontend)
