1- import { expect , test } from "bun:test"
1+ import { afterAll , beforeAll , describe , expect , test } from "bun:test"
22import { CodeModeTool } from "@opencode/core/codemode/tool"
3+ import { Permission } from "@opencode/core/permission"
34import { Tool } from "@opencode/core/tool"
45import { execute } from "@opencode/core/tool/runtime"
56import { Agent } from "@opencode/schema/agent"
67import { Session } from "@opencode/schema/session"
78import { SessionMessage } from "@opencode/schema/session-message"
89import type { Info } from "@opencode/schema/tool"
910import { Effect , Schema } from "effect"
11+ import { permissionService } from "./lib/permission"
1012
1113const context = {
1214 sessionID : Session . ID . make ( "ses_execute" ) ,
@@ -16,8 +18,10 @@ const context = {
1618 progress : ( ) => Effect . void ,
1719}
1820
19- const createCodeMode = ( tools : ReadonlyMap < string , Info > ) =>
20- CodeModeTool . create ( { tools } , ( _ , tool , input , context ) => execute ( tool , input , context ) )
21+ const allowAll = permissionService ( { assert : ( ) => Effect . void } )
22+
23+ const createCodeMode = ( tools : ReadonlyMap < string , Info > , permission = allowAll ) =>
24+ CodeModeTool . create ( { tools } , ( _ , tool , input , context ) => execute ( tool , input , context ) , permission )
2125
2226test ( "execute describes invariant Code Mode behavior" , ( ) => {
2327 expect ( createCodeMode ( new Map ( ) ) . description ) . toBe (
@@ -41,6 +45,65 @@ test("execute rejects filesystem fetches", async () => {
4145 expect ( result . output . output ) . toContain ( "fetch cannot be used for filesystem access." )
4246} )
4347
48+ describe ( "fetch permissions" , ( ) => {
49+ const hits : Array < string > = [ ]
50+ let server : ReturnType < typeof Bun . serve >
51+ beforeAll ( ( ) => {
52+ server = Bun . serve ( {
53+ port : 0 ,
54+ fetch : ( request ) => {
55+ hits . push ( `${ request . method } ${ new URL ( request . url ) . pathname } ` )
56+ return new Response ( "ok" )
57+ } ,
58+ } )
59+ } )
60+ afterAll ( ( ) => server . stop ( true ) )
61+
62+ test ( "fetch asks the webfetch permission for its URL as the execute call" , async ( ) => {
63+ hits . length = 0
64+ const asked : Array < Permission . AssertInput > = [ ]
65+ const url = `${ server . url . origin } /allowed`
66+ const result = await Effect . runPromise (
67+ createCodeMode (
68+ new Map ( ) ,
69+ permissionService ( { assert : ( input ) => Effect . sync ( ( ) => void asked . push ( input ) ) } ) ,
70+ ) . execute ( { code : `return await (await fetch("${ url } ", { method: "POST" })).text()` } , context ) ,
71+ )
72+
73+ expect ( result . output . output ) . toBe ( "ok" )
74+ expect ( hits ) . toEqual ( [ "POST /allowed" ] )
75+ expect ( asked ) . toEqual ( [
76+ {
77+ action : "webfetch" ,
78+ resources : [ url ] ,
79+ save : [ "*" ] ,
80+ metadata : { method : "POST" , url } ,
81+ sessionID : context . sessionID ,
82+ agent : context . agent ,
83+ source : { type : "tool" , messageID : context . messageID , id : context . id } ,
84+ } ,
85+ ] )
86+ } )
87+
88+ test ( "a URL denied for webfetch is never requested" , async ( ) => {
89+ hits . length = 0
90+ const url = `${ server . url . origin } /denied`
91+ const denied = permissionService ( {
92+ assert : ( input ) =>
93+ Effect . fail ( new Permission . BlockedError ( { rules : [ ] , permission : input . action , resources : input . resources } ) ) ,
94+ } )
95+ const result = await Effect . runPromise (
96+ createCodeMode ( new Map ( ) , denied ) . execute (
97+ { code : `try { await fetch("${ url } ") } catch (e) { return "blocked: " + e.message }` } ,
98+ context ,
99+ ) ,
100+ )
101+
102+ expect ( result . output . output ) . toStartWith ( "blocked: " )
103+ expect ( hits ) . toEqual ( [ ] )
104+ } )
105+ } )
106+
44107test ( "canonical execution distinguishes declared, model-only, and raw schema outputs" , async ( ) => {
45108 const declared : Info = {
46109 name : "declared" ,
@@ -177,10 +240,10 @@ test("executions share the prepared runtime of an inventory while keeping per-ca
177240 execute : ( input , context ) => Effect . succeed ( { content : `${ context . id } :${ ( input as { value : string } ) . value } ` } ) ,
178241 }
179242 const inventory = { tools : new Map ( [ [ "echo" , echo ] ] ) }
180- const codeMode = CodeModeTool . create ( inventory , ( _ , tool , input , context ) => execute ( tool , input , context ) )
243+ const codeMode = CodeModeTool . create ( inventory , ( _ , tool , input , context ) => execute ( tool , input , context ) , allowAll )
181244 const run = ( id : string ) => {
182245 const progress : Array < Tool . Metadata > = [ ]
183- return CodeModeTool . create ( inventory , ( _ , tool , input , context ) => execute ( tool , input , context ) )
246+ return CodeModeTool . create ( inventory , ( _ , tool , input , context ) => execute ( tool , input , context ) , allowAll )
184247 . execute (
185248 { code : `return await tools.echo({ value: "${ id } " })` } ,
186249 { ...context , id : Tool . CallID . make ( id ) , progress : ( update ) => Effect . sync ( ( ) => progress . push ( update ) ) } ,
0 commit comments