Home / patterns

Running AI or user output with eval, exec or new Function (code injection)

A developer's monitor at night showing a code editor with one highlighted line and a red warning badge, beside an AI assistant chat panel, with a coffee mug and an open notebook on the desk
Quick answer: AI-built apps sometimes take text from a model or a form and run it as code: eval() to compute a formula, new Function() to apply a "rule", exec() or a shell call to run a generated script. It works in the demo. But anyone who can influence that text, including through prompt injection, can make your server run their code. The fix is to never execute generated text, and to turn it into data you check instead.

Last updated November 13, 2026

Steps

  1. Search your code for eval(, new Function(, setTimeout or setInterval with a string, vm.runInContext, child_process exec, Python exec( and eval(, and subprocess calls with shell=True.
  2. For each hit, trace where the string comes from. If any part comes from a user, a URL, a file upload, a database field users can edit, or an AI model's response, treat it as attacker-controlled.
  3. Refactor so the code doesn't need eval at all. Ask the model for structured output (JSON with a fixed schema), then map it to a short list of actions your code already knows how to do.
  4. Validate every value against an allowlist: expected type, length and range, and reject anything else. Don't rely on blocking "bad" words.
  5. If you need math or formulas, use a safe expression parser that only supports the operations you allow, not a language interpreter. In Python, ast.literal_eval parses literals without running code, but limit input size.
  6. Never pass model output to a shell. If you must run a program, call it with fixed arguments and pass values as separate parameters.
  7. Give the AI feature the least access possible, and log what it outputs so unusual responses stand out.

Why it matters

MITRE tracks this as CWE-95 (Improper Neutralization of Directives in Dynamically Evaluated Code, also called Eval Injection): software takes input from an upstream component and uses it in a dynamic evaluation call like eval without neutralizing code syntax. MITRE lists consequences including reading data and files, bypassing protections and executing unauthorized code, and notes injected actions are often unlogged. Its first mitigation is to refactor so eval isn't needed at all. OWASP's LLM05:2025 Improper Output Handling names this exact case as a common example: LLM output entered directly into a system shell or a function like exec or eval, resulting in remote code execution. OWASP says to treat the model like any other user, with a zero-trust approach, and validate its responses before they reach backend functions.

Sources

MITRE CWE-95: Improper Neutralization of Directives in Dynamically Evaluated Code ('Eval Injection') — https://cwe.mitre.org/data/definitions/95.html

OWASP GenAI Security Project: LLM05:2025 Improper Output Handling — https://genai.owasp.org/llmrisk/llm052025-improper-output-handling/

Checked at the source on November 1, 2026.

Related patterns

Found a problem you'd rather not fix yourself?

Tell us what's wrong. A StorenTech engineer reviews your app and replies with a fixed quote, so you know the cost before any work starts.

Get it fixed by StorenTech

Vibe Security Weekly

One email a week with new failure patterns and how to fix them.

By joining you agree we may email you this newsletter. See our privacy policy. Free. Unsubscribe anytime. We'll never sell your email.