-
Notifications
You must be signed in to change notification settings - Fork 77
feat(serve-static): support absolute path #257
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8500774
feat(serve-static): support absolute path
yusukebe cbfed14
add ci-windows
yusukebe 2e9b4e9
fix the formats
yusukebe e420e47
fix the jest path
yusukebe 9a14f20
support Windows
yusukebe 33f3ccf
test support Windows
yusukebe 22d69c0
fix the logic
yusukebe 54ecac3
correct handling paths for Windows
yusukebe d5c1002
typo
yusukebe 52bd0e6
update README
yusukebe ee6159c
simplify the implementation
yusukebe 1737089
format
yusukebe ff18658
fixed file pathes in the test
yusukebe f6f5b90
resolve if both `root` and `path` set
yusukebe 8f15053
resolve root when initialize the app
yusukebe d26e6ee
Currect the behavior if root is set and path is `/favicon.ico`
yusukebe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import { Hono } from 'hono' | ||
|
|
||
| import request from 'supertest' | ||
| import path from 'node:path' | ||
| import { serveStatic } from './../src/serve-static' | ||
| import { createAdaptorServer } from './../src/server' | ||
|
|
||
|
|
@@ -68,7 +68,9 @@ describe('Serve Static Middleware', () => { | |
| expect(res.status).toBe(200) | ||
| expect(res.text).toBe('<h1>Hello Hono</h1>') | ||
| expect(res.headers['content-type']).toBe('text/html; charset=utf-8') | ||
| expect(res.headers['x-custom']).toBe('Found the file at ./test/assets/static/index.html') | ||
| expect(res.headers['x-custom']).toMatch( | ||
|
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. To simplify implementation, make the path passed to app.use(
'/static/*',
serveStatic({
root: './test/assets',
onFound: (path, c) => {
// path is an absolute path
c.header('X-Custom', `Found the file at ${path}`)
},
})
) |
||
| /Found the file at .*[\/\\]test[\/\\]assets[\/\\]static[\/\\]index\.html$/ | ||
| ) | ||
| }) | ||
|
|
||
| it('Should return hono.html', async () => { | ||
|
|
@@ -167,8 +169,8 @@ describe('Serve Static Middleware', () => { | |
| it('Should handle the `onNotFound` option', async () => { | ||
| const res = await request(server).get('/on-not-found/foo.txt') | ||
| expect(res.status).toBe(404) | ||
| expect(notFoundMessage).toBe( | ||
| './not-found/on-not-found/foo.txt is not found, request to /on-not-found/foo.txt' | ||
| expect(notFoundMessage).toMatch( | ||
yusukebe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /.*[\/\\]not-found[\/\\]on-not-found[\/\\]foo\.txt is not found, request to \/on-not-found\/foo\.txt$/ | ||
| ) | ||
| }) | ||
|
|
||
|
|
@@ -226,4 +228,95 @@ describe('Serve Static Middleware', () => { | |
| expect(res.headers['vary']).toBeUndefined() | ||
| expect(res.text).toBe('Hello Not Compressed') | ||
| }) | ||
|
|
||
| describe('Absolute path', () => { | ||
| const rootPaths = [ | ||
| path.join(__dirname, 'assets'), | ||
| __dirname + path.sep + '..' + path.sep + 'test' + path.sep + 'assets', | ||
| ] | ||
| rootPaths.forEach((root) => { | ||
| describe(root, () => { | ||
| const app = new Hono() | ||
| const server = createAdaptorServer(app) | ||
| app.use('/static/*', serveStatic({ root })) | ||
| app.use('/favicon.ico', serveStatic({ path: root + path.sep + 'favicon.ico' })) | ||
|
|
||
| it('Should return index.html', async () => { | ||
| const res = await request(server).get('/static') | ||
| expect(res.status).toBe(200) | ||
| expect(res.headers['content-type']).toBe('text/html; charset=utf-8') | ||
| expect(res.text).toBe('<h1>Hello Hono</h1>') | ||
| }) | ||
|
|
||
| it('Should return correct headers and data for text', async () => { | ||
| const res = await request(server).get('/static/plain.txt') | ||
| expect(res.status).toBe(200) | ||
| expect(res.headers['content-type']).toBe('text/plain; charset=utf-8') | ||
| expect(res.text).toBe('This is plain.txt') | ||
| }) | ||
| it('Should return correct headers for icons', async () => { | ||
| const res = await request(server).get('/favicon.ico') | ||
| expect(res.status).toBe(200) | ||
| expect(res.headers['content-type']).toBe('image/x-icon') | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('Root and path combination tests', () => { | ||
| const rootPaths = [ | ||
| path.join(__dirname, 'assets'), | ||
| path.join(__dirname, 'assets'), | ||
| __dirname + path.sep + '..' + path.sep + 'test' + path.sep + 'assets', | ||
| ] | ||
| const optionPaths = ['favicon.ico', '/favicon.ico'] | ||
| rootPaths.forEach((root) => { | ||
| optionPaths.forEach((optionPath) => { | ||
| describe(`${root} + ${optionPath}`, () => { | ||
| const app = new Hono() | ||
| const server = createAdaptorServer(app) | ||
|
|
||
| app.use( | ||
| '/favicon.ico', | ||
| serveStatic({ | ||
| root, | ||
| path: optionPath, | ||
| }) | ||
| ) | ||
|
|
||
| it('Should return 200 response if both root and path set', async () => { | ||
| const res = await request(server).get('/favicon.ico') | ||
| expect(res.status).toBe(200) | ||
| expect(res.headers['content-type']).toBe('image/x-icon') | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('Security tests', () => { | ||
| const app = new Hono() | ||
| const server = createAdaptorServer(app) | ||
| app.use('/static/*', serveStatic({ root: './test/assets' })) | ||
|
|
||
| it('Should prevent path traversal attacks with double dots', async () => { | ||
| const res = await request(server).get('/static/../secret.txt') | ||
| expect(res.status).toBe(404) | ||
| }) | ||
|
|
||
| it('Should prevent path traversal attacks with multiple levels', async () => { | ||
| const res = await request(server).get('/static/../../package.json') | ||
| expect(res.status).toBe(404) | ||
| }) | ||
|
|
||
| it('Should prevent path traversal attacks with mixed separators', async () => { | ||
| const res = await request(server).get('/static/..\\..\\package.json') | ||
| expect(res.status).toBe(404) | ||
| }) | ||
|
|
||
| it('Should prevent path traversal attacks with encoded dots', async () => { | ||
| const res = await request(server).get('/static/%2e%2e%2fsecret.txt') | ||
| expect(res.status).toBe(404) | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To support Windows on CI.