Skip to content

Commit 3d15519

Browse files
committed
🐛 修复沙盒context作用域问题
1 parent 2dcf9c2 commit 3d15519

2 files changed

Lines changed: 13 additions & 20 deletions

File tree

src/runtime/content/utils.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ describe('proxy context', () => {
1616
it('set contenxt', () => {
1717
_this['md5'] = 'ok';
1818
expect(_this['md5']).toEqual('ok');
19-
expect(context['md5']).toEqual('ok');
2019
expect(global['md5']).toEqual(undefined);
2120
});
2221

@@ -30,11 +29,9 @@ describe('proxy context', () => {
3029
it('update', () => {
3130
_this['okk'] = 'ok';
3231
expect(_this['okk']).toEqual('ok');
33-
expect(context['okk']).toEqual('ok');
3432
expect(global['okk']).toEqual(undefined);
3533
_this['okk'] = 'ok2';
3634
expect(_this['okk']).toEqual('ok2');
37-
expect(context['okk']).toEqual('ok2');
3835
expect(global['okk']).toEqual(undefined);
3936
});
4037

src/runtime/content/utils.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export function proxyContext(global: any, context: any) {
139139
// @ts-ignore
140140
const proxy = new Proxy(context, {
141141
defineProperty(_, name, desc) {
142-
if (Object.defineProperty(context, name, desc)) {
142+
if (Object.defineProperty(thisContext, name, desc)) {
143143
return true;
144144
}
145145
return false;
@@ -163,22 +163,19 @@ export function proxyContext(global: any, context: any) {
163163
break;
164164
}
165165
if (typeof name === "string" && name !== "undefined") {
166-
if (context[name]) {
167-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
168-
return context[name];
169-
}
170166
if (thisContext[name]) {
171-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
172167
return thisContext[name];
173168
}
169+
if (context[name]) {
170+
return context[name];
171+
}
174172
if (special[name] !== undefined) {
175173
if (
176174
typeof special[name] === "function" &&
177175
!(<{ prototype: any }>special[name]).prototype
178176
) {
179177
return (<{ bind: any }>special[name]).bind(global);
180178
}
181-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
182179
return special[name];
183180
}
184181
if (global[name] !== undefined) {
@@ -188,7 +185,6 @@ export function proxyContext(global: any, context: any) {
188185
) {
189186
return (<{ bind: any }>global[name]).bind(global);
190187
}
191-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
192188
return global[name];
193189
}
194190
}
@@ -199,26 +195,24 @@ export function proxyContext(global: any, context: any) {
199195
case "window":
200196
case "self":
201197
case "globalThis":
202-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
203198
return true;
204199
case "top":
205200
case "parent":
206201
if (global[name] === global.self) {
207-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
208202
return true;
209203
}
210-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
211204
return true;
212205
default:
213206
break;
214207
}
215208
if (typeof name === "string" && name !== "undefined") {
209+
if (thisContext[name]) {
210+
return true;
211+
}
216212
if (context[name]) {
217-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
218213
return true;
219214
}
220215
if (thisContext[name]) {
221-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
222216
return true;
223217
}
224218
if (special[name] !== undefined) {
@@ -228,7 +222,6 @@ export function proxyContext(global: any, context: any) {
228222
) {
229223
return true;
230224
}
231-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
232225
return true;
233226
}
234227
if (global[name] !== undefined) {
@@ -238,7 +231,6 @@ export function proxyContext(global: any, context: any) {
238231
) {
239232
return true;
240233
}
241-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
242234
return true;
243235
}
244236
}
@@ -266,12 +258,16 @@ export function proxyContext(global: any, context: any) {
266258
global[name] = val;
267259
return true;
268260
}
269-
context[name] = val;
261+
thisContext[name] = val;
270262
return true;
271263
},
272264
getOwnPropertyDescriptor(_, name) {
273265
try {
274-
let ret = Object.getOwnPropertyDescriptor(context, name);
266+
let ret = Object.getOwnPropertyDescriptor(thisContext, name);
267+
if (ret) {
268+
return ret;
269+
}
270+
ret = Object.getOwnPropertyDescriptor(context, name);
275271
if (ret) {
276272
return ret;
277273
}

0 commit comments

Comments
 (0)