various cooking

This commit is contained in:
velzie 2024-07-15 14:38:53 -04:00
parent 3fb846036e
commit 408bf39ffd
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
11 changed files with 199 additions and 132 deletions

1
.gitignore vendored
View file

@ -9,3 +9,4 @@ static/bare-mux-worker.js
meta.json
rewriter/target
rewriter/out
static/wasm.js

View file

@ -62,11 +62,13 @@ impl<'a> Visit<'a> for Rewriter {
if UNSAFE_GLOBALS.contains(&s.name.to_string().as_str()) {
self.jschanges.push(JsChange::GenericChange {
span: s.span,
text: format!("$s({})", s.name),
text: format!("(globalThis.$s({}))", s.name),
});
}
}
_ => {}
_ => {
walk::walk_assignment_expression(self, expr);
}
}
}
fn visit_variable_declarator(&mut self, it: &oxc_ast::ast::VariableDeclarator<'a>) {
@ -75,27 +77,17 @@ impl<'a> Visit<'a> for Rewriter {
if UNSAFE_GLOBALS.contains(&s.name.to_string().as_str()) {
self.jschanges.push(JsChange::GenericChange {
span: s.span,
text: format!("$s({})", s.name),
text: format!("(globalThis.$s({}))", s.name),
});
}
}
_ => {}
_ => {
walk::walk_variable_declarator(self, it);
}
}
}
fn visit_member_expression(&mut self, it: &MemberExpression<'a>) {
self.trace_member(it);
// match it {
// MemberExpression::StaticMemberExpression(s) => {
// dbg!(s);
// if s.property.name.to_string() == "location" {
// self.jschanges.push(JsChange::GenericChange {
// span: s.property.span,
// text: "$s(location)".to_string(),
// });
// }
// }
// _ => {}
// }
}
}
@ -119,14 +111,14 @@ impl Rewriter {
if UNSAFE_GLOBALS.contains(&obj.name.to_string().as_str()) {
self.jschanges.push(JsChange::GenericChange {
span: obj.span,
text: format!("$s({})", obj.name),
text: format!("(globalThis.$s({}))", obj.name),
});
}
}
Expression::ThisExpression(obj) => {
self.jschanges.push(JsChange::GenericChange {
span: obj.span,
text: "$s(this)".to_string(),
text: "(globalThis.$s(this))".to_string(),
});
}
_ => {
@ -140,14 +132,14 @@ impl Rewriter {
if UNSAFE_GLOBALS.contains(&obj.name.to_string().as_str()) {
self.jschanges.push(JsChange::GenericChange {
span: obj.span,
text: format!("$s({})", obj.name),
text: format!("(globalThis.$s({}))", obj.name),
});
}
}
Expression::ThisExpression(obj) => {
self.jschanges.push(JsChange::GenericChange {
span: obj.span,
text: "$s(this)".to_string(),
text: "(globalThis.$s(this))".to_string(),
});
}
_ => {

View file

@ -13,6 +13,7 @@ import "./css.ts";
import "./history.ts";
import "./worker.ts";
import "./beacon.ts"
import "./origin.ts";
declare global {
interface Window {

42
src/client/origin.ts Normal file
View file

@ -0,0 +1,42 @@
import { decodeUrl } from "../shared/rewriters/url";
// const descriptor = Object.getOwnPropertyDescriptor(window, "origin");
delete window.origin;
Object.defineProperty(window, "origin", {
get() {
return new URL(decodeUrl(location.href)).origin;
},
set() {
return false;
},
});
Object.defineProperty(document, "URL", {
get() {
return decodeUrl(location.href);
},
set() {
return false;
}
})
Object.defineProperty(document, "baseURI", {
get() {
return decodeUrl(location.href);
},
set() {
return false;
}
})
Object.defineProperty(document, "domain", {
get() {
return new URL(decodeUrl(location.href)).hostname;
},
set() {
return false;
}
})

View file

@ -1,3 +1,4 @@
import { decodeUrl } from "../../shared/rewriters/url";
import { BareClient } from "../shared";
const client = new BareClient();
@ -9,6 +10,7 @@ WebSocket = new Proxy(WebSocket, {
target,
{
"User-Agent": navigator.userAgent,
"Origin": new URL(decodeUrl(location.href)).origin,
},
ArrayBuffer.prototype
);

View file

@ -1,11 +1,13 @@
import { locationProxy } from "./location";
import { windowProxy } from "./window";
import { documentProxy, windowProxy } from "./window";
function scope(identifier: any) {
if (identifier instanceof Window) {
return windowProxy;
} else if (identifier instanceof Location) {
return locationProxy;
} else if (identifier instanceof Document) {
return documentProxy;
}
return identifier;

View file

@ -24,7 +24,9 @@ export const windowProxy = new Proxy(window, {
const value = Reflect.get(target, prop);
if (typeof value === "function") {
// this is bad! i don't know what the right thing to do is
if (typeof value === "function" && value != Object) {
return value.bind(target);
}
@ -45,3 +47,22 @@ export const windowProxy = new Proxy(window, {
return Reflect.set(target, prop, newValue);
},
});
export const documentProxy = new Proxy(document, {
get(target, prop) {
const propIsString = typeof prop === "string";
if (propIsString && prop === "location") {
return locationProxy;
}
const value = Reflect.get(target, prop);
if (typeof value === "function") {
return value.bind(target);
}
return value;
}
});

View file

@ -1,8 +1,8 @@
// import { parseModule } from "meriyah";
// import { generate } from "astring";
// import { makeTraveler } from "astravel";
// import { encodeUrl } from "./url";
// import * as ESTree from "estree";
import { parseModule } from "meriyah";
import { generate } from "astring";
import { makeTraveler } from "astravel";
import { encodeUrl } from "./url";
import * as ESTree from "estree";
// i am a cat. i like to be petted. i like to be fed. i like to be
@ -16,108 +16,114 @@
// top
// parent
import init, { rewrite_js } from "../../../rewriter/out/rewriter.js";
init("/rewriter_bg.wasm");
import { initSync, rewrite_js } from "../../../rewriter/out/rewriter.js";
import "../../../static/wasm.js";
initSync(new WebAssembly.Module(
Uint8Array.from(atob(WASM), c => c.charCodeAt(0))
))
// Also can take in origin?: URL
export function rewriteJs(js: string) {
const f = rewrite_js(js);
console.log(f)
return f
// console.log(f)
//
// return f
// try {
// const ast = parseModule(js, {
// module: true,
// webcompat: true,
// });
//
// const identifierList = [
// "window",
// "self",
// "globalThis",
// "this",
// "parent",
// "top",
// "location",
// ];
//
// const customTraveler = makeTraveler({
// ImportDeclaration: (node: ESTree.ImportDeclaration) => {
// node.source.value = encodeUrl(node.source.value as string, origin);
// },
//
// ImportExpression: (node: ESTree.ImportExpression) => {
// if (node.source.type === "Literal") {
// node.source.value = encodeUrl(node.source.value as string, origin);
// } else if (node.source.type === "Identifier") {
// // this is for things that import something like
// // const moduleName = "name";
// // await import(moduleName);
// node.source.name = `__wrapImport(${node.source.name})`;
// }
// },
//
// ExportAllDeclaration: (node: ESTree.ExportAllDeclaration) => {
// node.source.value = encodeUrl(node.source.value as string, origin);
// },
//
// ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => {
// // 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);
// },
//
// MemberExpression: (node: ESTree.MemberExpression) => {
// if (
// node.object.type === "Identifier" &&
// identifierList.includes(node.object.name)
// ) {
// node.object.name = `globalThis.$s(${node.object.name})`;
// }
// },
//
// AssignmentExpression: (node: ESTree.AssignmentExpression, more) => {
// if (
// node.left.type === "Identifier" &&
// identifierList.includes(node.left.name)
// ) {
// node.left.name = `globalThis.$s(${node.left.name})`;
// }
//
// if (
// node.right.type === "Identifier" &&
// identifierList.includes(node.right.name)
// ) {
// node.right.name = `globalThis.$s(${node.right.name})`;
// }
// },
// ArrayExpression: (node: ESTree.ArrayExpression) => {
// node.elements.forEach((element) => {
// if (element.type === "Identifier" && identifierList.includes(element.name)) {
// element.name = `globalThis.$s(${element.name})`;
// }
// });
// },
//
// VariableDeclarator: (node: ESTree.VariableDeclarator) => {
// if (
// node.init &&
// node.init.type === "Identifier" &&
// identifierList.includes(node.init.name)
// ) {
// node.init.name = `globalThis.$s(${node.init.name})`;
// }
// },
// });
//
// customTraveler.go(ast);
//
// return generate(ast);
// } catch (e) {
// console.error(e);
// console.log(js);
//
// return js;
// }
}
// try {
// const ast = parseModule(js, {
// module: true,
// webcompat: true,
// });
//
// const identifierList = [
// "window",
// "self",
// "globalThis",
// "this",
// "parent",
// "top",
// "location",
// ];
//
// const customTraveler = makeTraveler({
// ImportDeclaration: (node: ESTree.ImportDeclaration) => {
// node.source.value = encodeUrl(node.source.value as string, origin);
// },
//
// ImportExpression: (node: ESTree.ImportExpression) => {
// if (node.source.type === "Literal") {
// node.source.value = encodeUrl(node.source.value as string, origin);
// } else if (node.source.type === "Identifier") {
// // this is for things that import something like
// // const moduleName = "name";
// // await import(moduleName);
// node.source.name = `__wrapImport(${node.source.name})`;
// }
// },
//
// ExportAllDeclaration: (node: ESTree.ExportAllDeclaration) => {
// node.source.value = encodeUrl(node.source.value as string, origin);
// },
//
// ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => {
// // 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);
// },
//
// MemberExpression: (node: ESTree.MemberExpression) => {
// if (
// node.object.type === "Identifier" &&
// identifierList.includes(node.object.name)
// ) {
// node.object.name = `globalThis.$s(${node.object.name})`;
// }
// },
//
// AssignmentExpression: (node: ESTree.AssignmentExpression, more) => {
// console.log(node, more);
// if (
// node.left.type === "Identifier" &&
// identifierList.includes(node.left.name)
// ) {
// node.left.name = `globalThis.$s(${node.left.name})`;
// }
//
// if (
// node.right.type === "Identifier" &&
// identifierList.includes(node.right.name)
// ) {
// node.right.name = `globalThis.$s(${node.right.name})`;
// }
// },
// ArrayExpression: (node: ESTree.ArrayExpression) => {
// node.elements.forEach((element) => {
// if (element.type === "Identifier" && identifierList.includes(element.name)) {
// element.name = `globalThis.$s(${element.name})`;
// }
// });
// },
//
// VariableDeclarator: (node: ESTree.VariableDeclarator) => {
// if (
// node.init &&
// node.init.type === "Identifier" &&
// identifierList.includes(node.init.name)
// ) {
// node.init.name = `globalThis.$s(${node.init.name})`;
// }
// },
// });
//
// customTraveler.go(ast);
//
// return generate(ast);
// } catch (e) {
// console.error(e);
// console.log(js);
//
// return js;
// }

View file

@ -1,4 +1,4 @@
;import { rewriteJs } from "./js";
import { rewriteJs } from "./js";
function canParseUrl(url: string, origin?: URL) {
try {

View file

@ -187,6 +187,7 @@ self.ScramjetServiceWorker = class ScramjetServiceWorker {
if (!["document", "iframe"].includes(request.destination))
return new Response(undefined, { status: 500 });
console.log("FOUND ERROR");
console.error(err);
return renderError(err, decodeUrl(request.url));
@ -238,8 +239,7 @@ function errorTemplate(trace: string, fetchedURL: string) {
<button id="reload">Reload</button>
<hr />
<p><i>Scramjet v<span id="version"></span></i></p>
<script src="${
"data:application/javascript," + encodeURIComponent(script)
<script src="${"data:application/javascript," + encodeURIComponent(script)
}"></script>
</body>
</html>

Binary file not shown.