File tree 5 files changed +37
-3
lines changed
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<
29
29
Map < string , OutputAsset >
30
30
> ( )
31
31
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 (?: & | $ ) /
34
34
35
35
const assetCache = new WeakMap < ResolvedConfig , Map < string , string > > ( )
36
36
Original file line number Diff line number Diff line change @@ -174,7 +174,7 @@ export function isFileServingAllowed(
174
174
return false
175
175
}
176
176
177
- function ensureServingAccess (
177
+ export function ensureServingAccess (
178
178
url : string ,
179
179
server : ViteDevServer ,
180
180
res : ServerResponse ,
Original file line number Diff line number Diff line change @@ -35,6 +35,8 @@ import {
35
35
ERR_OUTDATED_OPTIMIZED_DEP
36
36
} from '../../plugins/optimizedDeps'
37
37
import { getDepsOptimizer } from '../../optimizer'
38
+ import { rawRE , urlRE } from '../../plugins/asset'
39
+ import { ensureServingAccess } from './static'
38
40
39
41
const debugCache = createDebugger ( 'vite:cache' )
40
42
const isDebug = ! ! process . env . DEBUG
@@ -147,6 +149,13 @@ export function transformMiddleware(
147
149
}
148
150
}
149
151
152
+ if (
153
+ ( rawRE . test ( url ) || urlRE . test ( url ) ) &&
154
+ ! ensureServingAccess ( url , server , res , next )
155
+ ) {
156
+ return
157
+ }
158
+
150
159
if (
151
160
isJSRequest ( url ) ||
152
161
isImportRequest ( url ) ||
Original file line number Diff line number Diff line change @@ -76,6 +76,11 @@ describe.runIf(isServe)('main', () => {
76
76
expect ( await page . textContent ( '.unsafe-fs-fetch-status' ) ) . toBe ( '403' )
77
77
} )
78
78
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
+
79
84
test ( 'unsafe fs fetch with special characters (#8498)' , async ( ) => {
80
85
expect ( await page . textContent ( '.unsafe-fs-fetch-8498' ) ) . toBe ( '' )
81
86
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>
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 >
@@ -166,6 +168,24 @@ <h2>Denied</h2>
166
168
console . error ( e )
167
169
} )
168
170
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
+
169
189
// outside root with special characters #8498
170
190
fetch ( '/@fs/' + ROOT + '/root/src/%2e%2e%2f%2e%2e%2funsafe%2ejson' )
171
191
. then ( ( r ) => {
You can’t perform that action at this time.
0 commit comments