cull paths unlikely to escape & add strictRewrites flag

This commit is contained in:
velzie 2024-10-20 12:05:04 -04:00
parent ba0aa479f0
commit bdf5fd4f95
No known key found for this signature in database
GPG key ID: AA51AEFB0A1F3820
6 changed files with 25 additions and 7 deletions

View file

@ -75,6 +75,7 @@ fn get_config(scramjet: &Object, url: &str) -> Config {
do_sourcemaps: get_flag(scramjet, url, "sourcemaps"), do_sourcemaps: get_flag(scramjet, url, "sourcemaps"),
capture_errors: get_flag(scramjet, url, "captureErrors"), capture_errors: get_flag(scramjet, url, "captureErrors"),
scramitize: get_flag(scramjet, url, "scramitize"), scramitize: get_flag(scramjet, url, "scramitize"),
strict_rewrites: get_flag(scramjet, url, "strictRewrites"),
} }
} }

View file

@ -124,6 +124,7 @@ fn dorewrite(source_text: &str) -> String {
capture_errors: true, capture_errors: true,
do_sourcemaps: true, do_sourcemaps: true,
scramitize: false, scramitize: false,
strict_rewrites: false,
}, },
) )
.as_slice(), .as_slice(),

View file

@ -35,7 +35,7 @@ pub type EncodeFn = Box<dyn Fn(String) -> String>;
struct Rewriter { struct Rewriter {
jschanges: Vec<JsChange>, jschanges: Vec<JsChange>,
base: Url, base: Url,
config: Config, config: Confistrig,
} }
pub struct Config { pub struct Config {
@ -52,6 +52,7 @@ pub struct Config {
pub capture_errors: bool, pub capture_errors: bool,
pub scramitize: bool, pub scramitize: bool,
pub do_sourcemaps: bool, pub do_sourcemaps: bool,
pub strict_rewrites: bool,
} }
impl Rewriter { impl Rewriter {
@ -128,6 +129,18 @@ impl<'a> Visit<'a> for Rewriter {
return; // unwise to walk the rest of the tree 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 if self.config.scramitize
&& !matches!(s.object, Expression::MetaProperty(_)) && !matches!(s.object, Expression::MetaProperty(_))
&& !matches!(s.object, Expression::Super(_)) && !matches!(s.object, Expression::Super(_))

View file

@ -45,6 +45,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
client.Proxy("Promise.prototype.catch", { client.Proxy("Promise.prototype.catch", {
apply(ctx) { apply(ctx) {
if (ctx.args[0])
ctx.args[0] = new Proxy(ctx.args[0], { ctx.args[0] = new Proxy(ctx.args[0], {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
// console.warn("CAUGHT PROMISE REJECTION", argArray); // console.warn("CAUGHT PROMISE REJECTION", argArray);

View file

@ -29,6 +29,7 @@ export class ScramjetController {
serviceworkers: false, serviceworkers: false,
naiiveRewriter: false, naiiveRewriter: false,
captureErrors: true, captureErrors: true,
strictRewrites: false,
syncxhr: false, syncxhr: false,
cleanerrors: false, cleanerrors: false,
scramitize: false, scramitize: false,

1
src/types.d.ts vendored
View file

@ -27,6 +27,7 @@ type ScramjetFlags = {
serviceworkers: boolean; serviceworkers: boolean;
naiiveRewriter: boolean; naiiveRewriter: boolean;
captureErrors: boolean; captureErrors: boolean;
strictRewrites: boolean;
cleanerrors: boolean; cleanerrors: boolean;
scramitize: boolean; scramitize: boolean;
sourcemaps: boolean; sourcemaps: boolean;