Skip to content

Commit

Permalink
fix: /@fs/ dir traversal with escaped chars (static)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jun 27, 2022
1 parent 3a9168b commit cbc971c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/middlewares/static.ts
Expand Up @@ -80,7 +80,7 @@ export function serveStaticMiddleware(
return next()
}

const url = decodeURI(req.url!)
const url = decodeURIComponent(req.url!)

// apply aliases to static requests as well
let redirected: string | undefined
Expand Down
7 changes: 7 additions & 0 deletions playground/fs-serve/__tests__/fs-serve.spec.ts
Expand Up @@ -35,6 +35,13 @@ describe.runIf(isServe)('main', () => {
expect(await page.textContent('.unsafe-fetch-status')).toBe('403')
})

test('unsafe fetch with special characters (#8498)', async () => {
expect(await page.textContent('.unsafe-fetch-8498')).toMatch(
'403 Restricted'
)
expect(await page.textContent('.unsafe-fetch-8498-status')).toBe('403')
})

test('safe fs fetch', async () => {
expect(await page.textContent('.safe-fs-fetch')).toBe(stringified)
expect(await page.textContent('.safe-fs-fetch-status')).toBe('200')
Expand Down
15 changes: 15 additions & 0 deletions playground/fs-serve/root/src/index.html
Expand Up @@ -17,6 +17,8 @@ <h2>Safe Fetch Subdirectory</h2>
<h2>Unsafe Fetch</h2>
<pre class="unsafe-fetch-status"></pre>
<pre class="unsafe-fetch"></pre>
<pre class="unsafe-fetch-8498-status"></pre>
<pre class="unsafe-fetch-8498"></pre>

<h2>Safe /@fs/ Fetch</h2>
<pre class="safe-fs-fetch-status"></pre>
Expand Down Expand Up @@ -85,6 +87,19 @@ <h2>Denied</h2>
console.error(e)
})

// outside of allowed dir with special characters #8498
fetch('/src/%2e%2e%2funsafe%2etxt')
.then((r) => {
text('.unsafe-fetch-8498-status', r.status)
return r.text()
})
.then((data) => {
text('.unsafe-fetch-8498', data)
})
.catch((e) => {
console.error(e)
})

// imported before, should be treated as safe
fetch('/@fs/' + ROOT + '/safe.json')
.then((r) => {
Expand Down

0 comments on commit cbc971c

Please sign in to comment.