Skip to content

Commit 1ac6b4b

Browse files
committed
fix(core): authorize external read paths
1 parent 0befd9b commit 1ac6b4b

2 files changed

Lines changed: 80 additions & 22 deletions

File tree

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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
export * as ReadTool from "./read"
22

33
import { ToolFailure } from "@opencode-ai/llm"
4-
import path from "path"
54
import { Effect, Layer, Schema } from "effect"
65
import { FileSystem } from "../filesystem"
7-
import { FSUtil } from "../fs-util"
86
import { Image } from "../image"
9-
import { Location } from "../location"
7+
import { LocationMutation } from "../location-mutation"
108
import { PermissionV2 } from "../permission"
119
import { AbsolutePath } from "../schema"
1210
import { ReadToolFileSystem } from "./read-filesystem"
@@ -30,17 +28,16 @@ const Output = Schema.Union([FileSystem.Content, ReadToolFileSystem.TextPage, Re
3028
export const layer = Layer.effectDiscard(
3129
Effect.gen(function* () {
3230
const tools = yield* Tools.Service
33-
const fs = yield* FSUtil.Service
3431
const reader = yield* ReadToolFileSystem.Service
35-
const location = yield* Location.Service
32+
const mutation = yield* LocationMutation.Service
3633
const image = yield* Image.Service
3734
const permission = yield* PermissionV2.Service
3835

3936
yield* tools
4037
.register({
4138
[name]: Tool.make({
4239
description:
43-
"Read a text file or supported image, page through a large UTF-8 text file by line offset, or list a directory page. Relative paths resolve from the current location; absolute paths are read directly.",
40+
"Read a text file or supported image, page through a large UTF-8 text file by line offset, or list a directory page. Relative paths resolve from the current location; absolute paths inside it are accepted, while external absolute paths require external_directory approval.",
4441
input: Input,
4542
output: Output,
4643
toModelOutput: ({ input, output }) => {
@@ -53,27 +50,33 @@ export const layer = Layer.effectDiscard(
5350
},
5451
execute: (input, context) => {
5552
return Effect.gen(function* () {
56-
const absolute = path.resolve(location.directory, input.path)
57-
const selected = path.isAbsolute(input.path) ? path.dirname(absolute) : location.directory
58-
if (!path.isAbsolute(input.path) && !FSUtil.contains(location.directory, absolute))
59-
return yield* Effect.die(new Error("Path escapes the allowed read root"))
60-
const real = yield* fs.realPath(absolute)
61-
const root = yield* fs.realPath(selected)
62-
if (!FSUtil.contains(root, real))
63-
return yield* Effect.die(new Error("Path escapes the allowed read root"))
64-
const resource = path.relative(root, real).replaceAll("\\", "/") || "."
65-
const target = AbsolutePath.make(real)
66-
const type = yield* reader.inspect(target)
53+
const source = {
54+
type: "tool" as const,
55+
messageID: context.assistantMessageID,
56+
callID: context.toolCallID,
57+
}
58+
const target = yield* mutation.resolve({ path: input.path, kind: "directory" })
59+
const external = target.externalDirectory
60+
if (external)
61+
yield* permission.assert({
62+
...LocationMutation.externalDirectoryPermission(external),
63+
sessionID: context.sessionID,
64+
agent: context.agent,
65+
source,
66+
})
67+
const resource = target.resource
68+
const absolute = AbsolutePath.make(target.canonical)
69+
const type = yield* reader.inspect(absolute)
6770
yield* permission.assert({
6871
action: name,
6972
resources: [resource],
7073
save: ["*"],
7174
sessionID: context.sessionID,
7275
agent: context.agent,
73-
source: { type: "tool", messageID: context.assistantMessageID, callID: context.toolCallID },
76+
source,
7477
})
75-
if (type === "directory") return yield* reader.list(target, { offset: input.offset, limit: input.limit })
76-
const content = yield* reader.read(target, resource, {
78+
if (type === "directory") return yield* reader.list(absolute, { offset: input.offset, limit: input.limit })
79+
const content = yield* reader.read(absolute, resource, {
7780
offset: input.offset,
7881
limit: input.limit,
7982
})

‎packages/core/test/tool-read.test.ts‎

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { PermissionV2 } from "@opencode-ai/core/permission"
1111
import { SessionV2 } from "@opencode-ai/core/session"
1212
import { AbsolutePath } from "@opencode-ai/core/schema"
1313
import { Global } from "@opencode-ai/core/global"
14+
import { LocationMutation } from "@opencode-ai/core/location-mutation"
1415
import { location } from "./fixture/location"
1516
import { ToolRegistry } from "@opencode-ai/core/tool/registry"
1617
import { ReadTool } from "@opencode-ai/core/tool/read"
@@ -97,6 +98,32 @@ const infrastructure = Layer.mergeAll(
9798
Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(process.cwd()) }))),
9899
Global.layerWith({ data: Global.Path.data }),
99100
)
101+
const mutation = Layer.succeed(
102+
LocationMutation.Service,
103+
LocationMutation.Service.of({
104+
resolve: (input) => {
105+
if (input.path === missingPath)
106+
return Effect.fail(new LocationMutation.PathError({ path: input.path, reason: "non_directory_ancestor" }))
107+
const canonical = path.resolve(process.cwd(), input.path)
108+
const external = path.isAbsolute(input.path) && !FSUtil.contains(process.cwd(), canonical)
109+
const resource = external ? canonical.replaceAll("\\", "/") : path.relative(process.cwd(), canonical) || "."
110+
const directory = path.dirname(canonical)
111+
const externalResource = path.join(directory, "*").replaceAll("\\", "/")
112+
return Effect.succeed({
113+
canonical,
114+
resource,
115+
externalDirectory: external
116+
? {
117+
action: "external_directory" as const,
118+
directory,
119+
resource: externalResource,
120+
save: externalResource,
121+
}
122+
: undefined,
123+
})
124+
},
125+
}),
126+
)
100127
const unavailableImage = Layer.succeed(
101128
Image.Service,
102129
Image.Service.of({ normalize: () => Effect.fail(new Image.ResizerUnavailableError()) }),
@@ -107,19 +134,21 @@ const read = ReadTool.layer.pipe(
107134
Layer.provide(permission),
108135
Layer.provide(config),
109136
Layer.provide(image),
137+
Layer.provide(mutation),
110138
Layer.provide(infrastructure),
111139
)
112-
const it = testEffect(Layer.mergeAll(registry, reader, permission, config, image, infrastructure, read))
140+
const it = testEffect(Layer.mergeAll(registry, reader, permission, config, image, mutation, infrastructure, read))
113141
const unavailableRead = ReadTool.layer.pipe(
114142
Layer.provide(registry),
115143
Layer.provide(reader),
116144
Layer.provide(permission),
117145
Layer.provide(config),
118146
Layer.provide(unavailableImage),
147+
Layer.provide(mutation),
119148
Layer.provide(infrastructure),
120149
)
121150
const itWithoutResizer = testEffect(
122-
Layer.mergeAll(registry, reader, permission, config, unavailableImage, infrastructure, unavailableRead),
151+
Layer.mergeAll(registry, reader, permission, config, unavailableImage, mutation, infrastructure, unavailableRead),
123152
)
124153
const sessionID = SessionV2.ID.make("ses_read_tool_test")
125154

@@ -174,6 +203,32 @@ describe("ReadTool", () => {
174203
}),
175204
)
176205

206+
it.effect("asks for external_directory approval before reading an external absolute path", () =>
207+
Effect.gen(function* () {
208+
const registry = yield* ToolRegistry.Service
209+
const external = path.join(path.parse(process.cwd()).root, "external-read", "notes.txt")
210+
211+
expect(
212+
yield* executeTool(registry, {
213+
sessionID,
214+
...toolIdentity,
215+
call: { type: "tool-call", id: "call-external-read", name: "read", input: { path: external } },
216+
}),
217+
).toMatchObject({ type: "json" })
218+
expect(assertions).toMatchObject([
219+
{
220+
sessionID,
221+
action: "external_directory",
222+
resources: [path.join(path.dirname(external), "*").replaceAll("\\", "/")],
223+
},
224+
{ sessionID, action: "read", resources: [external.replaceAll("\\", "/")], save: ["*"] },
225+
])
226+
expect(readCalls).toEqual([
227+
{ input: AbsolutePath.make(external), page: { offset: undefined, limit: undefined } },
228+
])
229+
}),
230+
)
231+
177232
it.effect("returns a small PNG as native media instead of durable base64 text", () =>
178233
Effect.gen(function* () {
179234
const png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="

0 commit comments

Comments
 (0)