From 52a5e49150faa3bb8866a06c6e962dcb364d6f8e Mon Sep 17 00:00:00 2001 From: velzie Date: Thu, 29 Aug 2024 15:39:49 -0400 Subject: [PATCH] proxy Object.getOwnPropertyDescriptor --- src/client/client.ts | 5 +++-- src/client/dom/element.ts | 4 +++- src/client/natives.ts | 3 +++ src/client/shared/event.ts | 3 ++- src/client/shared/realm.ts | 4 ++-- src/client/shared/unproxy.ts | 29 +++++++++++++++++++++++++++++ src/worker/fetch.ts | 6 +++--- 7 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 src/client/natives.ts diff --git a/src/client/client.ts b/src/client/client.ts index 07ccbaa..84e6b63 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -2,6 +2,7 @@ import { createDocumentProxy } from "./document"; import { createGlobalProxy } from "./global"; import { getOwnPropertyDescriptorHandler } from "./helpers"; import { createLocationProxy } from "./location"; +import { nativeGetOwnPropertyDescriptor } from "./natives"; import { CookieStore, config, decodeUrl } from "./shared"; declare global { @@ -236,7 +237,7 @@ export class ScramjetClient { const prop = split.pop(); const target = split.reduce((a, b) => a?.[b], this.global); - const original = Object.getOwnPropertyDescriptor(target, prop); + const original = nativeGetOwnPropertyDescriptor(target, prop); this.descriptors[name] = original; return this.RawTrap(target, prop, descriptor); @@ -250,7 +251,7 @@ export class ScramjetClient { if (!prop) return; if (!Reflect.has(target, prop)) return; - const oldDescriptor = Object.getOwnPropertyDescriptor(target, prop); + const oldDescriptor = nativeGetOwnPropertyDescriptor(target, prop); const ctx: TrapCtx = { this: null, diff --git a/src/client/dom/element.ts b/src/client/dom/element.ts index d0ce2f8..c7bd02e 100644 --- a/src/client/dom/element.ts +++ b/src/client/dom/element.ts @@ -1,4 +1,5 @@ import { ScramjetClient } from "../client"; +import { nativeGetOwnPropertyDescriptor } from "../natives"; import { config, decodeUrl, htmlRules, unrewriteHtml } from "../shared"; import { encodeUrl, @@ -33,7 +34,7 @@ export default function (client: ScramjetClient, self: typeof window) { for (const attr of attrs) { for (const element of attrObject[attr]) { - const descriptor = Object.getOwnPropertyDescriptor( + const descriptor = nativeGetOwnPropertyDescriptor( element.prototype, attr ); @@ -167,6 +168,7 @@ export default function (client: ScramjetClient, self: typeof window) { client.Trap("Node.prototype.ownerDocument", { get(ctx) { + return client.documentProxy; let doc = ctx.get() as Document | null; if (!doc) return null; diff --git a/src/client/natives.ts b/src/client/natives.ts new file mode 100644 index 0000000..39e75d5 --- /dev/null +++ b/src/client/natives.ts @@ -0,0 +1,3 @@ +export const nativeFunction = self.Function; +export const nativeGetOwnPropertyDescriptor = + self.Object.getOwnPropertyDescriptor; diff --git a/src/client/shared/event.ts b/src/client/shared/event.ts index fa12395..e2d2114 100644 --- a/src/client/shared/event.ts +++ b/src/client/shared/event.ts @@ -1,6 +1,7 @@ import { iswindow } from ".."; import { ScramjetClient } from "../client"; import { getOwnPropertyDescriptorHandler } from "../helpers"; +import { nativeGetOwnPropertyDescriptor } from "../natives"; import { unproxy } from "./unproxy"; const realOnEvent = Symbol.for("scramjet original onevent function"); @@ -138,7 +139,7 @@ export default function (client: ScramjetClient, self: Self) { key.startsWith("on") && handlers[key.slice(2)] ) { - const descriptor = Object.getOwnPropertyDescriptor(target, key); + const descriptor = nativeGetOwnPropertyDescriptor(target, key); if (!descriptor.get || !descriptor.set || !descriptor.configurable) continue; diff --git a/src/client/shared/realm.ts b/src/client/shared/realm.ts index b6300e1..35564ee 100644 --- a/src/client/shared/realm.ts +++ b/src/client/shared/realm.ts @@ -19,8 +19,8 @@ export default function (client: ScramjetClient, self: typeof globalThis) { return this; }, - writable: false, - configurable: false, + writable: true, + configurable: true, enumerable: false, }); } diff --git a/src/client/shared/unproxy.ts b/src/client/shared/unproxy.ts index 544aef6..c7c01a7 100644 --- a/src/client/shared/unproxy.ts +++ b/src/client/shared/unproxy.ts @@ -41,6 +41,35 @@ export default function (client: ScramjetClient, self: typeof window) { } catch (e) {} } } + + client.Proxy("Object.getOwnPropertyDescriptor", { + apply(ctx) { + const desc = ctx.fn.apply(ctx.this, ctx.args); + + if (!desc) return; + + if (desc.get) { + client.RawProxy(desc, "get", { + apply(ctx) { + // value of this in the getter needs to be corrected + unproxy(ctx, client); + }, + }); + } + + if (desc.set) { + client.RawProxy(desc, "set", { + apply(ctx) { + unproxy(ctx, client); + }, + }); + } + + // i don't think we have to care about value but it's worth looking into + + ctx.return(desc); + }, + }); } export function unproxy(ctx: ProxyCtx, client: ScramjetClient) { diff --git a/src/worker/fetch.ts b/src/worker/fetch.ts index 8a0816b..c3937a4 100644 --- a/src/worker/fetch.ts +++ b/src/worker/fetch.ts @@ -95,9 +95,9 @@ export async function swfetch( } // TODO this is wrong somehow - // headers.set("Sec-Fetch-Mode", "cors"); - // headers.set("Sec-Fetch-Site", "same-origin"); - // headers.set("Sec-Fetch-Dest", "empty"); + headers.set("Sec-Fetch-Mode", "cors"); + headers.set("Sec-Fetch-Site", "same-origin"); + headers.set("Sec-Fetch-Dest", "empty"); const response: BareResponseFetch = await this.client.fetch(url, { method: request.method,