Web security for front-end developers: XSS, CSRF, CORS and CSP – Front End interview prep
261 interview-style multiple-choice questions on Web security for front-end developers: XSS, CSRF, CORS and CSP, organized by topic: concepts, scenarios, code to read and tradeoffs, each with a short explanation. Learn the 62 key terms with the flashcards first, then test yourself.
Take the practice test · Flashcards · All study tools
What it covers
- 13.1 XSS attacks, sources and sinks
- 13.2 Safe rendering, encoding and sanitization
- 13.3 Cookies, CSRF and cross-site requests
- 13.4 Same-origin policy and CORS
- 13.5 Content Security Policy and security headers
- 13.6 Authentication and tokens in the browser
- 13.7 Dependencies, third-party scripts and supply chain
- 13.8 Clickjacking, iframes and cross-window attacks
- 13.9 Secure client-side coding and common pitfalls
Key terms
13.1 XSS attacks, sources and sinks
- XSS
- Cross-site scripting: injected script running in a victim's browser
- Output encoding
- Escaping data for the context where it is inserted
- DOMPurify
- Library that sanitizes untrusted HTML
- DOM-based XSS
- XSS that happens entirely in client-side JavaScript
- Source
- Where attacker data enters, like the URL
- Sink
- An API that can run or render data as code
- DOM XSS
- Client script moves untrusted data into a sink
- mXSS
- Sanitized markup that mutates into script on re-parse
- DOM clobbering
- HTML `id`s or `name`s shadowing globals
13.2 Safe rendering, encoding and sanitization
- Trusted Types
- Makes sinks accept only policy-created values
- Sanitizer API
- Built-in HTML sanitizing
- Dangling markup
- Unclosed tags that leak following content
- Template injection
- User input compiled as a template
13.3 Cookies, CSRF and cross-site requests
- CSRF
- Cross-site request forgery
- SameSite
- Cookie attribute limiting cross-site sending
- HttpOnly
- Cookie attribute that hides it from JavaScript
- Clickjacking
- Tricking users into clicking a framed page
- Double-submit cookie
- Token in both a cookie and the request, compared by the server
- Fetch Metadata
- Request headers describing where a request came from
- Login CSRF
- Forcing a victim to log into the attacker's account
- Cookie tossing
- A subdomain overriding a parent-domain cookie
13.4 Same-origin policy and CORS
- Origin
- Scheme, host and port of a URL
- CORS
- Cross-Origin Resource Sharing headers
- Preflight
- OPTIONS request a browser sends before some cross-origin calls
- Access-Control-Allow-Origin
- Header naming which origin may read a response
- Vary: Origin
- Tells caches the response differs by origin
- Expose-Headers
- Lets script read extra response headers
- Tainted canvas
- A canvas with unapproved cross-origin pixels
- CSWSH
- Cross-site WebSocket hijacking
13.5 Content Security Policy and security headers
- CSP
- Content Security Policy: allowlist of content sources
- Nonce
- Random per-response value that marks allowed scripts
- HSTS
- Header forcing HTTPS for a domain
- SRI
- Subresource Integrity: hash check for external files
- strict-dynamic
- Lets trusted scripts load others, ignoring allowlists
- Report-Only
- Reports CSP violations without blocking
- nosniff
- Stops MIME type guessing
- Permissions-Policy
- Limits browser features a page may use
- CORP
- Restricts which origins can embed a resource
- HSTS preload
- A browser-shipped list of HTTPS-only sites
13.6 Authentication and tokens in the browser
- BFF (for auth)
- A server-side component that holds tokens for an SPA
- DPoP
- Binds a token to a client key
- Conditional mediation
- Offering passkeys in autofill
- User enumeration
- Learning which accounts exist
- FedCM
- Browser-mediated identity provider sign-in
- Storage Access API
- Lets embedded content ask for its cookies
13.7 Dependencies, third-party scripts and supply chain
- Typosquatting
- Malicious packages named like popular ones
- Dependency confusion
- A public package shadowing a private name
- Provenance
- Proof of which repo and build made a package
- Trusted publishing
- Publishing from CI with OIDC, no long-lived token
- Web skimming
- Script stealing card data from checkout pages
- minimumReleaseAge
- Waits before installing new versions
13.8 Clickjacking, iframes and cross-window attacks
- frame-ancestors
- CSP directive listing who may frame a page
- noopener
- New window gets no `window.opener`
- XS-Leaks
- Side channels revealing cross-site state
- Subdomain takeover
- Claiming a service a dangling DNS record points to
- MessageChannel
- A private two-port messaging channel
13.9 Secure client-side coding and common pitfalls
- Open redirect
- Redirecting to any user-supplied URL
- Prototype pollution
- Injecting `__proto__` keys to affect all objects
- ReDoS
- Regex backtracking that freezes the thread
- Homograph attack
- Look-alike characters spoofing a domain
- security.txt
- How to report vulnerabilities to a site
- Web cache deception
- Getting a private page cached as static
Sample practice questions
What is cross-site scripting (XSS)?
- A bug where one site's CSS leaks into another site
- Untrusted input running as script in other users' browsers
- An attack that floods a server with junk requests
- A script that legally loads files from another website
Answer: B. Untrusted input running as script in other users' browsers Injected script runs with the page's privileges and can steal data or act as the user.
Why does React usually protect against XSS when rendering `{userInput}`?
- React blocks all user input from reaching the page
- React encrypts strings before rendering them
- React renders everything inside a sandboxed iframe
- React escapes text, so markup in the string shows as text
Answer: D. React escapes text, so markup in the string shows as text `dangerouslySetInnerHTML` and `href` values like `javascript:` URLs bypass this protection.
How does an `HttpOnly` cookie reduce the damage of an XSS attack?
- It stops the cookie from ever being sent over HTTP
- It stops the injected script from running at all
- Scripts can't read it, so injected code can't steal it
- It encrypts the page so the script can't see it
Answer: C. Scripts can't read it, so injected code can't steal it Injected script can still make requests as the user, so XSS remains serious.
Which pair of URLs is same-origin?
- `https://example.com` and `http://example.com`
- `https://app.example.com/a` and `https://app.example.com/b?x=1`
- `https://example.com` and `https://example.com:8443`
- `https://example.com` and `https://api.example.com`
Answer: B. `https://app.example.com/a` and `https://app.example.com/b?x=1` Origin is scheme + host + port; path and query don't matter.
What does a Content Security Policy (CSP) do?
- Encrypts all traffic between browser and server
- Checks user passwords for strength on sign-up
- Blocks search engine bots from crawling the site
- Tells the browser which content sources are allowed
Answer: D. Tells the browser which content sources are allowed A strict CSP can stop injected inline scripts from running.
What does a backend-for-frontend (BFF) do for SPA authentication?
- Stores the user's password in the browser for faster sign-ins
- Replaces the identity provider with a local user database
- Sends access tokens to the SPA inside the page's HTML each time
- Keeps tokens on the server and gives the browser an `HttpOnly` cookie
Answer: D. Keeps tokens on the server and gives the browser an `HttpOnly` cookie Token theft through XSS gets much harder.
What does Subresource Integrity (SRI) protect against?
- Slow downloads of large script files served from a CDN
- A CDN file being altered, by checking it against a hash
- Third-party scripts that use too much memory at runtime
- Other sites hotlinking or copying your published scripts
Answer: B. A CDN file being altered, by checking it against a hash If the hash doesn't match, the browser refuses to run the file.
What is clickjacking?
- Framing your site invisibly so users click it unknowingly
- A bug that makes a page's buttons unclickable
- Stealing clicks from the ads shown on a page
- Tracking every mouse movement with JavaScript
Answer: A. Framing your site invisibly so users click it unknowingly Prevent it with CSP `frame-ancestors` or the `X-Frame-Options` header.
Why is putting an API secret key in front-end JavaScript a mistake?
- Minification corrupts long strings like keys
- Browsers delete secrets whenever the page reloads
- Anyone can read the bundle, so the key is public
- It makes the JavaScript bundle too large
Answer: C. Anyone can read the bundle, so the key is public Keep secrets on a server and expose only scoped, public keys to the client.
Why is this code dangerous? ```js el.innerHTML = "Hello " + new URLSearchParams(location.search).get("name"); ```
- A crafted `name` can inject HTML with a handler that runs script
- It only fails in strict mode, so behavior is inconsistent
- `URLSearchParams` isn't supported in modern browsers
- `innerHTML` can't hold plain text, so the page breaks
Answer: A. A crafted `name` can inject HTML with a handler that runs script Use `textContent` for text, or sanitize HTML with a vetted library like DOMPurify.
More Front End
- HTML semantics & accessibility
- CSS: box model, Flexbox, Grid
- JavaScript fundamentals
- Async JavaScript & the event loop
- TypeScript
- React: components, hooks & rendering
- State management: Redux, Zustand, context
- Angular and Vue basics
- Browser internals & rendering
- Web performance
- Build tools & bundlers
- Front-end testing
- Mobile & cross-platform