opencode icon indicating copy to clipboard operation
opencode copied to clipboard

[SECURITY] allow ignoring files to prevent secrets being leaked from .env to LLM

Open aspiers opened this issue 1 year ago • 28 comments

Forgive me if I've missed it, but I couldn't see any mechanism in the code for ignoring files like .env which very often contain API secrets and other credentials and sensitive data.

The closest I could see was this hard-coded list:

https://github.com/sst/opencode/blob/67480e5a1c71a0b65467a724a7b7afa828f93168/packages/opencode/src/tool/ls.ts#L7-L19

If I'm right, that means that currently it's possible for security-critical data to be leaked to the LLM, which would be a pretty significant security concern. Other agentic coding environments have gone through the same pain, e.g. there are many threads such as this one in the Cursor forums. So it would be great if opencode got this right from early on.

I think it's fairly simple to solve: just support an .opencodeignore file at the top of the repo (and perhaps honour .gitignore by default too).

(On a related but tangential note, ideally everyone would standardise on .ai-ignore, .ai-rules etc. rather than every agent having its own set of config files, but that's a battle for another day ...)

aspiers avatar Jun 29 '25 14:06 aspiers

See https://github.com/google-gemini/gemini-cli/issues/2092 for a report of a similar issue there (although Gemini CLI does at least already respect .gitignore by default). Some approaches to the solution are also suggested in that issue.

aspiers avatar Jun 29 '25 14:06 aspiers

Also https://github.com/anthropics/claude-code/issues/79

aspiers avatar Jun 29 '25 15:06 aspiers

our ls tool should be reworked to use rg which respects gitgnore - i'll do this

thdxr avatar Jun 29 '25 17:06 thdxr

Thanks @thdxr this sounds like a great step in the right direction!

aspiers avatar Jun 29 '25 17:06 aspiers

It reads my entire user directory without a hitch. Is this not a little worrisome? One thing is secrets like encryption, - and API keys, another thing entirely is personal medical documents and whatnot. Would it be possible to implement some sort of whitelist functionality where you explicitly state what folders/files are accessible?

superDuperCyberTechno avatar Jul 11 '25 19:07 superDuperCyberTechno

It reads my entire user directory without a hitch. Is this not a little worrisome? One thing is secrets like encryption, - and API keys, another thing entirely is personal medical documents and whatnot. Would it be possible to implement some sort of whitelist functionality where you explicitly state what folders/files are accessible?

I created a separate issue for that here

In the meantime I'll just use it within a container.

SeanRoberts avatar Jul 12 '25 03:07 SeanRoberts

A few improvements have been shipped in this area list of improvements:

  • Proper permissions system: you can make the llm have to ask you for permission before taking actions
  • agent is locked to cwd by default (can't use read or edit tools on files outside)
  • you can make plugins if you need even more control here is an example from docs:
export const EnvProtection = async ({ client, $ }) => {
  return {
    "tool.execute.before": async (input, output) => {
      if (input.tool === "read" && output.args.filePath.includes(".env")) {
        throw new Error("Do not read .env files")
      }
    }
  }
}

rekram1-node avatar Aug 14 '25 20:08 rekram1-node

My $0.02:

  1. If preventing opencode from reading sensitive files requires the user to go through any process more than an extremely simple configuration task (e.g. adding a file to .ai-ignore), then I don't think the feature can reasonably be considered as implemented. For example, while supporting more sophisticated control via plugins is awesome, if the user is required to write code, then it's an inherently insecure design by default.

  2. I would strongly recommend making ignoring .env part of the default configuration. It's a very well-known industry-wide convention these days that this file typically contains secrets, so feeding those secrets to LLMs by default is really not at all good practice.

aspiers avatar Aug 14 '25 21:08 aspiers

definitely fair point about .env I tend to agree w/ you there

rekram1-node avatar Aug 14 '25 21:08 rekram1-node

agent is locked to cwd by default (can't use read or edit tools on files outside)

It can however, still use shell commands outside the CWD. Would it be feasible to just neuter opencode completely outside the CWD with options to open up its permissions if need be?

superDuperCyberTechno avatar Aug 15 '25 09:08 superDuperCyberTechno

agent is locked to cwd by default (can't use read or edit tools on files outside)

It can however, still use shell commands outside the CWD. Would it be feasible to just neuter opencode completely outside the CWD with options to open up its permissions if need be?

While this is an important topic, and certainly requires attention, I think it belongs in a separate issue, because in the vast majority of cases .env will be inside the CWD.

aspiers avatar Aug 15 '25 10:08 aspiers

@aspiers I will say the llm can't grep for this files but I think we may also want to add extra preventions by default in the read tool too. Thanks for pushing back on this a bit!

rekram1-node avatar Aug 15 '25 13:08 rekram1-node

.env file being ignore fixed with https://github.com/sst/opencode/commit/ff1f4d6bf9fead4683c8ae32da9d6522119783ad?

fkukuck avatar Sep 25 '25 10:09 fkukuck

.env file being ignore fixed with ff1f4d6?

On version 0.15.13 just had grok-code-fast-1 unashamedly read and modify my .env without warning. This doesn't seem to have blocked reading or editing files named .env

mitchell-wallace avatar Oct 22 '25 06:10 mitchell-wallace

Is the __pycache__ directory being force ignored? However, I noticed that the summary operation still reads from it. Here is my screenshot:

Image

hallelujah-shih avatar Oct 27 '25 04:10 hallelujah-shih

Security is not the only reason the give some control to the user on the context that being considered by opencode. For example, I have a project where I use git submodule. There is no point in opencode reading/considering the directory where the submodule is checked out.

Other examples, imagine there is a test data directory - in many cases it won't be relevant for the agent and the user should be able to ignore it. I imagine indeed something like .gitignore or .dockerignore. However, due to the different usages, it might make sense to have a dedicated ignore file .opencodeignore.

drorata avatar Nov 11 '25 11:11 drorata

I think this is a must feature, along with a more fine grained control on what tools and directories the agents can use.

betolink avatar Nov 11 '25 17:11 betolink

We would appreciate .aiignore support!

JosXa avatar Nov 12 '25 09:11 JosXa

OpenCode already supports .ignore files, but it's not blocking, for example, the read tool or grep tool from reading from those files. You can check this plugin, https://github.com/lgladysz/opencode-ignore which extends some blocking functionalities for those tools except the bash tool, so, for example, Agent can read a file through the cat command. Gemini CLI is not blocking that, though.

lgladysz avatar Nov 12 '25 14:11 lgladysz

Fell into this just now. https://github.com/sst/opencode/issues/4308

Kinda concerning that a tool like OpenCode does not have these safeguards by default.

raymelon avatar Nov 13 '25 23:11 raymelon