File tree Expand file tree Collapse file tree 5 files changed +37
-3
lines changed Expand file tree Collapse file tree 5 files changed +37
-3
lines changed Original file line number Diff line number Diff line change @@ -29,8 +29,8 @@ export const duplicateAssets = new WeakMap<
2929 Map < string , OutputAsset >
3030> ( )
3131
32- const rawRE = / ( \? | & ) r a w (?: & | $ ) /
33- const urlRE = / ( \? | & ) u r l (?: & | $ ) /
32+ export const rawRE = / ( \? | & ) r a w (?: & | $ ) /
33+ export const urlRE = / ( \? | & ) u r l (?: & | $ ) /
3434
3535const assetCache = new WeakMap < ResolvedConfig , Map < string , string > > ( )
3636
Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff line change @@ -35,6 +35,8 @@ import {
3535 ERR_OUTDATED_OPTIMIZED_DEP
3636} from '../../plugins/optimizedDeps'
3737import { getDepsOptimizer } from '../../optimizer'
38+ import { rawRE , urlRE } from '../../plugins/asset'
39+ import { ensureServingAccess } from './static'
3840
3941const debugCache = createDebugger ( 'vite:cache' )
4042const 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 ) ||
Original file line number Diff line number Diff 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' )
Original file line number Diff line number Diff 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 ) => {
You can’t perform that action at this time.
0 commit comments