Skip to content

Commit

Permalink
fix(serve-static): add error handler for decoding uri components (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
alumowa authored Oct 28, 2024
1 parent cd4d4d7 commit 3dcf537
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ export const serveStatic = (options: ServeStaticOptions = { root: '' }): Middlew
return next()
}

const filename = options.path ?? decodeURIComponent(c.req.path)
let filename: string

try {
filename = options.path ?? decodeURIComponent(c.req.path)
} catch {
await options.onNotFound?.(c.req.path, c)
return next()
}

let path = getFilePathWithoutDefaultDocument({
filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename,
Expand Down
5 changes: 5 additions & 0 deletions test/serve-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ describe('Serve Static Middleware', () => {
expect(res.status).toBe(404)
})

it('Should handle URIError thrown while decoding URI component', async () => {
const res = await request(server).get('/static/%c0%afsecret.txt')
expect(res.status).toBe(404)
})

it('Should handle an extension less files', async () => {
const res = await request(server).get('/static/extensionless')
expect(res.status).toBe(200)
Expand Down

0 comments on commit 3dcf537

Please sign in to comment.