This commit is contained in:
Christopher Tarry
2026-10-28 00:14:21 -04:00
parent 2dde7e5be3
commit ae35bb3f68
3 changed files with 55 additions and 57 deletions
+51 -15
View File
@@ -9,7 +9,8 @@
//!
//! The helper must be assigned once and forward parameters in order, except for swapping the
//! method and receiver. The method must be read from the receiver's local binding. This keeps
//! the same `this` value as a regular method call.
//! the same `this` value as a regular method call. Calls must supply exactly one argument per
//! parameter, without spreads, so the method receives the same arguments.
use swc_core::common::util::take::Take;
use swc_core::ecma::ast::{
@@ -18,7 +19,7 @@ use swc_core::ecma::ast::{
};
use swc_core::ecma::visit::{Visit, VisitMut, VisitMutWith, VisitWith};
use super::HashSet;
use super::HashMap;
use super::analysis::{Analysis, Bindings, Globals, names};
use crate::js::ast;
@@ -27,7 +28,7 @@ pub(super) fn run(program: &mut Program, analysis: &mut Analysis) -> bool {
let bindings = analysis.bindings(program);
let mut helpers = Helpers::default();
program.visit_with(&mut helpers);
helpers.0.retain(|id| bindings.is_written_once(id));
helpers.0.retain(|id, _| bindings.is_written_once(id));
if helpers.0.is_empty() {
return false;
}
@@ -72,23 +73,24 @@ fn is_helper(function: &Function) -> bool {
}
}
/// Helpers by binding.
/// Helpers by binding, with the number of parameters they forward.
#[derive(Default)]
struct Helpers(HashSet<Id>);
struct Helpers(HashMap<Id, usize>);
impl Visit for Helpers {
fn visit_expr(&mut self, expression: &Expr) {
if let Some((id, Expr::Fn(function))) = ast::assignment(expression)
&& is_helper(&function.function)
{
self.0.insert(id);
self.0.insert(id, function.function.params.len());
}
expression.visit_children_with(self);
}
fn visit_fn_decl(&mut self, declaration: &FnDecl) {
if is_helper(&declaration.function) {
self.0.insert(declaration.ident.to_id());
self.0
.insert(declaration.ident.to_id(), declaration.function.params.len());
}
declaration.visit_children_with(self);
}
@@ -98,14 +100,14 @@ impl Visit for Helpers {
&& let Some(Expr::Fn(function)) = declarator.init.as_deref()
&& is_helper(&function.function)
{
self.0.insert(name.to_id());
self.0.insert(name.to_id(), function.function.params.len());
}
declarator.visit_children_with(self);
}
}
struct Restore<'a> {
helpers: &'a HashSet<Id>,
helpers: &'a HashMap<Id, usize>,
bindings: &'a Bindings,
globals: Globals,
changed: bool,
@@ -113,13 +115,18 @@ struct Restore<'a> {
impl Restore<'_> {
/// Rewrites `helper(receiver, receiver.method, ...rest)` as a method call.
fn method_call(&self, arguments: &mut Vec<ExprOrSpread>) -> Option<Expr> {
fn method_call(&self, arguments: &mut Vec<ExprOrSpread>, parameters: usize) -> Option<Expr> {
// The helper forwards every parameter, including missing ones as undefined, and ignores
// extra arguments after evaluating them. Only a matching, spread-free call forwards the
// same arguments directly to the method.
if arguments.len() != parameters
|| arguments.iter().any(|argument| argument.spread.is_some())
{
return None;
}
let [receiver, method, ..] = &mut arguments[..] else {
return None;
};
if receiver.spread.is_some() || method.spread.is_some() {
return None;
}
let Expr::Member(member) = &mut *method.expr else {
return None;
};
@@ -156,8 +163,8 @@ impl VisitMut for Restore<'_> {
expression.visit_mut_children_with(self);
if let Expr::Call(call) = expression
&& let Some(callee) = call.callee.as_expr().and_then(|callee| callee.as_ident())
&& self.helpers.contains(&callee.to_id())
&& let Some(replacement) = self.method_call(&mut call.args)
&& let Some(&parameters) = self.helpers.get(&callee.to_id())
&& let Some(replacement) = self.method_call(&mut call.args, parameters)
{
*expression = replacement;
self.changed = true;
@@ -169,6 +176,35 @@ impl VisitMut for Restore<'_> {
mod tests {
use crate::deobfuscate::testing;
#[test]
fn keeps_calls_whose_arguments_do_not_match_the_helpers_parameters() {
let output = testing::rewrite(
"function call(s, m, a) { return (function() {}).call.call(m, s, a); }
call(x, x.send);
call(x, x.send, 1, effect());
call(x, x.send, ...values);
call(x, x.send, 1, ...values);
call(...values, x.send, 1);
call(x, ...methods, 1);
call(x, x.send, 1);",
super::run,
/* fix= */ true,
);
testing::assert_contains(
&output,
&[],
&[
"call(x, x.send);",
"call(x, x.send, 1, effect());",
"call(x, x.send, ...values);",
"call(x, x.send, 1, ...values);",
"call(...values, x.send, 1);",
"call(x, ...methods, 1);",
"x.send(1);",
],
);
}
#[test]
fn restores_method_calls_on_the_receiver_they_read() {
let output = testing::rewrite(
+2 -41
View File
@@ -507,46 +507,6 @@ mod tests {
sent
}
/// Expected header order from the tracked Chrome 155 profile, verified against a headed
/// browser's book.sv network log.
#[tokio::test]
async fn requests_send_the_browsers_headers_in_its_order() {
let own = [
("user-agent", "Mozilla/5.0"),
("sec-ch-ua", "\"Chromium\";v=\"155\""),
("sec-ch-ua-mobile", "?0"),
("sec-ch-ua-platform", "\"Linux\""),
];
let client = BrowserClient {
own: own
.iter()
.map(|(name, value)| (name.parse().unwrap(), value.parse().unwrap()))
.collect(),
..BrowserClient::from_http(Client::new())
};
let (_, heads) = flow(&client).await;
let expected = [
"sec-ch-ua sec-ch-ua-mobile sec-ch-ua-platform upgrade-insecure-requests user-agent \
accept sec-fetch-site sec-fetch-mode sec-fetch-user sec-fetch-dest accept-encoding \
accept-language priority",
"sec-ch-ua-platform user-agent sec-ch-ua sec-ch-ua-mobile accept sec-fetch-site \
sec-fetch-mode sec-fetch-dest sec-fetch-storage-access accept-encoding \
accept-language",
"sec-ch-ua sec-ch-ua-mobile sec-ch-ua-platform upgrade-insecure-requests user-agent \
accept sec-fetch-site sec-fetch-mode sec-fetch-dest sec-fetch-storage-access \
accept-encoding accept-language priority",
"content-length sec-ch-ua-platform user-agent sec-ch-ua content-type cf-chl cf-chl-ra \
sec-ch-ua-mobile accept origin sec-fetch-site sec-fetch-mode sec-fetch-dest referer \
accept-encoding accept-language priority",
"sec-ch-ua-platform user-agent sec-ch-ua sec-ch-ua-mobile accept sec-fetch-site \
sec-fetch-mode sec-fetch-dest sec-fetch-storage-access referer accept-encoding \
accept-language priority",
"cache-control user-agent accept sec-fetch-site sec-fetch-mode sec-fetch-dest \
accept-encoding accept-language priority",
];
assert_eq!(header_names(&heads), expected);
}
/// Expected header order from the tracked iOS Safari 26.6 profile, verified against
/// captchatest proxy traffic. Includes Safari's separate redirected-loader order.
#[tokio::test]
@@ -647,6 +607,7 @@ mod tests {
] {
assert!(headers.contains(expected), "missing {expected}: {headers}");
}
assert!(headers.contains(&format!("referer: http://{address}/iframe?lang=auto")));
// WebKit sends only the iframe's origin.
assert!(headers.contains(&format!("referer: http://{address}/\r\n")));
}
}
+2 -1
View File
@@ -341,7 +341,8 @@ impl PlanCompiler {
// Declarations do not bind their names; the names resolve as globals.
Stmt::Decl(Decl::Fn(_) | Decl::Class(_)) | Stmt::Empty(_) => Statement::Empty,
Stmt::Decl(_) => Statement::Expression(self.unsupported("a declaration")),
Stmt::Block(block) => Statement::Block(self.statements(&block.stmts)),
// A block shares its function's scope, so its function declarations bind at its start.
Stmt::Block(block) => Statement::Block(self.body(&block.stmts)),
Stmt::If(branch) => Statement::If(
self.expression(&branch.test),
Box::new(self.statement(&branch.cons, None)),