forked from scriptscat/scriptcat
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec_script.test.ts
More file actions
658 lines (611 loc) · 25.2 KB
/
exec_script.test.ts
File metadata and controls
658 lines (611 loc) · 25.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
import ExecScript from "./exec_script";
import { compileScript, compileScriptCode } from "./utils";
import { ExtVersion } from "@App/app/const";
import { describe, expect, it, vi } from "vitest";
import type { GMInfoEnv, ScriptFunc } from "./types";
import type { ScriptLoadInfo } from "../service_worker/types";
const nilFn: ScriptFunc = () => {};
const scriptRes = {
id: 0,
name: "test",
metadata: {
grant: ["none"],
version: ["1.0.0"],
},
code: "console.log('test')",
sourceCode: "sourceCode",
value: {},
} as unknown as ScriptLoadInfo;
const envInfo: GMInfoEnv = {
sandboxMode: "raw",
userAgentData: {
brands: [],
mobile: false,
platform: "",
},
isIncognito: false,
};
const noneExec = new ExecScript(scriptRes, {
envPrefix: "scripting",
message: undefined as any,
contentMsg: undefined as any,
code: nilFn,
envInfo,
});
const scriptRes2 = {
id: 0,
name: "test",
metadata: {
version: ["1.0.0"],
},
code: "console.log('test')",
sourceCode: "sourceCode",
value: {},
} as unknown as ScriptLoadInfo;
const sandboxExec = new ExecScript(scriptRes2, {
envPrefix: "scripting",
message: undefined as any,
contentMsg: undefined as any,
code: nilFn,
envInfo,
});
describe.concurrent("GM_info", () => {
it.concurrent("none", async () => {
expect(noneExec.sandboxContext).toBeUndefined();
expect(noneExec.named).not.toBeUndefined();
scriptRes.code = "return {_this:this,GM_info};";
noneExec.scriptFunc = compileScript(compileScriptCode(scriptRes));
const ret = await noneExec.exec();
expect(ret.GM_info.version).toEqual(ExtVersion);
expect(ret.GM_info.script.version).toEqual("1.0.0");
expect(ret._this).toEqual(global);
});
it.concurrent("sandbox", async () => {
expect(sandboxExec.sandboxContext).not.toBeUndefined();
expect(sandboxExec.named).toBeUndefined();
scriptRes2.code = "return {_this:this,GM_info};";
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await sandboxExec.exec();
expect(ret.GM_info.version).toEqual(ExtVersion);
expect(ret.GM_info.script.version).toEqual("1.0.0");
expect(ret._this).not.toEqual(global);
});
});
describe.concurrent("unsafeWindow", () => {
it.concurrent("unsafeWindow available", async () => {
const ret0 = sandboxExec.sandboxContext?.unsafeWindow === global;
expect(ret0).toEqual(true);
scriptRes2.code = `return unsafeWindow`;
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await sandboxExec.exec();
expect(ret).toEqual(global);
scriptRes2.code = `return window`;
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret3 = await sandboxExec.exec();
expect(ret3).not.toEqual(global);
});
it.concurrent("sandbox", async () => {
const ret0 = sandboxExec.sandboxContext?.unsafeWindow === global;
expect(ret0).toEqual(true);
// @ts-ignore
global.testUnsafeWindow = "ok";
scriptRes2.code = `return unsafeWindow.testUnsafeWindow`;
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await sandboxExec.exec();
expect(ret).toEqual("ok");
scriptRes2.code = "return window.testUnsafeWindow";
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret2 = await sandboxExec.exec();
expect(ret2).toEqual(undefined);
});
it.concurrent("sandbox NodeFilter", async () => {
const nodeFilter = global.NodeFilter;
expect(nodeFilter).toEqual(expect.any(Function));
scriptRes2.code = `return unsafeWindow.NodeFilter`;
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await sandboxExec.exec();
expect(ret).toEqual(nodeFilter);
scriptRes2.code = "return window.NodeFilter";
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret2 = await sandboxExec.exec();
expect(ret2).toEqual(nodeFilter);
});
});
describe.concurrent("sandbox", () => {
it.concurrent("global", async () => {
scriptRes2.code = "window.testObj = 'ok';return window.testObj";
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
let ret = await sandboxExec.exec();
expect(ret).toEqual("ok");
scriptRes2.code = "window.testObj = 'ok2';return testObj";
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
ret = await sandboxExec.exec();
expect(ret).toEqual("ok2");
});
it.concurrent("this", async () => {
scriptRes2.code = "this.testObj='ok2';return testObj;";
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await sandboxExec.exec();
expect(ret).toEqual("ok2");
});
it.concurrent("this2", async () => {
scriptRes2.code = `
!function(t, e) {
"object" == typeof exports ? module.exports = exports = e() : "function" == typeof define && define.amd ? define([], e) : t.CryptoJS = e()
} (this, function () {
return { test: "ok3" }
});
return CryptoJS.test;`;
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await sandboxExec.exec();
expect(ret).toEqual("ok3");
});
// 沉浸式翻译, 常量值被改变
it.concurrent("NodeFilter #214", async () => {
scriptRes2.code = `return NodeFilter.FILTER_REJECT;`;
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await sandboxExec.exec();
expect(ret).toEqual(2);
});
// RegExp.$x 内容被覆盖 https://github.com/scriptscat/scriptcat/issues/293
it.concurrent("RegExp", async () => {
scriptRes2.code = `let ok = /12(3)/.test('123');return RegExp.$1;`;
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await sandboxExec.exec();
expect(ret).toEqual("3");
});
});
describe("this", () => {
it("onload", async () => {
// null确认
global.onload = null;
expect(global.onload).toBeNull();
// onload 改变,global.onload不改变
scriptRes2.code = `onload = ()=>{};return onload;`;
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await sandboxExec.exec();
expect(ret).toEqual(expect.any(Function));
// global.onload
expect(global.onload).toBeNull();
});
it("this.onload", async () => {
// null确认
global.onload = null;
expect(global.onload).toBeNull();
// this.onload 改变,global.onload不改变
scriptRes2.code = `this.onload = () => "ok"; return this.onload;`;
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await sandboxExec.exec();
expect(ret).toEqual(expect.any(Function));
// global.onload
expect(global.onload).toBeNull();
});
it.concurrent("undefined variable", async () => {
scriptRes2.code = `return typeof testVar;`;
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await sandboxExec.exec();
expect(ret).toEqual("undefined");
});
it.concurrent("undefined variable in global", async () => {
scriptRes2.code = `return testVar;`;
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
// 在沙盒中访问未定义的变量会抛出错误
try {
await sandboxExec.exec();
// 如果没有抛出错误,测试应该失败
expect.fail("Expected an error to be thrown when accessing undefined variable");
} catch (e: any) {
expect(e.message).toContain("testVar is not defined");
}
});
});
describe("none this", () => {
it("onload", async () => {
scriptRes2.code = `onload = ()=>{};return onload;`;
noneExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await noneExec.exec();
expect(ret).toEqual(expect.any(Function));
// global.onload
expect(global.onload).toEqual(expect.any(Function));
global.onload = null; // 清理全局变量
});
it.concurrent("this.test", async () => {
scriptRes2.code = `this.test = "ok";return this.test;`;
noneExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await noneExec.exec();
expect(ret).toEqual("ok");
});
});
describe("沙盒环境测试", async () => {
//@ts-ignore
global.gbok = "gbok";
Object.assign(global, { gbok2: "gbok2" });
//@ts-ignore
global.gbok3 = function gbok3() {};
Object.assign(global, { gbok4: function gbok4() {} });
//@ts-ignore
global.gbok5 = { test: "gbok5" };
Object.assign(global, { gbok6: { test: "gbok6" } });
const _global = <any>global;
scriptRes2.code = `return [window, this];`;
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const [_win, _this] = await sandboxExec.exec();
expect(_win).toEqual(expect.any(Object));
expect(_win.setTimeout).toEqual(expect.any(Function));
describe.concurrent("测试全局变量访问性", () => {
it.concurrent("global gbok", () => {
expect(_global["gbok"]).toEqual("gbok");
expect(_global["gbok2"]).toEqual("gbok2");
expect(_global["gbok3"]?.name).toEqual("gbok3");
expect(_global["gbok4"]?.name).toEqual("gbok4");
expect(_global["gbok5"]?.test).toEqual("gbok5");
expect(_global["gbok6"]?.test).toEqual("gbok6");
// 这是后来新加入的值,沙盒中应该是无法访问的
expect(_this["gbok"]).toEqual(undefined);
});
it.concurrent("global sandboxTestValue", () => {
expect(_global["sandboxTestValue"]).toEqual("sandboxTestValue");
// 这是初始的值,沙盒中应该是可以访问的
expect(_this["sandboxTestValue"]).toEqual("sandboxTestValue");
// 删除不应该穿透到全局
delete _this["sandboxTestValue"];
expect(_this["sandboxTestValue"]).toBeUndefined();
expect(_global["sandboxTestValue"]).toEqual("sandboxTestValue");
// 全局删除同理
delete _global["sandboxTestValue2"];
expect(_this["sandboxTestValue2"]).toEqual("sandboxTestValue2");
expect(_global["sandboxTestValue2"]).toBeUndefined();
});
});
it.concurrent("set contenxt", () => {
_this["test_md5"] = "ok";
expect(_this["test_md5"]).toEqual("ok");
expect(_global["test_md5"]).toEqual(undefined);
});
describe("set window.onload null", () => {
it("初始状态确认", () => {
// null确认
_this["onload"] = null;
_global["onload"] = null;
expect(_this["onload"]).toBeNull();
expect(_global["onload"]).toBeNull();
});
describe("沙盒环境 onload 设置", () => {
it("设置 _this.onload 不影响 global.onload", () => {
const mockFn = vi.fn();
_this["onload"] = function thisOnLoad() {
mockFn();
};
expect(_this["onload"]?.name).toEqual("thisOnLoad");
expect(_global["onload"]).toBeNull();
});
// 在模拟环境无法测试:在模拟环境模拟 dispatchEvent 呼叫 this.onload 没有意义
// it("验证 onload 事件调用", () => {
// const mockFn = vi.fn();
// _this["onload"] = function thisOnLoad() {
// mockFn();
// };
// // 验证调用
// global.dispatchEvent(new Event("load"));
// expect(mockFn).toHaveBeenCalledTimes(1);
// });
// 在模拟环境无法测试:在实际操作中和TM一致
// 在非拦截式沙盒里删除 沙盒onload 后,会取得页面的真onload
// 在非拦截式沙盒里删除 真onload 后,会变undefined
// it.concurrent("删除 onload 后应该为 null", () => {
// const mockFn = vi.fn();
// _this["onload"] = function thisOnLoad() {
// mockFn();
// };
// // 验证删除
// delete _this["onload"];
// expect(_this["onload"]).toBeNull(); // 删除应该是null,而不是undefined
// // 验证删除后调用
// global.dispatchEvent(new Event("load"));
// expect(mockFn).not.toHaveBeenCalled(); // 删除后不应该再调用
// });
});
describe("全局环境 onload 设置", () => {
it("设置 global.onload 不影响 _this.onload", () => {
_this["onload"] = null;
_global["onload"] = function globalOnLoad() {};
expect(_this["onload"]).toBeNull();
expect(_global["onload"]?.name).toEqual("globalOnLoad");
});
it("清理后状态确认", () => {
_global["onload"] = null;
// 还原确认
expect(_this["onload"]).toEqual(null);
expect(_global["onload"]).toEqual(null);
});
});
});
it("update", () => {
_this["okk"] = "ok";
expect(_this["okk"]).toEqual("ok");
expect(_global["okk"]).toEqual(undefined);
_this["okk"] = "ok2";
expect(_this["okk"]).toEqual("ok2");
expect(_global["okk"]).toEqual(undefined);
});
// https://github.com/scriptscat/scriptcat/issues/273
it.concurrent("禁止穿透global对象", () => {
expect(_this["gbok"]).toBeUndefined();
expect(_this["gbok2"]).toBeUndefined();
expect(_this["gbok3"]).toBeUndefined();
expect(_this["gbok4"]).toBeUndefined();
expect(_this["gbok5"]).toBeUndefined();
expect(_this["gbok6"]).toBeUndefined();
});
it.concurrent("禁止修改window", () => {
// expect(() => (_this["window"] = "ok")).toThrow();
expect(() => {
const before = _this["window"];
_this["window"] = "ok";
if (before !== _this["window"]) throw new Error("err");
}).toThrow();
});
it.concurrent("访问location", () => {
expect(_this.location).not.toBeUndefined();
});
// 只允许访问onxxxxx
it.concurrent("window.onxxxxx", () => {
expect(_this.onanimationstart).toBeNull();
});
it.concurrent("[兼容问题] Ensure Illegal invocation can be tested", () => {
expect(global.setTimeout.name).toEqual("setTimeout");
// -----
//@ts-ignore
expect(global.setTimeoutForTest1.name).toEqual("setTimeoutForTest1");
expect(_this.setTimeoutForTest1.name).toEqual("bound setTimeoutForTest1");
//@ts-ignore
expect(() => global.setTimeout.call(global, () => {}, 1)).not.toThrow();
//@ts-ignore
expect(() => global.setTimeoutForTest1.call(global, () => {}, 1)).not.toThrow();
//@ts-ignore
expect(() => global.setTimeoutForTest1.call({}, () => {}, 1)).toThrow();
// -----
//@ts-ignore
expect(global.setTimeoutForTest2.name).toEqual("setTimeoutForTest2");
expect(_this.setTimeoutForTest2.name).toEqual("bound setTimeoutForTest2");
//@ts-ignore
expect(() => global.setTimeout.call(global, () => {}, 1)).not.toThrow();
//@ts-ignore
expect(() => global.setTimeoutForTest2.call(global, () => {}, 1)).not.toThrow();
//@ts-ignore
expect(() => global.setTimeoutForTest2.call({}, () => {}, 1)).toThrow();
});
// https://github.com/xcanwin/KeepChatGPT 环境隔离得不够干净导致的
it.concurrent("[兼容问题] Uncaught TypeError: Illegal invocation #189", () => {
// setTimeout 和 setTimeoutForTest1 都测试吧
const promise1 = new Promise((resolve) => {
console.log(_this.setTimeout.prototype);
_this.setTimeoutForTest1(resolve, 1);
});
const promise2 = new Promise((resolve) => {
console.log(_this.setTimeout.prototype);
_this.setTimeout(resolve, 1);
});
const res = Promise.all([promise1, promise2]);
expect(res.then((res) => (!res[0] && !res[1] ? "ok" : "ng"))).resolves.toBe("ok");
});
// AC-baidu-重定向优化百度搜狗谷歌必应搜索_favicon_双列
it.concurrent("[兼容问题] TypeError: Object.freeze is not a function #116", () => {
expect(() => _this.Object.freeze({})).not.toThrow();
});
it.concurrent("Proxy Function #985", () => {
// setTimeout 和 setTimeoutForTest2 都测试吧
const promise1 = new Promise((resolve) => {
console.log(_this.setTimeout.prototype);
_this.setTimeoutForTest2(resolve, 1);
});
const promise2 = new Promise((resolve) => {
console.log(_this.setTimeout.prototype);
_this.setTimeout(resolve, 1);
});
const res = Promise.all([promise1, promise2]);
expect(res.then((res) => (res[0] === "proxy" && !res[1] ? "ok" : "ng"))).resolves.toBe("ok");
});
const tag = (<any>global)[Symbol.toStringTag]; // 实际环境:'[object Window]' 测试环境:'[object global]'
// 允许往global写入Symbol属性,影响内容: https://bbs.tampermonkey.net.cn/thread-5509-1-1.html
it.concurrent("Symbol", () => {
const s = Symbol("test");
_this[s] = "ok";
expect(_this[s]).toEqual("ok");
});
// toString.call(window)返回的是'[object Object]',影响内容: https://github.com/scriptscat/scriptcat/issues/260
it.concurrent("toString.call(window)", () => {
expect(toString.call(_this)).toEqual(`[object Window]`);
});
// 与TM保持一致,toString返回global([object Window]) #737
it.concurrent("toString", async () => {
scriptRes2.code = `return {
toStringThis: {}.toString.call(this),
toStringWindow: {}.toString.call(window),
toString: toString(),
}`;
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = await sandboxExec.exec();
expect(ret).toEqual({
toStringThis: `[object Window]`,
toStringWindow: `[object Window]`,
toString: `[object ${tag}]`,
});
});
// Object.hasOwnProperty穿透 https://github.com/scriptscat/scriptcat/issues/272
it.concurrent("[穿透测试] Object.hasOwnProperty", () => {
expect(Object.prototype.hasOwnProperty.call(_this, "test1")).toEqual(false);
_this.test1 = "ok";
expect(Object.prototype.hasOwnProperty.call(_this, "test1")).toEqual(true);
expect(Object.prototype.hasOwnProperty.call(_this, "test")).toEqual(false);
});
// https://github.com/scriptscat/scriptcat/issues/962
// window.constructor === Window
// window instanceof Window === false
it.concurrent("TM Sandbox Window", () => {
const window = global;
//@ts-ignore
expect(_win.PERSISTENT === window.PERSISTENT).toEqual(true);
//@ts-ignore
expect(_win.TEMPORARY === window.TEMPORARY).toEqual(true);
//@ts-ignore
expect(_win.constructor === window.constructor).toEqual(true);
//@ts-ignore
expect(_win.__proto__ === window.__proto__).toEqual(true);
//@ts-ignore
expect(typeof window.constructor === "function").toEqual(true);
//@ts-ignore
expect(typeof _win.constructor === "function").toEqual(true);
//@ts-ignore
expect(window instanceof window.constructor === true).toEqual(true);
//@ts-ignore
expect(_win instanceof window.constructor === false).toEqual(true);
//@ts-ignore
expect(_win.addEventListener !== window.addEventListener).toEqual(true);
//@ts-ignore
expect(Object.getPrototypeOf(_win) === null).toEqual(true);
});
it.concurrent("特殊关键字不能穿透沙盒", async () => {
expect(_global["define"]).toEqual("特殊关键字不能穿透沙盒");
expect(_this["define"]).toBeUndefined();
_this["define"] = "ok";
expect(_this["define"]).toEqual("ok");
expect(_global["define"]).toEqual("特殊关键字不能穿透沙盒");
});
it.concurrent("RegExp", async () => {
const script = Object.assign({}, scriptRes2) as ScriptLoadInfo;
const exec = new ExecScript(script, {
envPrefix: "scripting",
message: undefined as any,
contentMsg: undefined as any,
code: nilFn,
envInfo,
});
script.code = `const str = "12345";
const reg = /(123)/;
return [str.match(reg), RegExp.$1];`;
exec.scriptFunc = compileScript(compileScriptCode(script));
const ret = await exec.exec();
expect(ret?.[0][1]).toEqual("123");
expect(ret?.[1]).toEqual("123");
});
it.concurrent("沙盒之间不应该共享变量", async () => {
const script = Object.assign({}, scriptRes2) as ScriptLoadInfo;
script.code = `this.testVar = "ok"; ttest1 = "ok"; return {testVar: this.testVar, testVar2: this.testVar2, ttest1: typeof ttest1, ttest2: typeof ttest2};`;
const exec1 = new ExecScript(script, {
envPrefix: "scripting",
message: undefined as any,
contentMsg: undefined as any,
code: nilFn,
envInfo,
});
exec1.scriptFunc = compileScript(compileScriptCode(script));
const ret1 = await exec1.exec();
expect(ret1).toEqual({ testVar: "ok", testVar2: undefined, ttest1: "string", ttest2: "number" });
const script2 = Object.assign({}, scriptRes2) as ScriptLoadInfo;
script2.code = `this.testVar2 = "ok"; ttest2 = "ok"; return {testVar: this.testVar, testVar2: this.testVar2, ttest1: typeof ttest1, ttest2: typeof ttest2};`;
const exec2 = new ExecScript(script2, {
envPrefix: "scripting",
message: undefined as any,
contentMsg: undefined as any,
code: nilFn,
envInfo,
});
exec2.scriptFunc = compileScript(compileScriptCode(script2));
const ret2 = await exec2.exec();
expect(ret2).toEqual({ testVar: undefined, testVar2: "ok", ttest1: "number", ttest2: "string" });
const script3 = Object.assign({}, scriptRes2) as ScriptLoadInfo;
script3.code = `onload = function (){return 123}; return {onload, thisOnload: this.onload, winOnload: window.onload};`;
const exec3 = new ExecScript(script3, {
envPrefix: "scripting",
message: undefined as any,
contentMsg: undefined as any,
code: nilFn,
envInfo,
});
exec3.scriptFunc = compileScript(compileScriptCode(script3));
const ret3 = await exec3.exec();
expect(ret3.onload).toEqual(expect.any(Function));
expect(ret3.thisOnload).toEqual(expect.any(Function));
expect(ret3.winOnload).toEqual(expect.any(Function));
expect(ret3.thisOnload).toEqual(ret3.onload);
expect(ret3.winOnload).toEqual(ret3.onload);
const cacheRet3Onload = ret3.onload;
const script4 = Object.assign({}, scriptRes2) as ScriptLoadInfo;
script4.code = `onload = function (){return 456}; return {onload, thisOnload: this.onload, winOnload: window.onload};`;
const exec4 = new ExecScript(script4, {
envPrefix: "scripting",
message: undefined as any,
contentMsg: undefined as any,
code: nilFn,
envInfo,
});
exec4.scriptFunc = compileScript(compileScriptCode(script4));
const ret4 = await exec4.exec();
expect(ret4.onload).toEqual(expect.any(Function));
expect(ret4.thisOnload).toEqual(expect.any(Function));
expect(ret4.winOnload).toEqual(expect.any(Function));
expect(ret4.thisOnload).toEqual(ret4.onload);
expect(ret4.winOnload).toEqual(ret4.onload);
// onload3 不等如 onload4
expect(ret4.onload).not.toEqual(cacheRet3Onload);
// onload3, onload4 能各自独立执行输出 123 及 456
expect(cacheRet3Onload() + ret4.onload()).toEqual(579);
});
it.concurrent("沙盒之间能用unsafeWindow(及全局作用域)共享变量", async () => {
const script = Object.assign({}, scriptRes2) as ScriptLoadInfo;
script.code = `unsafeWindow.testSVar1 = "shareA"; ggaa1 = "ok"; return {testSVar1: unsafeWindow.testSVar1, testSVar2: unsafeWindow.testSVar2, ggaa1: typeof ggaa1, ggaa2: typeof ggaa2};`;
const exec1 = new ExecScript(script, {
envPrefix: "scripting",
message: undefined as any,
contentMsg: undefined as any,
code: nilFn,
envInfo,
});
exec1.scriptFunc = compileScript(compileScriptCode(script));
const ret1 = await exec1.exec();
expect(ret1).toEqual({ testSVar1: "shareA", testSVar2: undefined, ggaa1: "string", ggaa2: "undefined" });
const script2 = Object.assign({}, scriptRes2) as ScriptLoadInfo;
script2.code = `unsafeWindow.testSVar2 = "shareB"; ggaa2 = "ok"; return {testSVar1: unsafeWindow.testSVar1, testSVar2: unsafeWindow.testSVar2, ggaa1: typeof ggaa1, ggaa2: typeof ggaa2};`;
const exec2 = new ExecScript(script2, {
envPrefix: "scripting",
message: undefined as any,
contentMsg: undefined as any,
code: nilFn,
envInfo,
});
exec2.scriptFunc = compileScript(compileScriptCode(script2));
const ret2 = await exec2.exec();
expect(ret2).toEqual({ testSVar1: "shareA", testSVar2: "shareB", ggaa1: "string", ggaa2: "string" });
});
it.concurrent("测试SC沙盒与TM沙盒有相近的特殊处理", async () => {
const script1 = Object.assign({}, scriptRes2) as ScriptLoadInfo;
script1.code = `onfocus = function(){}; onresize = 123; onblur = "123"; const ret = {onfocus, onresize, onblur}; onfocus = null; onresize = null; onblur = null; return ret;`;
const exec1 = new ExecScript(script1, {
envPrefix: "scripting",
message: undefined as any,
contentMsg: undefined as any,
code: nilFn,
envInfo,
});
exec1.scriptFunc = compileScript(compileScriptCode(script1));
const ret1 = await exec1.exec();
expect(ret1.onfocus).toEqual(expect.any(Function));
expect(ret1.onresize).toBeNull();
expect(ret1.onblur).toBeNull();
const script2 = Object.assign({}, scriptRes2) as ScriptLoadInfo;
script2.code = `window.onfocus = function(){}; window.onresize = 123; window.onblur = "123"; const {onfocus, onresize, onblur} = window; const ret = {onfocus, onresize, onblur}; window.onfocus = null; window.onresize = null; window.onblur = null; return ret;`;
const exec2 = new ExecScript(script2, {
envPrefix: "scripting",
message: undefined as any,
contentMsg: undefined as any,
code: nilFn,
envInfo,
});
exec2.scriptFunc = compileScript(compileScriptCode(script2));
const ret2 = await exec2.exec();
expect(ret2.onfocus).toEqual(expect.any(Function));
expect(ret2.onresize).toBeNull();
expect(ret2.onblur).toBeNull();
});
});