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

Last updated November 13, 2026
Steps
- 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.
- 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.
- 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.
- Validate every value against an allowlist: expected type, length and range, and reject anything else. Don't rely on blocking "bad" words.
- 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.
- Never pass model output to a shell. If you must run a program, call it with fixed arguments and pass values as separate parameters.
- 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
- Prompt injection in your app's AI chatbot or AI features
- Rendering user or AI output as raw HTML (XSS)
- SQL built from strings in raw queries (SQL injection)
- Regex denial of service (ReDoS): one crafted input that freezes your app
- OS command injection from unsanitized shell calls
- Server-side template injection from unsanitized template strings
- Prototype pollution from deep-merging user JSON into objects
- Deserializing untrusted data (pickle, unserialize or Java objects)
- NoSQL operator injection: login and search filters built from raw JSON
- AI features that can take actions you never meant to allow (excessive agency)
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 StorenTechVibe 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.