AI coding agents can do much more than generate source code. Modern agents can inspect repositories, modify files, execute shell commands, install packages, call APIs, run tests, and interact with development infrastructure.
That flexibility is useful, but it also changes the security model.
A traditional application usually performs predefined operations. An AI coding agent can decide which tools to call and in what sequence based on natural-language instructions and information discovered during execution. If the agent receives broad filesystem, shell, network, or credential access, a compromised instruction or malicious repository can potentially turn that authority into an unintended action.
This is why least privilege should be enforced as an execution boundary, not simply described in a system prompt.
A useful approach is capability-based access: instead of giving an agent broad access to a machine, expose a small set of explicitly scoped capabilities that represent the actions it is allowed to perform.
Microsoft's current guidance for agent security similarly emphasizes dedicated agent identities, least-privilege access, explicit tool scopes, auditing, and revocation. OWASP guidance also recommends giving each agent only the tools required for its task and requiring confirmation for high-impact actions.
What Is Capability-Based Access?
Capability-based security associates authority with a specific capability or permission rather than giving a process unrestricted access to a resource.
For an AI coding agent, a capability might represent:
Read files inside a particular workspace.
Write files inside a particular workspace.
Run approved test commands.
Read package metadata.
Access a specific development API.
Query a development database.
Execute a predefined build operation.
The important distinction is that the agent does not receive general-purpose authority.
For example, instead of:
Agent
└── Full access to developer machine
a safer model is:
Agent
├── Read: /workspace/project
├── Write: /workspace/project/src
├── Execute: dotnet test
└── Network: package registry only
The exact capabilities depend on the task.
A documentation agent might only need read access. A test-fixing agent may need read/write access to a repository and permission to run tests. A deployment agent should have considerably stronger controls and potentially require human approval.
Why Prompt Restrictions Are Not Enough
A common first attempt at securing an AI coding agent is to add instructions such as:
Do not access credentials.
Do not modify files outside the project.
Do not execute destructive commands.
These instructions can influence model behavior, but they should not be treated as the security boundary.
The model is still interpreting instructions and deciding what actions to request. External content can also influence that reasoning. A malicious README, issue description, source file, dependency, or generated instruction could attempt to convince the agent to perform an operation outside its intended task.
Research on coding-agent authorization has found that models can struggle to infer exactly which permissions are necessary while simultaneously avoiding unnecessary or sensitive access.
The stronger architecture is:
User
|
v
AI Coding Agent
|
| Tool request
v
Capability Policy Layer
|
+---- Allowed ----> Tool
|
+---- Denied -----> Audit Log
The policy layer should make the final authorization decision.
Designing Capabilities for a Coding Agent
A practical capability model can start with four dimensions:
| Dimension | Example | Security Question |
|---|---|---|
| Resource | /workspace/orders | What can the agent access? |
| Operation | Read / Write / Execute | What can it do? |
| Scope | Project directory only | How much of the resource? |
| Condition | Development environment | When is access allowed? |
This is more precise than simply assigning a role such as DeveloperAgent.
Consider a coding agent that needs to fix a failing unit test.
It may require:
Read -> repository
Write -> src/ and tests/
Execute -> dotnet test
It probably does not need:
Read -> ~/.ssh
Read -> browser credentials
Write -> operating-system directories
Execute -> arbitrary shell commands
Access -> production database
The objective is not to make the agent incapable. The objective is to make its authorized capability set match the task.
Implementing a Capability Policy in C#
The following example demonstrates a small authorization layer. It is intentionally framework-neutral so the same concept can be adapted to an existing agent orchestration system.
Define the Capabilities
public enum AgentCapability
{
ReadWorkspace,
WriteWorkspace,
RunTests,
ReadSecrets,
Deploy
}
Now define the policy assigned to an agent session:
public sealed class AgentPolicy
{
private readonly HashSet<AgentCapability> _capabilities;
public AgentPolicy(IEnumerable<AgentCapability> capabilities)
{
_capabilities = capabilities.ToHashSet();
}
public bool Allows(AgentCapability capability)
{
return _capabilities.Contains(capability);
}
}
A test-fixing agent could receive:
var policy = new AgentPolicy(
[
AgentCapability.ReadWorkspace,
AgentCapability.WriteWorkspace,
AgentCapability.RunTests
]);
It does not automatically receive deployment or secret-access privileges.
Authorize Before Executing a Tool
The important part is not defining the capability. The important part is checking it before the operation reaches the underlying system.
public sealed class AgentToolBroker
{
private readonly AgentPolicy _policy;
public AgentToolBroker(AgentPolicy policy)
{
_policy = policy;
}
public void Authorize(AgentCapability capability, string operation)
{
if (!_policy.Allows(capability))
{
throw new UnauthorizedAccessException(
$"Operation '{operation}' is not permitted.");
}
}
}
A file-writing operation can then enforce the policy:
public async Task WriteFileAsync(
string path,
string content,
AgentToolBroker broker)
{
broker.Authorize(
AgentCapability.WriteWorkspace,
"write-file");
await File.WriteAllTextAsync(path, content);
}
This example is intentionally simple. A production implementation also needs to validate the resource being accessed, not just the operation.
Giving an agent WriteWorkspace does not mean it should be able to write every directory on the machine.
Scope the Filesystem Capability
A safer implementation associates the capability with an allowed root.
public sealed class WorkspacePolicy
{
private readonly string _workspaceRoot;
public WorkspacePolicy(string workspaceRoot)
{
_workspaceRoot = Path.GetFullPath(workspaceRoot);
}
public string ValidatePath(string requestedPath)
{
var fullPath = Path.GetFullPath(requestedPath);
if (!fullPath.StartsWith(
_workspaceRoot,
StringComparison.OrdinalIgnoreCase))
{
throw new UnauthorizedAccessException(
"Path is outside the permitted workspace.");
}
return fullPath;
}
}
Join the conversation! Your thoughts help the community grow.