Code Injection

Improper Control of Generation of Code (Code Injection)

CWE-94 · Critical severity · Updated November 6, 2026

What it is

CWE-94 exists when an application takes user input and treats it as code that the application then executes. Unlike OS command injection (which targets a shell), code injection targets the application's own interpreter: JavaScript eval, Python exec, Ruby eval, PHP eval, or any dynamic code path that compiles or evaluates strings at runtime.

Why it matters

Code injection grants remote code execution with the privileges of the application itself, with no intermediate shell to bypass. The pattern recurs in expression evaluators, formula calculators, template engines, plugin systems, and ad-hoc REPLs exposed to web users. Even where eval is not used directly, dynamic-import, Function constructor, indirect VM access (node:vm), and template strings with unescaped substitution can each be a code injection vector.

Common patterns

  • eval(req.body.expr) or eval(req.query.code) anywhere in a request handler.
  • new Function(userInput) to dynamically compile user-supplied code.
  • Python exec or compile on data from a request, message queue, or database.
  • User input rendered as a template, rather than passed to one as data (server-side template injection).
  • node:vm Script with un-vetted user input (sandbox escapes are common).
  • JSON parsers implemented via eval (legacy code; CWE-95 overlap).

Languages affected

JavaScript, TypeScript, Python, Ruby, PHP

What Deva detects

Taint rules flag eval, new Function, Python exec, and Python compile when input traces from a request. Ruby eval variants and PHP eval are flagged on any use, including constant strings.

Example

Vulnerable

app.post('/calc', (req, res) => {
  const result = eval(req.body.expr)
  res.json({ result })
})

Fixed

// A small parser for + - * / and parentheses. No eval, no access to globals.
function calculate(input) {
  const tokens = input.match(/\d+(\.\d+)?|[()+\-*/]/g) ?? []
  if (tokens.join('') !== input.replace(/\s+/g, '')) throw new Error('Bad token')
  let i = 0
  const factor = () => {
    const token = tokens[i++]
    if (token === '-') return -factor()
    if (token === '(') {
      const value = sum()
      if (tokens[i++] !== ')') throw new Error('Unclosed parenthesis')
      return value
    }
    const value = Number(token)
    if (Number.isNaN(value)) throw new Error('Expected a number')
    return value
  }
  const product = () => {
    let value = factor()
    while (tokens[i] === '*' || tokens[i] === '/') {
      value = tokens[i++] === '*' ? value * factor() : value / factor()
    }
    return value
  }
  const sum = () => {
    let value = product()
    while (tokens[i] === '+' || tokens[i] === '-') {
      value = tokens[i++] === '+' ? value + product() : value - product()
    }
    return value
  }
  const result = sum()
  if (i !== tokens.length) throw new Error('Unexpected token')
  return result
}

app.post('/calc', (req, res) => {
  try {
    const expr = String(req.body.expr || '')
    if (expr.length > 100) return res.status(400).json({ error: 'Too long' })
    const result = calculate(expr)
    if (!Number.isFinite(result)) {
      return res.status(400).json({ error: 'Bad expression' })
    }
    res.json({ result })
  } catch {
    res.status(400).json({ error: 'Bad expression' })
  }
})

The vulnerable version eval()s a request body. An attacker posting { expr: 'require("child_process").execSync("id")' } gets remote code execution. The fix uses a small parser that accepts only numbers, four arithmetic operators, and parentheses, with no access to JavaScript globals. The input is also length-bounded and the result is checked. If you use a library instead, check its advisories first: expr-eval has a critical CVE (CVE-2025-12735). The pattern generalizes: never eval user input, always use a domain-specific parser with an explicit grammar.

Where this fits in OWASP Top 10

  • A05 InjectionUntrusted input interpreted as code or commands by a downstream interpreter.

Compliance framework references

These references explain how the weakness relates to control topics. They do not mean Deva offers evaluation for every framework listed. CMMC evaluation and SPRS scoring are currently unavailable. See the FAQ for offered frameworks and preview status. Deva provides code-level evidence for your assessment, without certifying compliance.

FrameworkControls
OWASP Top 10:2025A05:2025 Injection
NIST 800-53 Rev 5SI-10 Information Input Validation, SC-18 Mobile Code
PCI-DSS v4.0.16.2.4 Software engineering techniques
CMMC L2SI.L2-3.14.1 Flaw remediation

Related CWEs

  • CWE-95 · Eval injection specifically.
  • CWE-470 · Use of externally-controlled input to select code (unsafe reflection).
  • CWE-917 · Expression language injection.
  • CWE-78 · OS command injection (sibling in the injection family).

Related glossary terms