skip to content

Cursor's Agent printed a key from secrets/prod.env even though .cursorignore lists secrets/ — how did it read the file, and what actually stops that?

level: seniorimportance: should knowfreq 6%

answer

  1. which tools the ignore file governs
  2. outside the file access controls
  3. a shell read, or an MCP server
  4. default sandbox reads the workspace
  5. permissions or encryption, plus hooks

basics

~20 s

.cursorignore governs only Cursor's own file access: Agent's reads, search, Tab, Inline Edit and @-mentions. Terminal commands and MCP tools run outside it, so a cat reads the file. File system permissions or encryption, plus command hooks, are what stop it.

solid answer

~40 s

Cursor's docs are explicit: "Terminal commands and MCP tools run outside of Cursor's file access controls", and the enterprise guardrails page calls `.cursorignore` "not a security boundary". Agent's own read tool refused the file, but a shell command such as `cat secrets/prod.env`, or an MCP server with disk access, read it directly and the output came back into the conversation. Under Auto-review a sandboxed shell command can run without any prompt. What actually stops it is layered: protect the file with file system permissions or encryption, deny commands that touch secret paths with a `beforeShellExecution` hook, keep shell commands behind approval in sensitive repositories, and vet MCP servers. Keep `.cursorignore` for keeping files out of context, not for containment.

go deeper

for a junior

Recall that .cursorignore hides files from Cursor's own file tools, while terminal commands and MCP tools can still read them.

for a middle

Trace the route: Agent's read is refused, a shell command reads the file, and the output returns as a tool result into the conversation.

for a senior

Layer controls for a team: file permissions or encryption on secrets, a beforeShellExecution hook, approval for shell commands, and vetted MCP servers.

for a principal

Weigh developer friction against containment; a repository that must hold secrets locally needs deterministic gates, while one that never holds them makes the ignore file sufficient for context hygiene.

## The symptom A developer adds `secrets/` to `.cursorignore`. Later, during an Agent run, a key from `secrets/prod.env` appears in the chat. Nobody @-mentioned the file, and Agent's file-read tool would have refused it. The explanation is one sentence on Cursor's ignore-files help page: "Terminal commands and MCP tools run outside of Cursor's file access controls, so they may still be able to read ignored files." The ignore-file reference says the same: the terminal and MCP server tools used by Agent cannot block access to code governed by `.cursorignore`. ## What .cursorignore governs, and what it does not | Route to the file | Governed by `.cursorignore`? | |---|---| | Agent's file reading and codebase search | Yes | | Tab and Inline Edit | Yes | | @-mention references | Yes | | A shell command Agent runs (`cat`, `grep`, a script) | No | | A tool exposed by an MCP server | No | | You opening the file yourself | No | Cursor's enterprise guardrails page is blunt: `.cursorignore` "is not a security boundary". It excludes files from AI processing, but users can read ignored files, agents might find ways to reach ignored content, and terminal and MCP tools can still read them. "For true security, use file system permissions or encrypt sensitive data." ## How the read most likely happened 1. Agent wanted a value, could not open the file with its own tool, and reached for the terminal: `cat secrets/prod.env`, or a `grep` across the folder. 2. The command ran. Terminal commands need approval by default, so either someone approved it, or a Run Mode let it run. Under **Auto-review**, a shell command that fits the sandbox runs there without a prompt, and the default sandbox type, `workspace_readwrite`, allows reads and writes inside the workspace. 3. The command's output came back to Agent as a tool result, joined the conversation and was printed. The same route exists through any MCP server that can read the disk or run commands. ## Why the gap exists `.cursorignore` is a list that Cursor's own file tools consult before they open a file. A shell command is different: it is a program running with your user account's rights, and it can open any file that account can open. An MCP server is a separate process with whatever access it was started with. Neither asks Cursor's file access controls for permission, so the list never comes into play. Two details sharpen the picture: - Cursor's sandbox reference lists `.cursorignore` itself among the paths that are always **write-protected** for sandboxed commands, so a sandboxed command cannot quietly edit the ignore file. Write protection is not read protection: the files the ignore file names stay readable to the shell. - Agent does not need to be "trying" to leak anything. A command that greps a folder for a config value, or a test script that loads an env file, prints the value as part of its normal output. ## What actually blocks it Cursor's hardening guide pairs best-effort guardrails such as `.cursorignore` with deterministic ones. For a secrets file that means: - **Use file system permissions or encryption**, the docs' own answer for real protection, so the account Cursor's processes run as cannot read the file in plain text. - **Keep long-lived secrets out of the project folder where you can.** It shrinks what a casual workspace grep finds, though an unsandboxed command can still read anything your account can. - **Gate commands deterministically.** A `beforeShellExecution` hook in `.cursor/hooks.json` can deny commands that touch secret paths; `beforeMCPExecution` does the same for MCP calls. - **Keep shell commands behind approval** in sensitive repositories rather than Run Everything, remembering that the Auto-review classifier is best-effort, not a boundary. - **Review MCP servers before approving them.** By default each connection and each tool call asks for approval unless the tool is pre-allowlisted. Keep `.cursorignore` anyway. It still keeps the file out of Agent's direct reads, codebase search, Tab and @-mentions, which covers most everyday exposure. It is a filter on Cursor's own file tools, not a wall around the file. ## Common mistakes - Treating `.cursorignore` as an access-control list for everything Agent can run. - Counting on the sandbox to turn `.cursorignore` into a security boundary. - "Fixing" the leak by repeating the pattern in `.gitignore` or the global ignore list. - Assuming MCP servers honour the project's ignore files.

  • Does turning on sandboxing for Cursor's terminal commands close the .cursorignore gap?
    Do not rely on it. Cursor's default sandbox type, `workspace_readwrite`, gives sandboxed commands read and write access inside the workspace; what it limits is writes outside approved paths, protected config files and network access. Cursor's ignore pages say terminal commands run outside its file access controls, so protect the file itself.
  • Which Cursor hook can deny a shell command that reads a secrets path, and where is it configured?
    `beforeShellExecution`, a permission hook that runs before Agent executes a shell command and can block it; project hooks live in `.cursor/hooks.json`. `beforeMCPExecution` is the matching hook for MCP tool calls. Unlike `.cursorignore`, a hook decides deterministically on the actual command.

A librarian's restricted-shelf list: the librarian will not fetch those books for you, but anyone holding a key to the stacks can walk in and take one. Only a locked cabinet stops both.

  • .cursorignore stops every tool Agent uses from reading the file.
  • Turning on sandboxing makes .cursorignore a security boundary.
  • Repeating the pattern in .gitignore closes the terminal gap.
  • MCP servers honour the project's .cursorignore patterns.
  • Since Agent's read tool refuses the file, the secret is safe.