Summary
jmespath.php prior to 2.9.1 can generate and execute attacker-controlled PHP code when the JmesPath\CompilerRuntime is used with untrusted JMESPath expressions.
The compiler embeds function names from the expression directly into generated PHP source, enabling a crafted expression to inject arbitrary PHP into cached files and achieve remote code execution.
Affected context
This vulnerability arises in PHP applications that embed jmespath.php and enable the compiled runtime (JmesPath\CompilerRuntime) for evaluating JMESPath expressions.
It becomes relevant when expressions originate from users or other untrusted sources, and when the compilation feature (JP_PHP_COMPILE or equivalent configuration) is active so that expressions are turned into cached PHP files and then loaded at runtime.
Deployments using only the default AstRuntime for untrusted expressions are generally outside the primary risk path because they do not generate executable PHP code from user-provided query text.
Exploit preconditions
- Attack surface: Any API endpoint, CLI entry point, or background job that accepts JMESPath expressions which are processed by
JmesPath\CompilerRuntime. - Required access: Ability to supply or influence the JMESPath expression evaluated by the PHP application (e.g., request parameters, configuration, templates, or rules).
- Trigger point: Evaluation of an attacker-controlled expression by the compiler runtime, causing generation and inclusion of a cached PHP file derived from that expression.
- Why exploitation is possible: Parsed function names from the JMESPath expression are inserted into generated PHP source code without adequate escaping or validation, allowing an attacker to break out of the intended syntax and inject arbitrary PHP code into the compiled cache file which is then executed.
Why this happens
The core issue is an input validation and output encoding failure in the code generation path of jmespath.php's CompilerRuntime.
Instead of treating parsed function names as untrusted content requiring strict validation and escaping, the compiler directly interpolates them into PHP source code templates that are written to cache files and later required or included.
This pattern maps to CWE-94: Improper Control of Generation of Code ('Code Injection'), and is supported by CWE-20: Improper Input Validation and CWE-116: Improper Encoding or Escaping of Output, because untrusted expression components are not constrained to safe identifiers nor correctly encoded before being embedded into executable PHP.
Potential impact
- Execution of arbitrary PHP code within the web server or PHP process context.
- Full compromise of the affected application, including reading or modifying sensitive data.
- Lateral movement by using the compromised application to attack connected services or databases.
- Planting of persistent backdoors in application code or cache directories.
- Abuse of the server to perform further attacks, such as sending malicious responses or hosting rogue content.
General mitigation guidance
Effective mitigation typically focuses on avoiding or strictly constraining dynamic code generation paths and treating JMESPath expressions as untrusted input by default.
Security-oriented patterns include:
- Prefer interpreted execution (e.g., the default
AstRuntime) for any expression that can be influenced by users, external systems, or configuration that is not tightly controlled. - If compiled execution is required, apply strict allowlists for function names and other expression components, ensuring only known-safe identifiers are ever passed into code generation.
- Apply robust validation to JMESPath expressions before evaluation, rejecting or sanitizing constructs that do not conform to an expected grammar profile.
- Separate storage and permissions for cache directories, using least privilege file system permissions so that compromise of one area is less likely to cascade.
- Adopt a secure-by-default configuration that disables compilation for untrusted contexts, only enabling the compiler runtime explicitly for trusted, internal expressions.
How teams detect issues like this
Detection efforts can focus both on code-level patterns and runtime behavior:
- Static analysis: Flag any use of
JmesPath\CompilerRuntimewhere the expression argument is derived from request parameters, user input, or other untrusted sources. Rules can also search for dynamic code-generation patterns that concatenate unvalidated strings into PHP source. - Code review: During manual reviews, highlight locations that enable JMESPath compilation, and verify that those call sites are restricted to constant or strictly controlled expressions rather than user-supplied data.
- Configuration and policy checks: Enforce policies that disallow enabling
JP_PHP_COMPILE(or equivalent settings) in environments that process untrusted expressions, using CI checks or deployment-time configuration audits. - Runtime monitoring: Monitor for unexpected or frequently changing PHP cache files in jmespath-related cache directories, and alert on unusual file contents or modification patterns that may indicate injected code.

