@@ -330,7 +330,7 @@ describe('Serve Static Middleware', () => {
330330 } )
331331 } )
332332
333- describe ( 'Security tests' , ( ) => {
333+ describe ( 'Path traversal security tests' , ( ) => {
334334 const app = new Hono ( )
335335 const server = createAdaptorServer ( app )
336336 app . use ( '/static/*' , serveStatic ( { root : './test/assets' } ) )
@@ -361,6 +361,29 @@ describe('Serve Static Middleware', () => {
361361 } )
362362 } )
363363
364+ describe ( 'Path mismatch security tests' , ( ) => {
365+ const app = new Hono ( )
366+ const server = createAdaptorServer ( app )
367+
368+ app . use ( '/static/admin/*' , async ( c , next ) => {
369+ c . header ( 'X-Authorized' , 'true' )
370+ await next ( )
371+ } )
372+
373+ app . use ( '/static/*' , serveStatic ( { root : './test/assets' } ) )
374+
375+ it ( 'Should not allow bypass via path mismatch between middleware and serveStatic' , async ( ) => {
376+ const res = await request ( server ) . get ( '/static/admin/secret.txt' )
377+ expect ( res . headers [ 'x-authorized' ] ) . toBe ( 'true' )
378+ expect ( res . text ) . toBe ( 'secret' )
379+
380+ const res2 = await request ( server ) . get ( '/static/admin%2Fsecret.txt' )
381+ expect ( res2 . status ) . toBe ( 404 )
382+ expect ( res2 . headers [ 'x-authorized' ] ) . toBeUndefined ( )
383+ expect ( res2 . text ) . not . toBe ( 'secret' )
384+ } )
385+ } )
386+
364387 describe ( 'Stream error handling' , ( ) => {
365388 const testFile = path . join ( __dirname , 'assets' , 'static' , 'plain.txt' )
366389 console . log ( testFile )
0 commit comments