FSD Frontend System Design

Chapter 3 · Security

Revision sheet

Every lesson's idea and recap on one page. Read it before an interview, or print it.

  1. 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.
  2. 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 HttpOnly limit the damage of the injection you didn't catch.
  3. 3.3iFrame Protection

    Two problems: who may frame you, and what the frames you embed are allowed to do.

    • frame-ancestors controls who frames you; sandbox and allow control what you frame.
    • Never combine allow-scripts with allow-same-origin on untrusted content.
    • Always validate event.origin in postMessage handlers.
  4. 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.
  5. 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 + SameSite handles both.
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. 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.
  11. 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.
    • vm is isolation, not security; use process-level sandboxing if you must.
  12. 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.
  13. 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.
  14. 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.
  15. 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=Lax closes 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.