refactor globalProxy

This commit is contained in:
velzie 2024-10-12 22:45:12 -04:00
parent add57d720b
commit 5e4f498dfe
No known key found for this signature in database
GPG key ID: AA51AEFB0A1F3820
2 changed files with 18 additions and 20 deletions

View file

@ -4,34 +4,28 @@ import { indirectEval } from "./shared/eval";
// import { config } from "../shared"; // import { config } from "../shared";
import { getOwnPropertyDescriptorHandler } from "./helpers"; import { getOwnPropertyDescriptorHandler } from "./helpers";
export const UNSAFE_GLOBALS = [
"window",
"top",
"self",
"globalThis",
"parent",
"document",
"frames",
"eval",
];
export function createGlobalProxy( export function createGlobalProxy(
client: ScramjetClient, client: ScramjetClient,
self: typeof globalThis self: typeof globalThis
): typeof globalThis { ): typeof globalThis {
return new Proxy(self, { return new Proxy(self, {
get(target, prop) { get(target, prop) {
if (prop === "location") return client.locationProxy;
if (
typeof prop === "string" &&
[
"window",
"top",
"self",
"globalThis",
"parent",
"document",
"frames",
].includes(prop)
)
return client.wrapfn(self[prop]);
if (prop === "$scramjet") return;
if (prop === "eval") return indirectEval.bind(client);
const value = Reflect.get(target, prop); const value = Reflect.get(target, prop);
if (typeof prop === "string" && UNSAFE_GLOBALS.includes(prop))
return client.wrapfn(value);
return value; return value;
}, },

View file

@ -3,6 +3,7 @@ import { SCRAMJETCLIENT } from "../../symbols";
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { config } from "../../shared"; import { config } from "../../shared";
import { argdbg } from "./err"; import { argdbg } from "./err";
import { indirectEval } from "./eval";
export function createWrapFn(client: ScramjetClient, self: typeof globalThis) { export function createWrapFn(client: ScramjetClient, self: typeof globalThis) {
return function (identifier: any, args: any) { return function (identifier: any, args: any) {
@ -46,6 +47,9 @@ export function createWrapFn(client: ScramjetClient, self: typeof globalThis) {
} else if (isworker && identifier instanceof self.WorkerGlobalScope) { } else if (isworker && identifier instanceof self.WorkerGlobalScope) {
return client.globalProxy; return client.globalProxy;
} }
if (identifier == self.eval) {
return indirectEval.bind(client);
}
return identifier; return identifier;
}; };