CWE-94 is a weakness class for improper control of code generation. It covers cases where software builds executable code from untrusted input or unsafely constructed templates, which can lead to code injection, unexpected execution, or privilege abuse if the generated code runs with elevated rights.
How CWE-94 emerges in software
CWE-94 describes a weakness in the control of code generation, where software turns untrusted input, template data, or dynamic expressions into executable logic. The danger is not just malformed output, but the possibility that attacker-controlled content becomes runnable code, changing what the application can execute, access, or modify.
This weakness often appears in templating engines, expression evaluators, plug-in systems, script generation, or any feature that assembles source code or executable instructions at runtime. When the generation boundary is weak, the application can blur data and code, which is the core condition that makes injection-style abuse possible.
Why improper code generation is dangerous
The main risk is that generated code can inherit the privileges of the process that created it. If the application runs as a service account, admin process, or automation worker, a successful injection path may convert a simple input flaw into file access, command execution, data exposure, or broader privilege abuse.
Code generation flaws are especially dangerous because they may bypass security controls that operate only after code has already been assembled. Validation, sanitization, and allowlisting must be applied before generation, not after execution begins, otherwise the application may already have committed to unsafe instructions.
In practice, the impact depends on where the generated code is used. A weak template in a low-privilege feature may cause limited corruption, but the same weakness in a build pipeline, plugin host, or privileged admin console can become a full compromise path.
Common sources and failure patterns
CWE-94 is usually rooted in unsafe transformation rather than a single bad API call. Common patterns include string concatenation to build code, unsafe evaluation of expressions, use of user-controlled variables in templates, and dynamic execution of generated scripts without a strict trust boundary.
It also overlaps with insecure design choices around extensibility. When developers allow rich runtime customization but do not constrain the grammar, permitted operations, or execution context, they create a surface where unexpected code paths can be introduced through ordinary input channels.
For that reason, secure design depends on separating data from executable logic. The safest implementations avoid runtime code synthesis where possible and use narrowly defined parsers, fixed commands, or constrained configuration models instead of free-form code generation.
How organisations reduce exposure
Exposure review starts with locating every place the product generates or interprets code, then checking whether those paths accept user input, partner data, uploaded content, or externally supplied templates. Teams should also identify whether the generated code can reach privileged actions, sensitive data, or outbound network calls.
From a control perspective, organisations should prefer static allowlists, safe template primitives, and execution isolation over generic sanitization alone. Secure coding reviews, build-time analysis, and focused testing should look for places where a harmless-looking field can alter syntax, control flow, or command structure.
When the weakness sits in a platform or library rather than one application, vendor advisories and upstream issue trackers become important because the fix may arrive through dependency updates, configuration changes, or patched parsing rules rather than a simple application change. CWE Top 25 Guide is a useful reference for understanding where this weakness sits among the most dangerous software flaws, and CVE, CWE and CPE Guide helps connect a weakness class to concrete vulnerability tracking and remediation workflows.
Risk and Threat Considerations
Improper code generation is dangerous because it can turn input handling mistakes into executable abuse. The same flaw can produce code injection, unexpected execution, or privilege abuse, especially when generated code runs inside a high-trust service or automation context.
Failure mechanism: The application lets attacker-controlled or untrusted content shape executable syntax, so the generated output is interpreted as code instead of data.
Impact: An attacker may gain code execution, alter application logic, access data beyond intended scope, or pivot into privileged system actions through the compromised execution path.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
OWASP ASVS, NIST SP 800-53 Rev 5 and CIS Controls v8 set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP ASVS | V15 — Secure Coding and Architecture | CWE-94 is a code construction weakness that ASVS addresses through secure design and implementation. |
| Recommendation — Review dynamic code paths under V15 and replace unsafe code synthesis with constrained, validated logic. | ||
| NIST SP 800-53 Rev 5 | SI-10 — Information Input Validation | Untrusted input driving code generation must be validated before it can alter executable behavior. |
| Recommendation — Apply SI-10 to validate inputs before they can shape generated code or executable expressions. | ||
| CIS Controls v8 | CIS-16 — Application Software Security | This weakness is an application security failure that CIS guidance addresses through secure development and testing. |
| Recommendation — Use CIS-16 to identify and remediate unsafe dynamic code generation in applications. | ||
Practitioner Guidance
Why practitioners should care: CWE-94 is a design and implementation problem, not just a validation bug. If a product generates code in multiple places, teams need ownership for each generation path, because one missed boundary can undermine otherwise strong application security controls.
What to watch for: Treat runtime evaluation, template expansion, and dynamic script creation as high-risk by default. The clearest warning sign is any feature where a user can influence syntax, operators, function names, or control flow rather than only values.
Practitioner takeaway: The safest remediation is to reduce or eliminate code generation, then constrain the remaining paths so untrusted input can change data, not executable meaning.
Related resources from NHI Mgmt Group
Deepen Your Knowledge
Free weekly newsletter
Subscribe to the NHI & AI Identity Journal
The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.
Bonus 33% off our NHI Course when you subscribe.
Reviewed and updated by the NHIMG editorial team on November 25, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org