proxy Object.getOwnPropertyDescriptor

This commit is contained in:
velzie 2024-08-29 15:39:49 -04:00
parent ac5b03736a
commit 52a5e49150
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
7 changed files with 45 additions and 9 deletions

View file

@ -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<T> = {
this: null,

View file

@ -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;

3
src/client/natives.ts Normal file
View file

@ -0,0 +1,3 @@
export const nativeFunction = self.Function;
export const nativeGetOwnPropertyDescriptor =
self.Object.getOwnPropertyDescriptor;

View file

@ -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;

View file

@ -19,8 +19,8 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
return this;
},
writable: false,
configurable: false,
writable: true,
configurable: true,
enumerable: false,
});
}

View file

@ -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) {

View file

@ -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,