← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · ✎ updated 7d
← Back to glossary

Eval Injection

Security CWE-95 OWASP A3:2021 CVSS 9.8 PHP 5.0+ Intermediate
debt(d5/e3/b3/t9)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The term's detection_hints.tools list phpstan, psalm, and semgrep — all specialist static analysis tools. The pattern (eval( with variable interpolation) is automatable but requires these dedicated tools; no default linter catches it, and it won't surface as a compiler error.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix states: remove eval() entirely and replace with a data-driven strategy pattern. This is typically a contained refactor within one component — swapping eval() calls for a lookup table or callback dispatch — not a cross-cutting architectural change. Slightly above e1 because the replacement requires a small design choice (strategy pattern) rather than a single-line swap.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The applies_to scope is web and CLI contexts, but eval() usage tends to be localised to specific callsites rather than load-bearing across the whole codebase. Once removed, the rest of the codebase is unaffected. It doesn't impose a persistent structural tax on future maintainers beyond the callsite itself.

t9 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'catastrophic trap — the obvious way is always wrong' (t9). The misconception field explicitly states: 'eval() is safe if you sanitise the input first. Sanitising code intended for eval() is an arms race you will lose.' This is exactly the t9 pattern: the intuitive mitigation (sanitisation) is not merely insufficient but fundamentally the wrong mental model. Competent developers routinely believe sanitisation solves the problem, making the trap catastrophic.

About DEBT scoring →

Also Known As

code injection dynamic code execution PHP eval attack

TL;DR

User input passed to eval() executes as PHP code, giving attackers full server-side code execution.

Explanation

eval() executes a string as PHP code at runtime. If any part of that string is user-controlled, the attacker can execute arbitrary PHP — read files, run system commands, exfiltrate environment variables, or install a web shell. There is no safe way to pass user input to eval(); the only fix is to remove eval() and redesign the feature using whitelists, expression parsers, or template engines.

How It's Exploited

GET /calc?formula=system('id')
# eval() executes arbitrary PHP → remote code execution

Common Misconception

✗ eval() is safe if you sanitise the input first. Sanitising code intended for eval() is an arms race you will lose — the only fix is never passing user input to eval(), create_function(), or preg_replace() with /e.

Why It Matters

Passing user input to eval() is direct arbitrary PHP code execution — the most complete form of remote code execution possible.

Common Mistakes

  • Using eval() for any dynamic behaviour that could be achieved with a lookup table, strategy pattern, or callbacks.
  • Trusting 'sanitised' input — there is no safe way to sanitise PHP code for eval().
  • Using the deprecated preg_replace() /e modifier which implicitly called eval().
  • Template engines that fall back to eval() for dynamic expressions without sandboxing.

Code Examples

✗ Vulnerable
// eval() with user input — direct code execution
$formula = $_GET['formula'];
eval("$result = $formula;");
// ?formula=system('cat /etc/passwd')//  → RCE
✓ Fixed
// Never pass user input to eval()
// For math expressions, use a safe parser library:
use NXP\MathExecutor;
$calc   = new MathExecutor();
$result = $calc->execute($_GET['formula']); // sandboxed math only

// Or whitelist the allowed operations:
$allowed = '/^[\d\s+\-*\/().]+$/';
if (!preg_match($allowed, $_GET['formula'])) abort(400);
// Even then, avoid eval — prefer a real expression parser

Added 15 Mar 2026
Edited 22 Mar 2026
Views 160
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M
No pings yet today
No pings yesterday
Scrapy 17 ChatGPT 11 Google 10 Perplexity 9 Ahrefs 9 Amazonbot 7 SEMrush 7 Bing 6 PetalBot 6 Unknown AI 4 Applebot 3 Twitter/X 2 Sogou 2 Meta AI 1 Majestic 1
crawler 92 crawler_json 3
⚡ DEV INTEL Tools & Severity
🔴 Critical ⚙ Fix effort: Medium
⚡ Quick Fix
Remove eval() entirely — there is virtually no legitimate use case; replace dynamic code execution with a data-driven strategy pattern
📦 Applies To
PHP 5.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
eval( with any variable interpolation or user-controlled string
Auto-detectable: ✓ Yes phpstan psalm semgrep
🤖 AI Agent
Confidence: High False Positives: Low ✗ Manual fix Fix: High Context: Function Tests: Update
CWE-95


✓ schema.org compliant