honojs/hono

serveStatic root is incorrect on Windows + Bun for absolute path (other interpreters probably too)

Opened this issue · 2 comments

What version of Hono are you using?

4.6.3

What runtime/platform is your app running on?

Bun

What steps can reproduce the bug?

app.use("/static/*", 
    serveStatic({ 
        root: __dirname,
        onNotFound(path, c) {
            console.log(path)
        }
    }));

What is the expected behavior?

Server serve static file from __dirname +"/static"

What do you see instead?

Windows: Server always returns "resource not found". console.log in the onNotFound handler shows path starting from "./C:/Users/..."

Linux: Working as it should

Additional information

That is probably is the reason of the problem
image

I think serveStatic should always expect absolute path or not modify the path. To make it safe just avoid ".." in the file name.

Also "static" (or probably the url prefix used during the middleware registration) is always added to the root path when searching for the file. It seams to be strange too.

Perhaps it would be better to pass the __dirname prefix by replacing it...
I think hardcoding poses a security risk.

__dirname.replace(/^C:/, "")

I'm planning that the serveStatic supports Windows: #3477