Skip to content

Commit a4cefbc

Browse files
committed
🐛 修复沙盒toString问题 #737
1 parent 35b6f58 commit a4cefbc

2 files changed

Lines changed: 19 additions & 19 deletions

File tree

src/app/service/content/create_context.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,6 @@ const getAllPropertyDescriptors = (obj: any, callback: ForEachCallback<[string |
104104
}
105105
};
106106

107-
// mySandbox 不进行with变量拦截
108-
const unscopables: Record<PropertyKey, any> = {
109-
this: true,
110-
arguments: true,
111-
// "await": true,
112-
// "define": true,
113-
// "module": true,
114-
// "exports": true,
115-
[Symbol.unscopables]: true,
116-
};
117-
118107
// 在 CacheSet 加入的propKeys将会在 mySandbox 实装阶段时设置
119108
const descsCache: Set<string | symbol> = new Set(["eval", "window", "self", "globalThis", "top", "parent"]);
120109

@@ -174,6 +163,8 @@ descsCache.clear(); // 内存释放
174163
const sharedInitCopy = Object.create(null, {
175164
...initOwnDescs,
176165
...overridedDescs,
166+
// Symbol.toStringTag设置为 Window
167+
[Symbol.toStringTag]: { value: "Window", writable: false, enumerable: false, configurable: true },
177168
});
178169

179170
type GMWorldContext = typeof globalThis & Record<PropertyKey, any>;
@@ -290,12 +281,6 @@ export const createProxyContext = <const Context extends GMWorldContext>(context
290281
// 把初始Copy加上特殊变量后,生成一份新Copy
291282
mySandbox = Object.create(Object.getPrototypeOf(sharedInitCopy), ownDescs);
292283

293-
// 用於避开 mySandbox 的with拦截
294-
mySandbox[Symbol.unscopables] = {
295-
...(mySandbox[Symbol.unscopables] || {}),
296-
...unscopables,
297-
};
298-
299284
// 处理特殊关键字,不能穿越出沙盒,也不能被外部修改
300285
for (const key of ["define", "module", "exports"]) {
301286
mySandbox[key] = undefined;

src/app/service/content/exec_script.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,23 @@ describe("沙盒环境测试", async () => {
407407
});
408408
// toString.call(window)返回的是'[object Object]',影响内容: https://github.com/scriptscat/scriptcat/issues/260
409409
it("toString.call(window)", () => {
410-
expect(toString.call(_this)).toEqual(`[object ${tag}]`); // 与 global 一致
411-
expect(toString.call(_this)).not.toEqual("[object Object]"); // 不是 [object Object]
410+
expect(toString.call(_this)).toEqual(`[object Window]`);
411+
});
412+
413+
// 与TM保持一致,toString返回global([object Window]) #737
414+
it("toString", async () => {
415+
scriptRes2.code = `return {
416+
toStringThis: {}.toString.call(this),
417+
toStringWindow: {}.toString.call(window),
418+
toString: toString(),
419+
}`;
420+
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
421+
const ret = await sandboxExec.exec();
422+
expect(ret).toEqual({
423+
toStringThis: `[object Window]`,
424+
toStringWindow: `[object Window]`,
425+
toString: `[object ${tag}]`,
426+
});
412427
});
413428

414429
// Object.hasOwnProperty穿透 https://github.com/scriptscat/scriptcat/issues/272

0 commit comments

Comments
 (0)