Coding Agent Security: Prompt Injection, MCP and Permissions

Matthew Lake
Matthew Lake Security & DevSecOps Editor at CodeWalkers
Published

Coding agent security means controlling what an AI agent can read, change, run and send. An agent can encounter hostile instructions in a repository, issue or connected tool, then act on them using access you've given it.

Start with restricted file access, a sandbox and credentials limited to the task. Keep network access narrow and require approval before the agent deploys, changes permissions or sends private information elsewhere. These controls belong alongside your application security checks, because checking generated code won't tell you everything the agent did while writing it.

How Does Prompt Injection Affect Coding Agents?

Prompt injection tricks a coding agent into treating text it encounters as instructions to follow. The attacker might place that text in an issue, a source-code comment or a web page the agent reads during an otherwise legitimate task.

For example, you ask an agent to investigate a bug. The issue contains instructions to fetch a private file and attach it to a public pull request. If the agent follows them and has the necessary access, a routine investigation can expose private data.

Invariant Labs demonstrated this attack in May 2025 using the official GitHub MCP integration. The setup gave the agent access to public and private repositories, and the attack depended on the required tool calls being allowed. The researchers identified an agent-level trust problem; it wasn't evidence that the GitHub server itself contained malicious code.

The practical question is what the agent could do if it followed the wrong instruction. A model refusing one attack doesn't prove it will refuse the next. Restrict the files, accounts and destinations available to the task so that a mistaken decision has less room to cause damage. If the agent reviews pull requests, check the separate guidance on prompt injection in AI code review: the submitted code and comments are themselves untrusted inputs.

What Other Risks Do Coding Agents Create?

Coding agents also introduce risks through connected software, package installations and files that other programs execute. Prompt injection is one way to start an attack, but malicious software can steal data without persuading a model at all.

Malicious MCP Servers and Tool Descriptions

The Model Context Protocol, or MCP, lets an AI application connect to tools and data sources. A local MCP server is software running on your machine. Unless you separately restrict it, that software may run with the same privileges as the application that started it.

Verify the publisher and the exact startup command before installing a server. Pin reviewed versions and check updates before accepting them. The MCP security guidance recommends sandboxing local servers and limiting their file and network access. For remote services, check the account permissions you're granting and whether the tool can write or send data.

A tool description can also contain hostile instructions, commonly called tool poisoning. A familiar tool name doesn't establish that its implementation or returned content is trustworthy.

Secrets and Excessive Permissions

API keys, SSH keys and cloud credentials are valuable even when the task has nothing to do with them. Don't assume a workspace write restriction also prevents reads elsewhere on the machine. Inspect read permissions separately, including files, inherited environment variables and connected accounts.

An approved destination can still leak data. GitHub, for example, hosts both your private repositories and public repositories an attacker can read. A rule allowing the whole domain cannot distinguish every safe upload from an unsafe one. Limit the account's repositories and actions as well as its network access.

Packages, Hooks and Persistent Instructions

Installing a dependency can execute package code. Check that a suggested package exists, comes from the expected maintainer and has been reviewed before letting an agent install it. OWASP's secure coding guidance also covers hostile repository instructions and changes that weaken tests or security configuration.

Review changes to agent rules, saved instructions, editor settings and build hooks alongside application code. A small configuration change can affect later sessions or cause another program to run code automatically.

What Is the Difference Between Approval and Isolation?

Approval lets an agent attempt an action, while a sandbox limits which files, commands and network connections it can use. Both matter when you approve a test command: the script may have changed since you last read it, so approval alone gives you little assurance about what will run.

A rule written in a prompt relies on the model following it. Enforced permissions can block an action even when the model tries to proceed, but only through the tools they cover. A shell restriction, for example, does not automatically constrain a separately running MCP server. Check those connections separately.

When Should You Use a Separate Environment?

For supervised work on a familiar project, start with a local sandbox whose restrictions cover the tools you use. Unfamiliar code, unreviewed tools and unattended work warrant a separate environment you can discard afterwards, particularly where private files or accounts would otherwise be exposed.

EnvironmentWhen to Consider ItWhat Still Needs Checking
Local sandboxSupervised work on a familiar projectFile reads, network access and tools outside the sandbox
Development containerWork that needs a separate set of dependencies and a narrowly mounted projectShared host kernel, mounted credentials, privileged sockets and outgoing connections
Disposable VM or remote runnerUnfamiliar code, higher-risk tools or unattended tasksCredentials and services reachable from inside the environment, plus what is retained afterwards

