Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ module objects or loader through `process` taken as a value (`var p = process; p
computed member of `process`, or requests the `module` or `process` built-in is not split, but an
alias is not followed, since most bundles pass `process` around. And
no value the program tests is `document.all`, so `x != null ? x : y` becomes `x ?? y`: the two
differ only for that object, which compares loosely equal to `null` without being nullish. Nor does any code read a function's caller through `.caller`, `arguments.callee.caller` or a stack frame (`Error.prepareStackTrace` call sites with `getFunction()` or `getFunctionName()`) to observe which function called it (the non-standard `Function.prototype.caller`; strict code cannot): inlining a control-flow object's call proxy removes the proxy's frame, so such a read would see the function that held the call instead of the proxy. The default lane also assumes obfuscator.io's self-defending check would pass on the input: removing the trap may make an input that would trap (for example one reformatted after obfuscation) run normally. It does not assume an obfuscator.io domain lock would pass: the lock reads the host's `document` and `location` and enumerates the global object's names, so the default lane keeps it, the protections registered with it and the machinery it can see, and says so (`a domain lock was found …`); `--unsafe` assumes the lock passes and removes it. The default lane keeps a protection that may still run, one it cannot read or prove inert, and when that is a block registered through a call controller or a debug-protection timer it keeps every block registered that way and every debug-protection timer with it, including another layer's console silencing, because removing one layer's silencing and keeping another's changes what the program prints, and removing the silencing and keeping a timer, or the reverse, changes what it prints or whether it keeps running. Untwist also assumes no debugger or inspector session changes the program's values while it
differ only for that object, which compares loosely equal to `null` without being nullish. Nor does any code read a function's caller through `.caller`, `arguments.callee.caller` or a stack frame (`Error.prepareStackTrace` call sites with `getFunction()` or `getFunctionName()`) to observe which function called it (the non-standard `Function.prototype.caller`; strict code cannot): inlining a control-flow object's call proxy removes the proxy's frame, so such a read would see the function that held the call instead of the proxy. The default lane also assumes obfuscator.io's self-defending check would pass on the input: removing the trap may make an input that would trap (for example one reformatted after obfuscation) run normally. It does not assume an obfuscator.io domain lock would pass: the lock reads the host's `document` and `location` and enumerates the global object's names, so the default lane keeps it, the protections registered with it and the machinery it can see, and says so (`a domain lock was found …`); `--unsafe` assumes the lock passes and removes it. The default lane keeps a protection that may still run, one it cannot read or prove inert (a self-defending callback is inert only when the built-in scan proves that nothing in the program can replace a built-in member the callback calls), and when that is a block registered through a call controller or a debug-protection timer it keeps every block registered that way and every debug-protection timer with it, including another layer's console silencing, because removing one layer's silencing and keeping another's changes what the program prints, and removing the silencing and keeping a timer, or the reverse, changes what it prints or whether it keeps running. Untwist also assumes no debugger or inspector session changes the program's values while it
runs; every lane and every proof rests on this.
[`docs/known-divergences/deobfuscate.md`](docs/known-divergences/deobfuscate.md) lists every
premise.
Expand Down
2 changes: 1 addition & 1 deletion crates/untwist-core/src/semantic/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::semantic::has_module_syntax;

mod scan;

pub use scan::{intact, scan, scan_with_unknown_keys_plain};
pub use scan::{intact, scan, scan_outside, scan_with_unknown_keys_plain};

use oxc_semantic::Scoping;

Expand Down
46 changes: 42 additions & 4 deletions crates/untwist-core/src/semantic/builtins/scan.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use oxc_ast::ast::*;
use oxc_ast_visit::{walk_js, VisitJs};
use oxc_semantic::Scoping;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator};
use oxc_syntax::scope::ScopeFlags;
use oxc_syntax::symbol::{SymbolFlags, SymbolId};
use rustc_hash::FxHashMap;
use rustc_hash::{FxHashMap, FxHashSet};
use std::cell::{Cell, OnceCell, RefCell};

use super::{Builtins, Reliance, Scanned};
Expand All @@ -15,6 +16,7 @@ mod escapes;
mod functions;
mod keys;
mod modules;
mod numeric;
mod parts;
mod reach;
mod targets;
Expand Down Expand Up @@ -191,12 +193,14 @@ struct Scan<'s> {
functions: OnceCell<functions::FunctionBindings>,
escaping: OnceCell<escapes::Escaping>,
windows: OnceCell<windows::WindowLocals>,
numeric: OnceCell<numeric::KeyBindings>,
reached: RefCell<FxHashMap<usize, bool>>,
scoping: &'s Scoping,
reliance: &'s Reliance,
writes_only: bool,
unknown_keys_plain: bool,
first_only: bool,
skipped: Option<&'s FxHashSet<Span>>,
module: bool,
frames: Vec<Frame>,
verdict: Builtins,
Expand Down Expand Up @@ -274,6 +278,20 @@ pub fn scan_with_unknown_keys_plain(
scan.verdict
}

pub fn scan_outside(
program: &Program<'_>,
scoping: &Scoping,
module: bool,
reliance: &Reliance,
skipped: &FxHashSet<Span>,
) -> Builtins {
let mut scan = Scan::new(program, scoping, module, reliance);
scan.skipped = Some(skipped);
scan.visit_program(program);
scan.settle_window_flows();
scan.verdict
}

pub fn intact(program: &Program<'_>, scoping: &Scoping, module: bool, reliance: &Reliance) -> bool {
let mut scan = Scan::new(program, scoping, module, reliance);
scan.first_only = true;
Expand All @@ -295,12 +313,14 @@ impl<'s> Scan<'s> {
functions: OnceCell::new(),
escaping: OnceCell::new(),
windows: OnceCell::new(),
numeric: OnceCell::new(),
reached: RefCell::new(FxHashMap::default()),
scoping,
reliance,
writes_only: false,
unknown_keys_plain: false,
first_only: false,
skipped: None,
module,
frames: vec![Frame::of_program(program, module)],
verdict: first_verdict(scoping, reliance, module),
Expand All @@ -320,6 +340,20 @@ impl Scan<'_> {
.cloned()
}

pub(super) fn numeric_key(&self, key: &Expression<'_>) -> bool {
let numeric = self
.numeric
.get_or_init(|| numeric::key_bindings(self.program, self.scoping, self.module));
numeric::is_harmless_key(key, self.scoping, numeric)
}

pub(super) fn sharpen<'k>(&self, key: Key<'k>) -> Key<'k> {
match key {
Key::Unknown(expression) if self.numeric_key(expression) => Key::Other,
other => other,
}
}

pub(super) fn only_a_function(&self, symbol: SymbolId) -> bool {
self.functions
.get_or_init(|| functions::function_bindings(self.program))
Expand Down Expand Up @@ -347,7 +381,7 @@ impl Scan<'_> {
fn pattern_key_reaches(&self, computed: bool, key: &PropertyKey<'_>) -> bool {
let name = match (computed, key) {
(true, PropertyKey::StaticIdentifier(_) | PropertyKey::PrivateIdentifier(_)) => None,
(true, _) => match key.as_expression().map(key_of) {
(true, _) => match key.as_expression().map(|key| self.sharpen(key_of(key))) {
Some(Key::Name(name)) => Some(name),
Some(Key::Other) | None => return false,
Some(Key::Unknown(expression)) => {
Expand Down Expand Up @@ -874,7 +908,11 @@ impl<'a> VisitJs<'a> for Scan<'_> {
}

fn visit_statement(&mut self, it: &Statement<'a>) {
if self.settled() {
if self.settled()
|| self
.skipped
.is_some_and(|skipped| skipped.contains(&it.span()))
{
return;
}
walk_js::walk_statement(self, it);
Expand Down Expand Up @@ -909,7 +947,7 @@ impl<'a> VisitJs<'a> for Scan<'_> {
}

fn visit_computed_member_expression(&mut self, it: &ComputedMemberExpression<'a>) {
let key = key_of(&it.expression);
let key = self.sharpen(key_of(&it.expression));
if self.names_a_loader(key) {
self.not_allowed();
}
Expand Down
2 changes: 1 addition & 1 deletion crates/untwist-core/src/semantic/builtins/scan/allow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ impl Scan<'_> {
}

pub(super) fn read_allowed(&self, root: Root<'_>, key: Key<'_>) -> bool {
let name = match key {
let name = match self.sharpen(key) {
Key::Name(name) => name.to_string(),
Key::Other => return root == Root::Global,
Key::Unknown(expression) => match self.known_key(expression) {
Expand Down
Loading