Security weaknessCWE Top 25 · #10
CWE-94 Code injection
Your application builds and runs code from untrusted input, so attackers can make it execute instructions they wrote.
- Senior security engineers
- 540+ projects delivered
- Reply within one business day
Quick answer
What is CWE-94?
Improper Control of Generation of Code: The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.
Business impact
What CWE-94 could cost your business
MITRE documents what this weakness can do. Here is what that means for a website, store or app.
Confidentiality
Private data exposed
Customer records, credentials, orders or internal files can be read by people who should never see them.
- Execute Unauthorized Code or Commands
Integrity
Data, pages or code changed
Prices, orders, content or the code your site runs can be altered without your approval.
- Execute Unauthorized Code or Commands
Availability
Downtime and lost sales
Crashes, slowdowns or exhausted resources can take your site, store or app offline.
- Execute Unauthorized Code or Commands
Access Control
Accounts and admin areas exposed
Logins and permission checks can be bypassed, putting admin and customer accounts at risk.
- Bypass Protection Mechanism
- Gain Privileges or Assume Identity
Non-Repudiation
Actions you cannot trace
Changes can happen without a reliable record, which slows incident response and compliance work.
- Hide Activities
In plain English
What Code injection means for your business
- 540+projects delivered
- 9+years in business
- 23+certified experts
A senior engineer replies within one business day.
Many languages can turn a piece of text into running code. Functions such as eval do this, and so do template engines, dynamic includes and some plugin systems. Code injection happens when that text contains anything a user can influence. The application treats the input as part of its own program and runs it. The attacker’s code then has the same access as the application: its files, its database connection and its secrets.
For a business, this usually means full compromise of the site or app. Injected code can read customer data, create hidden admin accounts, alter pages or install a backdoor that survives updates. Its actions often go unlogged, which makes cleanup slow and uncertain. The safest position is simple: your application should never build code from outside input. Features that seem to need it can almost always be built another way.
Where it shows up
- PHP code that passes request values to eval, to dynamic include or require statements, or to functions that call another function by name.
- WordPress plugins and themes that offer custom code snippets, formulas or conditional logic and evaluate what an editor or visitor entered.
- Server-side template engines that render a template string containing user input, a pattern known as server-side template injection.
- Node.js or Python services that use eval, the Function constructor or exec to process calculations, filters or configuration sent by clients.
Free security check
Is CWE-94 hiding in your site?
A senior engineer checks your site, store or app for this weakness and the rest of the CWE Top 25, then sends a prioritized fix plan within one business day. No obligation.
Official definition
How MITRE defines CWE-94
Improper Control of Generation of Code
The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.
Also known as: Code Injection
Fix it
How to fix Code injection
Practical steps in priority order. Share them with your developer or agency.
Remove dynamic code evaluation
Refactor so the application never builds code from input. Replace eval with explicit logic, lookup tables or a dedicated parser. For math or rules, use a purpose-built expression library that cannot call arbitrary functions.
Parse data as data
Read structured input with safe parsers such as JSON.parse or json_decode. In Python, use ast.literal_eval for simple literals instead of eval, with a limit on input size. Never evaluate a string to convert it.
Allowlist includes and callbacks
When a request selects a file, class, template or function, map the value to a fixed list defined in code. Reject anything not on the list. Do not concatenate input into include paths or function names.
Keep input out of templates
Pass user content to templates as variables, never as part of the template source. If users must author templates, use a sandboxed, logic-less engine and restrict the objects and functions it can reach.
Sandbox and restrict the runtime
Run the application with least privilege in a container or restricted environment. Disable dangerous functions you do not use, for example through PHP’s disable_functions setting. Make code directories read-only to the web server account.
Signs you may be affected
- Your codebase uses eval or similar functions on values that come from requests or the database.
- Unfamiliar PHP files, admin users or scheduled tasks appear on the site.
- User-supplied text is rendered as a template rather than passed into one as data.
Questions to ask your developer
- Does our code or any plugin use eval or dynamic includes?
- Which features let staff or users enter formulas, snippets or templates?
- Can the web server account modify the application’s own code files?
Prevention
What MITRE recommends, phase by phase
Every mitigation from the CWE entry, in the order a project runs: plan, build, test, operate.
- 01
Architecture and Design
- Refactor your program so that you do not have to dynamically generate code.
- Run your code in a "jail" or similar sandbox environment that enforces strict boundaries between the process and the operating system. This may effectively restrict which code can be executed by your product. Examples include the Unix chroot jail and AppArmor. In general, managed code may provide some protection. This may not be a feasible solution, and it only limits the.
- 02
Implementation
- Assume all input is malicious. Use an "accept known good" input validation strategy, i.e., use a list of acceptable inputs that strictly conform to specifications. Reject any input that does not strictly conform to specifications, or transform it into something that does. When performing input validation, consider all potentially relevant properties, including length, type of.
- For Python programs, it is frequently encouraged to use the ast.literal_eval() function instead of eval, since it is intentionally designed to avoid executing code. However, an adversary could still cause excessive memory or stack consumption via deeply nested structures [REF-1372], so the python documentation discourages use of ast.literal_eval() on untrusted data [REF-1373].
- 03
Testing
- Use dynamic tools and techniques that interact with the product using large test suites with many diverse inputs, such as fuzz testing (fuzzing), robustness testing, and fault injection. The product’s operation may slow down, but it should not become unstable, crash, or generate incorrect results.
- 04
Operation
- Run the code in an environment that performs automatic taint propagation and prevents any command execution that uses tainted variables, such as Perl’s "-T" switch. This will force the program to perform validation steps that remove the taint, although you must be careful to correctly validate your inputs so that you do not accidentally mark dangerous inputs as untainted (see.
- Run the code in an environment that performs automatic taint propagation and prevents any command execution that uses tainted variables, such as Perl’s "-T" switch. This will force the program to perform validation steps that remove the taint, although you must be careful to correctly validate your inputs so that you do not accidentally mark dangerous inputs as untainted (see.
Detection and evidence
How CWE-94 is found, and where it has hit
The testing methods MITRE recommends, and real vulnerabilities this weakness has caused.
How it is detected
- Automated Static AnalysisAutomated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then.
Real vulnerabilities it caused
- CVE-2023-29374Math component in an LLM framework translates user input into a Python expression that is input into the Python exec() method, allowing code execution – one variant of a "prompt injection" attack.
- CVE-2024-5565Python-based library uses an LLM prompt containing user input to dynamically generate code that is then fed as input into the Python exec() method, allowing code execution – one variant of a "prompt injection" attack.
- CVE-2024-4181Framework for LLM applications allows eval injection via a crafted response from a hosting provider.
- CVE-2022-2054Python compiler uses eval() to execute malicious strings as Python code.
- CVE-2021-22204Chain: regex in EXIF processor code does not correctly determine where a string ends (CWE-625), enabling eval injection (CWE-95), as exploited in the wild per CISA KEV.
- CVE-2020-8218"Code injection" in VPN product, as exploited in the wild per CISA KEV.
How we fix it
From finding to fixed: how Codefreex closes CWE-94
Scan and confirm
We test your site, store or app for this weakness and the rest of the CWE Top 25 with automated scanning and manual review, and confirm what is actually exploitable.
Fix at the source
We change the code, configuration or dependency that causes it, not just the symptom, and plan the changes to avoid downtime.
Verify the fix
We retest every finding, check that nothing else broke and hand you a short report you can share with your team, client or auditor.
Keep it fixed
Updates, monitoring and maintenance so the weakness does not return with the next plugin, theme, app or code change.
Website SecurityPenetration TestingMalware Removal & Hack RepairWordPress Maintenance
Related weaknesses
Weaknesses related to CWE-94
FAQ
CWE-94: FAQs
Short answers for owners and developers. Not sure whether your site is affected? Ask us to check.
How serious is code injection?
It is at the top end of severity. Injected code runs with the application’s full permissions, so it can read and change data, bypass login checks and install persistent backdoors. MITRE rates the likelihood of exploitation as medium. When the flaw exists, though, the impact is usually complete compromise of the application.
Is code injection the same as command injection?
They are related but different. Code injection makes your application run attacker-written code in its own language, such as PHP, JavaScript or Python. Command injection makes the operating system shell run extra commands. One often leads to the other, since injected code can usually start system commands. Both come from mixing untrusted input with instructions.
How do I prevent code injection?
Do not generate code from input. Remove eval-style functions and replace them with fixed logic or safe parsers. Allowlist any value that selects a file, class or function. Keep user content out of template source. Restrict the runtime with least privilege and read-only code directories, and audit plugins that offer custom code features.
What is CWE-94?
CWE-94, “Improper Control of Generation of Code”, is a software weakness catalogued by MITRE in the Common Weakness Enumeration. The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.
How serious is CWE-94?
MITRE rates the likelihood of exploit as Medium. It can affect access control, integrity, confidentiality, availability, with impacts such as bypass protection mechanism, gain privileges or assume identity, execute unauthorized code or commands. It ranks number 10 on the 2025 CWE Top 25 Most Dangerous Software Weaknesses. The real risk depends on where the weakness sits and what data or functions it exposes.
How do you prevent CWE-94?
Refactor your program so that you do not have to dynamically generate code. Run your code in a “jail” or similar sandbox environment that enforces strict boundaries between the process and the operating system. This may effectively restrict which code can be executed by your.
Can Codefreex check my site for CWE-94?
Yes. A senior engineer reviews your site, store or app for CWE-94 and the rest of the CWE Top 25, confirms what is exploitable and sends a prioritized fix plan within one business day. Start with the free security check form on this page.
Free security check
Find out if CWE-94 affects your site
Send your URL. A senior engineer checks for this weakness and the rest of the CWE Top 25, and replies within one business day with what to fix first.
- A senior strategist reviews your request, not a sales bot
- Clear scope, timeline and estimate within one business day
- Free 30-minute discovery call, no obligation
- NDA available on request
- Emailhello@codefreex.com
- CallUSA +1 (209) 375-3475 · India +91 790 3375 530
- Prefer to talk?Book a 30-minute call
Weakness data from the Common Weakness Enumeration (CWE®) version 5.0, Copyright © 2006–2026, The MITRE Corporation. CWE is free to use by any organization or individual for any research, development, and/or commercial purposes under the CWE Terms of Use. CWE is a registered trademark of The MITRE Corporation. The business impact, risk profile and plain-English guidance on this page are written by Codefreex and are general information, not a security assessment of your system.