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,6 +18,8 @@ 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,
@ -76,7 +78,7 @@ 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})`;
} }
} }
@ -85,7 +87,8 @@ export function rewriteJs(js: string, origin?: URL) {
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) {

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>