Skip to content

Commit a6da450

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

5 files changed

Lines changed: 37 additions & 3 deletions

File tree

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

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

32-
constrawRE=/(\?|&)raw(?:&|$)/
33-
consturlRE=/(\?|&)url(?:&|$)/
32+
exportconstrawRE=/(\?|&)raw(?:&|$)/
33+
exportconsturlRE=/(\?|&)url(?:&|$)/
3434

3535
constassetCache=newWeakMap<ResolvedConfig,Map<string,string>>()
3636

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export function isFileServingAllowed(
174174
returnfalse
175175
}
176176

177-
functionensureServingAccess(
177+
exportfunctionensureServingAccess(
178178
url: string,
179179
server: ViteDevServer,
180180
res: ServerResponse,

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

Lines changed: 9 additions & 0 deletions
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
constdebugCache=createDebugger('vite:cache')
4042
constisDebug=!!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‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ describe.runIf(isServe)('main', () => {
7676
expect(awaitpage.textContent('.unsafe-fs-fetch-status')).toBe('403')
7777
})
7878

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

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ <h2>Safe /@fs/ Fetch</h2>
3535
<h2>Unsafe /@fs/ Fetch</h2>
3636
<preclass="unsafe-fs-fetch-status"></pre>
3737
<preclass="unsafe-fs-fetch"></pre>
38+
<preclass="unsafe-fs-fetch-raw-status"></pre>
39+
<preclass="unsafe-fs-fetch-raw"></pre>
3840
<preclass="unsafe-fs-fetch-8498-status"></pre>
3941
<preclass="unsafe-fs-fetch-8498"></pre>
4042
<preclass="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+
returnr.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)