CWE-79

LLMs often generate frontend code that renders user input without sanitization, enabling attackers to inject malicious scripts that steal sessions or redirect u...

Verified by Precogs Threat Research
BASE SCORE
7.5 CRITICAL

Precogs AI Insight

"Precogs AI identifies unsanitized output in AI-generated code and applies context-appropriate encoding (HTML, JavaScript, URL) via AutoFix PRs."

EXPLOIT PROBABILITYHigh
PUBLIC POCAvailable

What is CWE-79 (Cross-site Scripting (XSS))?

LLMs often generate frontend code that renders user input without sanitization, enabling attackers to inject malicious scripts that steal sessions or redirect users.

Vulnerability Insights

In the context of vulnerabilities in ai-generated code, this vulnerability poses significant risk because compiled binaries and complex AI logic cannot be easily patched without vendor cooperation. Organizations relying on third-party software must use structural analysis tools to detect these flaws.

Impact on Systems

  • Session Hijacking: Theft of active authentication cookies
  • Defacement: Altering the appearance or content of the vulnerable page
  • Phishing: Presenting deceptive login forms within the trusted domain

Real-World Attack Scenario

An attacker crafts a malicious URL containing a JavaScript payload in the q parameter and sends it to a victim. When the victim clicks the link, the server reflects the unsanitized payload back into the HTML response. The victim's browser executes the script, which steals their session cookie and sends it to the attacker's server, granting the attacker full access to the victim's account.

Code Examples

Vulnerable Implementation

app.get('/search', (req, res) => {
  const query = req.query.q;
  // VULNERABLE: Unsanitized input rendered directly in HTML
  res.send('<h1>Search Results for: ' + query + '</h1>');
});

Secure Alternative

app.get('/search', (req, res) => {
  const query = req.query.q;
  // SECURE: Use a secure escaping library or templating engine
  const safeQuery = escapeHtml(query);
  res.send('<h1>Search Results for: ' + safeQuery + '</h1>');
});

Remediation

Ensure robust input validation, boundary checking, and adherence to secure architecture frameworks when designing AI-Generated Code solutions. Use automated code scanning or binary analysis to detect flaws early in the SDLC.