Skip to content

Commit 9ced958

Browse files
committed
🐛 处理一些特殊的沙盒关键字
1 parent 9d31872 commit 9ced958

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

src/app/service/content/create_context.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ export const createProxyContext = <const Context extends GMWorldContext>(context
285285
...unscopables,
286286
};
287287

288+
// 处理特殊关键字,不能穿越出沙盒,也不能被外部修改
289+
for (const key of ["define", "module", "exports"]) {
290+
mySandbox[key] = undefined;
291+
}
292+
288293
// 脚本window设置
289294

290295
// 把 GM Api (或其他全域API) 复製到 脚本window

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,10 @@ describe("sandbox", () => {
132132
scriptRes2.code = `
133133
!function(t, e) {
134134
"object" == typeof exports ? module.exports = exports = e() : "function" == typeof define && define.amd ? define([], e) : t.CryptoJS = e()
135-
// console.log("object" == typeof exports,"function" == typeof define)
136135
} (this, function () {
137136
return { test: "ok3" }
138137
});
139-
// console.log(CryptoJS)
140-
return ((typeof CryptoJS === "object") ? CryptoJS?.test : undefined);`;
138+
return CryptoJS.test;`;
141139
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
142140
const ret = await sandboxExec.exec();
143141
expect(ret).toEqual("ok3");
@@ -569,6 +567,14 @@ describe("沙盒环境测试", async () => {
569567
expect(Object.prototype.hasOwnProperty.call(_this, "test")).toEqual(false);
570568
});
571569

570+
it("特殊关键字不能穿透沙盒", async () => {
571+
expect(_global["define"]).toEqual("特殊关键字不能穿透沙盒");
572+
expect(_this["define"]).toBeUndefined();
573+
_this["define"] = "ok";
574+
expect(_this["define"]).toEqual("ok");
575+
expect(_global["define"]).toEqual("特殊关键字不能穿透沙盒");
576+
});
577+
572578
it("RegExp", async () => {
573579
const script = Object.assign({}, scriptRes2) as ScriptLoadInfo;
574580
// @ts-ignore

tests/vitest.setup.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,6 @@ global.sandboxTestValue2 = "sandboxTestValue2";
115115
global.ttest1 = 1;
116116
//@ts-ignore
117117
global.ttest2 = 2;
118+
119+
//@ts-ignore
120+
global.define = "特殊关键字不能穿透沙盒";

0 commit comments

Comments
 (0)