Chapter 3 · Security
Interview drill
15 questions from this chapter. Answer each one out loud first, then open it to compare.
-
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.
-
3.2"React escapes everything. Are you safe from XSS?"
- No —
dangerouslySetInnerHTML,href={userInput}with ajavascript:URL, andrefDOM 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.
- No —
-
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, plusSameSite=Laxcookies so framed requests don't carry the session. - If embedding is a product requirement, allowlist specific parents and require confirmation steps for destructive actions.
-
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.
-
3.5"Where do you store the JWT?"
- Not
localStorageif you can avoid it: any XSS reads it instantly. - Prefer an
HttpOnly; Secure; SameSite=Laxcookie, 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.
- Not
-
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,
Securecookies, and eliminate mixed content.
-
3.7"
npm auditreports 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.
-
3.8"Where does compliance touch frontend architecture?"
- Third-party scripts become conditionally loaded modules, so your tag manager and CSP
connect-srcmust 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).
- Third-party scripts become conditionally loaded modules, so your tag manager and CSP
-
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.
-
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.
-
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.
-
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.
-
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-forwhere supported, and prefer self-hosting pinned versions.
-
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.
-
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=Laxcookies, plus a CSRF token on state-changing routes.