CWE-79: Cross-Site Scripting (XSS)
Untrusted input is rendered into a page without being neutralized, so an attacker's script runs in another user's browser with that user's session.
Also known as: XSS, Cross-site scripting, Script injection, HTML injection
Risk
Typically high. Stored XSS can hijack any user who views the affected content, including admins, leading to session theft, account takeover, and silent actions performed as the victim.
Definition
What it is
CWE-79 occurs when an application includes untrusted data in a web page without proper escaping or sanitization, so the browser executes attacker-supplied markup or script. Reflected XSS bounces input straight back in a response; stored XSS persists the payload (a comment, a profile field, a filename) and fires for everyone who views it; DOM-based XSS happens entirely in client code that writes input into the page. In every form, the attacker's script runs in the victim's browser with the victim's cookies and session.
Use-cases
How it shows up in real code
Stored XSS in a comment
A comment field stores raw HTML. An attacker posts a comment containing a script tag; every user who opens the thread runs it, and their session token is sent to the attacker.
Reflected XSS in a search result
A search page echoes the query back into the HTML unescaped. A malicious link with a scripted query, sent to a victim, runs code in their browser the moment they click.
DOM XSS from a URL fragment
Client code reads a value from the URL and writes it into the page with innerHTML. An attacker crafts a URL whose fragment contains markup, and the script executes without the server ever seeing it.
Dangerous raw-HTML rendering
A component renders user content with a raw-HTML escape hatch (such as dangerouslySetInnerHTML) to support formatting. Any markup the user supplies, including script, is injected verbatim.
AI risk
Why AI-generated code has increased the exposure
- Asked to 'render this HTML' or 'show formatted content', assistants readily reach for raw-HTML APIs (innerHTML, dangerouslySetInnerHTML) because they produce the requested visual result with the least code.
- Generated UI code often interpolates values straight into markup or templates without an escaping or sanitization step, since the prompt asked for display, not safety.
- Models trained on example code reproduce the common unescaped patterns; safe output encoding is less visually obvious in training data than the direct approach.
- When a feature needs rich text, the assistant tends to suggest rendering raw HTML rather than wiring up a vetted sanitizer, which is more setup than a quick demo wants.
- More generated views and components means more output sinks, each an independent opportunity for unescaped data to reach the DOM.
Vibe coding
How vibe coding amplifies the risk
- Vibe coding renders whatever the model produced. If a component writes user input into the page unescaped, the XSS sink ships without anyone reviewing the output path.
- A raw-HTML escape hatch added by the assistant to 'make the formatting work' survives to production because prompt-and-ship skips the security review that would question it.
- Rich-text and user-content features are exactly the ones people vibe-code, and exactly the ones that need a sanitizer, which rarely gets added when the diff is unread.
- Honest test input renders fine during vibe-coded testing, so the vulnerability stays invisible until a crafted payload arrives.
- Fast, prompt-driven UI growth outpaces manual review of every place input reaches the page.
Detection
How to find it
- Find places where request or user data is written into HTML, attributes, or script contexts without output encoding appropriate to that context.
- Flag every raw-HTML sink (innerHTML, document.write, dangerouslySetInnerHTML, template `|safe`/`raw` filters) and confirm the content is sanitized before it reaches them.
- Trace stored fields (comments, profiles, filenames) from write to every place they are later rendered, since stored XSS fires far from where the data entered.
- Test with payloads containing angle brackets and script and confirm they appear as text, not as executing markup.
Remediation
How to fix it
- Encode output for its context, HTML, attribute, JavaScript, URL, using the framework's built-in escaping, and let the templating layer escape by default.
- Avoid raw-HTML sinks; when rich text is required, run it through a maintained HTML sanitizer with a strict allow-list before rendering.
- Set a Content-Security-Policy that blocks inline script, so an injected payload cannot execute even if one slips through.
- Set cookies HttpOnly so a successful XSS cannot read the session token from JavaScript.
- Add a lint rule and CI gate flagging raw-HTML sinks so new XSS sinks fail the build.
Frequently asked questions
- What is CWE-79 (cross-site scripting)?
- CWE-79 is when an application puts untrusted data into a web page without escaping or sanitizing it, so the browser runs an attacker's script in another user's session, enabling session theft, account takeover, and actions performed as the victim.
- How have AI assistants and vibe coding increased XSS risk?
- Assistants readily use raw-HTML APIs and interpolate values straight into markup because that produces the requested visual result with the least code, and they learned these patterns from training data. Vibe coding ships those output sinks unread, and the raw-HTML escape hatch added to make formatting work survives because no one reviews the diff.
- How do I fix cross-site scripting?
- Encode output for its context using the framework's built-in escaping, avoid raw-HTML sinks or run content through a maintained sanitizer with a strict allow-list, add a Content-Security-Policy that blocks inline script, set cookies HttpOnly, and add a CI rule that flags raw-HTML sinks.
Related weaknesses
Catch CWE-79 before you ship
Tethlyn Audit scans your codebase and its git history for CWE-79 and the rest of the injection & code vulnerabilities class, then ranks each finding by real exploitability and data sensitivity.