Skip to content

Commit 3edc458

Browse files
committed
fix: backport #18112, fs raw query
1 parent 1a8728f commit 3edc458

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

packages/vite/src/node/plugins/asset.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export const duplicateAssets = new WeakMap<
2929
Map<string, OutputAsset>
3030
>()
3131

32-
const rawRE = /(\?|&)raw(?:&|$)/
33-
const urlRE = /(\?|&)url(?:&|$)/
32+
export const rawRE = /(\?|&)raw(?:&|$)/
33+
export const urlRE = /(\?|&)url(?:&|$)/
3434

3535
const assetCache = new WeakMap<ResolvedConfig, Map<string, string>>()
3636

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export function isFileServingAllowed(
174174
return false
175175
}
176176

177-
function ensureServingAccess(
177+
export function ensureServingAccess(
178178
url: string,
179179
server: ViteDevServer,
180180
res: ServerResponse,

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

+9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import {
3535
ERR_OUTDATED_OPTIMIZED_DEP
3636
} from '../../plugins/optimizedDeps'
3737
import { getDepsOptimizer } from '../../optimizer'
38+
import { rawRE, urlRE } from '../../plugins/asset'
39+
import { ensureServingAccess } from './static'
3840

3941
const debugCache = createDebugger('vite:cache')
4042
const isDebug = !!process.env.DEBUG
@@ -147,6 +149,13 @@ export function transformMiddleware(
147149
}
148150
}
149151

152+
if (
153+
(rawRE.test(url) || urlRE.test(url)) &&
154+
!ensureServingAccess(url, server, res, next)
155+
) {
156+
return
157+
}
158+
150159
if (
151160
isJSRequest(url) ||
152161
isImportRequest(url) ||

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

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

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

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>
@@ -166,6 +168,24 @@ <h2>Denied</h2>
166168
console.error(e)
167169
})
168170

171+
// not imported before, outside of root, treated as unsafe
172+
fetch(
173+
joinUrlSegments(
174+
base,
175+
joinUrlSegments('/@fs/', ROOT) + '/unsafe.json?import&raw',
176+
),
177+
)
178+
.then((r) => {
179+
text('.unsafe-fs-fetch-raw-status', r.status)
180+
return r.json()
181+
})
182+
.then((data) => {
183+
text('.unsafe-fs-fetch-raw', JSON.stringify(data))
184+
})
185+
.catch((e) => {
186+
console.error(e)
187+
})
188+
169189
// outside root with special characters #8498
170190
fetch('/@fs/' + ROOT + '/root/src/%2e%2e%2f%2e%2e%2funsafe%2ejson')
171191
.then((r) => {

0 commit comments

Comments
 (0)