@@ -8,6 +8,8 @@ import { isEarlyStartScript } from "./utils";
88import { ListenerManager } from "./listener_manager" ;
99import { createGMBase } from "./gm_api/gm_api" ;
1010
11+ // 不要使用 {}, 改使用 Object.create(null) - 避免在页面生成沙盒时,受到 Object.prototype 被注入的影响
12+
1113// 构建沙盒上下文
1214export 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
0 commit comments