Skip to content

Commit 03f1033

Browse files
committed
fix: fs raw query (#18112)
1 parent e710c2f commit 03f1033

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
@@ -229,7 +229,7 @@ export function isFileServingAllowed(
229229
return false
230230
}
231231

232-
function ensureServingAccess(
232+
export function ensureServingAccess(
233233
url: string,
234234
server: ViteDevServer,
235235
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,
@@ -34,6 +35,7 @@ import { ERR_CLOSED_SERVER } from '../pluginContainer'
3435
import { getDepsOptimizer } from '../../optimizer'
3536
import { cleanUrl, unwrapId, withTrailingSlash } from '../../../shared/utils'
3637
import { NULL_BYTE_PLACEHOLDER } from '../../../shared/constants'
38+
import { ensureServingAccess } from './static'
3739

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

@@ -157,6 +159,13 @@ export function transformMiddleware(
157159
warnAboutExplicitPublicPathInUrl(url)
158160
}
159161

162+
if (
163+
(rawRE.test(url) || urlRE.test(url)) &&
164+
!ensureServingAccess(url, server, res, next)
165+
) {
166+
return
167+
}
168+
160169
if (
161170
isJSRequest(url) ||
162171
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)