Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(serve-static): add error handler for decoding uri components #208

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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