@@ -221,3 +221,139 @@ test('parse does not reject a literal backslash in the query or fragment', (t) =
221221 t . notOk ( parsed . error , 'backslash in query/fragment does not mark the URI malformed' )
222222 t . equal ( parsed . host , 'host.example.com' , 'host parsed normally' )
223223} )
224+
225+ test ( 'parse rejects a malformed authority introducer (\\\\, /\\, \\/) in place of //' , ( t ) => {
226+ // Regression: "\\", "/\\", "\\/" after the scheme colon are not valid authority
227+ // introducers. Node's URL treats "\\" as interchangeable with "/" on special
228+ // schemes, so "http:\\\\evil.com/path" would be parsed as host "evil.com" by
229+ // Node, but fast-uri must reject it as malformed to prevent SSRF/redirect bypass.
230+ const cases = [
231+ 'http:\\\\evil.com/path' ,
232+ 'http:/\\evil.com/path' ,
233+ 'http:\\/evil.com/path' ,
234+ 'ws:\\\\evil.com/chat' ,
235+ 'wss:\\\\evil.com/chat' ,
236+ 'ftp:\\\\evil.com/' ,
237+ '\\\\evil.com/path'
238+ ]
239+
240+ t . plan ( cases . length )
241+
242+ cases . forEach ( ( input ) => {
243+ t . equal (
244+ fastURI . parse ( input ) . error ,
245+ 'URI authority must not contain a literal backslash.' ,
246+ input
247+ )
248+ } )
249+ } )
250+
251+ test ( 'normalize does not canonicalize a malformed-authority-introducer URI' , ( t ) => {
252+ const cases = [
253+ 'http:\\\\evil.com/path' ,
254+ 'http:/\\evil.com/path'
255+ ]
256+
257+ t . plan ( cases . length )
258+
259+ cases . forEach ( ( input ) => {
260+ t . equal ( fastURI . normalize ( input ) , input , input )
261+ } )
262+ } )
263+
264+ test ( 'equal returns false for malformed-authority-introducer URIs' , ( t ) => {
265+ const pairs = [
266+ [ 'http:\\\\evil.com/path' , 'http://evil.com/path' ] ,
267+ [ 'http:/\\evil.com/path' , 'http://evil.com/path' ]
268+ ]
269+
270+ t . plan ( pairs . length )
271+
272+ pairs . forEach ( ( [ left , right ] ) => {
273+ t . equal ( fastURI . equal ( left , right ) , false , `${ left } != ${ right } ` )
274+ } )
275+ } )
276+
277+ test ( 'resolve throws on malformed authority introducer' , ( t ) => {
278+ // resolve() returns a plain string with no error field, so the only safe
279+ // behavior is to throw when either component has a malformed authority.
280+ const pairs = [
281+ [ 'https://allowed.com/' , '\\\\evil.com/path' ] ,
282+ [ '\\\\evil.com/path' , 'https://allowed.com/' ] ,
283+ [ 'https://allowed.com/' , 'http:/\\evil.com/path' ] ,
284+ [ 'https://allowed.com/' , 'http:\\/evil.com/path' ]
285+ ]
286+
287+ t . plan ( pairs . length )
288+
289+ pairs . forEach ( ( [ base , rel ] ) => {
290+ t . throws (
291+ ( ) => fastURI . resolve ( base , rel ) ,
292+ / U R I a u t h o r i t y m u s t n o t c o n t a i n a l i t e r a l b a c k s l a s h / ,
293+ `${ base } + ${ rel } `
294+ )
295+ } )
296+ } )
297+
298+ test ( 'parse rejects a whitespace-split authority introducer (TAB, LF, CR)' , ( t ) => {
299+ // The WHATWG URL parser removes TAB (U+0009), LF (U+000A) and CR (U+000D) from
300+ // the input before parsing, so a stripped character wedged into the introducer
301+ // ("/<TAB>\\", "/<TAB>/", or a leading "<TAB>//") reaches an authority in Node
302+ // while fast-uri would otherwise fold it into the path. These must be rejected
303+ // like the adjacent "\\", "/\\", "\\/" forms.
304+ const cases = [
305+ { input : '/\t\\evil.com/path' , expectedError : 'URI authority must not contain a literal backslash.' } ,
306+ { input : '/\t/evil.com/path' , expectedError : 'URI authority introducer must not contain whitespace.' } ,
307+ { input : '/\n\\evil.com/path' , expectedError : 'URI authority must not contain a literal backslash.' } ,
308+ { input : '/\r\\evil.com/path' , expectedError : 'URI authority must not contain a literal backslash.' } ,
309+ { input : '\t//evil.com/path' , expectedError : 'URI authority introducer must not contain whitespace.' } ,
310+ { input : '\t/\\evil.com/path' , expectedError : 'URI authority must not contain a literal backslash.' } ,
311+ { input : 'https:/\t/evil.com/path' , expectedError : 'URI authority introducer must not contain whitespace.' }
312+ ]
313+
314+ t . plan ( cases . length )
315+
316+ cases . forEach ( ( { input, expectedError } ) => {
317+ t . equal ( fastURI . parse ( input ) . error , expectedError , JSON . stringify ( input ) )
318+ } )
319+ } )
320+
321+ test ( 'resolve throws on a whitespace-split authority introducer' , ( t ) => {
322+ const pairs = [
323+ [ 'https://allowed.com/' , '/\t\\evil.com/path' ] ,
324+ [ 'https://allowed.com/' , '/\t/evil.com/path' ] ,
325+ [ 'https://allowed.com/' , '/\n\\evil.com/path' ] ,
326+ [ '/\t/evil.com/path' , 'https://allowed.com/' ]
327+ ]
328+
329+ t . plan ( pairs . length )
330+
331+ pairs . forEach ( ( [ base , rel ] ) => {
332+ t . throws (
333+ ( ) => fastURI . resolve ( base , rel ) ,
334+ / U R I a u t h o r i t y ( m u s t n o t c o n t a i n a l i t e r a l b a c k s l a s h | i n t r o d u c e r m u s t n o t c o n t a i n w h i t e s p a c e ) / ,
335+ `${ JSON . stringify ( base ) } + ${ JSON . stringify ( rel ) } `
336+ )
337+ } )
338+ } )
339+
340+ test ( 'parse does not reject valid authority introducer patterns' , ( t ) => {
341+ // No false positives: "//" introducer and scheme-less "//" must be valid.
342+ const cases = [
343+ 'http://good.com/' ,
344+ 'https://good.com/' ,
345+ 'ws://good.com/chat' ,
346+ 'wss://good.com/chat' ,
347+ 'ftp://good.com/' ,
348+ '//good.com/path' ,
349+ '/absolute/path' ,
350+ 'relative/path'
351+ ]
352+
353+ t . plan ( cases . length )
354+
355+ cases . forEach ( ( input ) => {
356+ const parsed = fastURI . parse ( input )
357+ t . notOk ( parsed . error , input )
358+ } )
359+ } )
0 commit comments