File tree Expand file tree Collapse file tree 5 files changed +36
-3
lines changed Expand file tree Collapse file tree 5 files changed +36
-3
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ import { FS_PREFIX } from '../constants'
2929
3030export const assetUrlRE = / _ _ V I T E _ A S S E T _ _ ( [ a - z \d ] + ) _ _ (?: \$ _ ( .* ?) _ _ ) ? / g
3131
32- const rawRE = / (?: \? | & ) r a w (?: & | $ ) /
32+ export const rawRE = / (?: \? | & ) r a w (?: & | $ ) /
3333export const urlRE = / ( \? | & ) u r l (?: & | $ ) /
3434const jsSourceMapRE = / \. [ c m ] ? j s \. m a p $ /
3535const unnededFinalQueryCharRE = / [ ? & ] $ /
Original file line number Diff line number Diff line change @@ -211,7 +211,7 @@ export function isFileServingAllowed(
211211 return false
212212}
213213
214- function ensureServingAccess (
214+ export function ensureServingAccess (
215215 url : string ,
216216 server : ViteDevServer ,
217217 res : ServerResponse ,
Original file line number Diff line number Diff line change @@ -38,7 +38,8 @@ import {
3838} from '../../plugins/optimizedDeps'
3939import { ERR_CLOSED_SERVER } from '../pluginContainer'
4040import { getDepsOptimizer } from '../../optimizer'
41- import { urlRE } from '../../plugins/asset'
41+ import { rawRE , urlRE } from '../../plugins/asset'
42+ import { ensureServingAccess } from './static'
4243
4344const debugCache = createDebugger ( 'vite:cache' )
4445
@@ -166,6 +167,13 @@ export function transformMiddleware(
166167 }
167168 }
168169
170+ if (
171+ ( rawRE . test ( url ) || urlRE . test ( url ) ) &&
172+ ! ensureServingAccess ( url , server , res , next )
173+ ) {
174+ return
175+ }
176+
169177 if (
170178 isJSRequest ( url ) ||
171179 isImportRequest ( url ) ||
Original file line number Diff line number Diff 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' )
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 >
@@ -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 (
You can’t perform that action at this time.
0 commit comments