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

  1. 13.1 XSS attacks, sources and sinks
  2. 13.2 Safe rendering, encoding and sanitization
  3. 13.3 Cookies, CSRF and cross-site requests
  4. 13.4 Same-origin policy and CORS
  5. 13.5 Content Security Policy and security headers
  6. 13.6 Authentication and tokens in the browser
  7. 13.7 Dependencies, third-party scripts and supply chain
  8. 13.8 Clickjacking, iframes and cross-window attacks
  9. 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

  1. What is cross-site scripting (XSS)?

    1. A bug where one site's CSS leaks into another site
    2. Untrusted input running as script in other users' browsers
    3. An attack that floods a server with junk requests
    4. 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.

  2. Why does React usually protect against XSS when rendering `{userInput}`?

    1. React blocks all user input from reaching the page
    2. React encrypts strings before rendering them
    3. React renders everything inside a sandboxed iframe
    4. 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.

  3. How does an `HttpOnly` cookie reduce the damage of an XSS attack?

    1. It stops the cookie from ever being sent over HTTP
    2. It stops the injected script from running at all
    3. Scripts can't read it, so injected code can't steal it
    4. 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.

  4. Which pair of URLs is same-origin?

    1. `https://example.com` and `http://example.com`
    2. `https://app.example.com/a` and `https://app.example.com/b?x=1`
    3. `https://example.com` and `https://example.com:8443`
    4. `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.

  5. What does a Content Security Policy (CSP) do?

    1. Encrypts all traffic between browser and server
    2. Checks user passwords for strength on sign-up
    3. Blocks search engine bots from crawling the site
    4. 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.

  6. What does a backend-for-frontend (BFF) do for SPA authentication?

    1. Stores the user's password in the browser for faster sign-ins
    2. Replaces the identity provider with a local user database
    3. Sends access tokens to the SPA inside the page's HTML each time
    4. 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.

  7. What does Subresource Integrity (SRI) protect against?

    1. Slow downloads of large script files served from a CDN
    2. A CDN file being altered, by checking it against a hash
    3. Third-party scripts that use too much memory at runtime
    4. 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.

  8. What is clickjacking?

    1. Framing your site invisibly so users click it unknowingly
    2. A bug that makes a page's buttons unclickable
    3. Stealing clicks from the ads shown on a page
    4. 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.

  9. Why is putting an API secret key in front-end JavaScript a mistake?

    1. Minification corrupts long strings like keys
    2. Browsers delete secrets whenever the page reloads
    3. Anyone can read the bundle, so the key is public
    4. 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.

  10. Why is this code dangerous? ```js el.innerHTML = "Hello " + new URLSearchParams(location.search).get("name"); ```

    1. A crafted `name` can inject HTML with a handler that runs script
    2. It only fails in strict mode, so behavior is inconsistent
    3. `URLSearchParams` isn't supported in modern browsers
    4. `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.

Practice all 261 questions

More Front End