Cross-site scripting (XSS): how app teams prevent it
Review safe output handling, context-specific encoding and HTML sanitization to prevent untrusted content from executing in a browser.
XSS is when an app puts untrusted data into a page so that the browser treats it as code. OWASP's XSS Prevention Cheat Sheet says the name is a misnomer: it began as cross-site data theft and now covers injecting almost any content. It can lead to account impersonation, observing user behaviour, loading external content and stealing sensitive data. We read the whole sheet on 15 November 2026 (not the pages it links to). Everything attributed here is OWASP's.
The philosophy: cover every place
Untrusted data is anything your app did not create: form input, URL parameters, stored user data, and responses from other services. OWASP stresses coverage: an app that protects 99 places and misses one is still vulnerable. It recommends the approach many frameworks take, escaping every value they render by default, since that does not depend on correctly deciding which data is untrusted. Prefer safe APIs that treat data as text; use context-specific output encoding where the destination interprets syntax; use a maintained sanitiser when users may write HTML. Input validation can enforce formats but does not replace these.
Frameworks help, with escape hatches
Modern frameworks produce fewer XSS bugs. OWASP lists ways to undo that safety: direct DOM manipulation, React's dangerouslySetInnerHTML without sanitising, unvalidated URL values (it says React 19 blocks javascript: URLs in src and href but this does not replace your own URL validation), Angular's bypassSecurityTrust functions, Lit's unsafeHTML, Polymer's inner-h-t-m-l, template injection and out-of-date plugins. Know where your framework's protection ends.
Output encoding depends on where the data goes
Browsers parse HTML, JavaScript, URLs and CSS differently, so the wrong encoding can leave a hole or break the page.
- HTML body: HTML-encode. To show text from JavaScript, assign to textContent.
- HTML attributes: use attribute encoding for ordinary text attributes, quote all values, keep attribute names fixed. Encoding does not protect JavaScript in event-handler attributes; use addEventListener with trusted functions.
- JavaScript: use a maintained encoder for the exact script context. Never insert untrusted code or expressions, and passing a value to eval() is still unsafe. For JSON, serve application/json, not text/html.
- CSS: do not put untrusted data into CSS source. For dynamic styling use style.setProperty with a fixed property name and an allowlisted value; properties that can load resources (such as background-image) need separate URL validation.
- URLs: encode untrusted URL components, then attribute-encode the resulting URL when inserting it in an HTML attribute. Validate allowed schemes and destinations as well; do not encode an entire arbitrary URL and assume it is safe. In JavaScript use encodeURIComponent for each untrusted value; it does not validate a whole URL.
- Dangerous contexts: directly in a script, in HTML comments, in CSS, as tag or attribute names, in callback functions, and in eval(), setTimeout() or setInterval() strings. OWASP says do not place variables there, since encoding will not fully prevent XSS.
When users write HTML
If users need rich formatting, encoding would break it, so sanitise instead. OWASP recommends DOMPurify. If you change the content after sanitising, or pass it to a library that mutates it, you can void the protection, and you must patch the sanitiser regularly because bypasses are found.
Safe and unsafe sinks
A sink is where a variable lands in the page. Prefer sinks that treat the value as text, such as textContent, insertAdjacentText, className, setAttribute with a safe name, a form field's value, and createTextNode; refactor away from innerHTML. createElement's argument is an element name, not text, so use a constant or allowlist. A dynamically created script element can still run when later code fills and inserts it.
Other controls (extra layers)
- Cookie attributes limit the impact but do not stop malicious content from running.
- Content Security Policy is an allowlist for what can load; mistakes are easy, so it should not be the primary defence (see our CSP article).
- Trusted Types, where supported, make covered DOM sinks reject plain strings; policies must still validate or sanitise.
- WAFs cannot reliably stop all XSS and may miss DOM-based XSS that happens entirely in the browser.
Approaches OWASP calls ineffective
Relying only on CSP headers (browsers support it unevenly, legacy apps struggle, and it should be tailored per app); and relying on HTTP interceptors that encode output, which cannot pick the right context for every path, can double-encode and break rendering, miss DOM-based XSS, and miss data that originates outside your app.
Questions for your team
- Does our framework escape by default, and where do we bypass it?
- Where do we use innerHTML, dangerouslySetInnerHTML or similar?
- Do we let users submit HTML, and which sanitiser do we use and patch?
- Do we depend on CSP or a WAF as our only protection?
Related: DOM-based XSS explained, content security policy basics, input validation basics, CSRF prevention basics.
Limits
One OWASP page, read in full. Its linked references (Java Encoder, DOMPurify, Trusted Types and others) were not read. For browser-side flaws see our DOM-based XSS post. We give no code and no claims about specific libraries beyond what OWASP says. Libraries and framework behaviour change; check current documentation.
By Omnistu.