Home › Blog › Web security · Guide
XSS in Plain English: How AI-Generated Code Lets It In
Cross‑site scripting (XSS) is a flaw that lets an attacker inject malicious script into a web page viewed by another user. The script runs in the victim’s browser and can read or manipulate data as if it were the legitimate user.
What XSS actually does
When a web page includes a script that an attacker controls, the browser executes that script in the context of the page. This means the script can access everything the page can see – including form fields, cookies, and any data displayed on the screen. The attacker can then steal sensitive information, perform actions on behalf of the user, or redirect the user to a malicious site. Because the code runs in the victim’s browser, traditional server‑side defenses do not see the malicious activity.
XSS appears in three main forms. Stored XSS persists on a server, such as a comment that is later displayed to any visitor. Reflected XSS occurs when user input is immediately echoed in a response, often via a URL parameter. DOM‑based XSS happens entirely in the browser, where client‑side scripts write untrusted data into the page structure. All three give the attacker the ability to run code where the user trusts the site.
Common patterns that introduce XSS in AI‑generated code
Functions like element.innerHTML, document.write, or React’s dangerouslySetInnerHTML take a string and place it straight into the page without any automatic escaping. When the string contains user‑supplied data, the browser treats it as code rather than text, opening the door for XSS.
Another risky pattern is constructing URLs from user input without validation. If a link’s href attribute receives a value that begins with the javascript: scheme, the browser will treat the following text as code to execute.
- element.innerHTML
- document.write
- dangerouslySetInnerHTML
- user‑supplied URLs starting with javascript:
How to avoid raw HTML insertion
The safest approach is to keep raw HTML out of the rendering path entirely. Instead of building HTML strings, use the framework’s templating system or component model, which automatically escapes values. In React, for example, placing a variable inside JSX results in escaped output, preventing script execution. When the visual design truly requires HTML – such as rendering rich text from a CMS – the code should pass the content through a sanitization library that removes dangerous elements and attributes before insertion.
If sanitization is unavoidable, choose a well‑maintained library that follows current standards. The library should strip script tags, event handlers, and any URL schemes that could trigger code execution. After sanitizing, still treat the output as untrusted and avoid combining it with other dynamic content that could re‑introduce risk. Regularly update the library to incorporate the latest security patches.
Validating and protecting URLs
When an application builds links from user input, the first step is to validate that the input conforms to an allowed set of schemes, typically http and https. Reject or rewrite any value that begins with javascript, data, or other executable schemes. Whitelisting known domains can further reduce risk, ensuring that only trusted destinations are reachable from the page.
Beyond validation, adding a Content‑Security‑Policy (CSP) provides a second line of defense. A CSP can block the execution of inline scripts and restrict the sources from which scripts, styles, and frames may be loaded. By configuring the policy to disallow the javascript: scheme in URLs, even a missed validation step will not result in script execution. Combine CSP with HttpOnly cookies to keep session tokens out of reach from any injected script.
- Allow only http and https schemes
- Use a whitelist of trusted domains
- Set a CSP that blocks inline scripts and unsafe URLs
Practical checklist for developers using AI code generators
If the snippet must render HTML, wrap the data in a sanitization step and verify that the sanitization library is up to date. Review any link construction logic to ensure URLs are validated against a safe list and that the javascript: scheme is explicitly rejected.
Finally, configure your server to send a strict Content‑Security‑Policy header and set session cookies with the HttpOnly flag. By treating AI‑generated code as a draft rather than production‑ready, developers can catch these patterns early and keep their applications safe from XSS attacks.
What the documented sources report
Veracode's 2025 GenAI Code Security Report (published July 30, 2025) evaluated over 100 large language models across Java, Python, C# and JavaScript. 45% of code samples failed security tests and introduced OWASP Top 10 vulnerabilities. Security failure rates by language: Java 72%, Python 38%, JavaScript 43%, C# 45%. AI tools failed to defend against cross-site scripting (CWE-80) in 86% of relevant code samples. The report states that models got better at writing functional or syntactically correct code but were no better at writing secure code, and that security performance remained flat regardless of model size or training sophistication.
Source: Veracode, “2025 GenAI Code Security Report” (July 30, 2025)
Veracode's Spring 2026 update (published March 24, 2026) evaluated 80 coding tasks across Java, JavaScript, C# and Python and four vulnerability types: SQL injection, cross-site scripting, log injection and insecure cryptographic algorithms. In each task a model completes a function from a comment, and the requested functionality can be implemented in either a secure or an insecure way; Veracode then runs its static analysis (SAST) tool on the generated code. The overall security pass rate was about 55%, against syntax correctness above 95%. Pass rates by vulnerability type: SQL injection 82%, insecure cryptographic algorithms 86%, cross-site scripting 15%, log injection 13%. Pass rates by language: Python 62%, C# 58%, JavaScript 57%, Java 29%.
Source: Veracode, “Spring 2026 GenAI Code Security Update” (March 24, 2026)
Common questions
Can React’s default escaping prevent all XSS attacks?
React escapes values automatically, but using dangerouslySetInnerHTML or inserting raw HTML bypasses that protection. Those methods must be avoided or paired with proper sanitization.
What is the simplest way to check if a URL is safe?
Validate that the URL starts with http or https and matches a whitelist of allowed domains. Reject any URL that begins with javascript or other executable schemes.
Do Content‑Security‑Policy headers replace the need for sanitization?
CSP adds a valuable layer of defense, but it does not replace sanitization. Both should be used together to ensure that malicious code cannot execute even if one control fails.