File tree Expand file tree Collapse file tree 4 files changed +35
-1
lines changed
packages/vite/src/node/server/middlewares Expand file tree Collapse file tree 4 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -232,7 +232,7 @@ export function isFileServingAllowed(
232232 return false
233233}
234234
235- function ensureServingAccess (
235+ export function ensureServingAccess (
236236 url : string ,
237237 server : ViteDevServer ,
238238 res : ServerResponse ,
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import {
1212 isJSRequest ,
1313 normalizePath ,
1414 prettifyUrl ,
15+ rawRE ,
1516 removeImportQuery ,
1617 removeTimestampQuery ,
1718 urlRE ,
@@ -35,6 +36,7 @@ import { ERR_CLOSED_SERVER } from '../pluginContainer'
3536import { getDepsOptimizer } from '../../optimizer'
3637import { cleanUrl , unwrapId , withTrailingSlash } from '../../../shared/utils'
3738import { NULL_BYTE_PLACEHOLDER } from '../../../shared/constants'
39+ import { ensureServingAccess } from './static'
3840
3941const debugCache = createDebugger ( 'vite:cache' )
4042
@@ -161,6 +163,13 @@ export function transformMiddleware(
161163 warnAboutExplicitPublicPathInUrl ( url )
162164 }
163165
166+ if (
167+ ( rawRE . test ( url ) || urlRE . test ( url ) ) &&
168+ ! ensureServingAccess ( url , server , res , next )
169+ ) {
170+ return
171+ }
172+
164173 if (
165174 isJSRequest ( url ) ||
166175 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