make scramitize checks as strict as wrapfn

This commit is contained in:
Percs 2025-01-07 13:36:01 -06:00
parent 4b955d3dcb
commit 2978b4a12e
6 changed files with 244 additions and 233 deletions

View file

@ -66,21 +66,16 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
});
self.$scramitize = function (v) {
if (typeof v === "string" && v.includes("scramjet")) {
debugger;
if (v === self) debugger;
if (v === location) debugger;
if (iswindow) {
if (v === self.parent) debugger;
if (v === self.document) debugger;
if (v === self.top) debugger;
}
if (typeof v === "string" && v.includes(location.origin)) {
debugger;
}
if (v === self && v?.$scramjet) {
debugger;
}
if (iswindow && v instanceof Document && v.defaultView?.$scramjet) {
debugger;
}
if (typeof v === "string" && v.includes("scramjet")) debugger;
if (typeof v === "string" && v.includes(location.origin)) debugger;
return v;
};

View file

@ -1,5 +1,3 @@
// TODO this whole file should be inlined and deleted it's a weird relic from ssd era
import { URLMeta, rewriteUrl } from "./url";
import { BareHeaders } from "@mercuryworkshop/bare-mux";
const cspHeaders = [

View file

@ -21,26 +21,26 @@ const decoder = new TextDecoder();
function rewriteJsWrapper(
input: string | ArrayBuffer,
url: string | null,
source: string | null,
meta: URLMeta
): string | ArrayBuffer {
let out: RewriterOutput;
const before = performance.now();
try {
if (typeof input === "string") {
out = rewrite_js(input, meta.base.href, url || "(unknown)", $scramjet);
out = rewrite_js(input, meta.base.href, source || "(unknown)", $scramjet);
} else {
out = rewrite_js_from_arraybuffer(
new Uint8Array(input),
meta.base.href,
url || "(unknown)",
source || "(unknown)",
$scramjet
);
}
} catch (err) {
let err1 = err as Error;
console.error("failed rewriting js for", url, err1, input);
err1.message = `failed rewriting js for "${url}": ${err1.message}`;
const err1 = err as Error;
console.error("failed rewriting js for", source, err1, input);
err1.message = `failed rewriting js for "${source}": ${err1.message}`;
throw err1;
}
const after = performance.now();
@ -63,7 +63,7 @@ function rewriteJsWrapper(
}
const overhead = (after - before - Number(duration)).toFixed(2);
console.log(
`oxc rewrite for "${url || "(unknown)"}" was ${timespan} (${duration}ms; ${overhead}ms overhead)`
`oxc rewrite for "${source || "(unknown)"}" was ${timespan} (${duration}ms; ${overhead}ms overhead)`
);
}
@ -78,14 +78,10 @@ export function rewriteJs(
if (flagEnabled("naiiveRewriter", meta.origin)) {
const text = typeof js === "string" ? js : new TextDecoder().decode(js);
console.log("naiive");
return rewriteJsNaiive(text);
}
js = rewriteJsWrapper(js, url, meta);
return js;
return rewriteJsWrapper(js, url, meta);
}
// 1. does not work with modules

View file

@ -27,9 +27,7 @@ export function unrewriteBlob(url: string) {
}
export function rewriteUrl(url: string | URL, meta: URLMeta) {
if (url instanceof URL) {
url = url.toString();
}
if (url instanceof URL) url = url.toString();
if (url.startsWith("javascript:")) {
return (
@ -58,9 +56,7 @@ export function rewriteUrl(url: string | URL, meta: URLMeta) {
}
export function unrewriteUrl(url: string | URL) {
if (url instanceof URL) {
url = url.toString();
}
if (url instanceof URL) url = url.toString();
const prefixed = location.origin + $scramjet.config.prefix;