Code Injection

Attacker input reaches a sink that evaluates it as source code in the application’s own language, so the attacker runs code inside the process without ever touching a shell (CWE-94).

How it works

Distinct from Command Injection (shell) and Argument Injection (argv): here the interpreter is the application runtime. The input is parsed and executed as program text. Common sinks by language:

  • Python: eval(), exec(), compile(), pickle.loads(), yaml.load() (non-safe), __import__. Use ast.literal_eval for data; never eval on input.
  • JavaScript / Node: eval(), new Function(), setTimeout("string"), vm without isolation, template compilers.
  • PHP: eval(), assert(), create_function(), preg_replace('/e'), and template engines that compile to PHP.
  • Ruby: eval, instance_eval, send; Java/JVM: scripting engines and Expression Language Injection.

Code injection bypasses sandboxes that only filter data — the payload is first-class code, so it reaches any API the process can. It overlaps with Insecure Deserialization (deser topic) when the sink is an object deserializer and with Server-Side Template Injection (ssti topic) when it is a template engine.

Techniques / payloads

  • Python eval sink: input __import__('os').system('id'), or ().__class__.__bases__[0].__subclasses__() sandbox-escape gadget walks.
  • Pickle RCE: a class with __reduce__ returning (os.system, ('id',)) — deserializing the stream runs it. MLflow PickleEvaluationArtifact (CVE-2026-118623 / ZDI-26-839) is exactly this: unvalidated pickle parsing → RCE, minimal user interaction.
  • Template-compilation injection: Twig CVE-2026-46633 — a {% use %} template name containing a single quote terminates the surrounding PHP string literal in the compiled cache file, injecting arbitrary PHP that runs when the cache loads. Bypasses the Twig SecurityPolicy sandbox entirely because it executes at compile time, not render time. Root cause: Compiler::string() escapes ", $, \, NUL and TAB for double-quoted literals but not single quotes, and ModuleNode::compileConstructor() places the result inside a single-quoted literal; fixed in Twig 3.26.0 (Symfony advisory).
  • JS: new Function('return '+userInput)(); MDX/serializer sinks (@tinacms/mdx < 2.4.2, Contentlayer) compile attacker markdown into executable JS.

Notable cases

  • MLflow PickleEvaluationArtifact (CVE-2026-118623). Deserialization of an untrusted pickle in evaluation-artifact parsing → remote RCE on MLflow hosts.
  • Twig {% use %} (CVE-2026-46633). Single-quote escaping flaw in compiled template names → PHP code injection, sandbox bypass; reachable from sandboxed templates.
  • GitLab Snippets (CVE-2026-1606, CWE-94). Improper control of code generation lets an authenticated user craft snippets with concealed/obfuscated content; patched in 18.11.6 / 19.0.3 / 19.1.1.
  • yt saved datasets (CVE-2026-132751, CWE-95). SavedDataset._parse_parameter_file() passes the con_args attribute of a ytdata HDF5 file to eval(), so yt.load() on a shared dataset runs whatever Python the file’s author stored. Affected through 4.4.4 (Loading a dataset should not run code).
  • Puppet Server (CVE-2026-115731). A rich-data deserializer generates a Ruby class from an attacker-supplied type definition and class_evals it, so the RubyMethod annotation body becomes code (Ostorlab); see Polymorphic Deserialization.
  • binary-parser (CVE-2026-134814). The library compiles parser definitions to JavaScript; before 2.3.1, Context.generateOption() passed string-typed options (length, lengthInBytes, key, tag, offset, namely() aliases) into the generated source unvalidated, so whoever controls a parser definition runs JS in the Node process at compile time (GHSA-hhq6-2gpv-hrf4).
  • KindleEar (CVE-2026-135436). 3.0.0-3.5.3 hands uploaded recipe source from /recipe/upload to calibre’s compile_recipe, which exec()s it unsandboxed: any authenticated non-admin user gets root Python in the container and the password hashes (advisory).
  • AI-generated code. Snyk research shows LLM code assistants commonly emit eval/exec on request-derived input, reintroducing classic code-injection sinks at scale.

Defenses

  • Remove the sink: parse data with data parsers (ast.literal_eval, JSON.parse, yaml.safe_load), not code evaluators. A value read back from a saved file is still attacker data if the file can be shared (yt).
  • Never pickle.loads/Java-deserialize/yaml.load untrusted bytes; use signed, schema-validated formats.
  • For template engines, treat template source and template names as code — never take either from user input; keep the compiled cache out of attacker reach.
  • Defense in depth only: process sandboxing, seccomp, and dropped privileges reduce blast radius but do not fix the sink.

Sources