-
-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request from GHSA-3mpf-rcc7-5347
- Loading branch information
Showing
4 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { createMiddleware } from '../../helper' | ||
import { Hono } from '../../hono' | ||
import { serveStatic as baseServeStatic } from '.' | ||
|
||
describe('Serve Static Middleware', () => { | ||
const app = new Hono() | ||
|
||
const serveStatic = createMiddleware(async (c, next) => { | ||
const mw = baseServeStatic({ | ||
getContent: (path) => { | ||
if (path.endsWith('not-found.txt')) { | ||
return undefined | ||
} | ||
return `Hello in ${path}` | ||
}, | ||
pathResolve: (path) => { | ||
return `./${path}` | ||
}, | ||
}) | ||
return await mw(c, next) | ||
}) | ||
|
||
app.get('/static/*', serveStatic) | ||
|
||
it('Should return 200 response - /static/hello.html', async () => { | ||
const res = await app.request('/static/hello.html') | ||
expect(res.status).toBe(200) | ||
expect(res.headers.get('Content-Type')).toMatch(/^text\/html/) | ||
expect(await res.text()).toBe('Hello in ./static/hello.html') | ||
}) | ||
|
||
it('Should return 200 response - /static/sub', async () => { | ||
const res = await app.request('/static/sub') | ||
expect(res.status).toBe(200) | ||
expect(res.headers.get('Content-Type')).toMatch(/^text\/html/) | ||
expect(await res.text()).toBe('Hello in ./static/sub/index.html') | ||
}) | ||
|
||
it('Should decode URI strings - /static/%E7%82%8E.txt', async () => { | ||
const res = await app.request('/static/%E7%82%8E.txt') | ||
expect(res.status).toBe(200) | ||
expect(await res.text()).toBe('Hello in ./static/炎.txt') | ||
}) | ||
|
||
it('Should return 404 response - /static/not-found', async () => { | ||
const res = await app.request('/static/not-found.txt') | ||
expect(res.status).toBe(404) | ||
expect(await res.text()).toBe('404 Not Found') | ||
}) | ||
|
||
it('Should not allow a directory traversal - /static/%2e%2e/static/hello.html', async () => { | ||
const res = await app.fetch({ | ||
method: 'GET', | ||
url: 'http://localhost/static/%2e%2e/static/hello.html', | ||
} as Request) | ||
expect(res.status).toBe(404) | ||
expect(await res.text()).toBe('404 Not Found') | ||
}) | ||
}) |
This file contains 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