Skip to content

Commit dd636d7

Browse files
bukzorclaude
andcommitted
fix(read): narrow .env file blocking to not block .envrc and other files
The check `filepath.includes(".env")` was blocking any file with ".env" anywhere in the path, including .envrc (direnv config) and source files like environment.ts. Changed to basename-only regex that blocks .env, .env.local, .env.production etc. but allows .envrc and unrelated files. Fixes anomalyco#4969 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 29aaf4f commit dd636d7

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎packages/opencode/src/tool/read.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ export const ReadTool = Tool.define("read", {
6060
}
6161

6262
const block = iife(() => {
63-
const whitelist = [".env.sample", ".example"]
63+
const basename = path.basename(filepath)
64+
const whitelist = [".env.sample", ".env.example", ".example", ".env.template"]
6465

65-
if (whitelist.some((w) => filepath.endsWith(w))) return false
66-
if (filepath.includes(".env")) return true
66+
if (whitelist.some((w) => basename.endsWith(w))) return false
67+
// Block .env, .env.local, .env.production, etc. but not .envrc
68+
if (/^\.env(\.|$)/.test(basename)) return true
6769

6870
return false
6971
})
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, expect, test } from "bun:test"
2+
import path from "path"
3+
import { ReadTool } from "../../src/tool/read"
4+
import { Instance } from "../../src/project/instance"
5+
import { tmpdir } from "../fixture/fixture"
6+
7+
const ctx = {
8+
sessionID: "test",
9+
messageID: "",
10+
callID: "",
11+
agent: "build",
12+
abort: AbortSignal.any([]),
13+
metadata: () => {},
14+
}
15+
16+
describe("tool.read env file blocking", () => {
17+
test.each([
18+
[".env", true],
19+
[".env.local", true],
20+
[".env.production", true],
21+
[".env.sample", false],
22+
[".env.example", false],
23+
[".envrc", false],
24+
["environment.ts", false],
25+
])("%s blocked=%s", async (filename, blocked) => {
26+
await using tmp = await tmpdir({
27+
init: (dir) => Bun.write(path.join(dir, filename), "content"),
28+
})
29+
await Instance.provide({
30+
directory: tmp.path,
31+
fn: async () => {
32+
const read = await ReadTool.init()
33+
const promise = read.execute({ filePath: path.join(tmp.path, filename) }, ctx)
34+
if (blocked) {
35+
await expect(promise).rejects.toThrow("blocked")
36+
} else {
37+
expect((await promise).output).toContain("content")
38+
}
39+
},
40+
})
41+
})
42+
})

0 commit comments

Comments
 (0)