Skip to content

Commit b93be76

Browse files
committed
🐛 修复hasOwnProperty返回undefined的问题 #272
1 parent 328f84f commit b93be76

4 files changed

Lines changed: 52 additions & 15 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scriptcat",
3-
"version": "0.16.2",
3+
"version": "0.16.3",
44
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
55
"author": "CodFrm",
66
"license": "GPLv3",

‎src/manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 2,
33
"name": "ScriptCat",
4-
"version": "0.16.2",
4+
"version": "0.16.3",
55
"author": "CodFrm",
66
"description": "脚本猫,一个用户脚本管理器,支持后台脚本、定时脚本、页面脚本,可编写脚本每天帮你自动处理事务.",
77
"options_ui": {

‎src/runtime/content/utils.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ describe("proxy context", () => {
3838
it("访问global的对象", () => {
3939
expect(_this["gbok"]).toEqual("gbok");
4040
});
41+
42+
it("禁止修改window", () => {
43+
expect(() => (_this["window"] = "ok")).toThrow();
44+
});
4145
});
4246

4347
describe("兼容问题", () => {
@@ -68,3 +72,14 @@ describe("Symbol", () => {
6872
expect(toString.call(_this)).toEqual("[object Window]");
6973
});
7074
});
75+
76+
// Object.hasOwnProperty穿透 https://github.com/scriptscat/scriptcat/issues/272
77+
describe("Object", () => {
78+
const _this = proxyContext({}, {});
79+
it("hasOwnProperty", () => {
80+
expect(_this.hasOwnProperty("test1")).toEqual(false);
81+
_this.test1 = "ok";
82+
expect(_this.hasOwnProperty("test1")).toEqual(true);
83+
expect(_this.hasOwnProperty("test")).toEqual(true);
84+
});
85+
});

‎src/runtime/content/utils.ts‎

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,28 @@ Object.keys(descs).forEach((key) => {
147147
}
148148
});
149149

150+
export function warpObject(thisContext: Object, ...context: Object[]) {
151+
// 处理Object上的方法
152+
thisContext.hasOwnProperty = (name: PropertyKey) => {
153+
return (
154+
Object.hasOwnProperty.call(thisContext, name) ||
155+
context.some((val) => Object.hasOwnProperty.call(val, name))
156+
);
157+
};
158+
thisContext.isPrototypeOf = (name: Object) => {
159+
return (
160+
Object.isPrototypeOf.call(thisContext, name) ||
161+
context.some((val) => Object.isPrototypeOf.call(val, name))
162+
);
163+
};
164+
thisContext.propertyIsEnumerable = (name: PropertyKey) => {
165+
return (
166+
Object.propertyIsEnumerable.call(thisContext, name) ||
167+
context.some((val) => Object.propertyIsEnumerable.call(val, name))
168+
);
169+
};
170+
}
171+
150172
// 拦截上下文
151173
export function proxyContext(
152174
global: any,
@@ -161,6 +183,7 @@ export function proxyContext(
161183
}
162184
thisContext.eval = global.eval;
163185
thisContext.define = undefined;
186+
warpObject(thisContext, special, global, context);
164187
// keyword是与createContext时同步的,避免访问到context的内部变量
165188
const contextKeyword: { [key: string]: any } = {
166189
message: 1,
@@ -203,13 +226,13 @@ export function proxyContext(
203226
return thisContext[name];
204227
}
205228
if (typeof name === "string") {
206-
if (context[name]) {
207-
if (contextKeyword[name]) {
229+
if (has(context, name)) {
230+
if (has(contextKeyword, name)) {
208231
return undefined;
209232
}
210233
return context[name];
211234
}
212-
if (special[name] !== undefined) {
235+
if (has(special, name)) {
213236
if (
214237
typeof special[name] === "function" &&
215238
!(<{ prototype: any }>special[name]).prototype
@@ -250,22 +273,22 @@ export function proxyContext(
250273
}
251274
if (name !== "undefined") {
252275
if (typeof name === "string") {
253-
if (unscopables[name]) {
276+
if (has(unscopables, name)) {
254277
return false;
255278
}
256279
if (has(thisContext, name)) {
257280
return true;
258281
}
259-
if (context[name]) {
260-
if (contextKeyword[name]) {
282+
if (has(context, name)) {
283+
if (has(contextKeyword, name)) {
261284
return false;
262285
}
263286
return true;
264287
}
265-
if (special[name] !== undefined) {
288+
if (has(special, name)) {
266289
return true;
267290
}
268-
if (global[name] !== undefined) {
291+
if (has(global[name], name)) {
269292
return true;
270293
}
271294
} else if (typeof name === "symbol") {
@@ -279,12 +302,11 @@ export function proxyContext(
279302
case "window":
280303
case "self":
281304
case "globalThis":
282-
special.global = val;
283-
return true;
305+
return false;
284306
default:
285307
}
286-
if (special[name]) {
287-
special[name] = val;
308+
if (has(special, name)) {
309+
thisContext[name] = val;
288310
return true;
289311
}
290312
if (init.has(name)) {
@@ -293,7 +315,7 @@ export function proxyContext(
293315
if (des && des.get && !des.set && des.configurable) {
294316
return true;
295317
}
296-
global[name] = val;
318+
thisContext[name] = val;
297319
return true;
298320
}
299321
// @ts-ignore

0 commit comments

Comments
 (0)