Skip to content

Commit 6820bb3

Browse files
committed
fix: fs raw query (#18112)
1 parent 37881e7 commit 6820bb3

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

packages/vite/src/node/server/middlewares/static.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export function isFileServingAllowed(
232232
return false
233233
}
234234

235-
function ensureServingAccess(
235+
export function ensureServingAccess(
236236
url: string,
237237
server: ViteDevServer,
238238
res: ServerResponse,

packages/vite/src/node/server/middlewares/transform.ts

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
isJSRequest,
1313
normalizePath,
1414
prettifyUrl,
15+
rawRE,
1516
removeImportQuery,
1617
removeTimestampQuery,
1718
urlRE,
@@ -35,6 +36,7 @@ import { ERR_CLOSED_SERVER } from '../pluginContainer'
3536
import { getDepsOptimizer } from '../../optimizer'
3637
import { cleanUrl, unwrapId, withTrailingSlash } from '../../../shared/utils'
3738
import { NULL_BYTE_PLACEHOLDER } from '../../../shared/constants'
39+
import { ensureServingAccess } from './static'
3840

3941
const debugCache = createDebugger('vite:cache')
4042

@@ -161,6 +163,13 @@ export function transformMiddleware(
161163
warnAboutExplicitPublicPathInUrl(url)
162164
}
163165

166+
if (
167+
(rawRE.test(url) || urlRE.test(url)) &&
168+
!ensureServingAccess(url, server, res, next)
169+
) {
170+
return
171+
}
172+
164173
if (
165174
isJSRequest(url) ||
166175
isImportRequest(url) ||

playground/fs-serve/__tests__/fs-serve.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ describe.runIf(isServe)('main', () => {
7777
expect(await page.textContent('.unsafe-fs-fetch-status')).toBe('403')
7878
})
7979

80+
test('unsafe fs fetch', async () => {
81+
expect(await page.textContent('.unsafe-fs-fetch-raw')).toBe('')
82+
expect(await page.textContent('.unsafe-fs-fetch-raw-status')).toBe('403')
83+
})
84+
8085
test('unsafe fs fetch with special characters (#8498)', async () => {
8186
expect(await page.textContent('.unsafe-fs-fetch-8498')).toBe('')
8287
expect(await page.textContent('.unsafe-fs-fetch-8498-status')).toBe('404')

playground/fs-serve/root/src/index.html

+20
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ <h2>Safe /@fs/ Fetch</h2>
3535
<h2>Unsafe /@fs/ Fetch</h2>
3636
<pre class="unsafe-fs-fetch-status"></pre>
3737
<pre class="unsafe-fs-fetch"></pre>
38+
<pre class="unsafe-fs-fetch-raw-status"></pre>
39+
<pre class="unsafe-fs-fetch-raw"></pre>
3840
<pre class="unsafe-fs-fetch-8498-status"></pre>
3941
<pre class="unsafe-fs-fetch-8498"></pre>
4042
<pre class="unsafe-fs-fetch-8498-2-status"></pre>
@@ -188,6 +190,24 @@ <h2>Denied</h2>
188190
console.error(e)
189191
})
190192

193+
// not imported before, outside of root, treated as unsafe
194+
fetch(
195+
joinUrlSegments(
196+
base,
197+
joinUrlSegments('/@fs/', ROOT) + '/unsafe.json?import&raw',
198+
),
199+
)
200+
.then((r) => {
201+
text('.unsafe-fs-fetch-raw-status', r.status)
202+
return r.json()
203+
})
204+
.then((data) => {
205+
text('.unsafe-fs-fetch-raw', JSON.stringify(data))
206+
})
207+
.catch((e) => {
208+
console.error(e)
209+
})
210+
191211
// outside root with special characters #8498
192212
fetch(
193213
joinUrlSegments(

0 commit comments

Comments
 (0)