Skip to content
Myr

Merge commit from fork

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Reject percent signs in routed request paths before additional decoding. Add allowPercentInPath to preserve the existing behavior when explicitly enabled. Cover the double-decoding authorization bypass, default rejection, and the opt-in while preserving encoded spaces and Unicode paths.

authored by Taku Amano · committed by GitHub · parent 18331a9 · Browse files · 2 files+52 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+52 added−4 removed

  • tests:+47 added−4 removed
  • other:+5 added−0 removed

2 files+52 added−4 removed

modified:src/middleware/serve-static/index.test.ts+47 added-4 removed
66 unmodified lines
67 expect(res.headers.get('Content-Type')).toMatch(/^text\/html/)68 expect(await res.text()).toBe('Hello in static/hello.world/index.html')69 })7071 it('Should decode URI strings - /static/%E7%82%8E.txt', async () => {72 const res = await app.request('/static/%E7%82%8E.txt')71 it.each([72 ['/static/%E7%82%8E.txt', 'static/炎.txt'],73 ['/static/hello%20world.txt', 'static/hello world.txt'],74 ])('Should decode URI strings - %s', async (url, path) => {75 const res = await app.request(url)76 expect(res.status).toBe(200)77 expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/)75 expect(await res.text()).toBe('Hello in static/炎.txt')78 expect(await res.text()).toBe(`Hello in ${path}`)79 })8081 it.each(['/static/100%25.txt', '/static/100%25/hello.txt', '/static/%2Fadmin/secret.txt'])(82 'Should skip paths containing percent signs by default - %s',83 async (path) => {84 const onNotFound = vi.fn()85 const app = new Hono().use('*', baseServeStatic({ getContent, onNotFound }))8687 const res = await app.request(path)8889 expect(res.status).toBe(404)90 expect(getContent).not.toBeCalled()91 expect(onNotFound).toHaveBeenCalledWith(path, expect.anything())92 }93 )9495 it.each([96 ['/static/100%25.txt', 'static/100%.txt'],97 ['/static/100%25/hello.txt', 'static/100%/hello.txt'],98 ['/static/%2Fadmin/secret.txt', 'static/%2Fadmin/secret.txt'],99 ])('Should allow percent signs when opted in - %s', async (url, path) => {100 const app = new Hono().use('*', baseServeStatic({ getContent, allowPercentInPath: true }))101102 const res = await app.request(url)103104 expect(res.status).toBe(200)105 expect(await res.text()).toBe(`Hello in ${path}`)106 })107108 it('Should not bypass authentication through a second decode', async () => {109 const app = new Hono()110 app.use('/static/admin/*', (c) => c.text('Unauthorized', 401))111 app.use('/static/*', baseServeStatic({ getContent }))112113 const protectedRes = await app.request('/static/admin/secret.txt')114 expect(protectedRes.status).toBe(401)115116 const res = await app.request('/static/%%36%31dmin/secret.txt')117 expect(res.status).toBe(404)118 expect(getContent).not.toBeCalled()119 })120121 it('Should return 404 response - /static/not-found.txt', async () => {122 const res = await app.request('/static/not-found.txt')123 expect(res.status).toBe(404)124 expect(res.headers.get('Content-Encoding')).toBeNull()
244 unmodified lines
369 expect(await res.text()).toBe('Hello in admin/secret.txt')370371 const res2 = await app.request('/admin%2Fsecret.txt')372 expect(res2.headers.get('X-Authorized')).toBeNull()330 expect(await res2.text()).toBe('Hello in admin%2Fsecret.txt')373 expect(res2.status).toBe(404)374375 const res3 = await app.request('//admin/secret.txt')376 expect(res3.status).toBe(404)377 })
2 unmodified lines
modified:src/middleware/serve-static/index.ts+5 added