@@ -27,6 +27,8 @@ const webFetchUserAgent =
2727 "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko); compatible; OpenCode-User/1.0; +https://opencode.ai"
2828const requests : Array < { readonly url : string ; readonly headers : Record < string , string > } > = [ ]
2929const assertions : Permission . AssertInput [ ] = [ ]
30+ const checks : Permission . AssertInput [ ] = [ ]
31+ const denied = new Set < string > ( )
3032let respond = ( _request : HttpClientRequest . HttpClientRequest ) =>
3133 Effect . succeed ( new Response ( "hello" , { headers : { "content-type" : "text/plain" } } ) )
3234
@@ -39,7 +41,19 @@ const http = Layer.succeed(
3941 ) ,
4042 ) ,
4143)
42- const permission = permissionLayer ( { assert : ( input ) => Effect . sync ( ( ) => assertions . push ( input ) ) } )
44+ const permission = permissionLayer ( {
45+ assert : ( input ) => Effect . sync ( ( ) => assertions . push ( input ) ) ,
46+ assertNotDenied : ( input ) =>
47+ Effect . sync ( ( ) => checks . push ( input ) ) . pipe (
48+ Effect . andThen (
49+ input . resources . some ( ( resource ) => denied . has ( resource ) )
50+ ? Effect . fail (
51+ new Permission . BlockedError ( { rules : [ ] , permission : input . action , resources : input . resources } ) ,
52+ )
53+ : Effect . void ,
54+ ) ,
55+ ) ,
56+ } )
4357const toolLayer = ( replacements : LayerNode . Replacements = [ ] ) =>
4458 AppNodeBuilder . build ( LayerNode . group ( [ Tool . node , webFetchToolNode ] ) , [
4559 Permission . node . replace ( permission ) ,
@@ -52,6 +66,8 @@ const live = testEffect(toolLayer())
5266const reset = ( ) => {
5367 requests . length = 0
5468 assertions . length = 0
69+ checks . length = 0
70+ denied . clear ( )
5571 respond = ( ) => Effect . succeed ( new Response ( "hello" , { headers : { "content-type" : "text/plain" } } ) )
5672}
5773
@@ -456,7 +472,7 @@ describe("WebFetchTool registration", () => {
456472 } ) ,
457473 )
458474
459- live . effect ( "follows redirects while approving only the requested URL " , ( ) => {
475+ live . effect ( "follows redirects, checking each target against the rules without asking " , ( ) => {
460476 const received : Array < Record < string , string | null > > = [ ]
461477 return Effect . acquireUseRelease (
462478 Effect . sync ( ( ) =>
@@ -488,6 +504,8 @@ describe("WebFetchTool registration", () => {
488504 expect ( assertions ) . toMatchObject ( [
489505 { sessionID, action : "webfetch" , resources : [ url ] , save : [ "*" ] , metadata : { url, format : "text" } } ,
490506 ] )
507+ const target = new URL ( "/target" , server . url ) . toString ( )
508+ expect ( checks ) . toMatchObject ( [ { sessionID, action : "webfetch" , resources : [ target ] } ] )
491509 expect ( received ) . toEqual (
492510 Array . from ( { length : 2 } , ( ) => ( {
493511 accept : "text/plain;q=1.0, text/markdown;q=0.9, text/html;q=0.8, */*;q=0.1" ,
@@ -501,6 +519,45 @@ describe("WebFetchTool registration", () => {
501519 )
502520 } )
503521
522+ it . effect ( "stops at a redirect target the rules deny" , ( ) =>
523+ Effect . gen ( function * ( ) {
524+ reset ( )
525+ const registry = yield * Tool . Service
526+ const url = "https://example.com/start"
527+ const target = "http://169.254.169.254/latest/meta-data"
528+ denied . add ( target )
529+ respond = ( ) => Effect . succeed ( new Response ( "" , { status : 302 , headers : { location : target } } ) )
530+
531+ expect ( yield * executeTool ( registry , call ( { url, format : "text" } ) ) ) . toMatchObject ( {
532+ status : "error" ,
533+ error : { message : "Permission denied: webfetch" } ,
534+ } )
535+ expect ( checks . map ( ( check ) => check . resources ) ) . toEqual ( [ [ target ] ] )
536+ expect ( requests . map ( ( request ) => request . url ) ) . toEqual ( [ url ] )
537+ } ) ,
538+ )
539+
540+ it . effect ( "rejects redirects to non-HTTP schemes and endless redirect chains" , ( ) =>
541+ Effect . gen ( function * ( ) {
542+ reset ( )
543+ const registry = yield * Tool . Service
544+ respond = ( ) => Effect . succeed ( new Response ( "" , { status : 301 , headers : { location : "file:///etc/passwd" } } ) )
545+ expect ( yield * executeTool ( registry , call ( { url : "https://example.com/a" , format : "text" } ) ) ) . toMatchObject ( {
546+ status : "error" ,
547+ error : { message : "URL must use http:// or https://" } ,
548+ } )
549+ expect ( checks ) . toEqual ( [ ] )
550+
551+ reset ( )
552+ respond = ( request ) => Effect . succeed ( new Response ( "" , { status : 307 , headers : { location : `${ request . url } x` } } ) )
553+ expect ( yield * executeTool ( registry , call ( { url : "https://example.com/a" , format : "text" } ) ) ) . toMatchObject ( {
554+ status : "error" ,
555+ error : { message : `Too many redirects (more than ${ WebFetchTool . MAX_REDIRECTS } )` } ,
556+ } )
557+ expect ( requests ) . toHaveLength ( WebFetchTool . MAX_REDIRECTS + 1 )
558+ } ) ,
559+ )
560+
504561 it . effect ( "rejects non-HTTP schemes before permission or transport" , ( ) =>
505562 Effect . gen ( function * ( ) {
506563 reset ( )
0 commit comments