OOmnistu

Blog / Engineering Playbooks

Engineering Playbooks

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.

OOmnistu
Reviewed 2026-11-16 · 4 min read

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.

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)

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

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.

Sources

Previous
A vulnerable dependency was found: what to do next