Skip to content

Commit ea53b55

Browse files
committed
feat: allow writes to /tmp by default without permission prompt
Fixes anomalyco#5386 Fixes anomalyco#4743
1 parent f9d5e18 commit ea53b55

6 files changed

Lines changed: 13 additions & 6 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export const BashTool = Tool.define("bash", async () => {
111111
const agent = await Agent.get(ctx.agent)
112112

113113
const checkExternalDirectory = async (dir: string) => {
114-
if (Filesystem.contains(Instance.directory, dir)) return
114+
if (Filesystem.isAllowedPath(Instance.directory, dir)) return
115115
const title = `This command references paths outside of ${Instance.directory}`
116116
if (agent.permission.external_directory === "ask") {
117117
await Permission.ask({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const EditTool = Tool.define("edit", {
4242
const agent = await Agent.get(ctx.agent)
4343

4444
const filePath = path.isAbsolute(params.filePath) ? params.filePath : path.join(Instance.directory, params.filePath)
45-
if (!Filesystem.contains(Instance.directory, filePath)) {
45+
if (!Filesystem.isAllowedPath(Instance.directory, filePath)) {
4646
const parentDir = path.dirname(filePath)
4747
if (agent.permission.external_directory === "ask") {
4848
await Permission.ask({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const PatchTool = Tool.define("patch", {
5353
for (const hunk of hunks) {
5454
const filePath = path.resolve(Instance.directory, hunk.path)
5555

56-
if (!Filesystem.contains(Instance.directory, filePath)) {
56+
if (!Filesystem.isAllowedPath(Instance.directory, filePath)) {
5757
const parentDir = path.dirname(filePath)
5858
if (agent.permission.external_directory === "ask") {
5959
await Permission.ask({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const ReadTool = Tool.define("read", {
3030
const title = path.relative(Instance.worktree, filepath)
3131
const agent = await Agent.get(ctx.agent)
3232

33-
if (!ctx.extra?.["bypassCwdCheck"] && !Filesystem.contains(Instance.directory, filepath)) {
33+
if (!ctx.extra?.["bypassCwdCheck"] && !Filesystem.isAllowedPath(Instance.directory, filepath)) {
3434
const parentDir = path.dirname(filepath)
3535
if (agent.permission.external_directory === "ask") {
3636
await Permission.ask({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const WriteTool = Tool.define("write", {
2121
const agent = await Agent.get(ctx.agent)
2222

2323
const filepath = path.isAbsolute(params.filePath) ? params.filePath : path.join(Instance.directory, params.filePath)
24-
if (!Filesystem.contains(Instance.directory, filepath)) {
24+
if (!Filesystem.isAllowedPath(Instance.directory, filepath)) {
2525
const parentDir = path.dirname(filepath)
2626
if (agent.permission.external_directory === "ask") {
2727
await Permission.ask({

‎packages/opencode/src/util/filesystem.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { exists } from "fs/promises"
2-
import { dirname, join, relative } from "path"
2+
import { dirname, join, normalize, relative } from "path"
3+
import { tmpdir } from "os"
34

45
export namespace Filesystem {
6+
const systemTmpDir = normalize(tmpdir())
7+
8+
export function isAllowedPath(projectDir: string, filepath: string) {
9+
const normalized = normalize(filepath)
10+
return contains(projectDir, normalized) || contains(systemTmpDir, normalized) || contains("/tmp", normalized)
11+
}
512
export function overlaps(a: string, b: string) {
613
const relA = relative(a, b)
714
const relB = relative(b, a)

0 commit comments

Comments
 (0)