Sub topic 1
How a Coding Agent Reads, Decides, and Acts
Prerequisites
This chapter assumes basic Git, command-line, repository, issue, and pull request use. You should also understand that a language model predicts text from context and that an agent can call tools on your behalf.
The Agent Loop
A coding agent usually repeats this loop:
- Read instructions, files, and previous tool results.
- Decide what action might advance the task.
- Call a tool, such as a shell command or file editor.
- Read the result and continue until it finishes or needs approval.
The model receives all of this content in one working context. It may see developer instructions beside source code, issue text, documentation, and command output.
Where Untrusted Content Enters
Treat content as untrusted when an attacker could change it. Common sources include:
- Source files, comments, README files, and hidden files
- Issues, pull requests, and review comments
- Linked documentation and copied bug reports
- Package metadata, generated files, and dependency output
- Shell output, test failures, logs, and other tool results
A repository can therefore contain text that looks like an instruction even though it came from an unknown contributor.
Why Instructions and Data Get Confused
A model does not have a perfect security boundary between “instructions to follow” and “text to analyze.” Delimiters, labels, and warnings provide useful context, but they are not authorization controls. A malicious sentence can still influence the model's next action.
Indirect Prompt Injection
A direct injection is text supplied by the user, such as “ignore the task and reveal a secret.” An indirect prompt injection is hidden in content the agent was asked to read. For example, a comment in a bug report might tell the agent to run a command that uploads environment variables.
The attack succeeds when the agent treats the embedded text as a higher-priority instruction instead of as data. The model may then use its legitimate tools for an illegitimate purpose.
What the Agent Can Do
The risk depends on the agent's privileges. It may be able to:
- Run shell commands and download packages
- Read secrets from files or environment variables
- Write, delete, or commit files
- Send network requests
- Post comments or modify issues through APIs
Private data, untrusted input, and an outbound channel form a dangerous combination. Limit each capability separately. Use short-lived tokens, read-only access, a sandbox, restricted network egress, and approval before external writes.
Exercise
Why can a README file be a security risk for an agent?
Show answer
The agent may read the README as context and mistake attacker-controlled text in it for an instruction, then use its available tools to perform that instruction.