-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(serve-static): support absolute root #3420
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
Changes from 4 commits
9e761e2
46346a4
5d7a2af
2835570
a93839a
5f07dd7
5dd59c5
6365ba9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Bun! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| { | ||
| "eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact"], | ||
| "editor.codeActionsOnSave": { | ||
| "source.fixAll.eslint": true | ||
| "source.fixAll.eslint": "explicit" | ||
| }, | ||
| "deno.enable": true | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Deno! |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ type FilePathOptions = { | |||||
| filename: string | ||||||
| root?: string | ||||||
| defaultDocument?: string | ||||||
| allowAbsoluteRoot?: boolean | ||||||
| } | ||||||
|
|
||||||
| export const getFilePath = (options: FilePathOptions): string | undefined => { | ||||||
|
|
@@ -23,6 +24,7 @@ export const getFilePath = (options: FilePathOptions): string | undefined => { | |||||
|
|
||||||
| const path = getFilePathWithoutDefaultDocument({ | ||||||
| root: options.root, | ||||||
| allowAbsoluteRoot: options.allowAbsoluteRoot, | ||||||
| filename, | ||||||
| }) | ||||||
|
|
||||||
|
|
@@ -42,6 +44,9 @@ export const getFilePathWithoutDefaultDocument = ( | |||||
| // /foo.html => foo.html | ||||||
| filename = filename.replace(/^\.?[\/\\]/, '') | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to use
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the following code one of the dangerous cases? You mean this test should pass, right? // with the current implementation, the following will fail
expect(getFilePathWithoutDefaultDocument({ filename: '///foo.txt' })).toBe('foo.txt')
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, I'm sorry, the code I suggested might not be good. The case I'm worried about is as follows. import { serveStatic } from './src/adapter/bun'
import { Hono } from './src/hono'
const app = new Hono()
app.use('/static/*', serveStatic({ root: './' }))
app.use('/favicon.ico', serveStatic({ path: './favicon.ico' }))
app.get('/', (c) => c.text('You can access: /static/hello.txt'))
app.get('*', serveStatic({ root: '.' })) // fallback
// or app.get('*', serveStatic({}))
export default appI don't think this is a realistic setting, but it's not an invalid setting, and I think the relative path from the application directory will result in the expected result. With the current code in the feat/serve-static-absolute-root branch, if you access http://localhost:3000///etc/passwd, you can access any path from the absolute path using directory traversal.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the following changes will also prevent directory traversal. diff --git a/src/utils/filepath.ts b/src/utils/filepath.ts
index f3f2ea82..61471483 100644
--- a/src/utils/filepath.ts
+++ b/src/utils/filepath.ts
@@ -52,5 +52,9 @@ export const getFilePathWithoutDefaultDocument = (
let path = root ? root + '/' + filename : filename
path = path.replace(/^\.?\//, '')
+ if (root[0] !== '/' && path[0] === '/') {
+ return
+ }
+
return path
} |
||||||
|
|
||||||
| // assets\foo => assets/foo | ||||||
| root = root.replace(/\\/, '/') | ||||||
|
|
||||||
| // foo\bar.txt => foo/bar.txt | ||||||
| filename = filename.replace(/\\/, '/') | ||||||
|
|
||||||
|
|
@@ -50,7 +55,16 @@ export const getFilePathWithoutDefaultDocument = ( | |||||
|
|
||||||
| // ./assets/foo.html => assets/foo.html | ||||||
| let path = root ? root + '/' + filename : filename | ||||||
| path = path.replace(/^\.?\//, '') | ||||||
|
|
||||||
| if (!options.allowAbsoluteRoot) { | ||||||
| path = path.replace(/^\.?\//, '') | ||||||
| } else { | ||||||
| // assets => /assets | ||||||
| path = path.replace(/^(?!\/)/, '/') | ||||||
| // Using URL to normalize the path. | ||||||
| const url = new URL(`file://${path}`) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What was the purpose of using With the current code, the following results will be obtained, so I think some kind of modification is necessary. getFilePathWithoutDefaultDocument({
filename: '/%2e%2e/%2e%2e/%2e%2e/%2e%2e/etc/passwd',
root: '/p/p2',
allowAbsoluteRoot: true,
}) // /etc/passwd
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It uses
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! If the necessary requirements are as follows,
The following code would also achieve this, and I think it would be more performant than resolving diff --git a/src/middleware/serve-static/index.ts b/src/middleware/serve-static/index.ts
index 49cbcd3c..3517a2e7 100644
--- a/src/middleware/serve-static/index.ts
+++ b/src/middleware/serve-static/index.ts
@@ -40,6 +40,8 @@ export const serveStatic = <E extends Env = Env>(
isDir?: (path: string) => boolean | undefined | Promise<boolean | undefined>
}
): MiddlewareHandler => {
+ const root = new URL(`file://${options.root}`).pathname
+
return async (c, next) => {
// Do nothing if Response is already set
if (c.finalized) {
@@ -49,7 +51,6 @@ export const serveStatic = <E extends Env = Env>(
let filename = options.path ?? decodeURI(c.req.path)
filename = options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename
- const root = options.root
const allowAbsoluteRoot = options.allowAbsoluteRoot ?? false
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahhhh, goood idea!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated it. What do you think of the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, but I think the behavior will change if you specify a relative path that includes the parent directory. The following method may be better. let root: string = options.root
if (root && root.startsWith('/')) {
isAbsoluteRoot = true
root = new URL(`file://${root}`).pathname
}
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've updated the code: 6365ba9 This change includes the fix for #3420 (comment) and added some tests. Could you review it? |
||||||
| path = url.pathname | ||||||
| } | ||||||
|
|
||||||
| return path | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.