Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/untwist-cli/tests/reports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn the_deobfuscate_stage_times_prepare_then_every_round_in_order() {

#[test]
fn a_default_lane_alias_refusal_prints_the_unsafe_hint_on_stderr() {
let high = include_str!("../../../fixtures/reference/deobfuscate/obfuscator.io-high.js");
let high = include_str!("../../../fixtures/obfuscator-io/preset-medium--greeting-1.js");
let hint = untwist::UNSAFE_LANE_DECODES;
let out = stdin_output(high, &[]);
assert!(out.status.success());
Expand Down
5 changes: 5 additions & 0 deletions crates/untwist-deobfuscate/src/calls/agnostic/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,11 @@ impl<'s, 'a> Bindings<'s, 'a> {
}

pub(super) fn never_reassigned(&self, symbol: SymbolId) -> bool {
let redeclared_param = self.params.contains_key(&symbol)
&& !self.scoping.symbol_redeclarations(symbol).is_empty();
if redeclared_param {
return false;
}
match self.defs.get(symbol) {
Some(defs) => !defs.mutated && defs.writes.is_empty(),
None => !self.scoping.symbol_is_mutated(symbol),
Expand Down
4 changes: 4 additions & 0 deletions crates/untwist-deobfuscate/src/calls/agnostic/judge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ impl<'s, 'a> Sites<'s, 'a> {
fn into_saturated(mut self) -> (Agnostic<'a>, bool, bool) {
self.creations_native();
self.out.saturated = true;
self.judge_refined();
self.out.pending_texts = self.pending_texts.take();
self.out.judged_texts = self.judged_texts.take();
(self.out, true, true)
}

Expand Down Expand Up @@ -456,7 +458,9 @@ impl<'s, 'a> Sites<'s, 'a> {
self.out.code_text.get_or_insert(span);
}
}
self.judge_refined();
self.out.pending_texts = self.pending_texts.take();
self.out.judged_texts = self.judged_texts.take();
(self.out, chains, natives)
}

Expand Down
21 changes: 21 additions & 0 deletions crates/untwist-deobfuscate/src/calls/agnostic/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,24 @@ impl<'s, 'a> Sites<'s, 'a> {
}
}
}

impl<'a> Sites<'_, 'a> {
pub(super) fn refined(&self, hit: Option<bool>) -> bool {
if hit == Some(false) {
self.refined_away.set(true);
}
hit.unwrap_or(true)
}

pub(super) fn note_refined(&mut self, base: &Expression<'a>) {
if self.refined_away.take() {
let base = self.alloc_expr(base);
self.refined_bases.push(base);
}
}

pub(super) fn judge_refined(&mut self) {
let bases = std::mem::take(&mut self.refined_bases);
self.out.keyed_global |= bases.iter().any(|base| !self.proven_non_global(base));
}
}
2 changes: 2 additions & 0 deletions crates/untwist-deobfuscate/src/calls/agnostic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ pub struct Agnostic<'a> {
pub(super) observe_keyed: Vec<(Span, Cow<'a, str>)>,
pub(super) ambient: Vec<(Span, &'a str, bool)>,
pub(super) saturated: bool,
pub(super) keyed_global: bool,
pub(super) global_source: bool,
pub(super) observe_stored: Option<Span>,
pub(super) stores_global: bool,
pub(super) pending_texts: Vec<String>,
pub(super) judged_texts: Vec<String>,
pub bootstrap: Option<&'static str>,
#[cfg(feature = "refusal-spans")]
pub(super) trail: Vec<crate::refusal_spans::RefusalSpan>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ impl<'s, 'a> Walker<'s, 'a> {
}

impl Loose {
pub(super) fn kept(&self, spans: Vec<Span>) -> Vec<Span> {
spans
.into_iter()
.filter(|span| !self.exempt(*span))
.collect()
}

pub(in crate::calls::agnostic) fn exempt(&self, span: Span) -> bool {
self.blocks
.iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use oxc_allocator::{Address, GetAddress};
use oxc_ast::ast::*;
use oxc_ast::AstKind;
use oxc_syntax::symbol::SymbolFlags;
use std::borrow::Cow;

use super::{
function_literal, member_key, same, transparent, Walker, GLOBAL_RECEIVER_READERS, NAMESPACE,
function_literal, member_address, member_key, same, transparent, unsafe_read, Walker,
GLOBAL_RECEIVER_READERS, KIND_DEPTH, NAMESPACE, VALUE,
};

const FORWARDERS: [&str; 3] = ["call", "apply", "bind"];
Expand Down Expand Up @@ -89,4 +91,57 @@ impl<'s, 'a> Walker<'s, 'a> {
}),
}
}

pub(super) fn member_use(&self, at: usize, kind: u8) -> bool {
if kind & VALUE != 0 {
return false;
}
if kind & NAMESPACE == 0 {
return true;
}
let member = &self.stack[at];
let address = member.address();
let key = match member {
AstKind::StaticMemberExpression(m) => Some(Cow::Borrowed(m.property.name.as_str())),
AstKind::ComputedMemberExpression(m) => {
self.key_text(&m.expression, KIND_DEPTH).map(Cow::Owned)
}
_ => None,
};
if self.called_or_written(at, address) {
return true;
}
if key.as_deref().is_some_and(|key| !unsafe_read(key)) {
return true;
}
self.allowed(at, VALUE)
}

fn called_or_written(&self, at: usize, member: Address) -> bool {
let mut child = member;
let mut up = at;
while up > 0 {
up -= 1;
match &self.stack[up] {
kind if transparent(kind) => child = kind.address(),
AstKind::CallExpression(c) => return c.callee.address() == child,
AstKind::NewExpression(n) => return n.callee.address() == child,
AstKind::AssignmentExpression(a) => {
return a
.left
.as_member_expression()
.is_some_and(|m| member_address(m) == member)
}
AstKind::UpdateExpression(_)
| AstKind::ArrayAssignmentTarget(_)
| AstKind::AssignmentTargetWithDefault(_)
| AstKind::AssignmentTargetPropertyProperty(_)
| AstKind::AssignmentTargetRest(_) => return true,
AstKind::ForInStatement(f) => return !same(&f.right, child),
AstKind::ForOfStatement(f) => return !same(&f.right, child),
_ => return false,
}
}
false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
use oxc_ast::ast::*;
use oxc_ast::AstKind;
use oxc_syntax::operator::LogicalOperator;
use oxc_syntax::symbol::{SymbolFlags, SymbolId};
use std::borrow::Cow;

use super::keys::KeyProof;
use super::{key_kind, str_const, Walker, GLOBAL, KIND_DEPTH, NAMESPACE};
use crate::proof::GLOBAL_OBJECTS;

const BUILTIN_CONSTRUCTORS: [&str; 16] = [
"Object", "Function", "Array", "String", "Number", "Boolean", "Symbol", "BigInt", "RegExp",
"Date", "Error", "Map", "Set", "WeakMap", "WeakSet", "Promise",
];

impl<'s, 'a> Walker<'s, 'a> {
pub(super) fn computed_kind(
&self,
object: &Expression<'a>,
key: &Expression<'a>,
base: u8,
) -> u8 {
let text = self.key_text(key, KIND_DEPTH);
let open = text.is_none();
if open {
match self.key_proof(object, key) {
KeyProof::Open => {}
KeyProof::Symbol => return 0,
KeyProof::Enumerated => {
let global = base & GLOBAL != 0 || self.may_hold_global(object, KIND_DEPTH);
return if global { GLOBAL } else { 0 };
}
}
}
let kind = key_kind(text.map(Cow::Owned), base);
if kind == 0 && open && self.may_hold_global(object, KIND_DEPTH) {
return NAMESPACE;
}
kind
}

pub(super) fn key_text(&self, expr: &Expression<'a>, depth: u8) -> Option<String> {
let next = depth.checked_sub(1)?;
let Expression::Identifier(id) = expr.without_parentheses() else {
return str_const(expr, self.resolve, depth);
};
let symbol = self.symbol(id)?;
self.fixed_init(symbol)
.and_then(|init| self.key_text(init, next))
}

fn fixed_init(&self, symbol: SymbolId) -> Option<&'a Expression<'a>> {
let candidate = self.candidates.get(&symbol)?;
let fixed = !self.scoping.symbol_is_mutated(symbol)
&& self.scoping.symbol_redeclarations(symbol).is_empty()
&& candidate
.function
.is_none_or(|function| !self.arguments_used.contains(&function));
fixed.then_some(candidate.init)
}

fn may_hold_global(&self, expr: &Expression<'a>, depth: u8) -> bool {
let Some(next) = depth.checked_sub(1) else {
return true;
};
match expr.without_parentheses() {
Expression::StringLiteral(_)
| Expression::NumericLiteral(_)
| Expression::BigIntLiteral(_)
| Expression::BooleanLiteral(_)
| Expression::NullLiteral(_)
| Expression::RegExpLiteral(_)
| Expression::TemplateLiteral(_)
| Expression::ObjectExpression(_)
| Expression::ArrayExpression(_)
| Expression::FunctionExpression(_)
| Expression::ArrowFunctionExpression(_)
| Expression::ClassExpression(_)
| Expression::UnaryExpression(_)
| Expression::BinaryExpression(_)
| Expression::UpdateExpression(_) => false,
Expression::Identifier(id) => match self.symbol(id) {
None => GLOBAL_OBJECTS.contains(&id.name.as_str()),
Some(symbol) => self.symbol_may_hold_global(symbol, next),
},
Expression::ThisExpression(_) => self.this_open(),
Expression::CallExpression(call) => !self.array_source(call),
Expression::StaticMemberExpression(member) => !self.builtin_prototype(member),
Expression::ConditionalExpression(c) => {
self.may_hold_global(&c.consequent, next)
|| self.may_hold_global(&c.alternate, next)
}
Expression::LogicalExpression(l) if l.operator == LogicalOperator::And => {
self.may_hold_global(&l.right, next)
}
Expression::LogicalExpression(l) => {
self.may_hold_global(&l.left, next) || self.may_hold_global(&l.right, next)
}
Expression::SequenceExpression(s) => s
.expressions
.last()
.is_none_or(|last| self.may_hold_global(last, next)),
_ => true,
}
}

fn symbol_may_hold_global(&self, symbol: SymbolId, depth: u8) -> bool {
let fixed_callable = self
.scoping
.symbol_flags(symbol)
.intersects(SymbolFlags::Function | SymbolFlags::Class)
&& !self.scoping.symbol_is_mutated(symbol)
&& self.scoping.symbol_redeclarations(symbol).is_empty();
if fixed_callable {
return false;
}
self.fixed_init(symbol)
.is_none_or(|init| self.may_hold_global(init, depth))
}

fn this_open(&self) -> bool {
self.this_open.last().copied().unwrap_or(true)
}

pub(super) fn collect_array_source(&mut self, kind: AstKind<'a>) {
match kind {
AstKind::Function(function) if function.is_declaration() => {
let Some(id) = function.id.as_ref() else {
return;
};
let shaped = crate::string_array::is_string_array_function(function, &id.name);
if let (true, Some(symbol)) = (shaped, id.symbol_id.get()) {
self.array_functions.insert(symbol, function.span);
}
}
AstKind::AssignmentExpression(assign) => {
let AssignmentTarget::AssignmentTargetIdentifier(target) = &assign.left else {
return;
};
if let Some(symbol) = self.symbol(target) {
self.assigned_at
.entry(symbol)
.or_default()
.push(assign.span);
}
}
_ => {}
}
}

fn array_source(&self, call: &CallExpression<'a>) -> bool {
let Expression::Identifier(callee) = call.callee.without_parentheses() else {
return false;
};
let Some(symbol) = self.symbol(callee) else {
return false;
};
let Some(span) = self.array_functions.get(&symbol) else {
return false;
};
let writes = self
.scoping
.get_resolved_reference_ids(symbol)
.iter()
.filter(|reference| self.scoping.get_reference(**reference).is_write())
.count();
let inside = self.assigned_at.get(&symbol).map_or(&[][..], Vec::as_slice);
call.arguments.is_empty()
&& self.scoping.symbol_redeclarations(symbol).is_empty()
&& inside.len() == writes
&& inside
.iter()
.all(|write| span.start <= write.start && write.end <= span.end)
}

fn builtin_prototype(&self, member: &StaticMemberExpression<'a>) -> bool {
let Expression::Identifier(owner) = member.object.without_parentheses() else {
return false;
};
member.property.name == "prototype"
&& self.symbol(owner).is_none()
&& BUILTIN_CONSTRUCTORS.contains(&owner.name.as_str())
}
}
Loading
Loading