@@ -25,13 +25,21 @@ function makeStallingFetch(firstChunk: Uint8Array) {
2525 } ) ;
2626}
2727
28+ function makeLookupFn ( ) {
29+ return vi . fn ( async ( ) => [ { address : "149.154.167.220" , family : 4 } ] ) as unknown as NonNullable <
30+ Parameters < typeof fetchRemoteMedia > [ 0 ] [ "lookupFn" ]
31+ > ;
32+ }
33+
2834describe ( "fetchRemoteMedia" , ( ) => {
29- type LookupFn = NonNullable < Parameters < typeof fetchRemoteMedia > [ 0 ] [ "lookupFn" ] > ;
35+ const telegramToken = "123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcd" ;
36+ const redactedTelegramToken = `${ telegramToken . slice ( 0 , 6 ) } …${ telegramToken . slice ( - 4 ) } ` ;
37+ const telegramFileUrl = `https://api.telegram.org/file/bot${ telegramToken } /photos/1.jpg` ;
3038
3139 it ( "rejects when content-length exceeds maxBytes" , async ( ) => {
3240 const lookupFn = vi . fn ( async ( ) => [
3341 { address : "93.184.216.34" , family : 4 } ,
34- ] ) as unknown as LookupFn ;
42+ ] ) as unknown as NonNullable < Parameters < typeof fetchRemoteMedia > [ 0 ] [ "lookupFn" ] > ;
3543 const fetchImpl = async ( ) =>
3644 new Response ( makeStream ( [ new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] ) ] ) , {
3745 status : 200 ,
@@ -51,7 +59,7 @@ describe("fetchRemoteMedia", () => {
5159 it ( "rejects when streamed payload exceeds maxBytes" , async ( ) => {
5260 const lookupFn = vi . fn ( async ( ) => [
5361 { address : "93.184.216.34" , family : 4 } ,
54- ] ) as unknown as LookupFn ;
62+ ] ) as unknown as NonNullable < Parameters < typeof fetchRemoteMedia > [ 0 ] [ "lookupFn" ] > ;
5563 const fetchImpl = async ( ) =>
5664 new Response ( makeStream ( [ new Uint8Array ( [ 1 , 2 , 3 ] ) , new Uint8Array ( [ 4 , 5 , 6 ] ) ] ) , {
5765 status : 200 ,
@@ -70,7 +78,7 @@ describe("fetchRemoteMedia", () => {
7078 it ( "aborts stalled body reads when idle timeout expires" , async ( ) => {
7179 const lookupFn = vi . fn ( async ( ) => [
7280 { address : "93.184.216.34" , family : 4 } ,
73- ] ) as unknown as LookupFn ;
81+ ] ) as unknown as NonNullable < Parameters < typeof fetchRemoteMedia > [ 0 ] [ "lookupFn" ] > ;
7482 const fetchImpl = makeStallingFetch ( new Uint8Array ( [ 1 , 2 ] ) ) ;
7583
7684 await expect (
@@ -87,6 +95,48 @@ describe("fetchRemoteMedia", () => {
8795 } ) ;
8896 } , 5_000 ) ;
8997
98+ it ( "redacts Telegram bot tokens from fetch failure messages" , async ( ) => {
99+ const fetchImpl = vi . fn ( async ( ) => {
100+ throw new Error ( `dial failed for ${ telegramFileUrl } ` ) ;
101+ } ) ;
102+
103+ const error = await fetchRemoteMedia ( {
104+ url : telegramFileUrl ,
105+ fetchImpl,
106+ lookupFn : makeLookupFn ( ) ,
107+ maxBytes : 1024 ,
108+ ssrfPolicy : {
109+ allowedHostnames : [ "api.telegram.org" ] ,
110+ allowRfc2544BenchmarkRange : true ,
111+ } ,
112+ } ) . catch ( ( err : unknown ) => err as Error ) ;
113+
114+ expect ( error ) . toBeInstanceOf ( Error ) ;
115+ const errorText = error instanceof Error ? String ( error ) : "" ;
116+ expect ( errorText ) . not . toContain ( telegramToken ) ;
117+ expect ( errorText ) . toContain ( `bot${ redactedTelegramToken } ` ) ;
118+ } ) ;
119+
120+ it ( "redacts Telegram bot tokens from HTTP error messages" , async ( ) => {
121+ const fetchImpl = vi . fn ( async ( ) => new Response ( "unauthorized" , { status : 401 } ) ) ;
122+
123+ const error = await fetchRemoteMedia ( {
124+ url : telegramFileUrl ,
125+ fetchImpl,
126+ lookupFn : makeLookupFn ( ) ,
127+ maxBytes : 1024 ,
128+ ssrfPolicy : {
129+ allowedHostnames : [ "api.telegram.org" ] ,
130+ allowRfc2544BenchmarkRange : true ,
131+ } ,
132+ } ) . catch ( ( err : unknown ) => err as Error ) ;
133+
134+ expect ( error ) . toBeInstanceOf ( Error ) ;
135+ const errorText = error instanceof Error ? String ( error ) : "" ;
136+ expect ( errorText ) . not . toContain ( telegramToken ) ;
137+ expect ( errorText ) . toContain ( `bot${ redactedTelegramToken } ` ) ;
138+ } ) ;
139+
90140 it ( "blocks private IP literals before fetching" , async ( ) => {
91141 const fetchImpl = vi . fn ( ) ;
92142 await expect (
0 commit comments