File tree 4 files changed +35
-1
lines changed
packages/vite/src/node/server/middlewares
4 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -229,7 +229,7 @@ export function isFileServingAllowed(
229
229
return false
230
230
}
231
231
232
- function ensureServingAccess (
232
+ export function ensureServingAccess (
233
233
url : string ,
234
234
server : ViteDevServer ,
235
235
res : ServerResponse ,
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import {
12
12
isJSRequest ,
13
13
normalizePath ,
14
14
prettifyUrl ,
15
+ rawRE ,
15
16
removeImportQuery ,
16
17
removeTimestampQuery ,
17
18
urlRE ,
@@ -35,6 +36,7 @@ import { ERR_CLOSED_SERVER } from '../pluginContainer'
35
36
import { getDepsOptimizer } from '../../optimizer'
36
37
import { cleanUrl , unwrapId , withTrailingSlash } from '../../../shared/utils'
37
38
import { NULL_BYTE_PLACEHOLDER } from '../../../shared/constants'
39
+ import { ensureServingAccess } from './static'
38
40
39
41
const debugCache = createDebugger ( 'vite:cache' )
40
42
@@ -158,6 +160,13 @@ export function transformMiddleware(
158
160
warnAboutExplicitPublicPathInUrl ( url )
159
161
}
160
162
163
+ if (
164
+ ( rawRE . test ( url ) || urlRE . test ( url ) ) &&
165
+ ! ensureServingAccess ( url , server , res , next )
166
+ ) {
167
+ return
168
+ }
169
+
161
170
if (
162
171
isJSRequest ( url ) ||
163
172
isImportRequest ( url ) ||
Original file line number Diff line number Diff line change @@ -77,6 +77,11 @@ describe.runIf(isServe)('main', () => {
77
77
expect ( await page . textContent ( '.unsafe-fs-fetch-status' ) ) . toBe ( '403' )
78
78
} )
79
79
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
+
80
85
test ( 'unsafe fs fetch with special characters (#8498)' , async ( ) => {
81
86
expect ( await page . textContent ( '.unsafe-fs-fetch-8498' ) ) . toBe ( '' )
82
87
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>
35
35
< h2 > Unsafe /@fs/ Fetch</ h2 >
36
36
< pre class ="unsafe-fs-fetch-status "> </ pre >
37
37
< pre class ="unsafe-fs-fetch "> </ pre >
38
+ < pre class ="unsafe-fs-fetch-raw-status "> </ pre >
39
+ < pre class ="unsafe-fs-fetch-raw "> </ pre >
38
40
< pre class ="unsafe-fs-fetch-8498-status "> </ pre >
39
41
< pre class ="unsafe-fs-fetch-8498 "> </ pre >
40
42
< pre class ="unsafe-fs-fetch-8498-2-status "> </ pre >
@@ -188,6 +190,24 @@ <h2>Denied</h2>
188
190
console . error ( e )
189
191
} )
190
192
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
+
191
211
// outside root with special characters #8498
192
212
fetch (
193
213
joinUrlSegments (
You can’t perform that action at this time.
0 commit comments