Merge pull request from GHSA-3mpf-rcc7-5347
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent b38e40e · Browse files · 4 files·+68 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+68 added−4 removed
- tests:+66 added−0 removed
- other:+2 added−4 removed
Provenance
- Commit
92e65fb- Tree
ba834ad- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
b38e40e
4 files changed+68 added−4 removed
modified:deno_dist/middleware/serve-static/index.ts+1 added-2 removed
28 unmodified lines29303132333335343536373848 unmodified lines28 unmodified lines if (c.finalized) { await next() return } const url = new URL(c.req.url)
let filename = options.path ?? decodeURI(url.pathname) let filename = options.path ?? decodeURI(c.req.path) filename = options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename const root = options.root
let path = getFilePath({48 unmodified linesmodified:runtime_tests/deno/middleware.test.tsx+7 added
115 unmodified lines116 res = await app.request('http://localhost/dot-static/plain.txt')117 assertEquals(res.status, 200)118 assertEquals(await res.text(), 'Deno!!')119 assertSpyCalls(onNotFound, 1)120+121+ res = await app.fetch({122+ method: 'GET',123+ url: 'http://localhost/static/%2e%2e/static/plain.txt',124+ } as Request)125+ assertEquals(res.status, 404)126+ assertEquals(await res.text(), '404 Not Found')127 })128 129 Deno.test('JWT Authentication middleware', async () => {130 const app = new Hono<{ Variables: { 'x-foo': string } }>()25 unmodified linesadded:src/middleware/serve-static/index.test.ts+59 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import { 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') })})modified:src/middleware/serve-static/index.ts+1 added-2 removed
28 unmodified lines29 if (c.finalized) {30 await next()31 return32 }33- const url = new URL(c.req.url)33 35- let filename = options.path ?? decodeURI(url.pathname)34+ let filename = options.path ?? decodeURI(c.req.path)35 filename = options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename36 const root = options.root37 38 let path = getFilePath({48 unmodified lines