Keep production credentials and sensitive host folders out of development containers and other isolated environments. Moving a task to a remote runner achieves little if it can still reach your live application with the same credentials; restrict outgoing connections and test those limits with harmless data.

How Do Coding Agent Security Controls Differ?

Claude Code, Codex, Cursor and Copilot apply restrictions to different tools and processes. Check the actual session's file, network and approval settings before allowing it to work unattended.

AgentMain CheckImportant Limit
Claude CodeShell sandbox and file-tool permissionsMCP servers and hooks run outside the shell sandbox
CodexRead, write and network permissionsWorkspace write limits can coexist with broad reads
CursorRun Mode, Read Access and network modeIgnore files don't constrain every tool
Copilot in VS CodeSession sandbox policyOutbound network access is allowed by default
Copilot cloud agentRepository access and firewall settingsCloud controls differ from local sessions

Claude Code

Claude Code's shell sandbox runs on macOS, Linux and WSL2. It is off by default, and enabling it still leaves broad file reads unless you configure read restrictions. Native Windows commands aren't protected by this sandbox.

Run /sandbox, then review which files and hosts commands can reach. Apply file-tool permission rules too. Built-in file and web tools, MCP servers and hooks run outside the shell sandbox, so shell restrictions alone don't cover them.

Anthropic says auto mode reduces approval prompts without guaranteeing safety. Keep sensitive operations subject to review and check whether your settings permit retries outside the sandbox.

Codex

Codex separates approval decisions from the operating system restrictions on commands. OpenAI's sandbox explanation describes a default local setup with workspace writes, broad filesystem reads and network access disabled. Managed policies and session settings can change that access.

Check all three permissions rather than relying on the words workspace-write. Restrict secret reads and review MCP access separately; OpenAI also documents how to launch a local MCP server inside its own sandbox profile.

The same explanation shows why the surrounding editor matters. If an agent changes a local formatter and the editor later runs it outside the sandbox, the command restrictions no longer contain that execution. Review executable files and configuration, including changes Git may ignore.

Cursor

Cursor's Run Modes and sandbox settings control different parts of execution. In supported versions, choose Workspace under Read Access to restrict reads beyond the project, and inspect the network mode. The default network mode adds Cursor's built-in allowed destinations to your own list; it doesn't mean every connection is blocked.

Avoid Run Everything for routine work with private data. Cursor says Read Access controls don't apply in that mode. Its ignore-file documentation also warns that .cursorignore cannot prevent access through terminal and MCP tools.

Workspace Trust is a separate setting and is disabled by default. Enable it, and inspect unfamiliar repositories before trusting them. Cursor warns that automatic reloads can execute agent-written changes before you've reviewed them.

GitHub Copilot

For Copilot in VS Code, Microsoft's security guidance recommends checking the execution environment, using appropriate permissions and inspecting the effective sandbox policy. Outbound network access is allowed by default in the Copilot sandbox. Run /sandbox policy in a Copilot session to see the restrictions actually applied.

Review unfamiliar projects in Restricted Mode before granting Workspace Trust. Microsoft notes that workspace MCP configurations can start without a separate trust prompt once you trust the project.

Copilot cloud agent has separate repository and network protections, including an internet firewall. Check its configuration independently of your local editor, particularly if someone has widened its network access.

What Have Real Attacks Shown?

Published incidents show several ways compromised development tools can reach users. The outcome matters as much as the attacker's intention.

  • Amazon Q Developer, July 2025. AWS confirmed that malicious code reached version 1.84.0 of its VS Code extension. AWS's bulletin says a syntax error prevented execution and changes to customer environments. It released 1.85.0 as the fix.
  • Nx, August 2025. Compromised npm packages ran a post-install script that searched for sensitive data, attempted to use local AI tools and uploaded results to public GitHub repositories. The Nx postmortem traces the incident to a stolen publishing token.
  • Fake Postmark MCP package, September 2025. Postmark reported that the unofficial postmark-mcp package added a backdoor in version 1.0.16 that secretly copied outgoing emails. Postmark said its legitimate API and services were unaffected.

These incidents support checking the software you install, the privileges it receives and the data it can send. A prompt warning cannot make an already malicious package safe.

How Should You Secure a Coding Agent?

