diff --git a/src/serve-static.ts b/src/serve-static.ts index faea8bd..d058fd9 100644 --- a/src/serve-static.ts +++ b/src/serve-static.ts @@ -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, diff --git a/test/serve-static.test.ts b/test/serve-static.test.ts index 3735098..507318b 100644 --- a/test/serve-static.test.ts +++ b/test/serve-static.test.ts @@ -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)