diff --git a/rewriter/src/lib.rs b/rewriter/src/lib.rs index b32487f..2655058 100644 --- a/rewriter/src/lib.rs +++ b/rewriter/src/lib.rs @@ -75,6 +75,7 @@ fn get_config(scramjet: &Object, url: &str) -> Config { do_sourcemaps: get_flag(scramjet, url, "sourcemaps"), capture_errors: get_flag(scramjet, url, "captureErrors"), scramitize: get_flag(scramjet, url, "scramitize"), + strict_rewrites: get_flag(scramjet, url, "strictRewrites"), } } diff --git a/rewriter/src/main.rs b/rewriter/src/main.rs index 29a5293..ddd60ae 100644 --- a/rewriter/src/main.rs +++ b/rewriter/src/main.rs @@ -124,6 +124,7 @@ fn dorewrite(source_text: &str) -> String { capture_errors: true, do_sourcemaps: true, scramitize: false, + strict_rewrites: false, }, ) .as_slice(), diff --git a/rewriter/src/rewrite.rs b/rewriter/src/rewrite.rs index a5c5757..a3c595b 100644 --- a/rewriter/src/rewrite.rs +++ b/rewriter/src/rewrite.rs @@ -35,7 +35,7 @@ pub type EncodeFn = Box String>; struct Rewriter { jschanges: Vec, base: Url, - config: Config, + config: Confistrig, } pub struct Config { @@ -52,6 +52,7 @@ pub struct Config { pub capture_errors: bool, pub scramitize: bool, pub do_sourcemaps: bool, + pub strict_rewrites: bool, } impl Rewriter { @@ -128,6 +129,18 @@ impl<'a> Visit<'a> for Rewriter { return; // unwise to walk the rest of the tree } + if !self.config.strict_rewrites + && !UNSAFE_GLOBALS.contains(&s.property.name.as_str()) + { + if let Expression::Identifier(i) = &s.object { + // cull tree - this should be safe + return; + } + if let Expression::ThisExpression(_) = &s.object { + return; + } + } + if self.config.scramitize && !matches!(s.object, Expression::MetaProperty(_)) && !matches!(s.object, Expression::Super(_)) diff --git a/src/client/shared/err.ts b/src/client/shared/err.ts index 8c72ca6..64a192f 100644 --- a/src/client/shared/err.ts +++ b/src/client/shared/err.ts @@ -45,12 +45,13 @@ export default function (client: ScramjetClient, self: typeof globalThis) { client.Proxy("Promise.prototype.catch", { apply(ctx) { - ctx.args[0] = new Proxy(ctx.args[0], { - apply(target, thisArg, argArray) { - // console.warn("CAUGHT PROMISE REJECTION", argArray); - Reflect.apply(target, thisArg, argArray); - }, - }); + if (ctx.args[0]) + ctx.args[0] = new Proxy(ctx.args[0], { + apply(target, thisArg, argArray) { + // console.warn("CAUGHT PROMISE REJECTION", argArray); + Reflect.apply(target, thisArg, argArray); + }, + }); }, }); } diff --git a/src/controller/index.ts b/src/controller/index.ts index 0bb3c78..df2c3e4 100644 --- a/src/controller/index.ts +++ b/src/controller/index.ts @@ -29,6 +29,7 @@ export class ScramjetController { serviceworkers: false, naiiveRewriter: false, captureErrors: true, + strictRewrites: false, syncxhr: false, cleanerrors: false, scramitize: false, diff --git a/src/types.d.ts b/src/types.d.ts index 3c3aab0..2a48c03 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -27,6 +27,7 @@ type ScramjetFlags = { serviceworkers: boolean; naiiveRewriter: boolean; captureErrors: boolean; + strictRewrites: boolean; cleanerrors: boolean; scramitize: boolean; sourcemaps: boolean;