Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { ensureServingAccess } from './static'
const debugCache = createDebugger('vite:cache')

const knownIgnoreList = new Set(['/', '/favicon.ico'])
const trailingQuerySeparatorsRE = /[?&]+$/

export function transformMiddleware(
server: ViteDevServer,
Expand Down Expand Up @@ -167,9 +168,19 @@ export function transformMiddleware(
}
}

const urlWithoutTrailingQuerySeparators = url.replace(
trailingQuerySeparatorsRE,
'',
)
if (
(rawRE.test(url) || urlRE.test(url)) &&
!ensureServingAccess(url, server, res, next)
(rawRE.test(urlWithoutTrailingQuerySeparators) ||
urlRE.test(urlWithoutTrailingQuerySeparators)) &&
!ensureServingAccess(
urlWithoutTrailingQuerySeparators,
server,
res,
next,
)
) {
return
}
Expand Down
14 changes: 14 additions & 0 deletions playground/fs-serve/__tests__/fs-serve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ describe.runIf(isServe)('main', () => {
expect(await page.textContent('.unsafe-fs-fetch-raw-status')).toBe('403')
})

test('unsafe fs fetch query 1', async () => {
expect(await page.textContent('.unsafe-fs-fetch-raw-query1')).toBe('')
expect(await page.textContent('.unsafe-fs-fetch-raw-query1-status')).toBe(
'403',
)
})

test('unsafe fs fetch query 2', async () => {
expect(await page.textContent('.unsafe-fs-fetch-raw-query2')).toBe('')
expect(await page.textContent('.unsafe-fs-fetch-raw-query2-status')).toBe(
'403',
)
})

test('unsafe fs fetch with special characters (#8498)', async () => {
expect(await page.textContent('.unsafe-fs-fetch-8498')).toBe('')
expect(await page.textContent('.unsafe-fs-fetch-8498-status')).toBe('404')
Expand Down
38 changes: 38 additions & 0 deletions playground/fs-serve/root/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ <h2>Unsafe /@fs/ Fetch</h2>
<pre class="unsafe-fs-fetch"></pre>
<pre class="unsafe-fs-fetch-raw-status"></pre>
<pre class="unsafe-fs-fetch-raw"></pre>
<pre class="unsafe-fs-fetch-raw-query1-status"></pre>
<pre class="unsafe-fs-fetch-raw-query1"></pre>
<pre class="unsafe-fs-fetch-raw-query2-status"></pre>
<pre class="unsafe-fs-fetch-raw-query2"></pre>
<pre class="unsafe-fs-fetch-8498-status"></pre>
<pre class="unsafe-fs-fetch-8498"></pre>
<pre class="unsafe-fs-fetch-8498-2-status"></pre>
Expand Down Expand Up @@ -209,6 +213,40 @@ <h2>Denied</h2>
console.error(e)
})

fetch(
joinUrlSegments(
base,
joinUrlSegments('/@fs/', ROOT) + '/unsafe.json?import&raw??',
),
)
.then((r) => {
text('.unsafe-fs-fetch-raw-query1-status', r.status)
return r.json()
})
.then((data) => {
text('.unsafe-fs-fetch-raw-query1', JSON.stringify(data))
})
.catch((e) => {
console.error(e)
})

fetch(
joinUrlSegments(
base,
joinUrlSegments('/@fs/', ROOT) + '/unsafe.json?import&raw?&',
),
)
.then((r) => {
text('.unsafe-fs-fetch-raw-query2-status', r.status)
return r.json()
})
.then((data) => {
text('.unsafe-fs-fetch-raw-query2', JSON.stringify(data))
})
.catch((e) => {
console.error(e)
})

// outside root with special characters #8498
fetch(
joinUrlSegments(
Expand Down
Loading