Summary
AgentScope up to version 1.0.18 contains a code injection vulnerability in the execute_python_code and execute_shell_command tooling functions, allowing untrusted input to be executed as Python code or shell commands.
When exposed remotely through higher-level agent workflows or APIs, an attacker can craft input that gets passed into these functions, resulting in arbitrary code execution within the AgentScope runtime environment.
Affected context
The vulnerability arises in environments where AgentScope (modelscope/agentscope) is used to build agents that can execute arbitrary Python or shell commands through the execute_python_code and execute_shell_command helpers.
It is especially relevant when:
- These tools are wired into remote-facing interfaces (APIs, chatbots, orchestration layers, web frontends), and
- Untrusted or partially trusted user prompts, tool arguments, or model outputs can influence the code or command string passed to these functions, and
- There is no strict validation or restriction on what code or commands may be executed.
Exploit preconditions
- Attack surface: Remote, via any interface that can ultimately invoke
execute_python_codeorexecute_shell_commandwith attacker-controlled data (e.g., HTTP API, chat endpoint, task queue). - Required access: Ability to submit input (prompts, parameters, task descriptions, or tool calls) that is processed by AgentScope and routed into these execution functions.
- Trigger point: The moment AgentScope passes user- or model-supplied content directly into Python or shell execution without robust sanitization or policy enforcement.
- Why exploitation is possible: The implementation effectively treats dynamic input as executable code/commands, matching the patterns in CWE-94 and CWE-74 where data is injected into interpreters without adequate constraint, escaping, or isolation.
Why this happens
At its core, this issue stems from treating untrusted data as executable logic.
The execute_python_code and execute_shell_command functions appear to:
- Accept arbitrary strings derived from external sources (user prompts, model responses, or tool arguments), and
- Forward these strings directly to a Python interpreter or shell environment.
This aligns with:
- CWE-94: Improper Control of Generation of Code ('Code Injection'), where attackers can inject and run unintended code by influencing dynamically constructed code artifacts.
- CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection'), where untrusted input flows into a downstream interpreter (in this case, Python or the system shell) without neutralization of metacharacters or enforcement of a strict grammar.
Instead of separating data from code or using constrained, parameterized execution primitives, the tool layer appears to expose a powerful, low-level execution surface directly to potentially untrusted inputs.
Potential impact
- Execution of arbitrary Python code within the AgentScope process context.
- Execution of arbitrary system commands on the underlying host or container.
- Exfiltration or modification of sensitive data accessible to the agent (API keys, model weights, configuration, prompt logs).
- Lateral movement to other internal systems reachable from the host (e.g., internal services, databases).
- Use of the compromised agent infrastructure for follow-on attacks, such as cryptomining, spam, or supply-chain tampering of downstream AI workflows.
General mitigation guidance
Reducing risk from this class of vulnerability generally means removing or strictly constraining dynamic code and command execution in agent tools:
- Prefer whitelisted, parameterized operations over arbitrary code execution: expose high-level, task-specific APIs (e.g., "read file", "run safe computation") instead of generic Python or shell execution.
- When code execution is unavoidable, use sandboxing and isolation (separate process, container, or VM) with tightly limited privileges, filesystem access, network access, and resource quotas.
- Apply strict validation and allow-lists on commands or code constructs, limiting allowed modules, functions, and argument shapes rather than trying to blacklist dangerous patterns.
- Treat all inputs (including LLM outputs and tool arguments) as untrusted and avoid concatenating them directly into interpreter-bound strings.
- Enforce principle of least privilege on the runtime: run agents under low-privilege accounts, restrict filesystem paths, and disable unnecessary network connectivity.
- Implement configuration-level controls that allow operators to disable or severely restrict generic code and shell tools in production environments.
How teams detect issues like this
Identifying and monitoring for this issue typically involves both code-centric and runtime-centric approaches:
- Static analysis and code scanning:
- Flag patterns where external input is passed into Python execution primitives (e.g.,
exec,eval,runpy, dynamic imports) or shell execution APIs (e.g.,subprocess,os.system, shell=True usage) within tooling code. - Detect tool functions that accept free-form strings and route them to interpreters without explicit validation, allow-listing, or sandboxing logic.
- Code review signals:
- Scrutinize any generic "execute code" or "run shell" tool integrations into agents, especially those exposed through remote APIs or LLM tool-calling.
- Look for missing separation between trusted configuration and untrusted runtime input (including model-generated content).
- Policy and CI/CD enforcement:
- Define organizational policies that forbid or constrain dynamic code execution in production agent environments.
- Integrate automated checks in CI pipelines to block new uses of unsafe execution primitives or shell invocation patterns without justification and guardrails.
- Runtime monitoring:
- Instrument logging of all uses of code- and shell-execution tools, and alert on suspicious patterns (e.g., unexpected network utilities, package managers, or filesystem operations).
- Use host or container security tools to detect anomalous processes or command lines originating from the AgentScope runtime.

