Skip to content

Commit

Permalink
fix(serveStatic): add guard to prevent reading empty folders (#3639)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
oussamasf authored Nov 8, 2024
1 parent a6ccfa2 commit 65f2a3b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions runtime-tests/deno/middleware.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
5 changes: 5 additions & 0 deletions src/adapter/deno/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -30,6 +34,7 @@ export const serveStatic = <E extends Env = Env>(
} catch {}
return isDir
}

return baseServeStatic({
...options,
getContent,
Expand Down

0 comments on commit 65f2a3b

Please sign in to comment.