FSD Frontend System Design

Chapter 3 · Security

Interview drill

15 questions from this chapter. Answer each one out loud first, then open it to compare.

  1. 3.1"How do you think about security on the frontend?"
    • Name the surfaces — injection, third-party code, embedding, transport, and the client's inability to keep secrets.
    • Say the frontend can only reduce risk; authorisation is always re-checked server-side.
    • Lead with the two highest-leverage moves: contextual output escaping and a strict CSP.

    Review 3.1 Security Overview →

  2. 3.2"React escapes everything. Are you safe from XSS?"
    • No — dangerouslySetInnerHTML, href={userInput} with a javascript: URL, and ref DOM writes all bypass it.
    • Server-rendered JSON embedded in a <script> tag is a second common hole.
    • Layer it: escape by context, sanitize rich text with an allowlist, and ship a CSP for the case you missed.

    Review 3.2 Cross-site Scripting (XSS) →

  3. 3.3"Your dashboard can be embedded on any site. What's the risk and the fix?"
    • Clickjacking: an invisible overlay turns a real user click into a real destructive action.
    • Fix with frame-ancestors, plus SameSite=Lax cookies so framed requests don't carry the session.
    • If embedding is a product requirement, allowlist specific parents and require confirmation steps for destructive actions.

    Review 3.3 iFrame Protection →

  4. 3.4"You ship a CSP and your payment widget stops loading. What now?"
    • Read the console violation: it names the directive and the blocked origin.
    • Add that specific origin to the specific directive — never widen to * or add 'unsafe-inline'.
    • Long term, run report-only in staging so violations surface before release, not after.

    Review 3.4 Security Headers →

  5. 3.5"Where do you store the JWT?"
    • Not localStorage if you can avoid it: any XSS reads it instantly.
    • Prefer an HttpOnly; Secure; SameSite=Lax cookie, which XSS cannot read, and handle CSRF separately.
    • Keep access tokens short-lived with server-side refresh rotation, so a stolen token has a small window.

    Review 3.5 Client-side Security →

  6. 3.6"Site is HTTPS. Is the data safe?"
    • In transit, yes: encrypted, tamper-evident, and the server's identity is verified.
    • Not at rest, not in the browser, and not against XSS — TLS protects the pipe, not the endpoints.
    • Add HSTS for the first-request gap, Secure cookies, and eliminate mixed content.

    Review 3.6 Secure Communication (HTTPS) →

  7. 3.7"npm audit reports 40 vulnerabilities. What do you do?"
    • Triage by reachability first: a dev-only build tool's ReDoS is not a production incident.
    • Fix anything reaching production or with network/filesystem access; patch the rest on a schedule.
    • Then fix the process — audit gate in CI, automated patch PRs, and lockfile discipline.

    Review 3.7 Dependency Security →

  8. 3.8"Where does compliance touch frontend architecture?"
    • Third-party scripts become conditionally loaded modules, so your tag manager and CSP connect-src must both be consent-aware.
    • Analytics must degrade gracefully — the product works fully for users who refuse.
    • PII must be scrubbed before it reaches logging and error reporting (see Chapter 7).

    Review 3.8 Compliance & Regulation →

  9. 3.9"You sanitize on input. Why is that not enough?"
    • The safe form depends on the destination — the same string needs different treatment in HTML, a URL, and SQL.
    • Escaping early corrupts the stored data and double-escapes when rendered elsewhere.
    • Correct order: validate shape on input, store raw, escape at each output sink.

    Review 3.9 Input Validation and Sanitization →

  10. 3.10"Where does SSRF show up in a frontend-facing feature?"
    • Anywhere the user supplies a URL your backend then fetches: link previews, imports, webhooks, avatar-by-URL, PDF/screenshot renderers.
    • Headless-browser screenshot services are especially exposed — they follow redirects and run JS.
    • The fix is resolve-then-validate plus network egress restrictions, not a regex on the string.

    Review 3.10 Server-Side Request Forgery (SSRF) →

  11. 3.11"How is SSJI different from XSS?"
    • XSS executes in one user's browser inside the origin sandbox; SSJI executes on your server with no sandbox at all.
    • Impact: session theft versus environment variables, database credentials, and lateral movement.
    • Both come from the same root cause — data reaching an interpreter — but SSJI has no recoverable blast radius.

    Review 3.11 Server-side JavaScript Injection (SSJI) →

  12. 3.12"Why restrict features you never call?"
    • If XSS or a compromised third-party script lands, those APIs are available to it under your origin's trust.
    • Permission prompts show your domain, so abuse damages your reputation directly.
    • It's a one-line header covering every route, including ones added after you wrote it.

    Review 3.12 Feature Policy | Permissions-Policy →

  13. 3.13"Does SRI prevent supply-chain attacks?"
    • It prevents delivery-time tampering — a compromised CDN or MITM serving different bytes.
    • It does not help if the package was already malicious when you hashed it (see 3.7) or if the URL is versionless.
    • Pair it with CSP require-sri-for where supported, and prefer self-hosting pinned versions.

    Review 3.13 Subresource Integrity (SRI) →

  14. 3.14"You get a CORS error. Is it a frontend bug?"
    • No — CORS is granted by the server, so the fix is a response header, not client code.
    • Read the exact message: disallowed origin, disallowed header, or missing credentials support each need a different header.
    • The clean production answer is often to remove the cross-origin call entirely by proxying the API under the same origin.

    Review 3.14 Cross-Origin Resource Sharing (CORS) →

  15. 3.15"Are you safe from CSRF if your API uses JWT in an Authorization header?"
    • Structurally yes, because the browser never attaches that header automatically.
    • But storing the JWT where JS can read it hands it to any XSS — you traded one risk for another.
    • The balanced answer: HttpOnly + Secure + SameSite=Lax cookies, plus a CSRF token on state-changing routes.

    Review 3.15 Cross-Site Request Forgery (CSRF) →