Summary
AgentCore CLI before v0.14.2 improperly neutralizes triple-quote characters when generating Python code from collaboration instructions.
An authenticated attacker can craft a malicious collaborationInstruction so that, when another user imports the agent, arbitrary code runs under the agent's IAM execution role in AWS and on that user's local environment.
Affected context
This vulnerability appears in workflows where AgentCore CLI is used to import or collaborate on Bedrock Agents that contain shared collaboration instructions.
The issue arises when the CLI generates Python source code that embeds these instructions without safely handling triple-quote characters, allowing attacker-supplied content to escape the intended string literal.
It primarily affects authenticated users operating in the same AWS account where one party can define or modify collaborationInstruction content that another party later imports with AgentCore CLI.
Exploit preconditions
- Attack surface: Agent import and collaboration flows in AgentCore CLI that process Bedrock Agent collaborator instructions.
- Required access: Authenticated ability to create or modify an agent (or collaborator) with a crafted collaborationInstruction in a shared AWS account or environment.
- Trigger point: A separate user uses a vulnerable version of AgentCore CLI to import or process the attacker-controlled agent, causing code generation and execution.
- Why exploitation is possible: The code generation logic fails to correctly escape or sanitize triple-quote characters, allowing attacker input to terminate a string literal and inject arbitrary Python statements into the generated source file.
Why this happens
The core issue is unsafe code generation that directly embeds attacker-controlled text into Python source files without robust quoting or escaping.
When triple-quote characters in collaborationInstruction are not neutralized, the generated Python code can break out of the intended string context and introduce new, executable statements.
This design aligns with CWE-94: Improper Control of Generation of Code (Code Injection), where dynamically constructed code incorporates untrusted input as executable syntax rather than treating it as inert data.
Potential impact
- Execution of arbitrary Python code during agent import or runtime initialization.
- Actions performed under the imported agent's IAM execution role, including potential access to AWS services, data, and infrastructure reachable by that role.
- Execution of arbitrary code on the local machine running AgentCore CLI, within the permissions of the importing user.
- Lateral movement within the AWS account by abusing shared agent collaboration channels.
- Data exfiltration, modification of agent logic, or insertion of backdoors into generated artifacts.
General mitigation guidance
Mitigation generally focuses on hardening code generation and treating all remote or collaborative content as untrusted input:
- Encapsulate untrusted text in data structures rather than interpolating it directly into executable code; prefer configuration or templates interpreted as data, not as Python source.
- When dynamic code generation is unavoidable, apply strict escaping and encoding routines that safely handle quotes, newlines, and special characters before embedding input into strings.
- Introduce explicit validation and allowlists on collaborationInstruction content, limiting it to expected formats or schemas and rejecting syntactically suspicious constructs.
- Run generated or imported code under least-privilege IAM roles and local OS accounts so that unexpected execution has reduced blast radius.
- Separate code-generation responsibilities from execution, for example requiring manual review or signing of generated code before it can run in sensitive runtime environments.
How teams detect issues like this
Effective detection typically combines static analysis, code review, and runtime monitoring:
- Use static analysis or linters to flag patterns where user-controlled or remote text is concatenated into Python source files, especially inside triple-quoted strings or eval/exec-like constructs.
- During security-focused code review, look for code-generation utilities that handle collaboration or configuration text and verify that they apply consistent escaping and validation.
- Define policy checks in CI/CD pipelines that disallow direct interpolation of untrusted input into code templates, or require security approval for such changes.
- Monitor runtime environments (both local and in AWS) for anomalous behavior triggered during agent import, such as unexpected outbound network calls or unusual IAM API usage patterns after new agents are imported.
- Employ automated review tools that can scan both source repositories and generated artifacts for injection-prone code-generation patterns.

