Summary
Agenta's self-hosted API server exposed an unsafe Python sandbox configuration that allowed authenticated users to escape the RestrictedPython environment and execute arbitrary code.
By leveraging the incorrectly whitelisted numpy package, specifically numpy.ma.core.inspect, an attacker could access system modules and invoke powerful functions such as os.system, leading to full remote code execution on the API server.
Affected context
This vulnerability appears in Agenta's self-hosted API server, specifically in the server-side custom code evaluator feature that executes user-supplied evaluator code.
The issue is present when:
- The Agenta-API uses RestrictedPython for sandboxing
numpyis whitelisted as a safe module inside the sandbox- Users are allowed to submit or edit evaluator code via authenticated API or UI workflows
The SDK used purely as a local Python library, without the self-hosted API server's evaluator, is not directly affected.
Exploit preconditions
- Attack surface: Agenta self-hosted API server's custom code evaluator endpoint or feature.
- Required access: Authenticated access sufficient to define or modify evaluator code (e.g., project/user with "can run custom code" capability).
- Trigger point: Submission or execution of specially crafted evaluator code inside the RestrictedPython sandbox that imports and uses
numpy.ma.core.inspect. - Why exploitation is possible: The sandbox incorrectly treats
numpyas safe, butnumpy.ma.core.inspectexposes Python's introspection subsystem, includingsys.modules. From there, the attacker can reach non-sandboxed modules (e.g.,os) and invoke arbitrary system-level functions, breaking out of the RestrictedPython constraints.
Why this happens
The underlying flaw is an improperly configured execution sandbox combined with overbroad trust in a complex third-party library.
RestrictedPython was intended to constrain user-supplied evaluator code, but the sandbox's allowlist included numpy as a "safe" dependency.
numpy is not designed as a security-hardened library; through numpy.ma.core.inspect, it exposes Python's introspection tools (such as access to sys.modules), which in turn provide references to powerful system interfaces (e.g., os, subprocess).
This maps to CWE-94: Improper Control of Generation of Code (Code Injection) in the context of sandbox escape: user-controlled code is executed in an environment that is believed to be restricted but actually enables access to unrestricted interpreter capabilities.
Potential impact
- Arbitrary command execution on the Agenta API server host via functions such as
os.systemor equivalents. - Compromise of all data accessible to the API process, including model configurations, prompts, and evaluation datasets.
- Lateral movement to other internal services reachable from the API server's network environment.
- Installation of backdoors or persistence mechanisms on the server.
- Potential manipulation of evaluation results and pipelines (e.g., poisoning metrics or test outcomes).
General mitigation guidance
Effective mitigation centers on treating user-supplied code as fully untrusted and avoiding overreliance on in-process sandboxes:
- Prefer out-of-process execution for user code, using isolated containers, VMs, or micro-VMs with strict OS-level controls (seccomp, user namespaces, read-only filesystems).
- Avoid whitelisting large, general-purpose libraries (such as
numpy) as "safe" within language-level sandboxes; assume that any sufficiently complex library can expose escape hatches. - If sandboxing is retained, adopt a default-deny policy: only expose minimal, purpose-built APIs rather than full libraries, and regularly review the allowlist for introspection or reflection capabilities.
- Run the code-execution component with least privilege: non-root accounts, minimal filesystem access, restricted network egress, and tightly scoped credentials.
- Separate duties: isolate evaluation services from core control-plane components and sensitive data stores, so that a sandbox escape has limited blast radius.
How teams detect issues like this
Organizations can detect and monitor for similar issues using multiple layers:
- Static analysis and configuration review:
- Search for use of RestrictedPython (or similar sandbox frameworks) alongside broad module allowlists.
- Flag code paths where user-supplied code is executed in the same interpreter process as privileged application logic.
- Review allowlisted libraries to identify those that support introspection, dynamic imports, or execution helpers.
- Code review practices:
- Require security review of features that execute user-defined code or templates.
- Check for reliance on "safe" libraries without deep analysis of their APIs.
- Runtime monitoring:
- Monitor evaluator execution environments for unexpected module imports (e.g.,
os,subprocess,inspect) originating from sandboxed code. - Use audit logs or tracing to detect evaluator-related actions that spawn new processes or make network connections.
- Automated tooling:
- Integrate SAST tools and custom rules into CI/CD to flag insecure sandbox configurations and in-process execution of untrusted code.
- Periodically run targeted security tests (including fuzzing and sandbox-escape probes) against endpoints that accept user code.

