CWE-94: Code Injection
Untrusted input is evaluated as program code, letting an attacker run arbitrary logic in the application.
Also known as: Code injection, Eval injection, Dynamic code execution
Risk
Typically critical. Successful code injection runs arbitrary logic in the application process, usually leading to full compromise.
Definition
What it is
CWE-94 occurs when untrusted input is turned into executable code, through eval, dynamic function creation, or a template that runs input. The interpreter cannot tell intended code from an injected fragment, so the attacker's input executes. Because it runs in the application process, the attacker gains the application's capabilities. The fix is to stop evaluating input and use a safe parser or sandbox instead.
Use-cases
How it shows up in real code
Formula field evaluates code
A spreadsheet-like feature evaluates a user formula with eval. An attacker enters code instead of a formula and it executes on the server.
Plugin or rules engine
A rules engine compiles user text into executable code. A crafted rule runs arbitrary logic in the host process.
Template renders user input
User input is concatenated into template source rather than passed as data, so an injected directive executes during rendering.
Dynamic config as code
A setting is parsed with a language eval for convenience. An attacker who controls the setting gains code execution.
AI risk
Why AI-generated code has increased the exposure
- Asked to make behavior configurable or to evaluate an expression, assistants reach for eval, Function, or a template that executes input, because it is the shortest solution that works in the demo.
- Models learned from examples that use eval for parsing, math, or dynamic rules rather than a safe parser, so the dangerous pattern is reproduced by default.
- Generated code treats the input as a harmless expression or template, because the prompt described a calculator or rules engine and not an attacker supplying code.
- When scaffolding plugin systems, formula fields, or template rendering, the easiest generated approach feeds user input into an interpreter.
- Every generated feature that evaluates input is an independent execution site, so more generated dynamic features means more places code injection can enter.
Vibe coding
How vibe coding amplifies the risk
- Vibe coding ships the generated eval call unread, so user-controlled code reaches an interpreter in production unnoticed.
- Prompt-and-ship skips the review where an eval on request data would be rejected outright.
- Iterating by prompt extends the existing eval-based approach rather than replacing it with a safe parser.
- Expected inputs render correctly in testing, so the injection stays invisible until someone sends an executable payload.
- Rapid growth multiplies dynamic features faster than review can remove each unsafe evaluation.
Detection
How to find it
- Look for eval, Function, exec, template engines, or deserializers that receive request data.
- Trace every request value that reaches an interpreter or code-generation call and confirm it cannot carry executable content.
- Watch for dynamic template rendering where user input becomes part of the template rather than the data.
- Test with language-specific payloads and confirm they are treated as inert data, not executed.
Remediation
How to fix it
- Remove eval and dynamic code execution on untrusted input; use a dedicated parser or a safe expression library instead.
- For configurable logic, use a sandboxed, non-Turing-complete evaluator with an explicit allow-list of operations.
- Keep user input as data passed to templates, never as part of the template source.
- Validate and constrain any input that drives dynamic behavior against a fixed schema.
- Add a static analysis rule and CI gate that flags eval-like calls on request data.
Frequently asked questions
- What is CWE-94 (code injection)?
- CWE-94, code injection, is when untrusted input is evaluated as program code through eval, dynamic function creation, or a template that executes input, letting an attacker run arbitrary logic in the application process.
- How have AI assistants and vibe coding increased code injection risk?
- Assistants reach for eval or executing templates because they are the shortest solutions that work in the demo, learned from examples that use eval for parsing or rules. Vibe coding ships that call unread, so user-controlled code reaches an interpreter in production, and expected inputs render fine in testing until someone sends a payload.
- How do I fix code injection?
- Remove eval and dynamic execution on untrusted input and use a dedicated parser or safe expression library, sandbox any configurable logic with an allow-list of operations, keep user input as data passed to templates rather than template source, validate against a fixed schema, and add a CI rule flagging eval-like calls.
Related weaknesses
Catch CWE-94 before you ship
Tethlyn Audit scans your codebase and its git history for CWE-94 and the rest of the injection & code vulnerabilities class, then ranks each finding by real exploitability and data sensitivity.