mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-22 03:50:06 +00:00
fix(core): apply webfetch permissions to Code Mode fetch (#59954)
This commit is contained in:
parent
ab60ee227b
commit
bdca40ac0e
10 changed files with 208 additions and 72 deletions
7
packages/core/src/codemode/execution.ts
Normal file
7
packages/core/src/codemode/execution.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
export * as CodeModeExecution from "./execution.js"
|
||||
|
||||
import type { Context as ToolContext } from "@opencode/schema/tool"
|
||||
import { Context } from "effect"
|
||||
|
||||
/** The `execute` call a Code Mode program runs under, so extensions can act for its session like a tool does. */
|
||||
export class Service extends Context.Service<Service, ToolContext>()("@opencode/CodeModeExecution") {}
|
||||
78
packages/core/src/codemode/fetch.ts
Normal file
78
packages/core/src/codemode/fetch.ts
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
export * as CodeModeFetch from "./fetch.js"
|
||||
|
||||
import { Extension } from "@opencode/codemode"
|
||||
import { isRecord } from "@opencode/util/record"
|
||||
import { Effect } from "effect"
|
||||
import { Permission } from "../permission.js"
|
||||
import { CodeModeExecution } from "./execution.js"
|
||||
|
||||
const TIMEOUT_MS = 30_000
|
||||
|
||||
type Init = {
|
||||
readonly method?: string
|
||||
readonly headers?: Record<string, string> | Array<[string, string]>
|
||||
readonly body?: string | Uint8Array<ArrayBuffer> | URLSearchParams
|
||||
}
|
||||
|
||||
const fetch = (input: string | URL, init: Init = {}) =>
|
||||
Effect.gen(function* () {
|
||||
const url = String(input)
|
||||
if (URL.parse(url)?.protocol === "file:")
|
||||
return yield* Effect.fail(new Error("fetch cannot be used for filesystem access."))
|
||||
const execution = yield* CodeModeExecution.Service
|
||||
const permission = yield* Permission.Service
|
||||
// The same rules as the webfetch tool, so a URL denied there is denied here.
|
||||
yield* permission
|
||||
.assert({
|
||||
action: "webfetch",
|
||||
resources: [url],
|
||||
save: ["*"],
|
||||
metadata: display([input, init]),
|
||||
sessionID: execution.sessionID,
|
||||
agent: execution.agent,
|
||||
source: { type: "tool", messageID: execution.messageID, id: execution.id },
|
||||
})
|
||||
.pipe(
|
||||
// The feedback is the user talking to the model, so it must reach the program as the error's message.
|
||||
Effect.catchTag("Permission.CorrectedError", (error) =>
|
||||
Effect.fail(new Error(`The user rejected fetching ${url}: ${error.feedback}`)),
|
||||
),
|
||||
)
|
||||
return yield* Effect.tryPromise({ try: () => send(input, init), catch: (error) => error })
|
||||
})
|
||||
|
||||
const send = async (input: string | URL, init: Init) => {
|
||||
const response = await globalThis.fetch(input, {
|
||||
method: init.method,
|
||||
headers: init.headers,
|
||||
body: init.body,
|
||||
signal: AbortSignal.timeout(TIMEOUT_MS),
|
||||
})
|
||||
const bytes = await response.bytes()
|
||||
const headers = Object.fromEntries(response.headers)
|
||||
const text = () => new TextDecoder().decode(bytes)
|
||||
return {
|
||||
url: response.url,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
ok: response.ok,
|
||||
redirected: response.redirected,
|
||||
headers: {
|
||||
get: (name: string) => headers[name.toLowerCase()] ?? null,
|
||||
has: (name: string) => name.toLowerCase() in headers,
|
||||
entries: () => Object.entries(headers),
|
||||
},
|
||||
text: async () => text(),
|
||||
json: async () => JSON.parse(text()) as unknown,
|
||||
bytes: async () => bytes,
|
||||
}
|
||||
}
|
||||
|
||||
export const extension = Extension.make({ name: "fetch", globals: { fetch: Extension.async(fetch) } })
|
||||
|
||||
/** What to show for a fetch call: its method and URL. */
|
||||
export const display = (args: ReadonlyArray<unknown>) => {
|
||||
const [input, init] = args
|
||||
const method = isRecord(init) && typeof init.method === "string" ? init.method.toUpperCase() : "GET"
|
||||
return { method, url: String(input) }
|
||||
}
|
||||
|
|
@ -14,8 +14,10 @@ import type {
|
|||
import { Clock, Context, Effect, Ref, Schema, Semaphore } from "effect"
|
||||
import { toSessionError } from "../session/to-session-error.js"
|
||||
import { definition, normalizedName } from "../tool/runtime.js"
|
||||
import { Permission } from "../permission.js"
|
||||
import { CodeModeCatalog } from "./catalog.js"
|
||||
import { CodeModeWeb } from "./web.js"
|
||||
import { CodeModeExecution } from "./execution.js"
|
||||
import { CodeModeFetch } from "./fetch.js"
|
||||
|
||||
const ExecuteFile = Schema.Struct({
|
||||
data: Schema.String,
|
||||
|
|
@ -82,6 +84,7 @@ const description = [
|
|||
export const create = (
|
||||
inventory: Inventory,
|
||||
executeTool: (name: string, tool: Info, input: unknown, context: ToolContext) => Effect.Effect<Result, Error>,
|
||||
permission: Permission.Interface,
|
||||
) => {
|
||||
return {
|
||||
name: "execute",
|
||||
|
|
@ -142,6 +145,8 @@ export const create = (
|
|||
Effect.onError(() =>
|
||||
lock.withPermit(Ref.get(trailing).pipe(Effect.flatMap((pending) => (pending ? publish : Effect.void)))),
|
||||
),
|
||||
Effect.provideService(CodeModeExecution.Service, context),
|
||||
Effect.provideService(Permission.Service, permission),
|
||||
)
|
||||
const toolCalls = yield* Ref.get(calls)
|
||||
const collected = (yield* Ref.get(files))
|
||||
|
|
@ -203,7 +208,7 @@ function progressHooks(record: (update: (items: Array<ExecuteCall>) => Array<Exe
|
|||
"extension.before": (call) => {
|
||||
switch (call.name) {
|
||||
case "fetch":
|
||||
return start(call, { tool: call.name, status: "running", input: CodeModeWeb.display(call.args) })
|
||||
return start(call, { tool: call.name, status: "running", input: CodeModeFetch.display(call.args) })
|
||||
default:
|
||||
return Effect.void
|
||||
}
|
||||
|
|
@ -257,15 +262,15 @@ function renderCatalog(root: CatalogNode): ReadonlyArray<CodeModeCatalog.Tool |
|
|||
|
||||
// Inventories are immutable snapshots, so tool definitions, schemas, and the search index are built
|
||||
// once per inventory and shared by its catalog and every execution.
|
||||
const runtimes = new WeakMap<Inventory, CodeMode.Runtime<Run>>()
|
||||
const runtimes = new WeakMap<Inventory, CodeMode.Runtime<Run | CodeModeExecution.Service | Permission.Service>>()
|
||||
|
||||
function runtime(inventory: Inventory) {
|
||||
const cached = runtimes.get(inventory)
|
||||
if (cached) return cached
|
||||
const tools = inventoryTools(inventory)
|
||||
const created = CodeMode.make<typeof tools, [typeof CodeModeWeb.extension]>({
|
||||
const created = CodeMode.make<typeof tools, [typeof CodeModeFetch.extension]>({
|
||||
tools,
|
||||
extensions: [CodeModeWeb.extension],
|
||||
extensions: [CodeModeFetch.extension],
|
||||
hooks: runHooks,
|
||||
})
|
||||
runtimes.set(inventory, created)
|
||||
|
|
|
|||
|
|
@ -1,50 +0,0 @@
|
|||
export * as CodeModeWeb from "./web.js"
|
||||
|
||||
import { Extension } from "@opencode/codemode"
|
||||
import { isRecord } from "@opencode/util/record"
|
||||
|
||||
const TIMEOUT_MS = 30_000
|
||||
|
||||
type Init = {
|
||||
readonly method?: string
|
||||
readonly headers?: Record<string, string> | Array<[string, string]>
|
||||
readonly body?: string | Uint8Array<ArrayBuffer> | URLSearchParams
|
||||
}
|
||||
|
||||
const fetch = async (input: string | URL, init: Init = {}) => {
|
||||
if (URL.parse(String(input))?.protocol === "file:")
|
||||
throw new Error("fetch cannot be used for filesystem access.")
|
||||
const response = await globalThis.fetch(input, {
|
||||
method: init.method,
|
||||
headers: init.headers,
|
||||
body: init.body,
|
||||
signal: AbortSignal.timeout(TIMEOUT_MS),
|
||||
})
|
||||
const bytes = await response.bytes()
|
||||
const headers = Object.fromEntries(response.headers)
|
||||
const text = () => new TextDecoder().decode(bytes)
|
||||
return {
|
||||
url: response.url,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
ok: response.ok,
|
||||
redirected: response.redirected,
|
||||
headers: {
|
||||
get: (name: string) => headers[name.toLowerCase()] ?? null,
|
||||
has: (name: string) => name.toLowerCase() in headers,
|
||||
entries: () => Object.entries(headers),
|
||||
},
|
||||
text: async () => text(),
|
||||
json: async () => JSON.parse(text()) as unknown,
|
||||
bytes: async () => bytes,
|
||||
}
|
||||
}
|
||||
|
||||
export const extension = Extension.make({ name: "web", globals: { fetch } })
|
||||
|
||||
/** What to show for a fetch call: its method and URL. */
|
||||
export const display = (args: ReadonlyArray<unknown>) => {
|
||||
const [input, init] = args
|
||||
const method = isRecord(init) && typeof init.method === "string" ? init.method.toUpperCase() : "GET"
|
||||
return { method, url: String(input) }
|
||||
}
|
||||
|
|
@ -86,6 +86,7 @@ const layer = Layer.effect(
|
|||
Effect.gen(function* () {
|
||||
const hooks = yield* PluginHooks.Service
|
||||
const image = yield* Image.Service
|
||||
const permission = yield* Permission.Service
|
||||
|
||||
type NormalizedItem = Tool.Content | "decode" | "size"
|
||||
const normalizeImages = Effect.fnUntraced(function* (content: ReadonlyArray<Tool.Content>) {
|
||||
|
|
@ -283,10 +284,13 @@ const layer = Layer.effect(
|
|||
const cached = catalog?.data === data && catalog.names === names ? catalog : undefined
|
||||
const codeModeInventory = cached?.inventory ?? { tools: codeModeTools, namespaces: data.namespaces }
|
||||
const codeModeTool = codeModeEnabled
|
||||
? CodeModeTool.create(codeModeInventory, (name, tool, input, context) =>
|
||||
beforeExecute(name, input, context).pipe(
|
||||
Effect.flatMap((event) => executeTool(tool, name, event.input, context)),
|
||||
),
|
||||
? CodeModeTool.create(
|
||||
codeModeInventory,
|
||||
(name, tool, input, context) =>
|
||||
beforeExecute(name, input, context).pipe(
|
||||
Effect.flatMap((event) => executeTool(tool, name, event.input, context)),
|
||||
),
|
||||
permission,
|
||||
)
|
||||
: undefined
|
||||
const codeModeCatalog = !codeModeEnabled
|
||||
|
|
@ -413,5 +417,5 @@ function namespaceError(name: string) {
|
|||
export const node = makeLocationNode({
|
||||
service: Service,
|
||||
layer,
|
||||
deps: [PluginHooks.node, Image.node],
|
||||
deps: [PluginHooks.node, Image.node, Permission.node],
|
||||
})
|
||||
|
|
|
|||
|
|
@ -3,3 +3,10 @@ import { Effect, Layer } from "effect"
|
|||
|
||||
export const permissionLayer = (overrides: Partial<Permission.Interface> = {}) =>
|
||||
Layer.mock(Permission.Service, { close: Effect.void, ...overrides })
|
||||
|
||||
export const permissionService = (overrides: Partial<Permission.Interface> = {}) =>
|
||||
Effect.runSync(
|
||||
Effect.gen(function* () {
|
||||
return yield* Permission.Service
|
||||
}).pipe(Effect.provide(permissionLayer(overrides))),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { expect, test } from "bun:test"
|
||||
import { afterAll, beforeAll, describe, expect, test } from "bun:test"
|
||||
import { CodeModeTool } from "@opencode/core/codemode/tool"
|
||||
import { Permission } from "@opencode/core/permission"
|
||||
import { Tool } from "@opencode/core/tool"
|
||||
import { execute } from "@opencode/core/tool/runtime"
|
||||
import { Agent } from "@opencode/schema/agent"
|
||||
|
|
@ -9,6 +10,7 @@ import type { Info } from "@opencode/schema/tool"
|
|||
import { Deferred, Effect, Fiber, Schema } from "effect"
|
||||
import { TestClock } from "effect/testing"
|
||||
import { it } from "./lib/effect"
|
||||
import { permissionService } from "./lib/permission"
|
||||
|
||||
const context = {
|
||||
sessionID: Session.ID.make("ses_execute"),
|
||||
|
|
@ -18,8 +20,10 @@ const context = {
|
|||
progress: () => Effect.void,
|
||||
}
|
||||
|
||||
const createCodeMode = (tools: ReadonlyMap<string, Info>) =>
|
||||
CodeModeTool.create({ tools }, (_, tool, input, context) => execute(tool, input, context))
|
||||
const allowAll = permissionService({ assert: () => Effect.void })
|
||||
|
||||
const createCodeMode = (tools: ReadonlyMap<string, Info>, permission = allowAll) =>
|
||||
CodeModeTool.create({ tools }, (_, tool, input, context) => execute(tool, input, context), permission)
|
||||
|
||||
test("execute describes invariant Code Mode behavior", () => {
|
||||
expect(createCodeMode(new Map()).description).toBe(
|
||||
|
|
@ -43,6 +47,82 @@ test("execute rejects filesystem fetches", async () => {
|
|||
expect(result.output.output).toContain("fetch cannot be used for filesystem access.")
|
||||
})
|
||||
|
||||
describe("fetch permissions", () => {
|
||||
const hits: Array<string> = []
|
||||
let server: ReturnType<typeof Bun.serve>
|
||||
beforeAll(() => {
|
||||
server = Bun.serve({
|
||||
port: 0,
|
||||
fetch: (request) => {
|
||||
hits.push(`${request.method} ${new URL(request.url).pathname}`)
|
||||
return new Response("ok")
|
||||
},
|
||||
})
|
||||
})
|
||||
afterAll(() => server.stop(true))
|
||||
|
||||
test("fetch asks the webfetch permission for its URL as the execute call", async () => {
|
||||
hits.length = 0
|
||||
const asked: Array<Permission.AssertInput> = []
|
||||
const url = `${server.url.origin}/allowed`
|
||||
const result = await Effect.runPromise(
|
||||
createCodeMode(
|
||||
new Map(),
|
||||
permissionService({ assert: (input) => Effect.sync(() => void asked.push(input)) }),
|
||||
).execute({ code: `return await (await fetch("${url}", { method: "POST" })).text()` }, context),
|
||||
)
|
||||
|
||||
expect(result.output.output).toBe("ok")
|
||||
expect(hits).toEqual(["POST /allowed"])
|
||||
expect(asked).toEqual([
|
||||
{
|
||||
action: "webfetch",
|
||||
resources: [url],
|
||||
save: ["*"],
|
||||
metadata: { method: "POST", url },
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
test("a rejection with feedback reaches the program as the error message", async () => {
|
||||
hits.length = 0
|
||||
const url = `${server.url.origin}/corrected`
|
||||
const corrected = permissionService({
|
||||
assert: () => Effect.fail(new Permission.CorrectedError({ feedback: "use the docs site instead" })),
|
||||
})
|
||||
const result = await Effect.runPromise(
|
||||
createCodeMode(new Map(), corrected).execute(
|
||||
{ code: `try { await fetch("${url}") } catch (e) { return e.message }` },
|
||||
context,
|
||||
),
|
||||
)
|
||||
|
||||
expect(result.output.output).toBe(`The user rejected fetching ${url}: use the docs site instead`)
|
||||
expect(hits).toEqual([])
|
||||
})
|
||||
|
||||
test("a URL denied for webfetch is never requested and rejects like a failed fetch", async () => {
|
||||
hits.length = 0
|
||||
const url = `${server.url.origin}/denied`
|
||||
const denied = permissionService({
|
||||
assert: (input) =>
|
||||
Effect.fail(new Permission.BlockedError({ rules: [], permission: input.action, resources: input.resources })),
|
||||
})
|
||||
const result = await Effect.runPromise(
|
||||
createCodeMode(new Map(), denied).execute(
|
||||
{ code: `const [denied] = await Promise.allSettled([fetch("${url}")]); return denied.status` },
|
||||
context,
|
||||
),
|
||||
)
|
||||
|
||||
expect(result.output.output).toBe("rejected")
|
||||
expect(hits).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
test("canonical execution distinguishes declared, model-only, and raw schema outputs", async () => {
|
||||
const declared: Info = {
|
||||
name: "declared",
|
||||
|
|
@ -179,10 +259,10 @@ test("executions share the prepared runtime of an inventory while keeping per-ca
|
|||
execute: (input, context) => Effect.succeed({ content: `${context.id}:${(input as { value: string }).value}` }),
|
||||
}
|
||||
const inventory = { tools: new Map([["echo", echo]]) }
|
||||
const codeMode = CodeModeTool.create(inventory, (_, tool, input, context) => execute(tool, input, context))
|
||||
const codeMode = CodeModeTool.create(inventory, (_, tool, input, context) => execute(tool, input, context), allowAll)
|
||||
const run = (id: string) => {
|
||||
const progress: Array<Tool.Metadata> = []
|
||||
return CodeModeTool.create(inventory, (_, tool, input, context) => execute(tool, input, context))
|
||||
return CodeModeTool.create(inventory, (_, tool, input, context) => execute(tool, input, context), allowAll)
|
||||
.execute(
|
||||
{ code: `return await tools.echo({ value: "${id}" })` },
|
||||
{ ...context, id: Tool.CallID.make(id), progress: (update) => Effect.sync(() => progress.push(update)) },
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { describe, expect } from "bun:test"
|
||||
import { Agent } from "@opencode/core/agent"
|
||||
import type { Permission } from "@opencode/core/permission"
|
||||
import { Permission } from "@opencode/core/permission"
|
||||
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
|
||||
import { Image } from "@opencode/core/image"
|
||||
import { PluginHooks } from "@opencode/core/plugin/hooks"
|
||||
|
|
@ -14,6 +14,7 @@ import { codeModeListings, executeTool, toolDefinitions } from "./lib/tool"
|
|||
import { Deferred, Effect, Exit, Fiber, Layer, Logger, Schema, SchemaGetter, SchemaIssue, Scope } from "effect"
|
||||
import { z } from "zod"
|
||||
import { testEffect } from "./lib/effect"
|
||||
import { permissionLayer } from "./lib/permission"
|
||||
|
||||
const imageStore = Layer.mock(Image.Service, {
|
||||
normalize: (resource, content) => {
|
||||
|
|
@ -39,6 +40,7 @@ const imageStore = Layer.mock(Image.Service, {
|
|||
})
|
||||
const registryLayer = AppNodeBuilder.build(LayerNode.group([Tool.node, PluginHooks.node]), [
|
||||
Image.node.replace(imageStore),
|
||||
Permission.node.replace(permissionLayer()),
|
||||
])
|
||||
const it = testEffect(registryLayer)
|
||||
const identity = {
|
||||
|
|
@ -719,10 +721,7 @@ describe("Tool", () => {
|
|||
expect(snapshot.codeModeCatalog).toBeUndefined()
|
||||
expect((yield* snapshot.execute(call("mcp_lookup"))).output).toEqual({ text: "mcp_lookup" })
|
||||
|
||||
const hidden = yield* service.snapshot([
|
||||
...denyExecute,
|
||||
{ action: "mcp_lookup", resource: "*", effect: "deny" },
|
||||
])
|
||||
const hidden = yield* service.snapshot([...denyExecute, { action: "mcp_lookup", resource: "*", effect: "deny" }])
|
||||
expect(hidden.definitions.map((tool) => tool.name)).toEqual(["bash"])
|
||||
expect(yield* hidden.execute(call("mcp_lookup")).pipe(Effect.flip)).toBeInstanceOf(Tool.Error)
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { DateTime, Effect, Layer, Stream } from "effect"
|
|||
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
|
||||
import { Config } from "@opencode/core/config"
|
||||
import { Image } from "@opencode/core/image"
|
||||
import { Permission } from "@opencode/core/permission"
|
||||
import { Schedule } from "@opencode/core/schedule"
|
||||
import { Session } from "@opencode/core/session"
|
||||
import { Tool } from "@opencode/core/tool"
|
||||
|
|
@ -22,7 +23,9 @@ const names = ["schedule_create", "schedule_list", "schedule_pause", "schedule_r
|
|||
(name) => `opencode.${name}`,
|
||||
)
|
||||
|
||||
const it = testEffect(AppNodeBuilder.build(Tool.node, [Image.node.replace(imagePassthrough)]))
|
||||
const it = testEffect(
|
||||
AppNodeBuilder.build(Tool.node, [Image.node.replace(imagePassthrough), Permission.node.replace(permissionLayer())]),
|
||||
)
|
||||
|
||||
/** An in-memory Schedule service. The real one is covered in schedule.test.ts; this exercises the tool layer. */
|
||||
const memory = () => {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { Effect, Layer, Stream } from "effect"
|
|||
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
|
||||
import { Config } from "@opencode/core/config"
|
||||
import { Image } from "@opencode/core/image"
|
||||
import { Permission } from "@opencode/core/permission"
|
||||
import { Session } from "@opencode/core/session"
|
||||
import { Tool } from "@opencode/core/tool"
|
||||
import { TodoTool } from "@opencode/core/tool/plugin/todo"
|
||||
|
|
@ -22,7 +23,9 @@ const todos = [
|
|||
{ content: "Rewrite everything", status: "cancelled" },
|
||||
] as const
|
||||
|
||||
const it = testEffect(AppNodeBuilder.build(Tool.node, [Image.node.replace(imagePassthrough)]))
|
||||
const it = testEffect(
|
||||
AppNodeBuilder.build(Tool.node, [Image.node.replace(imagePassthrough), Permission.node.replace(permissionLayer())]),
|
||||
)
|
||||
|
||||
const setup = (experimental: ConfigExperimental.Info | undefined) =>
|
||||
Effect.gen(function* () {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue