fix unrewriting stolen functions

This commit is contained in:
Toshit Chawda 2025-03-09 13:26:15 -07:00
parent 3948171dc6
commit ad73d45b45
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
4 changed files with 46 additions and 17 deletions

View file

@ -17,6 +17,7 @@ import type { BareClient as BareClientType } from "@mercuryworkshop/bare-mux";
import { createWrapFn } from "./shared/wrap"; import { createWrapFn } from "./shared/wrap";
import { NavigateEvent } from "./events"; import { NavigateEvent } from "./events";
import type { URLMeta } from "../shared/rewriters/url"; import type { URLMeta } from "../shared/rewriters/url";
import { SourceMaps } from "./shared/sourcemaps";
type NativeStore = { type NativeStore = {
store: Record<string, any>; store: Record<string, any>;
@ -77,6 +78,7 @@ export class ScramjetClient {
natives: NativeStore; natives: NativeStore;
descriptors: DescriptorStore; descriptors: DescriptorStore;
sourcemaps: SourceMaps;
wrapfn: (i: any, ...args: any) => any; wrapfn: (i: any, ...args: any) => any;
cookieStore = new CookieStore(); cookieStore = new CookieStore();
@ -102,17 +104,6 @@ export class ScramjetClient {
throw new Error(); throw new Error();
} }
this.serviceWorker = this.global.navigator.serviceWorker;
if (iswindow) {
this.documentProxy = createDocumentProxy(this, global);
global.document[SCRAMJETCLIENT] = this;
}
this.locationProxy = createLocationProxy(this, global);
this.globalProxy = createGlobalProxy(this, global);
this.wrapfn = createWrapFn(this, global);
if (iswindow) { if (iswindow) {
this.bare = new BareClient(); this.bare = new BareClient();
} else { } else {
@ -130,6 +121,19 @@ export class ScramjetClient {
}) })
); );
} }
this.serviceWorker = this.global.navigator.serviceWorker;
if (iswindow) {
this.documentProxy = createDocumentProxy(this, global);
global.document[SCRAMJETCLIENT] = this;
}
this.locationProxy = createLocationProxy(this, global);
this.globalProxy = createGlobalProxy(this, global);
this.wrapfn = createWrapFn(this, global);
this.sourcemaps = {};
this.natives = { this.natives = {
store: new Proxy( store: new Proxy(
{}, {},

View file

@ -1,4 +1,5 @@
import { flagEnabled } from "../../scramjet"; import { flagEnabled } from "../../scramjet";
import { SCRAMJETCLIENT, SCRAMJETCLIENTNAME } from "../../symbols";
import { ProxyCtx, ScramjetClient } from "../client"; import { ProxyCtx, ScramjetClient } from "../client";
enum RewriteType { enum RewriteType {
@ -20,6 +21,8 @@ type Rewrite = {
} }
); );
export type SourceMaps = Record<string, Rewrite[]>;
function getEnd(rewrite: Rewrite): number { function getEnd(rewrite: Rewrite): number {
if (rewrite.type === RewriteType.Insert) { if (rewrite.type === RewriteType.Insert) {
return rewrite.start + rewrite.size; return rewrite.start + rewrite.size;
@ -31,6 +34,30 @@ function getEnd(rewrite: Rewrite): number {
const scramtag_ident = "/*scramtag "; const scramtag_ident = "/*scramtag ";
// some sites like to steal funcs from frames and then unrewrite them
function searchRewrites(tag: string): Rewrite[] | undefined {
function searchFrame(globalThis: Self) {
const SCRAMJETCLIENT = globalThis.Symbol.for(SCRAMJETCLIENTNAME);
if (globalThis[SCRAMJETCLIENT].sourcemaps[tag])
return globalThis[SCRAMJETCLIENT].sourcemaps[tag];
// no enhanced for :frowning2:
for (let i = 0; i < globalThis.frames.length; i++) {
const rewrites = searchFrame(globalThis.frames[i].self);
if (rewrites) return rewrites;
}
}
let globalThis = self;
let rewrites = searchFrame(globalThis);
if (rewrites) return rewrites;
while (globalThis.parent && globalThis.parent !== globalThis.window) {
globalThis = globalThis.parent.self;
let rewrites = searchFrame(globalThis);
if (rewrites) return rewrites;
}
}
function registerRewrites(buf: Array<number>, tag: string) { function registerRewrites(buf: Array<number>, tag: string) {
const sourcemap = Uint8Array.from(buf); const sourcemap = Uint8Array.from(buf);
const view = new DataView(sourcemap.buffer); const view = new DataView(sourcemap.buffer);
@ -65,7 +92,7 @@ function registerRewrites(buf: Array<number>, tag: string) {
} }
} }
sourcemaps[tag] = rewrites; self[SCRAMJETCLIENT].sourcemaps[tag] = rewrites;
} }
function doUnrewrite(ctx: ProxyCtx) { function doUnrewrite(ctx: ProxyCtx) {
@ -94,7 +121,7 @@ function doUnrewrite(ctx: ProxyCtx) {
const scramtagend = stringified.indexOf("*/", scramtagstart); const scramtagend = stringified.indexOf("*/", scramtagstart);
const tag = stringified.substring(firstspace + 1, scramtagend); const tag = stringified.substring(firstspace + 1, scramtagend);
const rewrites = sourcemaps[tag]; const rewrites = searchRewrites(tag);
if (!rewrites) { if (!rewrites) {
console.warn("failed to get rewrites for tag", tag); console.warn("failed to get rewrites for tag", tag);
@ -137,8 +164,6 @@ function doUnrewrite(ctx: ProxyCtx) {
return ctx.return(newString); return ctx.return(newString);
} }
const sourcemaps: Record<string, Rewrite[]> = {};
export const enabled = (client: ScramjetClient) => export const enabled = (client: ScramjetClient) =>
flagEnabled("sourcemaps", client.url); flagEnabled("sourcemaps", client.url);

View file

@ -57,7 +57,6 @@ function rewriteJsWasm(
} catch (err) { } catch (err) {
const err1 = err as Error; const err1 = err as Error;
console.warn("failed rewriting js for", source, err1, input); console.warn("failed rewriting js for", source, err1, input);
err1.message = `failed rewriting js for "${source}": ${err1.message}`;
return { js: input, tag: "", map: null }; return { js: input, tag: "", map: null };
} }

View file

@ -1,3 +1,4 @@
// see types.d.ts for what these mean // see types.d.ts for what these mean
export const SCRAMJETCLIENT = Symbol.for("scramjet client global"); export const SCRAMJETCLIENTNAME = "scramjet client global";
export const SCRAMJETCLIENT = Symbol.for(SCRAMJETCLIENTNAME);
export const SCRAMJETFRAME = Symbol.for("scramjet frame handle"); export const SCRAMJETFRAME = Symbol.for("scramjet frame handle");