add setRealm rewriting

This commit is contained in:
velzie 2024-08-25 20:53:43 -04:00
parent 6d498950e3
commit 5024e19c08
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
3 changed files with 28 additions and 1 deletions

View file

@ -52,6 +52,7 @@ fn get_config(scramjet: &Object) -> Config {
importfn: get_str(config, "importfn"),
rewritefn: get_str(config, "rewritefn"),
metafn: get_str(config, "metafn"),
setrealmfn: get_str(config, "setrealmfn"),
}
}

View file

@ -119,6 +119,7 @@ fn dorewrite(source_text: &str) -> String {
importfn: "$import".to_string(),
rewritefn: "$rewrite".to_string(),
metafn: "$meta".to_string(),
setrealmfn: "$setrealm".to_string(),
},
)
.as_slice(),

View file

@ -1,6 +1,8 @@
use oxc_allocator::Allocator;
use oxc_ast::{
ast::{AssignmentTarget, Expression, IdentifierReference, ObjectPropertyKind},
ast::{
AssignmentTarget, Expression, IdentifierReference, MemberExpression, ObjectPropertyKind,
},
visit::walk,
Visit,
};
@ -38,6 +40,7 @@ pub struct Config {
pub wrapfn: String,
pub importfn: String,
pub rewritefn: String,
pub setrealmfn: String,
pub metafn: String,
pub encode: EncodeFn,
}
@ -100,6 +103,28 @@ impl<'a> Visit<'a> for Rewriter {
self.walk_member_expression(&it.callee);
walk::walk_arguments(self, &it.arguments);
}
fn visit_member_expression(&mut self, it: &oxc_ast::ast::MemberExpression<'a>) {
match it {
MemberExpression::StaticMemberExpression(s) => {
if s.property.name == "postMessage" {
self.jschanges.push(JsChange::GenericChange {
span: s.property.span,
// an empty object will let us safely reconstruct the realm later
text: format!("{}({{}}).{}", self.config.setrealmfn, s.property.name),
});
return; // unwise to walk the rest of the tree
}
}
_ => {
// TODO
// you could break this with ["postMessage"] etc
// however this code only exists because of recaptcha whatever
// and it would slow down js execution a lot
}
}
walk::walk_member_expression(self, it);
}
fn visit_this_expression(&mut self, it: &oxc_ast::ast::ThisExpression) {
self.jschanges.push(JsChange::GenericChange {
span: it.span,