# 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 ([[raw/symfony.com/twig-cve-2026-46633-php-code-injection-via-use-template-name-15865|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 ([[raw/r3vpwnx.medium.com/loading-a-dataset-should-not-run-code-15851|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_eval`s it, so the `RubyMethod` annotation body becomes code ([[raw/blog.ostorlab.co/puppet-server-deserialization-rce-cve-2026-115731-ostorlab-15875|Ostorlab]]); see [[Polymorphic Deserialization]].
- **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.
## Related
- [[Remote Code Execution]] · [[Command Injection]] · [[Expression Language Injection]] · [[Insecure Deserialization]] · [[Server-Side Template Injection]]
## Sources
- [[raw/advisories.gitlab.com/twig-php-code-injection-via-use-template-name-14934|Twig PHP code injection via {% use %} template name (CVE-2026-46633)]]
- [[raw/blog.ostorlab.co/puppet-server-deserialization-rce-cve-2026-115731-ostorlab-15875|Puppet Server deserialization RCE (CVE-2026-115731) - Ostorlab]]
- [[raw/github.com/mlflow-pickleevaluationartifact-deserialization-rce-13582|MLflow PickleEvaluationArtifact Deserialization RCE]]
- [[raw/r3vpwnx.medium.com/loading-a-dataset-should-not-run-code-15851|Loading a Dataset Should Not Run Code]]
- [[raw/rescana.com/cve-2026-1606-code-injection-vulnerability-in-gitlab-ce-ee-snippets-af-9831|CVE-2026-1606: Code Injection in GitLab CE/EE Snippets]]
- [[raw/snyk.io/code-injection-in-python-examples-and-prevention-9665|Code injection in Python: examples and prevention]]
- [[raw/symfony.com/twig-cve-2026-46633-php-code-injection-via-use-template-name-15865|Twig CVE-2026-46633: PHP code injection via {% use %} template name]]