Repository navigation
Expand file tree
/
Copy pathsandbox_function_test.js
More file actions
920 lines (844 loc) · 26.8 KB
/
Copy pathsandbox_function_test.js
File metadata and controls
920 lines (844 loc) · 26.8 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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
// ==UserScript==
// @name 半沙盒环境测试
// @namespace https://docs.scriptcat.org/
// @version 0.2.0
// @description 测试默认 userscript 半沙盒环境,不使用 @inject-into content 或 @grant none
// @author ScriptCat
// @match https://*/*?SANDBOX_TEST_SC
// @grant GM_info
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_listValues
// @grant GM_setValues
// @grant GM_getValues
// @grant GM_addElement
// @grant GM_addStyle
// @grant GM_cookie
// @grant GM.setValue
// @grant window.close
// @grant window.focus
// @grant unsafeWindow
// @require https://cdn.jsdelivr.net/gh/scriptscat/scriptcat@36ab4ce5ff23c820a32cd13ac5a04d8834ab4d82/example/tests/lib/sctest.js
// @run-at document-end
// ==/UserScript==
var testVar1001 = 1001;
const mpt = document.body.appendChild(document.createElement("test-element-1002"));
mpt.id = "test-element-1002";
mpt.name = "test-element-1002";
(async function () {
"use strict";
const { describe, check, expect, run } = SCTest.create({ name: "半沙盒环境测试" });
const markerPrefix = `__scriptcat_sandbox_${Date.now()}_${Math.random().toString(36).slice(2)}`;
function formatValue(value) {
if (value === window) return "[sandbox window]";
if (value === unsafeWindow) return "[unsafeWindow]";
if (value === document) return "[document]";
if (value && value.nodeType) return `[node ${value.nodeName}]`;
if (typeof value === "function") return `[function ${value.name || "anonymous"}]`;
if (typeof value === "symbol") return value.toString();
try {
return JSON.stringify(value);
} catch {
return String(value);
}
}
// assertSame 只保留给 assertThrowsOrKeepsValue 内部使用(其实现不改,见下);
// 其余断言点已迁移为 expect(actual).toBe(expected)。
function assertSame(expected, actual, message) {
if (!Object.is(expected, actual)) {
throw new Error(`${message} - 期望 ${formatValue(expected)}, 实际 ${formatValue(actual)}`);
}
}
function assertNotSame(unexpected, actual, message) {
if (unexpected === actual) {
throw new Error((message || "断言失败") + " - 不应等于 " + JSON.stringify(unexpected));
}
}
function assertThrowsOrKeepsValue(assign, read, expected, message) {
let threw = false;
try {
assign();
} catch {
threw = true;
}
assertSame(expected, read(), `${message}${threw ? "(赋值抛出,值保持不变)" : ""}`);
}
function waitForEventLoop() {
return new Promise((resolve) => setTimeout(resolve, 0));
}
function withTimeout(promise, label, ms = 5000) {
let timer = null;
const timeout = new Promise((_, reject) => {
timer = setTimeout(() => {
reject(new Error(`${label} 超时 ${ms}ms`));
}, ms);
});
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
}
async function withCleanup(fn, cleanup) {
try {
return await fn();
} finally {
cleanup();
}
}
function snapshotPageProps(keys) {
const snapshots = Object.create(null);
for (const key of keys) {
snapshots[key] = Object.getOwnPropertyDescriptor(unsafeWindow, key);
}
return snapshots;
}
function restorePageProps(snapshots) {
for (const key of Object.keys(snapshots)) {
const desc = snapshots[key];
if (desc) {
Object.defineProperty(unsafeWindow, key, desc);
} else {
delete unsafeWindow[key];
}
}
}
describe("沙盒全局身份", () => {
check(
"自动断言",
"检测全局 testVar1001 会否跳出沙盒",
() => {
expect(window["testVar1001"]).toBe(undefined);
expect(unsafeWindow["testVar1001"]).toBe(undefined);
},
null,
null,
null
);
check(
"自动断言",
"检测全局 test-element-1002",
() => {
expect(unsafeWindow["test-element-1002"]?.id).toBe("test-element-1002");
expect(window["test-element-1002"]?.id).toBe(undefined);
},
null,
null,
null
);
check(
"自动断言",
"window/self/globalThis/top/parent/frames 均指向沙盒对象",
() => {
expect(typeof unsafeWindow).toBe("object");
assertNotSame(unsafeWindow, window, "默认 grant 环境下 window 不应是页面 window");
expect(self).toBe(window);
expect(globalThis).toBe(window);
expect(top).toBe(window);
expect(parent).toBe(window);
expect(frames).toBe(window);
expect(Object.prototype.toString.call(window)).toBe("[object Window]");
},
null,
null,
null
);
check(
"自动断言",
"沙盒 window 使用空原型,但保留页面 Window 外观 (Issue #962)",
() => {
expect(Object.getPrototypeOf(window)).toBe(null);
expect(window.constructor).toBe(unsafeWindow.constructor);
expect(window.__proto__).toBe(unsafeWindow.__proto__);
expect(window instanceof unsafeWindow.constructor).toBe(false);
},
null,
null,
null
);
check(
"自动断言",
"页面 DOM getter 返回真实页面对象",
() => {
expect(document).toBe(unsafeWindow.document);
expect(location.href).toBe(unsafeWindow.location.href);
expect(document.documentElement).toBe(unsafeWindow.document.documentElement);
},
null,
null,
null
);
check(
"自动断言",
"页面全局变量不会自动穿透到沙盒 window",
() =>
withCleanup(
() => {
const key = `${markerPrefix}_page_global`;
unsafeWindow[key] = "page-value";
expect(unsafeWindow[key]).toBe("page-value");
expect(window[key]).toBe(undefined);
window[key] = "sandbox-value";
expect(window[key]).toBe("sandbox-value");
expect(unsafeWindow[key]).toBe("page-value");
},
() => {
delete window[`${markerPrefix}_page_global`];
delete unsafeWindow[`${markerPrefix}_page_global`];
}
),
null,
null,
null
);
check(
"自动断言",
"页面 DOM named property 不应穿透为沙盒全局变量 (Issue #273, #700)",
() =>
withCleanup(
() => {
const id = `${markerPrefix}_named_element`;
const div = document.createElement("div");
div.id = id;
document.body.appendChild(div);
expect(unsafeWindow[id]).toBe(div);
expect(window[id]).toBe(undefined);
},
() => {
document.getElementById(`${markerPrefix}_named_element`)?.remove();
}
),
null,
null,
null
);
check(
"自动断言",
"删除沙盒全局变量不应删除页面同名全局变量 (Issue #522)",
() =>
withCleanup(
() => {
const key = `${markerPrefix}_delete_page_global`;
unsafeWindow[key] = "page-value";
expect(window[key]).toBe(undefined);
window[key] = "sandbox-value";
expect(window[key]).toBe("sandbox-value");
expect(unsafeWindow[key]).toBe("page-value");
delete window[key];
expect(window[key]).toBe(undefined);
expect(unsafeWindow[key]).toBe("page-value");
},
() => {
window[`${markerPrefix}_delete_page_global`] = undefined;
delete window[`${markerPrefix}_delete_page_global`];
delete unsafeWindow[`${markerPrefix}_delete_page_global`];
}
),
null,
null,
null
);
check(
"自动断言",
"裸 delete 页面全局变量不应删除沙盒同名全局变量",
() =>
withCleanup(
() => {
const key = `${markerPrefix}_delete_bare_page_global`;
unsafeWindow[key] = "page-value";
window[key] = "sandbox-value";
expect(window[key]).toBe("sandbox-value");
expect(unsafeWindow[key]).toBe("page-value");
try {
Function(`return delete ${key};`)(); // 半沙盒在页面执行
} catch (e) {
console.error(e);
delete unsafeWindow[key]; // fallback
}
expect(unsafeWindow[key]).toBe(undefined);
expect(window[key]).toBe("sandbox-value");
},
() => {
window[`${markerPrefix}_delete_bare_page_global`] = undefined;
delete window[`${markerPrefix}_delete_bare_page_global`];
delete unsafeWindow[`${markerPrefix}_delete_bare_page_global`];
}
),
null,
null,
null
);
check(
"自动断言",
"Object.prototype 污染不会穿透到沙盒 window",
() =>
withCleanup(
() => {
const key = `${markerPrefix}_polluted`;
Object.prototype[key] = "polluted-value";
expect({}[key]).toBe("polluted-value");
expect(window[key]).toBe(undefined);
expect(key in window).toBe(false);
},
() => {
delete Object.prototype[`${markerPrefix}_polluted`];
}
),
null,
null,
null
);
check(
"自动断言",
"特殊关键字与内部字段不从页面或 GM context 泄漏",
() =>
withCleanup(
() => {
for (const key of ["define", "module", "exports"]) {
const desc = Object.getOwnPropertyDescriptor(unsafeWindow, key);
if (!desc || desc.writable || desc.set) {
unsafeWindow[key] = `page-${key}`;
}
}
expect(window.define).toBe(undefined);
expect(window.module).toBe(undefined);
expect(window.exports).toBe(undefined);
for (const key of [
"runFlag",
"prefix",
"message",
"contentMsg",
"scriptRes",
"valueChangeListener",
"EE",
"context",
"grantSet",
"eventId",
"loadScriptResolve",
"loadScriptPromise",
"setInvalidContext",
"isInvalidContext",
]) {
expect(window[key]).toBe(undefined);
}
},
(() => {
const snapshots = snapshotPageProps(["define", "module", "exports"]);
return () => restorePageProps(snapshots);
})()
),
null,
null,
null
);
check(
"自动断言",
"console 与页面 console 隔离,但方法可用",
() => {
assertNotSame(unsafeWindow.console, console, "沙盒 console 应与页面 console 不是同一个对象");
expect(typeof console.log).toBe("function");
expect(typeof console.error).toBe("function");
},
null,
null,
null
);
});
describe("原生函数与事件代理", () => {
check(
"自动断言",
"裸调用原生函数已绑定真实页面 window,避免 Illegal invocation (Issue #189)",
async () => {
const rawSetTimeout = setTimeout;
const rawSetInterval = setInterval;
const rawClearInterval = clearInterval;
let called = false;
await new Promise((resolve) => {
rawSetTimeout(() => {
called = true;
resolve();
}, 0);
});
expect(called).toBe(true);
let intervalCount = 0;
await new Promise((resolve) => {
const timer = rawSetInterval(() => {
intervalCount++;
rawClearInterval(timer);
resolve();
}, 0);
});
expect(intervalCount).toBe(1);
const rawAddEventListener = addEventListener;
const rawRemoveEventListener = removeEventListener;
const eventName = `${markerPrefix}_bare_listener`;
let count = 0;
const handler = () => {
count++;
};
rawAddEventListener(eventName, handler);
unsafeWindow.dispatchEvent(new Event(eventName));
rawRemoveEventListener(eventName, handler);
expect(count).toBe(1);
if (typeof fetch === "function") {
const rawFetch = fetch;
expect(typeof rawFetch).toBe("function");
}
},
null,
null,
null
);
check(
"自动断言",
"取出 window.addEventListener 后调用不会 Illegal invocation (Issue #773)",
() =>
withCleanup(
() => {
const rawAddEventListener = window.addEventListener;
const rawRemoveEventListener = window.removeEventListener;
const eventName = `${markerPrefix}_window_listener`;
let count = 0;
const handler = () => {
count++;
};
rawAddEventListener(eventName, handler);
unsafeWindow.dispatchEvent(new Event(eventName));
rawRemoveEventListener(eventName, handler);
expect(count).toBe(1);
},
() => {}
),
null,
null,
null
);
check(
"自动断言",
"被 Proxy 包装的原生函数仍可安全裸调用 (Issue #1030)",
async () => {
const proxiedSetTimeout = new Proxy(setTimeout, {});
let called = false;
await new Promise((resolve) => {
proxiedSetTimeout(() => {
called = true;
resolve();
}, 0);
});
expect(called).toBe(true);
},
null,
null,
null
);
check(
"自动断言",
"getter 返回页面 window 时会替换为沙盒 window (Issue #1427)",
() => {
expect(self).toBe(window);
expect(parent).toBe(window);
expect(top).toBe(window);
expect(frames).toBe(window);
},
null,
null,
null
);
check(
"自动断言",
"onxxx 函数赋值由页面事件触发,event.target 为 unsafeWindow",
() =>
withCleanup(
() => {
let count = 0;
let thisIsNotWindow = false;
let eventTargetIsUnsafeWindow = false;
const eventName = `${markerPrefix}_onresize_probe`;
window.onresize = function (event) {
count++;
thisIsNotWindow = this !== unsafeWindow;
eventTargetIsUnsafeWindow = event.target === unsafeWindow;
expect(event.type).toBe("resize");
};
unsafeWindow.dispatchEvent(new Event("resize"));
expect(count).toBe(1);
expect(thisIsNotWindow).toBe(true);
expect(eventTargetIsUnsafeWindow).toBe(true);
window.onresize = null;
unsafeWindow.dispatchEvent(new Event("resize"));
unsafeWindow.dispatchEvent(new Event(eventName));
expect(count).toBe(1);
},
() => {
window.onresize = null;
}
),
null,
null,
null
);
check(
"自动断言",
"onxxx 普通对象只保存不注册监听,primitive 值应移除已注册的监听",
() =>
withCleanup(
async () => {
let handled = false;
const listenerObject = {
handleEvent() {
handled = true;
},
};
window.onfocus = listenerObject;
expect(window.onfocus).toBe(listenerObject);
unsafeWindow.dispatchEvent(new Event("focus"));
await waitForEventLoop();
expect(handled).toBe(false);
handled = false;
const func = function () {
handled = true;
};
window.onfocus = func;
expect(window.onfocus).toBe(func);
unsafeWindow.dispatchEvent(new Event("focus"));
await waitForEventLoop();
expect(handled).toBe(true);
handled = false;
window.onfocus = 123;
assertNotSame(func, window.onfocus, "primitive 对象时注册能被移除 (1)");
unsafeWindow.dispatchEvent(new Event("focus"));
await waitForEventLoop();
expect(handled).toBe(false);
},
() => {
window.onfocus = null;
window.onblur = null;
}
),
null,
null,
null
);
check(
"自动断言",
"onxxx 函数替换后只调用最新函数",
() =>
withCleanup(
() => {
let oldCount = 0;
let newCount = 0;
window.onhashchange = function () {
oldCount++;
};
window.onhashchange = function () {
newCount++;
};
unsafeWindow.dispatchEvent(new Event("hashchange"));
expect(oldCount).toBe(0);
expect(newCount).toBe(1);
},
() => {
window.onhashchange = null;
}
),
null,
null,
null
);
// 测试对象仅限于 window 和 top
check(
"自动断言",
"window/top 不能被脚本改写",
() => {
assertThrowsOrKeepsValue(
() => {
window.window = "bad";
},
() => window.window,
window,
"window 自引用应保持不变"
);
assertThrowsOrKeepsValue(
() => {
window.top = "bad";
},
() => window.top,
window,
"top 自引用应保持不变"
);
},
null,
null,
null
);
check(
"自动断言",
"TM半沙盒:把祖先类别继承直接写在半沙盒上 (Issue #1462 PR #1463)",
async () => {
const trueWindow = unsafeWindow;
const sandboxWindow = window;
expect(Object.hasOwn(trueWindow, "addEventListener")).toBe(false);
expect(Reflect.has(trueWindow, "addEventListener")).toBe(true);
expect(Object.hasOwn(sandboxWindow, "addEventListener")).toBe(true);
expect(Reflect.has(sandboxWindow, "addEventListener")).toBe(true);
},
null,
null,
null
);
});
describe("GM API 注入与命名空间", () => {
check(
"自动断言",
"GM_info、GM.info 与 unsafeWindow 正确暴露",
() => {
expect(typeof GM_info).toBe("object");
expect(typeof GM.info).toBe("object");
expect(JSON.stringify(GM.info)).toBe(JSON.stringify(GM_info));
expect(window.unsafeWindow).toBe(unsafeWindow);
expect(typeof GM_info.script).toBe("object");
},
null,
null,
null
);
check(
"自动断言",
"GM_ 与 GM.* 双命名空间由 grant 自动补齐",
() => {
expect(typeof GM_getValue).toBe("function");
expect(typeof GM.getValue).toBe("function");
expect(typeof GM_setValue).toBe("function");
expect(typeof GM.setValue).toBe("function");
expect(typeof GM_deleteValue).toBe("function");
expect(typeof GM.deleteValue).toBe("function");
expect(typeof GM_listValues).toBe("function");
expect(typeof GM.listValues).toBe("function");
},
null,
null,
null
);
check(
"自动断言",
"GM_setValue/GM_getValue/GM_deleteValue 同步路径正常",
() =>
withCleanup(
() => {
const key = `${markerPrefix}_value`;
GM_setValue(key, { env: "sandbox", ok: true });
const stored = GM_getValue(key);
expect(stored.env).toBe("sandbox");
expect(stored.ok).toBe(true);
expect(GM_listValues().includes(key)).toBeTruthy();
GM_deleteValue(key);
expect(GM_getValue(key, "fallback")).toBe("fallback");
},
() => {
GM_deleteValue(`${markerPrefix}_value`);
}
),
null,
null,
null
);
check(
"自动断言",
"GM.setValue/GM.getValue/GM.deleteValue Promise 路径正常",
async () =>
withCleanup(
async () => {
const key = `${markerPrefix}_async_value`;
await withTimeout(GM.setValue(key, "async-value"), "GM.setValue");
expect(await withTimeout(GM.getValue(key), "GM.getValue")).toBe("async-value");
expect((await withTimeout(GM.listValues(), "GM.listValues")).includes(key)).toBeTruthy();
await withTimeout(GM.deleteValue(key), "GM.deleteValue");
expect(await withTimeout(GM.getValue(key, "fallback"), "GM.getValue fallback")).toBe("fallback");
},
() => {
GM_deleteValue(`${markerPrefix}_async_value`);
}
),
null,
null,
null
);
check(
"自动断言",
"GM_setValues/GM_getValues 以及 GM.getValues 依赖注入正常",
() =>
withCleanup(
async () => {
const keyA = `${markerPrefix}_multi_a`;
const keyB = `${markerPrefix}_multi_b`;
const keyMissing = `${markerPrefix}_multi_missing`;
GM_setValues({ [keyA]: "A", [keyB]: { deep: 1 } });
const picked = GM_getValues([keyA, keyB, keyMissing]);
expect(picked[keyA]).toBe("A");
expect(picked[keyB].deep).toBe(1);
expect(Object.prototype.hasOwnProperty.call(picked, keyMissing)).toBe(false);
const defaults = GM_getValues({
[keyA]: "default-a",
[keyMissing]: "default-missing",
});
expect(defaults[keyA]).toBe("A");
expect(defaults[keyMissing]).toBe("default-missing");
const asyncPicked = await withTimeout(GM.getValues({ [keyB]: null }), "GM.getValues");
expect(asyncPicked[keyB].deep).toBe(1);
},
() => {
GM_deleteValue(`${markerPrefix}_multi_a`);
GM_deleteValue(`${markerPrefix}_multi_b`);
}
),
null,
null,
null
);
check(
"自动断言",
"GM_cookie grant 构建函数对象与多级命名空间",
() => {
expect(typeof GM_cookie).toBe("function");
expect(typeof GM_cookie.set).toBe("function");
expect(typeof GM_cookie.list).toBe("function");
expect(typeof GM_cookie.delete).toBe("function");
expect(typeof GM.cookie.set).toBe("function");
expect(typeof GM.cookie.list).toBe("function");
expect(typeof GM.cookie.delete).toBe("function");
},
null,
null,
null
);
check(
"自动断言",
"GM_addStyle 与 GM.addStyle 都插入页面 document",
async () =>
withCleanup(
async () => {
const className = `${markerPrefix}_style`;
const style = GM_addStyle(`.${className} { color: rgb(1, 2, 3) !important; }`);
expect(style.tagName).toBe("STYLE");
expect(style.ownerDocument).toBe(document);
const asyncStyle = await withTimeout(
GM.addStyle(`.${className}_async { color: rgb(4, 5, 6) !important; }`),
"GM.addStyle"
);
expect(asyncStyle.tagName).toBe("STYLE");
expect(asyncStyle.ownerDocument).toBe(document);
style.dataset.scriptcatSandboxTest = "sync";
asyncStyle.dataset.scriptcatSandboxTest = "async";
},
() => {
document.querySelectorAll("style[data-scriptcat-sandbox-test]").forEach((node) => node.remove());
}
),
null,
null,
null
);
check(
"自动断言",
"GM_addElement 支持默认 parent、显式 parent、非字符串 property",
async () =>
withCleanup(
async () => {
const key = `${markerPrefix}_gm_script`;
const div = GM_addElement("div", {
id: `${markerPrefix}_div`,
textContent: "ScriptCat sandbox test",
hidden: true,
});
expect(div.tagName).toBe("DIV");
expect(div.ownerDocument).toBe(document);
expect(div.hidden).toBe(true);
const child = await withTimeout(
GM.addElement(div, "span", {
textContent: "child",
}),
"GM.addElement"
);
expect(child.tagName).toBe("SPAN");
expect(child.parentNode).toBe(div);
const script = GM_addElement("script", {
textContent: `window["${key}"] = "from-gm-add-element";`,
});
expect(unsafeWindow[key]).toBe("from-gm-add-element");
expect(window[key]).toBe(undefined);
script.remove();
},
() => {
document.getElementById(`${markerPrefix}_div`)?.remove();
delete unsafeWindow[`${markerPrefix}_gm_script`];
}
),
null,
null,
null
);
check(
"自动断言",
"window.close/window.focus grant 暴露为沙盒 window 方法",
() => {
expect(typeof window.close).toBe("function");
expect(typeof window.focus).toBe("function");
assertNotSame(unsafeWindow.close, window.close, "沙盒 close 应不是页面原始 close");
assertNotSame(unsafeWindow.focus, window.focus, "沙盒 focus 应不是页面原始 focus");
},
null,
null,
null
);
});
describe("兼容行为", () => {
check(
"自动断言",
"Object 静态方法与 RegExp 静态状态保持可用",
() => {
expect(Object.isFrozen(Object.freeze({}))).toBe(true);
const match = "abc123".match(/(\d+)/);
expect(match && match[1]).toBe("123");
expect(RegExp.$1).toBe("123");
},
null,
null,
null
);
check(
"自动断言",
"Symbol 属性只写入当前沙盒,不影响页面 window",
() =>
withCleanup(
() => {
const symbolKey = Symbol(`${markerPrefix}_symbol`);
window[symbolKey] = "sandbox-symbol";
expect(window[symbolKey]).toBe("sandbox-symbol");
expect(unsafeWindow[symbolKey]).toBe(undefined);
},
() => {}
),
null,
null,
null
);
if (location.origin.includes("content-security-policy")) {
// CSP 不测试 eval
} else {
// eval 不一定能通过
// 这跟沙盒无关。不应进行此测试
check(
"自动断言",
"eval 保持可用,并在当前沙盒内解析全局",
() => {
const key = `${markerPrefix}_eval`;
eval(`window["${key}"] = "from-eval";`);
expect(window[key]).toBe("from-eval");
expect(unsafeWindow[key]).toBe(undefined);
delete window[key];
},
null,
null,
null
);
}
});
await run();
})();