Chapter 3 · Security
Revision sheet
Every lesson's idea and recap on one page. Read it before an interview, or print it.
-
3.1Security Overview
Everything the browser receives is attacker-reachable — your job is to shrink what a breach can reach.
- Injected code inherits every power your own code has.
- Layer defences so a single failure isn't a breach.
- Headers scale better than fixes, because they cover routes you forgot.
-
3.2Cross-site Scripting (XSS)
Untrusted text becomes executable code, and that code owns the user's session.
- Any path from untrusted input to an HTML/JS sink is XSS.
- Escape on output and by context; sanitize with an allowlist when markup is required.
- CSP and
HttpOnlylimit the damage of the injection you didn't catch.
-
3.3iFrame Protection
Two problems: who may frame you, and what the frames you embed are allowed to do.
frame-ancestorscontrols who frames you;sandboxandallowcontrol what you frame.- Never combine
allow-scriptswithallow-same-originon untrusted content. - Always validate
event.origininpostMessagehandlers.
-
3.4Security Headers
Six response headers that close whole attack classes without touching application code.
- Headers are the highest-leverage security work: config, not code, applied everywhere.
- A CSP containing
'unsafe-inline'is theatre. - Roll out report-only first, then enforce.
-
3.5Client-side Security
The client is fully controlled by the user — it can enforce nothing and keep no secret.
- Every client-side check is a hint; every server-side check is the rule.
- Nothing shipped to the browser is secret — proxy anything that must stay private.
- Token storage is a trade between XSS exposure and CSRF exposure;
HttpOnly+SameSitehandles both.
-
3.6Secure Communication (HTTPS)
TLS gives you three things at once: encryption, integrity, and proof of who you're talking to.
- Encryption, integrity, identity — three guarantees from one protocol.
- HSTS closes the plaintext gap that a redirect alone leaves open.
- HTTPS protects the wire; it says nothing about what either end does with the data.
-
3.7Dependency Security
You wrote 5% of the code you ship — the other 95% is your attack surface too.
- Transitive dependencies are the real surface, and install scripts run before any code review.
- Lockfile, CI audit gate, automated patches, fewer packages.
- Vet on maintainer health, not just on GitHub stars.
-
3.8Compliance & Regulation
Consent gates data collection, and the frontend is where that gate is actually built.
- Consent is a load-time gate, not a runtime flag.
- IP addresses, device ids, and cookie ids are personal data.
- Export and delete are features you build, and refusal must be as easy as acceptance.
-
3.9Input Validation and Sanitization
Validate shape on the way in, escape by context on the way out — they are different jobs.
- Validation rejects; sanitization modifies. Only rich text needs the second.
- Allowlists survive attacker creativity; blocklists don't.
- Escape at the sink, because the sink defines what "safe" means.
-
3.10Server-Side Request Forgery (SSRF)
Your server fetches a URL the attacker chose, from inside your trusted network.
- SSRF turns your server into the attacker's proxy inside the perimeter.
- Validate the resolved IP right before connecting, and again after each redirect.
- Cloud metadata endpoints are the classic target — block them at the network too.
-
3.11Server-side JavaScript Injection (SSJI)
User input reaches an evaluator on your Node server, and now they own the process.
- Any string-to-code path on the server is a full compromise, not a bug.
- Replace evaluators with parsers, and shell strings with argument arrays.
vmis isolation, not security; use process-level sandboxing if you must.
-
3.12Feature Policy | Permissions-Policy
Switch off browser capabilities your app never uses — for your code and every frame you embed.
- Everything is enabled by default; the header is how you opt out.
- Frames inherit a narrowed set — a parent can never grant what it doesn't have.
- Blocked APIs fail silently, so verify with real feature tests.
-
3.13Subresource Integrity (SRI)
Pin a cryptographic hash to every CDN script, so altered code simply refuses to run.
- One attribute turns "trust the CDN" into "trust this exact file".
crossorigin="anonymous"is mandatory or verification can't happen.- SRI covers tampering in transit; it can't validate code that was hostile from the start.
-
3.14Cross-Origin Resource Sharing (CORS)
The browser blocks cross-origin reads by default; CORS is the server's way of granting an exception.
- Same origin = scheme + host + port, all three.
- Non-simple requests (including any JSON body) pay a preflight; cache it with
Max-Age. - CORS protects the user's data from other sites — it never protects your API from a direct client.
-
3.15Cross-Site Request Forgery (CSRF)
Another site makes the user's browser send an authenticated request — cookies attach automatically.
- CSRF abuses automatic cookie attachment; it needs the write, not the read.
SameSite=Laxcloses most of it; tokens cover the rest.- Header-based auth avoids CSRF but moves the risk to XSS — pick deliberately.
Capstone
Write a Content-Security-Policy for a React app that loads Google Fonts, Stripe Checkout, and Sentry — then explain every directive.