Skip to content

Commit 577f7e5

Browse files
committed
🐛 修复window穿透问题 #273
1 parent b93be76 commit 577f7e5

3 files changed

Lines changed: 38 additions & 18 deletions

File tree

‎src/runtime/content/exec_script.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe("GM_info", () => {
4646
expect(ret.version).toEqual(ExtVersion);
4747
expect(ret.script.version).toEqual("1.0.0");
4848
});
49-
it("sandbox", async() => {
49+
it("sandbox", async () => {
5050
scriptRes2.code = "return GM_info";
5151
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
5252
const ret = await sandboxExec.exec();
@@ -63,6 +63,10 @@ describe("unsafeWindow", () => {
6363
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
6464
const ret = await sandboxExec.exec();
6565
expect(ret).toEqual("ok");
66+
scriptRes2.code = "return window.testUnsafeWindow";
67+
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
68+
const ret2 = await sandboxExec.exec();
69+
expect(ret2).toEqual(undefined);
6670
});
6771
});
6872

‎src/runtime/content/utils.test.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe("proxy context", () => {
88
eval: () => {
99
console.log("eval");
1010
},
11+
addEventListener: () => {},
1112
};
1213
init.set("onload", true);
1314
init.set("gbok", true);
@@ -22,8 +23,7 @@ describe("proxy context", () => {
2223
it("set window null", () => {
2324
_this["onload"] = "ok";
2425
expect(_this["onload"]).toEqual("ok");
25-
expect(context["onload"]).toEqual(undefined);
26-
expect(global["onload"]).toEqual("ok");
26+
expect(global["onload"]).toEqual(null);
2727
});
2828

2929
it("update", () => {
@@ -36,14 +36,22 @@ describe("proxy context", () => {
3636
});
3737

3838
it("访问global的对象", () => {
39-
expect(_this["gbok"]).toEqual("gbok");
39+
expect(_this["gbok"]).toBeUndefined();
4040
});
4141

4242
it("禁止修改window", () => {
4343
expect(() => (_this["window"] = "ok")).toThrow();
4444
});
4545
});
4646

47+
// 只允许访问onxxxxx
48+
describe("window", () => {
49+
const _this = proxyContext({ onanimationstart: null }, {});
50+
it("window", () => {
51+
expect(_this.onanimationstart).toBeNull();
52+
});
53+
});
54+
4755
describe("兼容问题", () => {
4856
const _this = proxyContext({}, {});
4957
// https://github.com/xcanwin/KeepChatGPT 环境隔离得不够干净导致的

‎src/runtime/content/utils.ts‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,12 @@ export function proxyContext(
208208
case "self":
209209
case "globalThis":
210210
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
211-
return special.global || proxy;
211+
return proxy;
212212
case "top":
213213
case "parent":
214214
if (global[name] === global.self) {
215-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
216215
return special.global || proxy;
217216
}
218-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
219217
return global.top;
220218
default:
221219
break;
@@ -241,14 +239,17 @@ export function proxyContext(
241239
}
242240
return special[name];
243241
}
244-
if (global[name] !== undefined) {
245-
if (
246-
typeof global[name] === "function" &&
247-
!(<{ prototype: any }>global[name]).prototype
248-
) {
249-
return (<{ bind: any }>global[name]).bind(global);
242+
// 只处理onxxxx的事件
243+
if (has(global, name)) {
244+
if (name.startsWith("on")) {
245+
if (
246+
typeof global[name] === "function" &&
247+
!(<{ prototype: any }>global[name]).prototype
248+
) {
249+
return (<{ bind: any }>global[name]).bind(global);
250+
}
251+
return global[name];
250252
}
251-
return global[name];
252253
}
253254
} else if (name === Symbol.unscopables) {
254255
return unscopables;
@@ -288,8 +289,11 @@ export function proxyContext(
288289
if (has(special, name)) {
289290
return true;
290291
}
292+
// 只处理onxxxx的事件
291293
if (has(global[name], name)) {
292-
return true;
294+
if (name.startsWith("on")) {
295+
return true;
296+
}
293297
}
294298
} else if (typeof name === "symbol") {
295299
return has(thisContext, name);
@@ -306,7 +310,7 @@ export function proxyContext(
306310
default:
307311
}
308312
if (has(special, name)) {
309-
thisContext[name] = val;
313+
special[name] = val;
310314
return true;
311315
}
312316
if (init.has(name)) {
@@ -315,8 +319,12 @@ export function proxyContext(
315319
if (des && des.get && !des.set && des.configurable) {
316320
return true;
317321
}
318-
thisContext[name] = val;
319-
return true;
322+
// 只处理onxxxx的事件
323+
if (has(global, name) && name.startsWith("on")) {
324+
global.addEventListener(name.slice(2), val);
325+
thisContext[name] = val;
326+
return true;
327+
}
320328
}
321329
// @ts-ignore
322330
thisContext[name] = val;

0 commit comments

Comments
 (0)