|
| 1 | +import { describe, expect } from "bun:test" |
| 2 | +import path from "node:path" |
| 3 | +import { pathToFileURL } from "node:url" |
| 4 | +import { Effect, Fiber } from "effect" |
| 5 | +import { Agent } from "@opencode/core/agent" |
| 6 | +import { Database } from "@opencode/core/database/database" |
| 7 | +import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" |
| 8 | +import { Image } from "@opencode/core/image" |
| 9 | +import { Location } from "@opencode/core/location" |
| 10 | +import { Permission } from "@opencode/core/permission" |
| 11 | +import { Project } from "@opencode/core/project" |
| 12 | +import { ProjectTable } from "@opencode/core/project/sql" |
| 13 | +import { Session } from "@opencode/core/session" |
| 14 | +import { SessionTable } from "@opencode/core/session/sql" |
| 15 | +import { Tool } from "@opencode/core/tool" |
| 16 | +import { LayerNode } from "@opencode/util/effect/layer-node" |
| 17 | +import { tempLocationLayer } from "../fixture/location" |
| 18 | +import { tmpdirScoped } from "../fixture/tmpdir" |
| 19 | +import { testEffect } from "../lib/effect" |
| 20 | +import { imagePassthrough } from "../lib/image" |
| 21 | +import { toolIdentity } from "../lib/tool" |
| 22 | + |
| 23 | +const it = testEffect( |
| 24 | + AppNodeBuilder.build(LayerNode.group([Tool.node, Location.node, Database.node, Agent.node, Permission.node]), [ |
| 25 | + Location.node.replace(tempLocationLayer), |
| 26 | + Image.node.replace(imagePassthrough), |
| 27 | + ]), |
| 28 | +) |
| 29 | +const sessionID = Session.ID.make("ses_codemode_web") |
| 30 | +const invocation = { ...toolIdentity, agent: Agent.ID.make("test"), sessionID } |
| 31 | + |
| 32 | +const setup = Effect.fnUntraced(function* (rules: Permission.Ruleset = []) { |
| 33 | + const database = yield* Database.Service |
| 34 | + const location = yield* Location.Service |
| 35 | + yield* database.db |
| 36 | + .insert(ProjectTable) |
| 37 | + .values({ id: Project.ID.global, worktree: location.directory, sandboxes: [] }) |
| 38 | + .onConflictDoNothing() |
| 39 | + .run() |
| 40 | + .pipe(Effect.orDie) |
| 41 | + yield* database.db |
| 42 | + .insert(SessionTable) |
| 43 | + .values({ |
| 44 | + id: sessionID, |
| 45 | + project_id: Project.ID.global, |
| 46 | + slug: "test", |
| 47 | + directory: location.directory, |
| 48 | + title: "test", |
| 49 | + version: "test", |
| 50 | + agent: "test", |
| 51 | + }) |
| 52 | + .onConflictDoNothing() |
| 53 | + .run() |
| 54 | + .pipe(Effect.orDie) |
| 55 | + const agents = yield* Agent.Service |
| 56 | + yield* agents.transform((editor) => |
| 57 | + editor.update(invocation.agent, (agent) => { |
| 58 | + agent.permissions = [{ action: "*", resource: "*", effect: "allow" }, ...rules] |
| 59 | + }), |
| 60 | + ) |
| 61 | +}) |
| 62 | + |
| 63 | +const execute = Effect.fnUntraced(function* (code: string) { |
| 64 | + const tools = yield* Tool.Service |
| 65 | + const snapshot = yield* tools.snapshot() |
| 66 | + return yield* snapshot.execute({ |
| 67 | + ...invocation, |
| 68 | + call: { type: "tool-call", id: "call-fetch", name: "execute", input: { code } }, |
| 69 | + }) |
| 70 | +}) |
| 71 | + |
| 72 | +describe("Code Mode fetch", () => { |
| 73 | + it.live("uses read permissions for encoded file URLs and preserves allowed response methods", () => |
| 74 | + Effect.gen(function* () { |
| 75 | + yield* setup([ |
| 76 | + { action: "read", resource: "*", effect: "deny" }, |
| 77 | + { action: "read", resource: "report # café.txt", effect: "allow" }, |
| 78 | + ]) |
| 79 | + const location = yield* Location.Service |
| 80 | + const file = path.join(location.directory, "report # café.txt") |
| 81 | + yield* Effect.promise(() => Bun.write(file, '{"value":"allowed"}')) |
| 82 | + const url = JSON.stringify(pathToFileURL(file).href) |
| 83 | + const result = yield* execute(` |
| 84 | + const response = await fetch(new URL(${url})) |
| 85 | + return [response.status, response.ok, await response.text(), await response.json(), (await response.bytes()).length] |
| 86 | + `) |
| 87 | + expect(result.metadata?.error).toBeUndefined() |
| 88 | + expect(JSON.parse(result.content[0].type === "text" ? result.content[0].text : "")).toEqual([ |
| 89 | + 200, |
| 90 | + true, |
| 91 | + '{"value":"allowed"}', |
| 92 | + { value: "allowed" }, |
| 93 | + 19, |
| 94 | + ]) |
| 95 | + }), |
| 96 | + ) |
| 97 | + |
| 98 | + for (const action of ["read", "external_directory"]) { |
| 99 | + it.live(`refuses ${action}-denied file reads even when the program catches the error`, () => |
| 100 | + Effect.gen(function* () { |
| 101 | + yield* setup([{ action, resource: "*", effect: "deny" }]) |
| 102 | + const location = yield* Location.Service |
| 103 | + const external = yield* tmpdirScoped() |
| 104 | + const file = path.join(action === "read" ? location.directory : external.path, "secret.txt") |
| 105 | + yield* Effect.promise(() => Bun.write(file, "must not be disclosed")) |
| 106 | + const result = yield* execute(` |
| 107 | + try { return await (await fetch(${JSON.stringify(pathToFileURL(file).href)})).text() } |
| 108 | + catch (error) { return error.message } |
| 109 | + `) |
| 110 | + expect(result.content).toEqual([{ type: "text", text: `Permission denied: ${action}` }]) |
| 111 | + }), |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + it.live("waits for file approval and attributes it to the invoking session and tool call", () => |
| 116 | + Effect.gen(function* () { |
| 117 | + yield* setup([{ action: "read", resource: "*", effect: "ask" }]) |
| 118 | + const location = yield* Location.Service |
| 119 | + const file = path.join(location.directory, "pending.txt") |
| 120 | + yield* Effect.promise(() => Bun.write(file, "before approval")) |
| 121 | + const permissions = yield* Permission.Service |
| 122 | + const fiber = yield* execute( |
| 123 | + `return await (await fetch(${JSON.stringify(pathToFileURL(file).href)})).text()`, |
| 124 | + ).pipe(Effect.forkScoped) |
| 125 | + const request = yield* Effect.gen(function* () { |
| 126 | + while (true) { |
| 127 | + const pending = yield* permissions.forSession(sessionID) |
| 128 | + if (pending[0]) return pending[0] |
| 129 | + yield* Effect.promise(() => Bun.sleep(1)) |
| 130 | + } |
| 131 | + }).pipe(Effect.timeout("5 seconds")) |
| 132 | + expect(request).toMatchObject({ |
| 133 | + sessionID, |
| 134 | + action: "read", |
| 135 | + resources: ["pending.txt"], |
| 136 | + source: { type: "tool", messageID: invocation.messageID, id: "call-fetch" }, |
| 137 | + }) |
| 138 | + yield* Effect.promise(() => Bun.write(file, "after approval")) |
| 139 | + yield* permissions.reply({ requestID: request.id, reply: "once" }) |
| 140 | + expect((yield* Fiber.join(fiber)).content).toEqual([{ type: "text", text: "after approval" }]) |
| 141 | + }), |
| 142 | + ) |
| 143 | + |
| 144 | + it.live("cancels pending file approval when the execution is interrupted", () => |
| 145 | + Effect.gen(function* () { |
| 146 | + yield* setup([{ action: "read", resource: "*", effect: "ask" }]) |
| 147 | + const location = yield* Location.Service |
| 148 | + const permissions = yield* Permission.Service |
| 149 | + const fiber = yield* execute( |
| 150 | + `return await fetch(${JSON.stringify(pathToFileURL(path.join(location.directory, "pending.txt")).href)})`, |
| 151 | + ).pipe(Effect.forkScoped) |
| 152 | + yield* Effect.gen(function* () { |
| 153 | + while ((yield* permissions.forSession(sessionID)).length === 0) yield* Effect.promise(() => Bun.sleep(1)) |
| 154 | + }).pipe(Effect.timeout("5 seconds")) |
| 155 | + yield* Fiber.interrupt(fiber) |
| 156 | + expect(yield* permissions.forSession(sessionID)).toEqual([]) |
| 157 | + }), |
| 158 | + ) |
| 159 | + |
| 160 | + it.live("preserves Bun's file method handling and non-file URL schemes", () => |
| 161 | + Effect.gen(function* () { |
| 162 | + yield* setup() |
| 163 | + const location = yield* Location.Service |
| 164 | + const file = path.join(location.directory, "unchanged.txt") |
| 165 | + yield* Effect.promise(() => Bun.write(file, "unchanged")) |
| 166 | + const result = yield* execute(` |
| 167 | + const values = [] |
| 168 | + for (const init of [ |
| 169 | + {}, |
| 170 | + { method: "HEAD" }, |
| 171 | + { method: "POST", body: "replacement" }, |
| 172 | + { method: "PUT", body: "replacement" }, |
| 173 | + { method: "PATCH", body: "replacement" }, |
| 174 | + { method: "DELETE" }, |
| 175 | + { body: "replacement" }, |
| 176 | + ]) { |
| 177 | + values.push(await (await fetch(${JSON.stringify(pathToFileURL(file).href)}, init)).text()) |
| 178 | + } |
| 179 | + values.push(await (await fetch("data:text/plain,hello")).text()) |
| 180 | + return values |
| 181 | + `) |
| 182 | + expect(result.content[0].type === "text" ? JSON.parse(result.content[0].text) : []).toEqual([ |
| 183 | + ...Array(7).fill("unchanged"), |
| 184 | + "hello", |
| 185 | + ]) |
| 186 | + expect(yield* Effect.promise(() => Bun.file(file).text())).toBe("unchanged") |
| 187 | + }), |
| 188 | + ) |
| 189 | + |
| 190 | + it.live("preserves HTTP redirects and rejects redirects to file URLs", () => |
| 191 | + Effect.gen(function* () { |
| 192 | + yield* setup([{ action: "read", resource: "*", effect: "deny" }]) |
| 193 | + const location = yield* Location.Service |
| 194 | + const file = path.join(location.directory, "secret.txt") |
| 195 | + yield* Effect.promise(() => Bun.write(file, "must not be disclosed")) |
| 196 | + const server = yield* Effect.acquireRelease( |
| 197 | + Effect.sync(() => |
| 198 | + Bun.serve({ |
| 199 | + port: 0, |
| 200 | + fetch: (request) => { |
| 201 | + const url = new URL(request.url) |
| 202 | + if (url.pathname === "/file") return Response.redirect(pathToFileURL(file).href, 302) |
| 203 | + if (url.pathname === "/redirect") return Response.redirect(new URL("/target", url).href, 302) |
| 204 | + return Response.json({ value: "network" }) |
| 205 | + }, |
| 206 | + }), |
| 207 | + ), |
| 208 | + (server) => Effect.promise(() => server.stop(true)), |
| 209 | + ) |
| 210 | + const allowed = yield* execute( |
| 211 | + `return await (await fetch(${JSON.stringify(new URL("/redirect", server.url).href)})).json()`, |
| 212 | + ) |
| 213 | + expect(allowed.content).toEqual([{ type: "text", text: '{\n "value": "network"\n}' }]) |
| 214 | + const denied = yield* execute( |
| 215 | + `return await (await fetch(${JSON.stringify(new URL("/file", server.url).href)})).text()`, |
| 216 | + ) |
| 217 | + expect(denied.metadata?.error).toBe(true) |
| 218 | + expect(JSON.stringify(denied.content)).not.toContain("must not be disclosed") |
| 219 | + }), |
| 220 | + ) |
| 221 | +}) |
0 commit comments