fix meriyah crash

This commit is contained in:
velzie 2024-07-14 16:30:56 -04:00
parent 37b268bfeb
commit c703ed39ca
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
4 changed files with 31 additions and 17 deletions

View file

@ -1,10 +1,13 @@
import { encodeUrl } from "../shared/rewriters/url"; import { encodeUrl } from "../shared/rewriters/url";
export const URL = globalThis.URL;
URL = new Proxy(URL, { if (globalThis.window) {
construct(target, argArray, newTarget) { window.URL = new Proxy(URL, {
if (typeof argArray[0] === "string") argArray[0] = encodeUrl(argArray[0]); construct(target, argArray, newTarget) {
if (typeof argArray[1] === "string") argArray[1] = encodeUrl(argArray[1]); if (typeof argArray[0] === "string") argArray[0] = encodeUrl(argArray[0]);
if (typeof argArray[1] === "string") argArray[1] = encodeUrl(argArray[1]);
return Reflect.construct(target, argArray, newTarget); return Reflect.construct(target, argArray, newTarget);
}, },
}) })
}

View file

@ -18,12 +18,14 @@ import * as ESTree from "estree";
export function rewriteJs(js: string, origin?: URL) { export function rewriteJs(js: string, origin?: URL) {
const htmlcomment = /<!--[\s\S]*?-->/g;
js = js.replace(htmlcomment, "");
try { try {
const ast = parseModule(js, { const ast = parseModule(js, {
module: true, module: true,
webcompat: true webcompat: true
}); });
const identifierList = [ const identifierList = [
"window", "window",
"self", "self",
@ -33,12 +35,12 @@ export function rewriteJs(js: string, origin?: URL) {
"top", "top",
"location" "location"
] ]
const customTraveler = makeTraveler({ const customTraveler = makeTraveler({
ImportDeclaration: (node: ESTree.ImportDeclaration) => { ImportDeclaration: (node: ESTree.ImportDeclaration) => {
node.source.value = encodeUrl(node.source.value as string, origin); node.source.value = encodeUrl(node.source.value as string, origin);
}, },
ImportExpression: (node: ESTree.ImportExpression) => { ImportExpression: (node: ESTree.ImportExpression) => {
if (node.source.type === "Literal") { if (node.source.type === "Literal") {
node.source.value = encodeUrl(node.source.value as string, origin); node.source.value = encodeUrl(node.source.value as string, origin);
@ -49,11 +51,11 @@ export function rewriteJs(js: string, origin?: URL) {
node.source.name = `__wrapImport(${node.source.name})`; node.source.name = `__wrapImport(${node.source.name})`;
} }
}, },
ExportAllDeclaration: (node: ESTree.ExportAllDeclaration) => { ExportAllDeclaration: (node: ESTree.ExportAllDeclaration) => {
node.source.value = encodeUrl(node.source.value as string, origin); node.source.value = encodeUrl(node.source.value as string, origin);
}, },
ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => { ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => {
// strings are Literals in ESTree syntax but these will always be strings // strings are Literals in ESTree syntax but these will always be strings
if (node.source) node.source.value = encodeUrl(node.source.value as string, origin); if (node.source) node.source.value = encodeUrl(node.source.value as string, origin);
@ -76,16 +78,17 @@ export function rewriteJs(js: string, origin?: URL) {
}, },
VariableDeclarator: (node: ESTree.VariableDeclarator) => { VariableDeclarator: (node: ESTree.VariableDeclarator) => {
if (node.init.type === "Identifier" && identifierList.includes(node.init.name)) { if (node.init && node.init.type === "Identifier" && identifierList.includes(node.init.name)) {
node.init.name = `$s(${node.init.name})`; node.init.name = `$s(${node.init.name})`;
} }
} }
}); });
customTraveler.go(ast); customTraveler.go(ast);
return generate(ast); return generate(ast);
} catch { } catch (e) {
console.error(e);
console.log(js); console.log(js);
return js; return js;

View file

@ -1,3 +1,4 @@
import { URL } from "../../client/url";
import { rewriteJs } from "./js"; import { rewriteJs } from "./js";
function canParseUrl(url: string, origin?: URL) { function canParseUrl(url: string, origin?: URL) {
@ -42,4 +43,4 @@ export function decodeUrl(url: string | URL) {
} else { } else {
return url; return url;
} }
} }

7
tests/location.html Normal file
View file

@ -0,0 +1,7 @@
<head></head>
<script>
function f() {
location = "http://www.google.com";
}
</script>
<button onclick='f()'>Google</button>