--- name: sh-kb-xss description: "Knowledge base for finding Cross-Site Scripting - reflected, stored, and DOM-based XSS. Use when hunting XSS or reviewing untrusted data rendered into HTML/JS/attributes/URLs without context-correct encoding, or sanitizer and framework escape-hatch misuse. CWE-79, OWASP A03:2021-Injection." --- # Cross-Site Scripting (XSS) - Hunter Knowledge Base Untrusted input is rendered into a page such that the browser executes it as script. Impact runs in the victim's session/origin: session theft, action-on-behalf, credential capture, worming. ## Sub-classes - **Reflected**: input echoed straight back in the response (needs a crafted link/request). - **Stored**: input persisted then rendered to other users (worst - hits many victims, incl. admins). Includes "blind" stored XSS rendered only in back-office tools, log viewers, support consoles, exports and HTML emails. - **DOM**: client JS writes attacker data into a dangerous DOM sink without encoding (server never sees it). ## Sources Request params/body/headers/cookies/URL fragment (`location.hash`/`search`/`pathname`), `window.name`, `postMessage` (handlers lacking an `event.origin` check), stored values (profile, comments, filenames, `User-Agent`/`Referer` echoed into admin log views), `document.referrer`, any DB/API/third-party value that was attacker-influenced, uploaded files served inline (SVG, HTML, XML). ## Sinks (grep targets) - **Server templates (raw output)**: Jinja `| safe`, `{% autoescape false %}`, `Markup(`, `render_template_string` with input; Django `mark_safe`, `|safe`, `format_html` with unescaped args, `HttpResponse(user)`; ERB `raw`/`html_safe`, Rails `link_to user_url` (allows `javascript:`); Handlebars `{{{ }}}`; Laravel Blade `{!! !!}`; PHP `echo $_GET[..]` without `htmlspecialchars(..., ENT_QUOTES)`; Go `template.HTML(`/`template.JS(`/`template.URL(`/`text/template`; Thymeleaf `th:utext`; JSP `${param.x}` or `<%= %>` without `c:out`; ASP.NET `Html.Raw`, `Response.Write`, Blazor `MarkupString`; Express `res.send(`/`res.write(` with input and an HTML content type; Svelte `{@html}`. - **DOM (JS)**: `innerHTML`, `outerHTML`, `insertAdjacentHTML`, `document.write`, `srcdoc`, `eval`, `setTimeout(str)`, `Function(`, `element.setAttribute('href'|'src', userUrl)`, `location = userStr`, `a.href = userStr`, `window.open(userStr)`, jQuery `.html()`/`.append(userStr)`/`$(userStr)`, React `dangerouslySetInnerHTML` and `href={userUrl}` (React does not block `javascript:`), Vue `v-html` and `:href`, Angular `bypassSecurityTrust*` and runtime-compiled templates, markdown renderers with raw HTML enabled (`marked`, `markdown-it` with `html: true`) whose output is not sanitized. - **Attribute/JS context**: input into an inline event handler, ``), `href="javascript:"`, `style`, unquoted attribute values. ## Detection recipe 1. `graft grep "innerHTML|outerHTML|dangerouslySetInnerHTML|v-html|@html|\| ?safe|mark_safe|html_safe|raw\(|Html\.Raw|template\.(HTML|JS|URL)|document\.write|insertAdjacentHTML|th:utext|\{!!|bypassSecurityTrust|srcdoc" --json`. 2. Trace each sink back to an untrusted source (server: entry point; DOM: `location`/`postMessage`/stored API data). 3. Determine the **output context** (HTML body / attribute / JS / URL / CSS) - encoding must match context; a value HTML-encoded but placed in a JS or URL context is still vulnerable. 4. For stored data, find **every** place the value is rendered (other users' views, admin pages, emails, PDF/HTML exports); safe in one view does not mean safe in all. 5. For links built from data (`href`, `src`, `action`, redirects), check scheme allowlisting (`http`/`https` after parsing), not a prefix test. ## Payloads / PoC - Probe: ``, `">`, `'-alert(1)-'` (JS string context), `javascript:alert(1)` (href), ``. - DOM: `#` in the fragment when `location.hash` flows to `innerHTML`. - Note `