mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-16 07:30:02 -04:00
fix rewriter
This commit is contained in:
parent
f53bc623ff
commit
3462785c39
3 changed files with 89 additions and 8 deletions
|
@ -25,7 +25,7 @@ fn main() -> std::io::Result<()> {
|
||||||
let path = Path::new(&name);
|
let path = Path::new(&name);
|
||||||
let source_text = std::fs::read_to_string(path)?;
|
let source_text = std::fs::read_to_string(path)?;
|
||||||
|
|
||||||
dbg!(rewrite(&source_text));
|
println!("{}", rewrite(&source_text));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
|
@ -1,6 +1,9 @@
|
||||||
use oxc_allocator::Allocator;
|
use oxc_allocator::Allocator;
|
||||||
use oxc_ast::{
|
use oxc_ast::{
|
||||||
ast::{AssignmentTarget, Class, Function, IdentifierReference, MemberExpression, TSImportType},
|
ast::{
|
||||||
|
AssignmentTarget, Class, Expression, Function, IdentifierReference, MemberExpression,
|
||||||
|
TSImportType,
|
||||||
|
},
|
||||||
visit::walk,
|
visit::walk,
|
||||||
Visit,
|
Visit,
|
||||||
};
|
};
|
||||||
|
@ -55,19 +58,85 @@ impl<'a> Visit<'a> for Rewriter {
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
fn visit_member_expression(&mut self, it: &MemberExpression<'a>) {
|
fn visit_member_expression(&mut self, it: &MemberExpression<'a>) {
|
||||||
match it {
|
self.trace_member(it);
|
||||||
MemberExpression::StaticMemberExpression(s) => {
|
// match it {
|
||||||
if s.property.name.to_string() == "location" {
|
// 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(),
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// _ => {}
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// js MUST not be able to get a reference to any of these because sbx
|
||||||
|
const UNSAFE_GLOBALS: [&str; 7] = [
|
||||||
|
"window",
|
||||||
|
"self",
|
||||||
|
"globalThis",
|
||||||
|
"this",
|
||||||
|
"parent",
|
||||||
|
"top",
|
||||||
|
"location",
|
||||||
|
];
|
||||||
|
|
||||||
|
impl Rewriter {
|
||||||
|
fn trace_member<'a>(&mut self, it: &MemberExpression<'a>) {
|
||||||
|
match &it {
|
||||||
|
MemberExpression::StaticMemberExpression(s) => match &s.object {
|
||||||
|
Expression::Identifier(obj) => {
|
||||||
|
dbg!(obj);
|
||||||
|
if UNSAFE_GLOBALS.contains(&obj.name.to_string().as_str()) {
|
||||||
self.jschanges.push(JsChange::GenericChange {
|
self.jschanges.push(JsChange::GenericChange {
|
||||||
span: s.property.span,
|
span: obj.span,
|
||||||
text: "$s(location)".to_string(),
|
text: format!("$s({})", obj.name),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Expression::ThisExpression(obj) => {
|
||||||
|
self.jschanges.push(JsChange::GenericChange {
|
||||||
|
span: obj.span,
|
||||||
|
text: "$s(this)".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
if it.object().is_member_expression() {
|
||||||
|
self.trace_member(it.object().as_member_expression().unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
MemberExpression::ComputedMemberExpression(s) => match &s.object {
|
||||||
|
Expression::Identifier(obj) => {
|
||||||
|
dbg!(obj);
|
||||||
|
if UNSAFE_GLOBALS.contains(&obj.name.to_string().as_str()) {
|
||||||
|
self.jschanges.push(JsChange::GenericChange {
|
||||||
|
span: obj.span,
|
||||||
|
text: format!("$s({})", obj.name),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Expression::ThisExpression(obj) => {
|
||||||
|
self.jschanges.push(JsChange::GenericChange {
|
||||||
|
span: obj.span,
|
||||||
|
text: "$s(this)".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
if it.object().is_member_expression() {
|
||||||
|
self.trace_member(it.object().as_member_expression().unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rewrite(js: &str) -> String {
|
pub fn rewrite(js: &str) -> String {
|
||||||
let source_text = js.to_string();
|
let source_text = js.to_string();
|
||||||
let allocator = Allocator::default();
|
let allocator = Allocator::default();
|
||||||
|
@ -81,6 +150,8 @@ pub fn rewrite(js: &str) -> String {
|
||||||
|
|
||||||
let program = ret.program;
|
let program = ret.program;
|
||||||
|
|
||||||
|
dbg!(&program);
|
||||||
|
|
||||||
let mut ast_pass = Rewriter::default();
|
let mut ast_pass = Rewriter::default();
|
||||||
ast_pass.visit_program(&program);
|
ast_pass.visit_program(&program);
|
||||||
|
|
||||||
|
|
10
rewriter/test.js
Normal file
10
rewriter/test.js
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
|
||||||
|
window.location.href = "http://example.com"
|
||||||
|
|
||||||
|
|
||||||
|
console.log(top.window.aaa)
|
||||||
|
consle.log(globalThis["win" + "dow"])
|
||||||
|
|
||||||
|
globalThis.eval("..")
|
||||||
|
|
||||||
|
let ref = { b: this.top.window, c: globalThis["win" + "dow"] }
|
Loading…
Add table
Add a link
Reference in a new issue