mirror of
https://github.com/honojs/hono.git
synced 2024-12-01 11:51:01 +01:00
fix(serveStatic): add guard to prevent reading empty folders (#3639)
Fixes #3628 * fix(serveStatic): add guard to prevent reading empty folders * fix(serveStatic): remove unnecessary Deno.stat * test(serveStatic): add test cases related to isDir guard
This commit is contained in:
parent
a6ccfa29ca
commit
65f2a3be93
@ -139,6 +139,22 @@ Deno.test('Serve Static middleware', async () => {
|
||||
res = await app.request('http://localhost/static-absolute-root/plain.txt')
|
||||
assertEquals(res.status, 200)
|
||||
assertEquals(await res.text(), 'Deno!')
|
||||
|
||||
res = await app.request('http://localhost/static')
|
||||
assertEquals(res.status, 404)
|
||||
assertEquals(await res.text(), '404 Not Found')
|
||||
|
||||
res = await app.request('http://localhost/static/dir')
|
||||
assertEquals(res.status, 404)
|
||||
assertEquals(await res.text(), '404 Not Found')
|
||||
|
||||
res = await app.request('http://localhost/static/helloworld/nested')
|
||||
assertEquals(res.status, 404)
|
||||
assertEquals(await res.text(), '404 Not Found')
|
||||
|
||||
res = await app.request('http://localhost/static/helloworld/../')
|
||||
assertEquals(res.status, 404)
|
||||
assertEquals(await res.text(), '404 Not Found')
|
||||
})
|
||||
|
||||
Deno.test('JWT Authentication middleware', async () => {
|
||||
|
@ -10,6 +10,10 @@ export const serveStatic = <E extends Env = Env>(
|
||||
return async function serveStatic(c, next) {
|
||||
const getContent = async (path: string) => {
|
||||
try {
|
||||
if (isDir(path)) {
|
||||
return null
|
||||
}
|
||||
|
||||
const file = await open(path)
|
||||
return file.readable
|
||||
} catch (e) {
|
||||
@ -30,6 +34,7 @@ export const serveStatic = <E extends Env = Env>(
|
||||
} catch {}
|
||||
return isDir
|
||||
}
|
||||
|
||||
return baseServeStatic({
|
||||
...options,
|
||||
getContent,
|
||||
|
Loading…
Reference in New Issue
Block a user