@@ -104,6 +104,53 @@ for (const [network, prefix] of [
104104 NON_PUBLIC_ADDRESSES . addSubnet ( network , prefix , "ipv6" ) ;
105105}
106106
107+ // Globally reachable assignments inside those ranges (anycast services and
108+ // the like), allowed as the fetch server allows its _PUBLIC_EXCEPTIONS.
109+ const PUBLIC_EXCEPTIONS = new BlockList ( ) ;
110+ PUBLIC_EXCEPTIONS . addAddress ( "192.0.0.9" , "ipv4" ) ;
111+ PUBLIC_EXCEPTIONS . addAddress ( "192.0.0.10" , "ipv4" ) ;
112+ for ( const [ network , prefix ] of [
113+ [ "2001:1::1" , 128 ] ,
114+ [ "2001:1::2" , 128 ] ,
115+ [ "2001:3::" , 32 ] ,
116+ [ "2001:4:112::" , 48 ] ,
117+ [ "2001:20::" , 28 ] ,
118+ [ "2001:30::" , 28 ] ,
119+ ] as const ) {
120+ PUBLIC_EXCEPTIONS . addSubnet ( network , prefix , "ipv6" ) ;
121+ }
122+
123+ /** True when `address` is not a public destination. */
124+ function isNonPublic ( address : string , family : number ) : boolean {
125+ const type = family === 6 ? "ipv6" : "ipv4" ;
126+ return (
127+ ! PUBLIC_EXCEPTIONS . check ( address , type ) &&
128+ NON_PUBLIC_ADDRESSES . check ( address , type )
129+ ) ;
130+ }
131+
132+ /**
133+ * Settles as `promise` does, or rejects with the signal's reason as soon as it
134+ * aborts, so that the fetch deadline also bounds work that takes no signal
135+ * (the DNS lookup).
136+ */
137+ async function raceAbort < T > (
138+ promise : Promise < T > ,
139+ signal : AbortSignal ,
140+ ) : Promise < T > {
141+ signal . throwIfAborted ( ) ;
142+ let onAbort = ( ) => { } ;
143+ const aborted = new Promise < never > ( ( _ , reject ) => {
144+ onAbort = ( ) => reject ( signal . reason ) ;
145+ signal . addEventListener ( "abort" , onAbort , { once : true } ) ;
146+ } ) ;
147+ try {
148+ return await Promise . race ( [ promise , aborted ] ) ;
149+ } finally {
150+ signal . removeEventListener ( "abort" , onAbort ) ;
151+ }
152+ }
153+
107154// Tool input schema
108155const GZipFileAsResourceSchema = z . object ( {
109156 name : z
@@ -221,9 +268,13 @@ export const registerGZipFileAsResourceTool = (
221268 * checked address for the connection, which this demo server does not do.
222269 *
223270 * @param {URL } url The URL about to be requested.
271+ * @param {AbortSignal } signal The fetch's deadline, which bounds the lookup too.
224272 * @throws {Error } If the request is refused, or the host cannot be resolved.
225273 */
226- async function assertPermittedTarget ( url : URL ) : Promise < void > {
274+ async function assertPermittedTarget (
275+ url : URL ,
276+ signal : AbortSignal ,
277+ ) : Promise < void > {
227278 const refuse = ( reason : string ) =>
228279 new Error ( `Error processing file ${ url } : ${ reason } ` ) ;
229280 if ( url . protocol === "data:" ) return ; // carries its own bytes
@@ -232,17 +283,21 @@ async function assertPermittedTarget(url: URL): Promise<void> {
232283 `Unsupported URL protocol for ${ url } . Only http, https, and data URLs are supported.` ,
233284 ) ;
234285 }
235- // An IPv6 literal's hostname keeps its brackets: [::1]
236- const host = url . hostname . replace ( / ^ \[ ( .* ) \] $ / , "$1" ) ;
286+ // Names are matched as the URL spells them, an IPv6 literal with its
287+ // brackets ([::1]), as they always have been
288+ const domain = url . hostname ;
237289 if (
238290 GZIP_ALLOWED_DOMAINS . length > 0 &&
239- ! GZIP_ALLOWED_DOMAINS . some ( ( d ) => host === d || host . endsWith ( `.${ d } ` ) )
291+ ! GZIP_ALLOWED_DOMAINS . some ( ( d ) => domain === d || domain . endsWith ( `.${ d } ` ) )
240292 ) {
241- throw refuse ( `Domain ${ host } is not in the allowed domains list.` ) ;
293+ throw refuse ( `Domain ${ domain } is not in the allowed domains list.` ) ;
242294 }
243295 if ( GZIP_ALLOW_PRIVATE_IPS ) return ;
244- for ( const { address, family } of await lookup ( host , { all : true } ) ) {
245- if ( NON_PUBLIC_ADDRESSES . check ( address , family === 6 ? "ipv6" : "ipv4" ) ) {
296+ // The resolver takes an IPv6 literal without its brackets
297+ const host = domain . replace ( / ^ \[ ( .* ) \] $ / , "$1" ) ;
298+ const addresses = await raceAbort ( lookup ( host , { all : true } ) , signal ) ;
299+ for ( const { address, family } of addresses ) {
300+ if ( isNonPublic ( address , family ) ) {
246301 throw refuse (
247302 `${ host } resolves to ${ address } , which is not a public address. Set GZIP_ALLOW_PRIVATE_IPS=1 to allow private, loopback and link-local addresses.` ,
248303 ) ;
@@ -266,7 +321,7 @@ async function fetchPermitted(
266321) : Promise < Response > {
267322 let target = url ;
268323 for ( let redirects = 0 ; ; redirects ++ ) {
269- await assertPermittedTarget ( target ) ;
324+ await assertPermittedTarget ( target , signal ) ;
270325 const response = await fetch ( target , { signal, redirect : "manual" } ) ;
271326 const location = response . headers . get ( "location" ) ;
272327 if ( ! REDIRECT_STATUSES . has ( response . status ) || location === null ) {
0 commit comments