Skip to content

Commit

Permalink
🐛 修复沙盒context作用域问题
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Mar 3, 2023
1 parent 2dcf9c2 commit 3d15519
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
3 changes: 0 additions & 3 deletions src/runtime/content/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('proxy context', () => {
it('set contenxt', () => {
_this['md5'] = 'ok';
expect(_this['md5']).toEqual('ok');
expect(context['md5']).toEqual('ok');
expect(global['md5']).toEqual(undefined);
});

Expand All @@ -30,11 +29,9 @@ describe('proxy context', () => {
it('update', () => {
_this['okk'] = 'ok';
expect(_this['okk']).toEqual('ok');
expect(context['okk']).toEqual('ok');
expect(global['okk']).toEqual(undefined);
_this['okk'] = 'ok2';
expect(_this['okk']).toEqual('ok2');
expect(context['okk']).toEqual('ok2');
expect(global['okk']).toEqual(undefined);
});

Expand Down
30 changes: 13 additions & 17 deletions src/runtime/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function proxyContext(global: any, context: any) {
// @ts-ignore
const proxy = new Proxy(context, {
defineProperty(_, name, desc) {
if (Object.defineProperty(context, name, desc)) {
if (Object.defineProperty(thisContext, name, desc)) {
return true;
}
return false;
Expand All @@ -163,22 +163,19 @@ export function proxyContext(global: any, context: any) {
break;
}
if (typeof name === "string" && name !== "undefined") {
if (context[name]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return context[name];
}
if (thisContext[name]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return thisContext[name];
}
if (context[name]) {
return context[name];
}
if (special[name] !== undefined) {
if (
typeof special[name] === "function" &&
!(<{ prototype: any }>special[name]).prototype
) {
return (<{ bind: any }>special[name]).bind(global);
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return special[name];
}
if (global[name] !== undefined) {
Expand All @@ -188,7 +185,6 @@ export function proxyContext(global: any, context: any) {
) {
return (<{ bind: any }>global[name]).bind(global);
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return global[name];
}
}
Expand All @@ -199,26 +195,24 @@ export function proxyContext(global: any, context: any) {
case "window":
case "self":
case "globalThis":
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
case "top":
case "parent":
if (global[name] === global.self) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
default:
break;
}
if (typeof name === "string" && name !== "undefined") {
if (thisContext[name]) {
return true;
}
if (context[name]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
}
if (thisContext[name]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
}
if (special[name] !== undefined) {
Expand All @@ -228,7 +222,6 @@ export function proxyContext(global: any, context: any) {
) {
return true;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
}
if (global[name] !== undefined) {
Expand All @@ -238,7 +231,6 @@ export function proxyContext(global: any, context: any) {
) {
return true;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
}
}
Expand Down Expand Up @@ -266,12 +258,16 @@ export function proxyContext(global: any, context: any) {
global[name] = val;
return true;
}
context[name] = val;
thisContext[name] = val;
return true;
},
getOwnPropertyDescriptor(_, name) {
try {
let ret = Object.getOwnPropertyDescriptor(context, name);
let ret = Object.getOwnPropertyDescriptor(thisContext, name);
if (ret) {
return ret;
}
ret = Object.getOwnPropertyDescriptor(context, name);
if (ret) {
return ret;
}
Expand Down

0 comments on commit 3d15519

Please sign in to comment.