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(serveStatic): add guard to prevent reading empty folders #3639

Merged
merged 3 commits into from
Nov 8, 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
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
Loading