@@ -107,3 +107,31 @@ test('equal distinguishes escaped reserved path data from literal syntax', (t) =
107107 )
108108 t . end ( )
109109} )
110+
111+ // A suffix reference never emits its scheme token, so it is a scheme-less
112+ // (path-noscheme) reference: a bare ':' in its first path segment must be
113+ // escaped, or the serialized output reparses as an absolute URI with a scheme.
114+ // @see https://github.com/fastify/fast-uri/security/advisories/GHSA-333x-w4r6-6crp
115+ test ( 'suffix reference escapes a first-segment colon instead of emitting a scheme' , ( t ) => {
116+ const dangerous = / ^ (?: j a v a s c r i p t | d a t a | v b s c r i p t ) : / i
117+ const options = { reference : 'suffix' , scheme : 'x' }
118+
119+ for ( const input of [ 'javascript:alert(1)' , '%6Aavascript:alert(1)' , 'data:text/html,x' , 'vbscript:msgbox(1)' ] ) {
120+ t . notOk ( dangerous . test ( fastURI . normalize ( input , options ) ) , `${ input } normalizes to an inert reference` )
121+ }
122+
123+ t . equal (
124+ fastURI . normalize ( 'javascript:alert(1)' , options ) ,
125+ 'javascript%3Aalert(1)' ,
126+ 'escapes the leading-segment colon to %3A'
127+ )
128+ t . notOk (
129+ dangerous . test ( fastURI . resolve ( 'https://a.example/base/' , '%6Aavascript:alert(1)' , { reference : 'suffix' } ) ) ,
130+ 'resolve in suffix mode does not emit a live scheme'
131+ )
132+
133+ // legitimate suffix usage is unchanged
134+ t . equal ( fastURI . normalize ( 'path/to/file' , options ) , 'path/to/file' , 'plain path unchanged' )
135+ t . equal ( fastURI . normalize ( 'a/b:c' , options ) , 'a/b:c' , 'colon in a later segment stays literal' )
136+ t . end ( )
137+ } )
0 commit comments