Secure a coding agent by giving each task the smallest useful set of files, tools and account permissions. Start with one repository and supervised work before expanding its access.

  1. Remove unnecessary credentials. Use test credentials and task-scoped tokens where possible. Keep production keys and unrelated accounts out of the environment.
  2. Set read and write limits separately. Give the agent the source it needs, protect secret files and restrict changes to security settings. Check tools outside the shell too.
  3. Limit outgoing connections. Allow only necessary destinations, then restrict what the connected account can do there. A permitted host is still a route out.
  4. Review consequential actions. Keep deployments, permission changes and external sharing behind a deliberate approval. Read the action and its arguments before approving.
  5. Check the whole change. Review dependencies, tests, hooks and configuration as well as source code. Run appropriate checks independently of the agent's summary.
  6. Prove the restrictions work. Use harmless dummy files and a disposable project to check denied reads, writes and network requests. Test relevant MCP and file tools separately.

How Should Teams Manage Coding Agents?

Teams need a current record of their agents, connected servers and plugins, and the repositories and accounts each can reach. Assign someone to maintain it and review new connections before use, because an approved agent can gain much broader access when a server is added.

Enforce shared restrictions through administrator-controlled settings where the product supports them. With separate accounts and short-lived credentials, you can trace an agent's actions and revoke its task access without disabling a developer's account. Changes to tool definitions or wider permissions still need review.

Keep logs of attempted actions, approvals and results outside the agent's control where practical, without recording secret values. Any temporary access should have an expiry and someone responsible for removing it.

What Should You Check Before Merging Agent-Written Code?

Agent-written code needs independent review and testing even when the agent followed every permission rule. It can stay inside a sandbox and still write a login check that lets the wrong person in.

Review the full diff, including dependencies, hooks, security configuration and deleted tests. A passing test suite is weak evidence if the agent removed the assertion that would have caught the defect.

Run the project's tests, static analysis, dependency checks and secret scanning. When access control changes, use separate test accounts to check that one user cannot reach another user's data in the running application. Keep those checks and human approval outside the agent's control.

Use the agent's summary to locate changes, then inspect the code and results through your normal secure code review process.

What If a Coding Agent Acts Suspiciously?

Stop a suspicious agent session and contain its access before asking it to do more work. Unexpected credential reads, unfamiliar uploads or attempts to weaken security settings deserve investigation even if the final code looks normal.

Preserve relevant logs, revoke potentially exposed credentials from a trusted environment and inspect changed files, dependencies and saved instructions. If malicious software ran, treat the environment as potentially compromised and rebuild it as appropriate before resuming. Keep logs access-controlled and avoid recording secret values.

OWASP's agent security checklist includes monitoring and memory controls. Persistent instructions need attention because a fresh conversation may still load contaminated material.

Frequently Asked Questions

Can prompt injection be prevented completely?

Prompt-injection defences reduce risk, but a warning in the prompt or a model-based filter cannot guarantee that every attack will fail. Combine those checks with enforced permissions, restricted data access and approval for consequential actions.

Is read-only access enough to protect secrets?

Read-only access prevents changes through that access path, but it can still expose secrets. An agent that reads private data and can send messages, call external tools or make network requests may disclose the data without modifying the original file.

Does a container make approval bypass safe?

A container reduces risk only to the extent that its isolation and permissions are configured correctly. Host credential mounts, a Docker socket or unrestricted network access can expose resources beyond the task. Use a disposable environment with limited access before considering approval bypass.

Start with the Access You Can Justify

Start with one project, limited credentials and supervised work. Expand access only when the task needs it and you've checked what a mistaken action could expose. For teams, that decision needs a named owner who can also remove access when something goes wrong.

Sources

  1. [1]
    Choose a permission mode
    (code.claude.com)
  2. [2]
  3. [3]
    Claude Code permissions
    (code.claude.com)
  4. [4]
    Claude Code security
    (code.claude.com)
  5. [5]
    Agent approvals and security
    (learn.chatgpt.com)
  6. [6]
  7. [7]
    Cursor Run Modes
    (cursor.com)
  8. [8]
  9. [9]
  10. [10]
  11. [11]
  12. [12]
    MCP security best practices
    (modelcontextprotocol.io)
  13. [13]
  14. [14]
  15. [15]
  16. [16]
  17. [17]
  18. [18]
  19. [19]
  20. [20]
  21. [21]
  22. [22]
  23. [23]
  24. [24]
    CVE-2025-54135
    (nvd.nist.gov)
  25. [25]
  26. [26]
    CVE-2025-54794
    (nvd.nist.gov)
  27. [27]
    CVE-2025-54795
    (nvd.nist.gov)
  28. [28]
  29. [29]