Skip to content

Commit 9407557

Browse files
authored
🐛 create_context 的空物件改为 Object.create(null) (#1397)
* fix: create_context 的空物件改为 Object.create(null) * 修正 GM 及 window 的物件生成 * 修正 GM 及 window 的物件生成
1 parent 385aedc commit 9407557

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

src/app/service/content/create_context.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { isEarlyStartScript } from "./utils";
88
import { ListenerManager } from "./listener_manager";
99
import { createGMBase } from "./gm_api/gm_api";
1010

11+
// 不要使用 {}, 改使用 Object.create(null) - 避免在页面生成沙盒时,受到 Object.prototype 被注入的影响
12+
1113
// 构建沙盒上下文
1214
export const createContext = (
1315
scriptRes: TScriptInfo,
@@ -29,6 +31,8 @@ export const createContext = (
2931
});
3032
}
3133
let invalid = false;
34+
const GM = Object.create(null);
35+
GM.info = GMInfo;
3236
const context = createGMBase({
3337
prefix: envPrefix,
3438
message,
@@ -38,11 +42,9 @@ export const createContext = (
3842
EE,
3943
runFlag: uuidv4(),
4044
eventId: 10000,
41-
GM: { info: GMInfo },
45+
GM: GM,
4246
GM_info: GMInfo,
43-
window: {
44-
// onurlchange: null,
45-
},
47+
window: Object.create(null),
4648
grantSet: new Set(),
4749
loadScriptPromise,
4850
loadScriptResolve,
@@ -62,7 +64,7 @@ export const createContext = (
6264
return invalid;
6365
},
6466
});
65-
const grantedAPIs: { [key: string]: any } = {};
67+
const grantedAPIs: { [key: string]: any } = Object.create(null);
6668
const __methodInject__ = (grant: string): boolean => {
6769
const grantSet: Set<string> = context.grantSet;
6870
const s = GMContextApiGet(grant);
@@ -98,7 +100,7 @@ export const createContext = (
98100
for (let i = 0; i < m; i++) {
99101
const part = fnKeyArray[i];
100102
s += `${i ? "." : ""}${part}`;
101-
g = g[part] || (g[part] = grantedAPIs[s] || {});
103+
g = g[part] || (g[part] = grantedAPIs[s] || Object.create(null));
102104
}
103105
}
104106
context.unsafeWindow = window;
@@ -166,10 +168,10 @@ const initOwnDescs = Object.getOwnPropertyDescriptors(global);
166168

167169
// overridedDescs将以物件OwnPropertyDescriptor方式进行物件属性修改
168170
// 覆盖原有的 OwnPropertyDescriptor定义 或 父类的PropertyDescriptor定义
169-
const overridedDescs: Record<string, PropertyDescriptor> = {};
171+
const overridedDescs: Record<string, PropertyDescriptor> = Object.create(null);
170172

171173
// 记录原生 onxxxxx 的 PropertyDescriptor
172-
const eventDescs: Record<string, PropertyDescriptor> = {};
174+
const eventDescs: Record<string, PropertyDescriptor> = Object.create(null);
173175

174176
// 包含物件本身及所有父类(不包含Object)的PropertyDescriptor
175177
// 主要是找出哪些 function值, setter/getter 需要替换 global window

src/app/service/content/exec_script.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export default class ExecScript {
5353
// 不注入任何GM api
5454
// ScriptCat行为:GM.info 和 GM_info 同时注入
5555
// 在不改变 Context 的情况下,以 named 传入多个全域变量
56-
this.named = { GM: { info: GM_info }, GM_info };
56+
const GM = Object.create(null);
57+
GM.info = GM_info;
58+
this.named = { GM, GM_info };
5759
} else {
5860
// 构建脚本GM上下文
5961
this.sandboxContext = createContext(scriptRes, GM_info, envPrefix, message, contentMsg, grantSet);

0 commit comments

Comments
 (0)