@@ -7,6 +7,9 @@ import DESCRIPTION from "./webfetch.txt"
77import { isImageAttachment } from "@/util/media"
88import { isIP } from "node:net"
99import { lookup } from "node:dns/promises"
10+ import { request as httpsRequest } from "node:https"
11+ import { Readable } from "node:stream"
12+ import type { LookupFunction } from "node:net"
1013
1114const MAX_RESPONSE_SIZE = 5 * 1024 * 1024 // 5MB
1215const DEFAULT_TIMEOUT = 30 * 1000 // 30 seconds
@@ -105,13 +108,18 @@ export function isBlockedAddress(address: string): boolean {
105108 return false
106109}
107110
108- type PinnedTarget = { dial : string ; host ?: string }
111+ type Pin = { address : string ; family : 4 | 6 }
112+ type PinnedTarget = { dial : string ; host ?: string ; pin ?: Pin }
109113
110- // Resolve and vet once, then dial the vetted IP for plain HTTP so a name that
111- // rebinds between check and use cannot reach a private address. HTTPS keeps the
112- // hostname to preserve SNI/certificate validation (residual documented in the lane).
114+ const ALLOWED_PROTOCOLS = new Set ( [ "http:" , "https:" ] )
115+
116+ // Resolve and vet once, then pin the connection to the vetted address so a name
117+ // that rebinds between check and use cannot reach a private address. Plain HTTP
118+ // rewrites the URL host to the vetted IP; HTTPS keeps the hostname for SNI and
119+ // certificate validation and pins the address through a custom DNS lookup.
113120async function pinnedTarget ( raw : string ) : Promise < PinnedTarget > {
114121 const url = new URL ( raw )
122+ if ( ! ALLOWED_PROTOCOLS . has ( url . protocol ) ) throw new Error ( `Unsupported URL scheme: ${ url . protocol } ` )
115123 const hostname = url . hostname . replace ( / ^ \[ | \] $ / g, "" )
116124 if ( isIP ( hostname ) ) {
117125 if ( isBlockedAddress ( hostname ) ) throw new Error ( `Refusing to fetch blocked address: ${ hostname } ` )
@@ -123,11 +131,57 @@ async function pinnedTarget(raw: string): Promise<PinnedTarget> {
123131 if ( isBlockedAddress ( record . address ) )
124132 throw new Error ( `Refusing to fetch ${ hostname } : resolves to ${ record . address } ` )
125133 }
126- if ( url . protocol !== "http:" ) return { dial : raw }
127134 const chosen = records . find ( ( record ) => record . family === 4 ) ?? records [ 0 ]
128- const dial = new URL ( url )
129- dial . hostname = chosen . family === 6 ? `[${ chosen . address } ]` : chosen . address
130- return { dial : dial . toString ( ) , host : url . host }
135+ const family : 4 | 6 = chosen . family === 6 ? 6 : 4
136+ if ( url . protocol === "http:" ) {
137+ const dial = new URL ( url )
138+ dial . hostname = family === 6 ? `[${ chosen . address } ]` : chosen . address
139+ return { dial : dial . toString ( ) , host : url . host }
140+ }
141+ return { dial : raw , pin : { address : chosen . address , family } }
142+ }
143+
144+ // A fetch that resolves the request hostname to the already-vetted address while
145+ // keeping the URL hostname for TLS SNI and certificate validation. Only HTTPS
146+ // requests reach here.
147+ function pinnedFetch ( pin : Pin ) : typeof globalThis . fetch {
148+ const pinnedLookup : LookupFunction = ( _hostname , options , callback ) => {
149+ // Node's happy-eyeballs path requests `all: true` and expects an address list.
150+ if ( options . all ) return callback ( null , [ { address : pin . address , family : pin . family } ] )
151+ return callback ( null , pin . address , pin . family )
152+ }
153+ const fetch = async ( input : RequestInfo | URL , init ?: RequestInit ) : Promise < Response > => {
154+ const raw = typeof input === "string" ? input : input instanceof URL ? input . toString ( ) : input . url
155+ const url = new URL ( raw )
156+ const headers = new Headers ( init ?. headers )
157+ const response = await new Promise < import ( "node:http" ) . IncomingMessage > ( ( resolve , reject ) => {
158+ const req = httpsRequest (
159+ {
160+ protocol : url . protocol ,
161+ hostname : url . hostname ,
162+ port : url . port || 443 ,
163+ path : `${ url . pathname } ${ url . search } ` ,
164+ method : init ?. method ?? "GET" ,
165+ headers : Object . fromEntries ( headers . entries ( ) ) ,
166+ signal : init ?. signal ?? undefined ,
167+ lookup : pinnedLookup ,
168+ } ,
169+ resolve ,
170+ )
171+ req . on ( "error" , reject )
172+ req . end ( )
173+ } )
174+ return new Response ( Readable . toWeb ( response ) as unknown as ReadableStream < Uint8Array > , {
175+ status : response . statusCode ?? 500 ,
176+ statusText : response . statusMessage ,
177+ headers : Object . fromEntries (
178+ Object . entries ( response . headers ) . flatMap ( ( [ key , value ] ) =>
179+ value === undefined ? [ ] : [ [ key , Array . isArray ( value ) ? value . join ( ", " ) : value ] ] ,
180+ ) ,
181+ ) ,
182+ } )
183+ }
184+ return Object . assign ( fetch , { preconnect : ( ) => undefined } )
131185}
132186
133187export async function assertAllowedUrl ( raw : string ) : Promise < void > {
@@ -159,13 +213,20 @@ export const WebFetchTool = Tool.define(
159213 let current = url
160214 let hop = 0
161215 while ( true ) {
162- const target = ctx . extra ?. [ "bypassNetworkCheck" ]
163- ? { dial : current }
164- : yield * Effect . promise ( ( ) => pinnedTarget ( current ) )
216+ // `bypassNetworkCheck` is a test-only escape hatch: `session/tools.ts` builds
217+ // the tool context `extra` from a fixed key set and never sets this flag.
218+ const target =
219+ ctx . extra ?. [ "bypassNetworkCheck" ] === true
220+ ? { dial : current }
221+ : yield * Effect . promise ( ( ) => pinnedTarget ( current ) )
165222 const requestHeaders = target . host ? { ...headers , Host : target . host } : headers
166- const response = yield * http
167- . execute ( HttpClientRequest . get ( target . dial ) . pipe ( HttpClientRequest . setHeaders ( requestHeaders ) ) )
223+ const request = HttpClientRequest . get ( target . dial ) . pipe ( HttpClientRequest . setHeaders ( requestHeaders ) )
224+ const raw = http
225+ . execute ( request )
168226 . pipe ( Effect . provideService ( FetchHttpClient . RequestInit , { redirect : "manual" } ) )
227+ const response = target . pin
228+ ? yield * raw . pipe ( Effect . provideService ( FetchHttpClient . Fetch , pinnedFetch ( target . pin ) ) )
229+ : yield * raw
169230
170231 // Cloudflare challenge 403s clear with an honest User-Agent (the browser UA's TLS fingerprint is rejected).
171232 if (
@@ -182,7 +243,10 @@ export const WebFetchTool = Tool.define(
182243 if ( hop >= MAX_REDIRECTS ) throw new Error ( `Too many redirects (max ${ MAX_REDIRECTS } )` )
183244 const location = response . headers [ "location" ]
184245 if ( ! location ) throw new Error ( "Redirect response missing a Location header" )
185- current = new URL ( location , current ) . toString ( )
246+ const next = new URL ( location , current )
247+ if ( ! ALLOWED_PROTOCOLS . has ( next . protocol ) )
248+ throw new Error ( `Refusing redirect to unsupported scheme: ${ next . protocol } ` )
249+ current = next . toString ( )
186250 hop ++
187251 }
188252 } )
0 commit comments