Static analysis (SAST) check · JavaScript

Llm output to sink from request input (JavaScript)

Text generated by a language model reaches code execution, a shell command, a SQL query or raw HTML.

Why it matters

Model output follows instructions planted in its input (prompt injection), so treat it as untrusted user input.

How to fix it

Never eval or shell out model output. Parse it into a fixed structure (JSON schema, an enum of allowed actions), pass values to SQL as bound parameters, and render it as text (textContent, JSX children) or through a sanitiser such as DOMPurify.

Example

The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.

Vulnerable: reported (JavaScript)
import OpenAI from "openai";

const client = new OpenAI();

export async function renderAnswer(el, question) {
  const resp = await client.chat.completions.create({
    model: "gpt-4o-mini",
    max_tokens: 300,
    messages: [{ role: "user", content: question }],
  });
  el.innerHTML = resp.choices[0].message.content;
}
Fixed: not reported (JavaScript)
import OpenAI from "openai";

const client = new OpenAI();

export async function renderAnswer(el, question) {
  const resp = await client.chat.completions.create({
    model: "gpt-4o-mini",
    max_tokens: 300,
    messages: [{ role: "user", content: question }],
  });
  el.textContent = resp.choices[0].message.content;
}

What TrustIvan reads

Runs on repository scans of JavaScript source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.

Weakness classes and references

Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10

All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation

See TrustIvan on your own estate.

Book thirty minutes with an engineer and bring an image, a repository or a cloud account, or start free on the hosted service.