Skip to content

Interview question · Application & API Security

Explain XSS and how contextual output encoding and CSP prevent it.

'Sanitise input' is the answer that fails. The interviewer wants encoding at the point of output, for the specific context, with CSP behind it.

easyEssentialNewPreparation tags: Google, Meta, MicrosoftUpdated Nov 2026

TL;DR: Cross-site scripting is attacker-controlled content executing as script in your origin, with the victim's session: stored (saved and served to others), reflected (echoed from a request) or DOM-based (client code moving data into a dangerous sink). Prevent it by encoding at output for the exact context (HTML text, attribute, URL, JavaScript), which frameworks do by default unless bypassed; by sanitising with an allowlist when HTML must be rendered; and by a strict, nonce-based CSP that blocks injected scripts as a second layer.

How to approach it

Define XSS by its impact, give the three kinds, then explain why encoding happens at output and depends on context. Close with CSP's role and its limits.

A strong answer

FIG · XSS DEFENCE · ENCODE FOR THE CONTEXT, CONSTRAIN WITH CSP
Layers from where data is written to what the browser may run
  1. 1

    1 · Contextual encoding

    At output, per context

    • HTML body, attribute, URL, JavaScript each differ
    • Framework auto-escaping by default
  2. 2

    2 · Safe sinks

    Avoid raw HTML APIs

    • textContent over innerHTML; sanitise rich text
  3. 3

    3 · Strict CSP

    Nonce or hash based

    • Blocks injected inline script
  4. 4

    4 · Cookie hardening

    Limit what script can steal

    • HttpOnly; script can still act as the user

Impact. Script in your page can do anything the user can: call APIs with their cookies, read the page, change what they see, capture input. HttpOnly cookies prevent reading the session cookie, not acting as the user.

Encode at output, by context. The same data needs different treatment depending on where it lands:

ContextDangerEncoding
HTML body<script>, event-handler attributesHTML-entity encode
Quoted attributeBreaking out with a quoteAttribute-encode; always quote
URL in href/srcjavascript: URLsValidate scheme (http, https, mailto); percent-encode parameters
Inside <script>Breaking out of strings or the tagAvoid; pass data as JSON in a data attribute

Encoding at input is the wrong place: input may end up in several contexts, and encoding for one breaks another. Frameworks such as React escape interpolated values automatically; the bugs live in bypasses like dangerouslySetInnerHTML, v-html, innerHTML and template |safe filters.

Sanitise when rendering HTML is the feature. Rich text and Markdown produce HTML deliberately. Use a vetted sanitiser such as DOMPurify with a strict allowlist of tags and attributes, disable raw HTML in Markdown renderers, and restrict link schemes.

CSP as defence in depth. A policy like script-src 'nonce-<random>' 'strict-dynamic'; object-src 'none'; base-uri 'none' means injected inline scripts and event handlers do not run, because they lack the nonce. Add img-src restrictions to stop data exfiltration through images. CSP does not fix the bug; it makes many bugs unexploitable. For DOM XSS, Trusted Types enforce safe values at dangerous sinks.

AI chat interfaces. Model output is attacker-influenced whenever the model reads attacker content, so the renderer must treat it like user input: safe Markdown, sanitised HTML, restricted links and images, and stored conversations treated as stored content.

What interviewers probe next

  • "Does CSP stop DOM XSS?" Partly: it blocks injected script execution if unsafe-inline and unsafe-eval are absent, but not all DOM manipulation; Trusted Types cover the sinks.
  • "Is HttpOnly enough?" No; script can still make requests as the user while the page is open.
  • "How do you find XSS in a large React app?" Search for raw-HTML sinks and URL attributes built from data, and test with a browser-based scanner.

What if?

What if · interview follow-ups

Hold your answer as the constraints change

A product review page displays the reviewer's name and comment, both written by users, and some reviews are shown in an admin dashboard too.

  1. Start here

    What forms does cross-site scripting take?

How it's scored

Weak: Says "sanitise user input" and stops, or confuses XSS with CSRF.

Hire: Explains the three kinds, contextual output encoding, and framework escaping and its bypasses.

Strong Hire: Covers URL schemes, sanitisation for rich content, a concrete strict CSP, Trusted Types, and model output in chat UIs.

Common mistakes

  • HTML-encoding at input and storing encoded data.
  • Allowing javascript: links from user or model content.
  • Using unsafe-inline in CSP, which defeats its purpose.
  • Rendering model Markdown with raw HTML enabled.
You just read a free answer. Every topic opens its first 5 answers to anyone. Sign in and the count per topic goes to 10, and we keep track of how far you get in every topic.Google sign-in · free · no payment details
HOW DID IT GO?
0
READING SIGNED OUT

Signing in doubles your free answers, from 5 to 10 per topic, and the site starts remembering you: mastery per topic, bookmarks, and a next-focus recommendation. Free, no card.

Sign in free
UP NEXT ON YOUR JOURNEY
DISCUSSION · 0

No comments yet. Share a correction or a different way to answer it.