cfsolve 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -0
- data/README.md +26 -6
- data/UPSTREAM.md +23 -8
- data/lib/cfsolve/profiles/safari-26-ios/answers.json +805 -797
- data/lib/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +909 -899
- data/lib/cfsolve/version.rb +2 -2
- data/lib/cfsolve.rb +22 -11
- data/vendor/cfsolve/README.md +5 -53
- data/vendor/cfsolve/examples/listing_dump.rs +24 -0
- data/vendor/cfsolve/examples/probe_similarity.rs +37 -0
- data/vendor/cfsolve/examples/rebase_reference.rs +96 -0
- data/vendor/cfsolve/examples/reread_capture.rs +31 -0
- data/vendor/cfsolve/examples/turnstile_smoke.rs +24 -3
- data/vendor/cfsolve/profiles/safari-26-ios/answers.json +805 -797
- data/vendor/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +909 -899
- data/vendor/cfsolve/src/capture/sessions.rs +88 -4
- data/vendor/cfsolve/src/capture.rs +1 -0
- data/vendor/cfsolve/src/deobfuscate/ast.rs +16 -0
- data/vendor/cfsolve/src/deobfuscate/control_flow.rs +1 -1
- data/vendor/cfsolve/src/deobfuscate/dead_branches.rs +33 -0
- data/vendor/cfsolve/src/deobfuscate/merged_functions.rs +24 -4
- data/vendor/cfsolve/src/embedding_page/extra_params.rs +10 -113
- data/vendor/cfsolve/src/embedding_page/page.rs +70 -99
- data/vendor/cfsolve/src/session/initial.rs +6 -7
- data/vendor/cfsolve/src/session/probes.rs +0 -1
- data/vendor/cfsolve/src/stage2/analysis/analyze.rs +4 -33
- data/vendor/cfsolve/src/stage2/analysis/discovery.rs +9 -17
- data/vendor/cfsolve/src/stage2/benchmark.rs +42 -62
- data/vendor/cfsolve/src/stage2/call_stack.rs +133 -227
- data/vendor/cfsolve/src/stage2/challenge_page.rs +10 -3
- data/vendor/cfsolve/src/stage2/probes/dates.rs +0 -44
- data/vendor/cfsolve/src/stage2/probes/parent_info.rs +345 -61
- data/vendor/cfsolve/src/stage2/probes/performance.rs +42 -5
- data/vendor/cfsolve/src/stage2/probes/stack.rs +45 -117
- data/vendor/cfsolve/src/stage2/probes/websocket_echo.rs +7 -76
- data/vendor/cfsolve/src/stage2/probes.rs +0 -1
- data/vendor/cfsolve/src/stage2/reference.json +1 -1
- data/vendor/cfsolve/src/stage2/reference.rs +70 -115
- data/vendor/cfsolve/src/stage2/registry.rs +6 -11
- data/vendor/cfsolve/src/stage2/replay.rs +3 -34
- data/vendor/cfsolve/src/stage2/stylesheets.rs +0 -35
- data/vendor/cfsolve/src/stage2/trace.rs +1 -1
- data/vendor/cfsolve/src/stage2/vocabulary.rs +6 -111
- data/vendor/cfsolve/src/stage2.rs +0 -1
- data/vendor/cfsolve/src/vm/dataflow.rs +15 -6
- data/vendor/cfsolve/src/vm/disassemble.rs +5 -68
- data/vendor/cfsolve/src/vm/encoded_strings.rs +1553 -0
- data/vendor/cfsolve/src/vm/extract.rs +193 -343
- data/vendor/cfsolve/src/vm/interpret/access.rs +2 -10
- data/vendor/cfsolve/src/vm/interpret/builtin.rs +0 -13
- data/vendor/cfsolve/src/vm/interpret/intrinsic.rs +28 -43
- data/vendor/cfsolve/src/vm/interpret/mix32.rs +216 -86
- data/vendor/cfsolve/src/vm/interpret/native_dispatch.rs +42 -21
- data/vendor/cfsolve/src/vm/interpret/pattern.rs +272 -100
- data/vendor/cfsolve/src/vm/interpret/register_helpers.rs +259 -36
- data/vendor/cfsolve/src/vm/interpret/run.rs +0 -4
- data/vendor/cfsolve/src/vm/machine.rs +14 -70
- data/vendor/cfsolve/src/vm/output.rs +3 -5
- data/vendor/cfsolve/src/vm/registers.rs +26 -46
- data/vendor/cfsolve/src/vm/session.rs +11 -9
- data/vendor/cfsolve/src/vm.rs +1 -1
- metadata +6 -12
- data/lib/cfsolve/profiles/chrome-157-macos/answers.json +0 -4777
- data/lib/cfsolve/profiles/chrome-157-macos.json +0 -613
- data/vendor/cfsolve/profiles/chrome-157-macos/answers.json +0 -4777
- data/vendor/cfsolve/profiles/chrome-157-macos.json +0 -613
- data/vendor/cfsolve/src/stage2/fixtures/encoded_benchmark_worker.js +0 -1
- data/vendor/cfsolve/src/stage2/probes/parent_info_array.rs +0 -326
- data/vendor/cfsolve/src/stage2/value_labels.rs +0 -185
- data/vendor/cfsolve/src/vm/encoded_names.rs +0 -635
- data/vendor/cfsolve/src/vm/fixtures/free_functions/README.md +0 -9
- data/vendor/cfsolve/src/vm/fixtures/free_functions/machine.js +0 -7438
- data/vendor/cfsolve/src/vm/fixtures/free_functions/program.base64 +0 -1
|
@@ -1,29 +1,28 @@
|
|
|
1
1
|
//! Reconstructs `new Error()` stacks from the original script: the checkbox click's, the stack
|
|
2
2
|
//! probe's, and the challenge page's render call.
|
|
3
3
|
//!
|
|
4
|
-
//! The chain runs from the listener through the VM's runner and
|
|
4
|
+
//! The chain runs from the listener through the VM's runner and run function to the opcode
|
|
5
5
|
//! handler that constructs the error. Some builds use the runner directly as listener; others
|
|
6
6
|
//! add an arity-specific wrapper. Forwarders, helper-object methods, and selector-based
|
|
7
7
|
//! `Reflect.apply` helpers add their own frames. Recovery decodes only the string table,
|
|
8
8
|
//! preserving original positions and method keys.
|
|
9
9
|
//!
|
|
10
|
-
//! V8 names frames by call form, such as `Class.
|
|
10
|
+
//! V8 names frames by call form, such as `Class.handler` or `HTMLInputElement.<anonymous>`, and
|
|
11
11
|
//! uses callee positions. JavaScriptCore names them by definition, leaving many methods
|
|
12
12
|
//! anonymous, uses the opening argument parenthesis, and includes native `Reflect.construct`
|
|
13
13
|
//! frames. The script is sloppy code, so JavaScriptCore does not eliminate tail calls.
|
|
14
14
|
|
|
15
15
|
use std::collections::{HashMap, HashSet};
|
|
16
|
-
use std::mem;
|
|
17
16
|
use std::sync::Arc;
|
|
18
17
|
|
|
19
18
|
use anyhow::{Context, Result, bail};
|
|
20
19
|
use oxc::ast::AstKind;
|
|
21
20
|
use oxc::ast::ast::{
|
|
22
21
|
Argument, ArrayExpression, ArrayExpressionElement, AssignmentExpression, AssignmentTarget,
|
|
23
|
-
BindingPattern, CallExpression, Class,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
BindingPattern, CallExpression, Class, Expression, FormalParameter, Function, MemberExpression,
|
|
23
|
+
MethodDefinition, NewExpression, ObjectExpression, ObjectProperty, ObjectPropertyKind,
|
|
24
|
+
PropertyKind, Statement, SwitchCase, SwitchStatement, ThrowStatement, TryStatement,
|
|
25
|
+
VariableDeclarator,
|
|
27
26
|
};
|
|
28
27
|
use oxc::ast_visit::{Visit, walk};
|
|
29
28
|
use oxc::semantic::SymbolId;
|
|
@@ -238,12 +237,12 @@ pub fn recover_method_call(
|
|
|
238
237
|
})
|
|
239
238
|
}
|
|
240
239
|
|
|
241
|
-
/// Dispatch loop called by
|
|
240
|
+
/// Dispatch loop called by the run function, with details used to check stack recovery offline.
|
|
242
241
|
#[derive(Debug, Serialize)]
|
|
243
242
|
pub struct DispatchSummary {
|
|
244
243
|
pub function: String,
|
|
245
244
|
pub guarded: bool,
|
|
246
|
-
/// V8 frames from the dispatch loop to
|
|
245
|
+
/// V8 frames from the dispatch loop to the run function, innermost first.
|
|
247
246
|
pub frames: Vec<String>,
|
|
248
247
|
/// Number of opcodes dispatched by the loop.
|
|
249
248
|
pub opcodes: usize,
|
|
@@ -251,7 +250,8 @@ pub struct DispatchSummary {
|
|
|
251
250
|
pub unresolved: Vec<u8>,
|
|
252
251
|
}
|
|
253
252
|
|
|
254
|
-
/// Summarizes dispatch loops called by
|
|
253
|
+
/// Summarizes dispatch loops called by the run function, including whether each case resolves to
|
|
254
|
+
/// a function.
|
|
255
255
|
pub fn dispatch_summary(source: &str, cases: &CaseOpcodes) -> Result<Vec<DispatchSummary>> {
|
|
256
256
|
let decoded = deobfuscate::decode(source.to_owned())?;
|
|
257
257
|
let machine = Machine::new(decoded.script(), cases)?;
|
|
@@ -277,7 +277,7 @@ pub fn dispatch_summary(source: &str, cases: &CaseOpcodes) -> Result<Vec<Dispatc
|
|
|
277
277
|
unresolved.push(opcode);
|
|
278
278
|
}
|
|
279
279
|
}
|
|
280
|
-
let frames = dispatch_loop.call.frames(
|
|
280
|
+
let frames = dispatch_loop.call.frames(machine.run.frame());
|
|
281
281
|
summaries.push(DispatchSummary {
|
|
282
282
|
function: name.to_owned(),
|
|
283
283
|
guarded: dispatch_loop.guarded,
|
|
@@ -399,68 +399,48 @@ impl Callee<'_> {
|
|
|
399
399
|
}
|
|
400
400
|
}
|
|
401
401
|
|
|
402
|
-
/// The VM
|
|
403
|
-
/// parameter count.
|
|
402
|
+
/// The VM's run function, the closure runner that calls it, and listeners that wrap the closure
|
|
403
|
+
/// runner for each closure parameter count.
|
|
404
404
|
struct Machine<'a, 'cases> {
|
|
405
405
|
script: Script<'a>,
|
|
406
|
-
|
|
407
|
-
/// The dispatch loops
|
|
406
|
+
run: RunFunction<'a>,
|
|
407
|
+
/// The dispatch loops the run function calls.
|
|
408
408
|
loops: Vec<DispatchLoop<'a>>,
|
|
409
409
|
cases: &'cases CaseOpcodes,
|
|
410
|
+
/// Frames from the call to the run function out to the closure runner, innermost first.
|
|
410
411
|
runner: Vec<Call>,
|
|
411
412
|
listeners: Vec<Invocation<'a>>,
|
|
412
413
|
}
|
|
413
414
|
|
|
414
415
|
impl<'a, 'cases> Machine<'a, 'cases> {
|
|
415
416
|
fn new(js: ast::Script<'a>, cases: &'cases CaseOpcodes) -> Result<Self> {
|
|
416
|
-
let
|
|
417
|
-
let
|
|
418
|
-
// The runner passes the closure's program counter, key, and
|
|
419
|
-
// copying arguments when required by the VM function.
|
|
417
|
+
let script = Script::new(js);
|
|
418
|
+
let run = RunFunction::installed(&script)?;
|
|
419
|
+
// The closure runner passes the machine and the closure's program counter, key, and
|
|
420
|
+
// arguments to the run function, copying arguments when required by the VM function.
|
|
420
421
|
let mut runners = Vec::new();
|
|
421
422
|
for (name, function) in &script.functions {
|
|
422
|
-
let
|
|
423
|
+
let caller = Callee::Binding(*name);
|
|
424
|
+
let caller_name = script.appliers.name(caller);
|
|
425
|
+
let scope = Scope::new(caller_name, function, &script.appliers);
|
|
423
426
|
for call in calls_of(js, function) {
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
+
let Ok(target) = scope.target(call) else {
|
|
428
|
+
continue;
|
|
429
|
+
};
|
|
430
|
+
if target.callee == Some(run.name)
|
|
431
|
+
&& let Some(handed) = handed(Arguments::of(call), &target)
|
|
432
|
+
&& let [_, _, _, Operand::Expression(cells)] = handed[..]
|
|
433
|
+
&& matches!(
|
|
434
|
+
cells.without_parentheses(),
|
|
427
435
|
Expression::ConditionalExpression(_)
|
|
428
436
|
)
|
|
429
|
-
}) {
|
|
430
|
-
continue;
|
|
431
|
-
}
|
|
432
|
-
let calls_run = member(&call.callee)
|
|
433
|
-
.is_some_and(|member| ast::member_str(member) == Some("run"));
|
|
434
|
-
if calls_run && call.arguments.len() == 3 {
|
|
435
|
-
runners.push((*name, Invocation::at(Site::new(call)), None));
|
|
436
|
-
} else if let Ok(invocation) = scope.target(call)
|
|
437
|
-
&& invocation.passed == Some(4)
|
|
438
|
-
&& let Some(callee) = invocation.callee
|
|
439
|
-
&& let Ok(run) = script.function(callee)
|
|
440
|
-
&& let Some(machine) =
|
|
441
|
-
run.parameters().and_then(|params| params.first().copied())
|
|
442
437
|
{
|
|
443
|
-
|
|
444
|
-
if !loops.is_empty() {
|
|
445
|
-
runners.push((
|
|
446
|
-
*name,
|
|
447
|
-
invocation,
|
|
448
|
-
Some((
|
|
449
|
-
DispatchMethod {
|
|
450
|
-
class: None,
|
|
451
|
-
run,
|
|
452
|
-
frame: Name::same(script.appliers.name(callee).to_owned()),
|
|
453
|
-
},
|
|
454
|
-
loops,
|
|
455
|
-
)),
|
|
456
|
-
));
|
|
457
|
-
}
|
|
438
|
+
runners.push((caller, target.frames(Name::same(caller_name.to_owned()))));
|
|
458
439
|
}
|
|
459
440
|
}
|
|
460
441
|
}
|
|
461
|
-
let (runner,
|
|
442
|
+
let (runner, runner_frames) =
|
|
462
443
|
candidates::exactly_one(runners, "closure-to-VM runner call")?;
|
|
463
|
-
let runner = Callee::Binding(runner);
|
|
464
444
|
let mut listeners = Listeners {
|
|
465
445
|
runner,
|
|
466
446
|
appliers: &script.appliers,
|
|
@@ -468,20 +448,14 @@ impl<'a, 'cases> Machine<'a, 'cases> {
|
|
|
468
448
|
};
|
|
469
449
|
listeners.visit_program(js.program);
|
|
470
450
|
let listeners: Vec<Invocation> = listeners.found.into_iter().collect::<Result<_>>()?;
|
|
471
|
-
let
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
let loops = dispatch_loops(&script, &dispatch.run, None);
|
|
476
|
-
(!loops.is_empty()).then_some((dispatch, loops))
|
|
477
|
-
});
|
|
478
|
-
candidates::exactly_one(dispatches, "VM dispatch method")?
|
|
479
|
-
}
|
|
480
|
-
};
|
|
451
|
+
let loops = dispatch_loops(&script, &run);
|
|
452
|
+
if loops.is_empty() {
|
|
453
|
+
bail!("the run function calls no dispatch loop");
|
|
454
|
+
}
|
|
481
455
|
Ok(Self {
|
|
482
|
-
runner:
|
|
456
|
+
runner: runner_frames,
|
|
483
457
|
listeners,
|
|
484
|
-
|
|
458
|
+
run,
|
|
485
459
|
loops,
|
|
486
460
|
script,
|
|
487
461
|
cases,
|
|
@@ -510,12 +484,7 @@ impl<'a, 'cases> Machine<'a, 'cases> {
|
|
|
510
484
|
let name = appliers.name(handler);
|
|
511
485
|
let frame = Name {
|
|
512
486
|
v8: if dispatch.method {
|
|
513
|
-
format!(
|
|
514
|
-
"{}.{name}",
|
|
515
|
-
self.dispatch
|
|
516
|
-
.class
|
|
517
|
-
.context("missing VM class for method handler")?
|
|
518
|
-
)
|
|
487
|
+
format!("{}.{name}", self.run.class)
|
|
519
488
|
} else {
|
|
520
489
|
name.to_owned()
|
|
521
490
|
},
|
|
@@ -531,12 +500,13 @@ impl<'a, 'cases> Machine<'a, 'cases> {
|
|
|
531
500
|
bail!("no dispatch of opcode {opcode} (guarded: {guarded})")
|
|
532
501
|
}
|
|
533
502
|
|
|
534
|
-
/// Appends frames from the call to `handler` through the dispatch loop,
|
|
503
|
+
/// Appends frames from the call to `handler` through the dispatch loop, the run function, and
|
|
504
|
+
/// the closure runner.
|
|
535
505
|
fn entered(&self, handler: &Handler<'a, '_>, mut frames: Vec<Call>) -> Vec<Call> {
|
|
536
506
|
let dispatch_loop = handler.dispatch_loop;
|
|
537
507
|
let name = self.script.appliers.name(dispatch_loop.name);
|
|
538
508
|
frames.extend(handler.dispatch.frames(Name::same(name.to_owned())));
|
|
539
|
-
frames.extend(dispatch_loop.call.frames(self.
|
|
509
|
+
frames.extend(dispatch_loop.call.frames(self.run.frame()));
|
|
540
510
|
frames.extend(self.runner.iter().cloned());
|
|
541
511
|
frames
|
|
542
512
|
}
|
|
@@ -649,7 +619,7 @@ impl<'a, 'cases> Machine<'a, 'cases> {
|
|
|
649
619
|
}
|
|
650
620
|
}
|
|
651
621
|
|
|
652
|
-
/// The function
|
|
622
|
+
/// The function a dispatch loop calls for an opcode.
|
|
653
623
|
struct Handler<'a, 'machine> {
|
|
654
624
|
/// Call from the dispatch loop to this handler.
|
|
655
625
|
dispatch: Invocation<'a>,
|
|
@@ -658,10 +628,10 @@ struct Handler<'a, 'machine> {
|
|
|
658
628
|
dispatch_loop: &'machine DispatchLoop<'a>,
|
|
659
629
|
}
|
|
660
630
|
|
|
661
|
-
/// Function called by
|
|
631
|
+
/// Function called by the run function with the machine to dispatch opcodes.
|
|
662
632
|
struct DispatchLoop<'a> {
|
|
663
633
|
name: Callee<'a>,
|
|
664
|
-
/// Call from
|
|
634
|
+
/// Call from the run function, either direct or through a helper.
|
|
665
635
|
call: Invocation<'a>,
|
|
666
636
|
/// Whether the call occurs in a `try` block that handles exceptions.
|
|
667
637
|
guarded: bool,
|
|
@@ -671,17 +641,13 @@ struct DispatchLoop<'a> {
|
|
|
671
641
|
/// helper switch.
|
|
672
642
|
const DISPATCH_CASES: usize = 40;
|
|
673
643
|
|
|
674
|
-
/// Finds functions with dispatch switches that
|
|
675
|
-
fn dispatch_loops<'a>(
|
|
676
|
-
script: &Script<'a>,
|
|
677
|
-
run: &Body<'a>,
|
|
678
|
-
machine: Option<SymbolId>,
|
|
679
|
-
) -> Vec<DispatchLoop<'a>> {
|
|
644
|
+
/// Finds functions with dispatch switches that the run function calls with the machine.
|
|
645
|
+
fn dispatch_loops<'a>(script: &Script<'a>, run: &RunFunction<'a>) -> Vec<DispatchLoop<'a>> {
|
|
680
646
|
struct Calls<'a> {
|
|
681
647
|
js: ast::Script<'a>,
|
|
648
|
+
machine: SymbolId,
|
|
682
649
|
found: Vec<(&'a CallExpression<'a>, bool)>,
|
|
683
650
|
guarded: bool,
|
|
684
|
-
machine: Option<SymbolId>,
|
|
685
651
|
}
|
|
686
652
|
impl<'a> Visit<'a> for Calls<'a> {
|
|
687
653
|
fn visit_function(&mut self, _: &Function<'a>, _: ScopeFlags) {}
|
|
@@ -705,13 +671,8 @@ fn dispatch_loops<'a>(
|
|
|
705
671
|
fn visit_call_expression(&mut self, call: &CallExpression<'a>) {
|
|
706
672
|
if !is_optional_call(call)
|
|
707
673
|
&& call.arguments.iter().any(|argument| {
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
Expression::Identifier(reference) => {
|
|
711
|
-
self.machine.is_some() && self.js.symbol(reference) == self.machine
|
|
712
|
-
}
|
|
713
|
-
_ => false,
|
|
714
|
-
}
|
|
674
|
+
self.js
|
|
675
|
+
.refers_to(ast::argument_expression(argument), self.machine)
|
|
715
676
|
})
|
|
716
677
|
{
|
|
717
678
|
let call = self.js.lend(call, AstKind::as_call_expression);
|
|
@@ -722,12 +683,12 @@ fn dispatch_loops<'a>(
|
|
|
722
683
|
}
|
|
723
684
|
let mut calls = Calls {
|
|
724
685
|
js: script.js(),
|
|
686
|
+
machine: run.machine,
|
|
725
687
|
found: Vec::new(),
|
|
726
688
|
guarded: false,
|
|
727
|
-
machine,
|
|
728
689
|
};
|
|
729
|
-
run.visit(&mut calls);
|
|
730
|
-
let scope = Scope::new(
|
|
690
|
+
run.body.visit(&mut calls);
|
|
691
|
+
let scope = Scope::new(run.frame_name, &run.body, &script.appliers);
|
|
731
692
|
calls
|
|
732
693
|
.found
|
|
733
694
|
.into_iter()
|
|
@@ -811,11 +772,10 @@ impl<'a> Body<'a> {
|
|
|
811
772
|
}
|
|
812
773
|
}
|
|
813
774
|
|
|
814
|
-
/// The script's declared and assigned functions,
|
|
815
|
-
/// `Reflect.
|
|
775
|
+
/// The script's declared and assigned functions, and aliases for `Reflect.construct` and
|
|
776
|
+
/// `Reflect.apply`.
|
|
816
777
|
struct Script<'a> {
|
|
817
778
|
functions: Vec<(Binding<'a>, Body<'a>)>,
|
|
818
|
-
classes: Vec<DispatchMethod<'a>>,
|
|
819
779
|
constructs: Vec<Callee<'a>>,
|
|
820
780
|
/// Array slots containing `Reflect.construct`, indexed by array and slot.
|
|
821
781
|
construct_slots: Vec<(Binding<'a>, usize)>,
|
|
@@ -1031,11 +991,70 @@ fn number<'a>(expression: &Expression<'a>, constants: &Constants<'a>) -> Option<
|
|
|
1031
991
|
})
|
|
1032
992
|
}
|
|
1033
993
|
|
|
1034
|
-
/// The VM
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
994
|
+
/// The VM's run function, which calls the dispatch loops. The program runner installs it as
|
|
995
|
+
/// `<global>.<name> = function(p) { return <run>(new <Class>(p), pc, key, []); }`, possibly
|
|
996
|
+
/// calling it through a helper.
|
|
997
|
+
struct RunFunction<'a> {
|
|
998
|
+
/// The VM class, which V8 names handlers called as methods of the machine after.
|
|
999
|
+
class: &'a str,
|
|
1000
|
+
name: Callee<'a>,
|
|
1001
|
+
/// The run function's name in the source, which both engines name its frame after.
|
|
1002
|
+
frame_name: &'a str,
|
|
1003
|
+
body: Body<'a>,
|
|
1004
|
+
/// The parameter receiving the machine.
|
|
1005
|
+
machine: SymbolId,
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
impl<'a> RunFunction<'a> {
|
|
1009
|
+
/// Finds the run function from the one program runner.
|
|
1010
|
+
fn installed(script: &Script<'a>) -> Result<Self> {
|
|
1011
|
+
let js = script.js();
|
|
1012
|
+
let mut found = Vec::new();
|
|
1013
|
+
for function in js.functions() {
|
|
1014
|
+
let body = Body::of(function);
|
|
1015
|
+
let Some(Statement::ReturnStatement(returned)) = body.only_statement() else {
|
|
1016
|
+
continue;
|
|
1017
|
+
};
|
|
1018
|
+
let Some(Expression::CallExpression(call)) = &returned.argument else {
|
|
1019
|
+
continue;
|
|
1020
|
+
};
|
|
1021
|
+
let Ok(target) = Scope::new("", &body, &script.appliers).target(call) else {
|
|
1022
|
+
continue;
|
|
1023
|
+
};
|
|
1024
|
+
if let Some(name) = target.callee
|
|
1025
|
+
&& let Some(handed) = handed(Arguments::of(call), &target)
|
|
1026
|
+
&& let [
|
|
1027
|
+
Operand::Expression(machine),
|
|
1028
|
+
_,
|
|
1029
|
+
_,
|
|
1030
|
+
Operand::Expression(cells),
|
|
1031
|
+
] = handed[..]
|
|
1032
|
+
&& let Expression::NewExpression(machine) = machine.without_parentheses()
|
|
1033
|
+
&& let Expression::Identifier(class) = &machine.callee
|
|
1034
|
+
&& array(cells).is_some_and(|cells| cells.elements.is_empty())
|
|
1035
|
+
{
|
|
1036
|
+
found.push((name, class.name.as_str()));
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
let (name, class) = candidates::exactly_one(found, "program runner")?;
|
|
1040
|
+
let body = script.function(name)?;
|
|
1041
|
+
let machine = body
|
|
1042
|
+
.parameters()
|
|
1043
|
+
.and_then(|parameters| parameters.first().copied())
|
|
1044
|
+
.context("the run function takes no machine")?;
|
|
1045
|
+
Ok(Self {
|
|
1046
|
+
class,
|
|
1047
|
+
name,
|
|
1048
|
+
frame_name: script.appliers.name(name),
|
|
1049
|
+
body,
|
|
1050
|
+
machine,
|
|
1051
|
+
})
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
/// The run function's frame.
|
|
1055
|
+
fn frame(&self) -> Name {
|
|
1056
|
+
Name::same(self.frame_name.to_owned())
|
|
1057
|
+
}
|
|
1039
1058
|
}
|
|
1040
1059
|
|
|
1041
1060
|
impl<'a> Script<'a> {
|
|
@@ -1050,7 +1069,6 @@ impl<'a> Script<'a> {
|
|
|
1050
1069
|
let appliers = Appliers::new(js, &functions);
|
|
1051
1070
|
let mut script = Self {
|
|
1052
1071
|
functions,
|
|
1053
|
-
classes: Vec::new(),
|
|
1054
1072
|
constructs: Vec::new(),
|
|
1055
1073
|
construct_slots: Vec::new(),
|
|
1056
1074
|
applies: Vec::new(),
|
|
@@ -1058,10 +1076,8 @@ impl<'a> Script<'a> {
|
|
|
1058
1076
|
appliers,
|
|
1059
1077
|
};
|
|
1060
1078
|
for node in js.nodes.iter() {
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
AstKind::Class(class) if class.is_declaration() => script.class(class),
|
|
1064
|
-
_ => {}
|
|
1079
|
+
if let AstKind::AssignmentExpression(assignment) = node.kind() {
|
|
1080
|
+
script.assignment(assignment);
|
|
1065
1081
|
}
|
|
1066
1082
|
}
|
|
1067
1083
|
script
|
|
@@ -1106,33 +1122,6 @@ impl<'a> Script<'a> {
|
|
|
1106
1122
|
}
|
|
1107
1123
|
}
|
|
1108
1124
|
|
|
1109
|
-
/// Records a class declaration's `run` method.
|
|
1110
|
-
fn class(&mut self, class: &'a Class<'a>) {
|
|
1111
|
-
let run = class.body.body.iter().find_map(|element| match element {
|
|
1112
|
-
ClassElement::MethodDefinition(method)
|
|
1113
|
-
if method.kind != MethodDefinitionKind::Constructor
|
|
1114
|
-
&& ast::static_key_str(&method.key, method.computed) == Some("run") =>
|
|
1115
|
-
{
|
|
1116
|
-
Some(method)
|
|
1117
|
-
}
|
|
1118
|
-
_ => None,
|
|
1119
|
-
});
|
|
1120
|
-
if let Some(method) = run
|
|
1121
|
-
&& let Some(name) = &class.id
|
|
1122
|
-
{
|
|
1123
|
-
self.classes.push(DispatchMethod {
|
|
1124
|
-
class: Some(name.name.as_str()),
|
|
1125
|
-
run: Body::of(&method.value),
|
|
1126
|
-
frame: Name {
|
|
1127
|
-
v8: format!("{}.run", name.name),
|
|
1128
|
-
jsc: ast::key_str(&method.key, method.computed)
|
|
1129
|
-
.unwrap_or_default()
|
|
1130
|
-
.to_owned(),
|
|
1131
|
-
},
|
|
1132
|
-
});
|
|
1133
|
-
}
|
|
1134
|
-
}
|
|
1135
|
-
|
|
1136
1125
|
fn function(&self, callee: Callee<'a>) -> Result<Body<'a>> {
|
|
1137
1126
|
match callee {
|
|
1138
1127
|
Callee::Binding(binding) => self
|
|
@@ -2025,8 +2014,9 @@ impl<'a, 's> Scope<'a, 's> {
|
|
|
2025
2014
|
}
|
|
2026
2015
|
}
|
|
2027
2016
|
|
|
2028
|
-
/// Records forwarders defined by an object literal assigned to `holder`.
|
|
2029
|
-
///
|
|
2017
|
+
/// Records forwarders defined by an object literal assigned to `holder`. V8 names functions
|
|
2018
|
+
/// by their property keys, including computed keys. JavaScriptCore leaves functions under
|
|
2019
|
+
/// computed keys anonymous: obfuscation computes them with string decoder calls.
|
|
2030
2020
|
fn literal(&mut self, holder: Holder<'a>, object: &'a ObjectExpression<'a>) {
|
|
2031
2021
|
// Matches `key: function` properties and methods, but not accessors.
|
|
2032
2022
|
for property in &object.properties {
|
|
@@ -2034,7 +2024,14 @@ impl<'a, 's> Scope<'a, 's> {
|
|
|
2034
2024
|
&& let Expression::FunctionExpression(function) = &property.value
|
|
2035
2025
|
&& property.kind == PropertyKind::Init
|
|
2036
2026
|
&& let Some(key) = ast::static_key_str(&property.key, property.computed)
|
|
2037
|
-
&& let Some(frames) = self.forwarder(
|
|
2027
|
+
&& let Some(frames) = self.forwarder(
|
|
2028
|
+
function,
|
|
2029
|
+
if property.computed {
|
|
2030
|
+
Name::anonymous(key.to_owned())
|
|
2031
|
+
} else {
|
|
2032
|
+
Name::same(key.to_owned())
|
|
2033
|
+
},
|
|
2034
|
+
)
|
|
2038
2035
|
{
|
|
2039
2036
|
let forwarder = Forwarder {
|
|
2040
2037
|
method: frames.clone(),
|
|
@@ -2263,97 +2260,6 @@ impl<'a> Visit<'a> for Listeners<'a, '_> {
|
|
|
2263
2260
|
mod tests {
|
|
2264
2261
|
use super::*;
|
|
2265
2262
|
|
|
2266
|
-
#[test]
|
|
2267
|
-
fn recovers_method_and_free_dispatch_stacks() {
|
|
2268
|
-
let cases: String = (0..=DISPATCH_CASES)
|
|
2269
|
-
.map(|opcode| format!("case {opcode}: construct(vm); break;"))
|
|
2270
|
-
.collect();
|
|
2271
|
-
for (declaration, call, v8, jsc) in [
|
|
2272
|
-
(
|
|
2273
|
-
"class Machine { run(pc,key,args) { dispatch(this); } }",
|
|
2274
|
-
"vm.run(pc,key,copy ? args.slice() : args)",
|
|
2275
|
-
"Machine.run",
|
|
2276
|
-
"run",
|
|
2277
|
-
),
|
|
2278
|
-
(
|
|
2279
|
-
"function drive(vm,pc,key,args) { dispatch(vm); }",
|
|
2280
|
-
"drive(vm,pc,key,copy ? args.slice() : args)",
|
|
2281
|
-
"drive",
|
|
2282
|
-
"drive",
|
|
2283
|
-
),
|
|
2284
|
-
] {
|
|
2285
|
-
let source = format!(
|
|
2286
|
-
"function construct(vm) {{ return new Error(); }}\n\
|
|
2287
|
-
function dispatch(vm) {{ switch(opcode) {{ {cases} }} }}\n\
|
|
2288
|
-
{declaration}\n\
|
|
2289
|
-
function resume(vm,pc,key,args,copy) {{ return {call}; }}"
|
|
2290
|
-
);
|
|
2291
|
-
let (_, opcodes) = deobfuscate_with_cases(source.clone()).unwrap();
|
|
2292
|
-
let result =
|
|
2293
|
-
geometry(&source, &opcodes, |machine| machine.construction(1, false)).unwrap();
|
|
2294
|
-
assert_eq!(
|
|
2295
|
-
result
|
|
2296
|
-
.frames
|
|
2297
|
-
.iter()
|
|
2298
|
-
.map(|frame| frame.function.as_str())
|
|
2299
|
-
.collect::<Vec<_>>(),
|
|
2300
|
-
["construct", "dispatch", v8, "resume"]
|
|
2301
|
-
);
|
|
2302
|
-
assert_eq!(
|
|
2303
|
-
result
|
|
2304
|
-
.jsc_frames
|
|
2305
|
-
.iter()
|
|
2306
|
-
.map(|frame| frame.function.as_str())
|
|
2307
|
-
.collect::<Vec<_>>(),
|
|
2308
|
-
["construct", "dispatch", jsc, "resume"]
|
|
2309
|
-
);
|
|
2310
|
-
for frames in [&result.frames, &result.jsc_frames] {
|
|
2311
|
-
assert_eq!(
|
|
2312
|
-
frames.iter().map(|frame| frame.line).collect::<Vec<_>>(),
|
|
2313
|
-
[0, 1, 2, 3]
|
|
2314
|
-
);
|
|
2315
|
-
}
|
|
2316
|
-
}
|
|
2317
|
-
}
|
|
2318
|
-
|
|
2319
|
-
#[test]
|
|
2320
|
-
fn free_dispatch_stacks_preserve_helpers_and_merged_frames() {
|
|
2321
|
-
let cases: String = (0..=DISPATCH_CASES)
|
|
2322
|
-
.map(|opcode| format!("case {opcode}: construct(vm); break;"))
|
|
2323
|
-
.collect();
|
|
2324
|
-
for (declaration, call, expected) in [
|
|
2325
|
-
(
|
|
2326
|
-
"function drive(vm,pc,key,args) { dispatch(vm); } function apply(s,f,a,b,c,d) { switch(s^5) { case 6: return f(a,b,c,d); } }",
|
|
2327
|
-
"apply(3,drive,vm,pc,key,copy ? args.slice() : args)",
|
|
2328
|
-
&["construct", "dispatch", "drive", "apply", "resume"][..],
|
|
2329
|
-
),
|
|
2330
|
-
(
|
|
2331
|
-
"function choose(s,vm,pc,key,args) { switch(s^5) { case 6: { dispatch(vm); } return; case 7: { unrelated(); } return; } }",
|
|
2332
|
-
"choose(3,vm,pc,key,copy ? args.slice() : args)",
|
|
2333
|
-
&["construct", "dispatch", "choose", "resume"][..],
|
|
2334
|
-
),
|
|
2335
|
-
] {
|
|
2336
|
-
let source = format!(
|
|
2337
|
-
"function construct(vm) {{ return new Error(); }}\n\
|
|
2338
|
-
function dispatch(vm) {{ switch(opcode) {{ {cases} }} }}\n\
|
|
2339
|
-
{declaration}\n\
|
|
2340
|
-
function resume(vm,pc,key,args,copy) {{ return {call}; }}"
|
|
2341
|
-
);
|
|
2342
|
-
let (_, opcodes) = deobfuscate_with_cases(source.clone()).unwrap();
|
|
2343
|
-
let result =
|
|
2344
|
-
geometry(&source, &opcodes, |machine| machine.construction(1, false)).unwrap();
|
|
2345
|
-
for frames in [&result.frames, &result.jsc_frames] {
|
|
2346
|
-
assert_eq!(
|
|
2347
|
-
frames
|
|
2348
|
-
.iter()
|
|
2349
|
-
.map(|frame| frame.function.as_str())
|
|
2350
|
-
.collect::<Vec<_>>(),
|
|
2351
|
-
expected
|
|
2352
|
-
);
|
|
2353
|
-
}
|
|
2354
|
-
}
|
|
2355
|
-
}
|
|
2356
|
-
|
|
2357
2263
|
/// The decoded program captured during deobfuscation equals the one decoding alone
|
|
2358
2264
|
/// produces, for a saved script named by `CFSOLVE_CAPTURE_SCRIPT`.
|
|
2359
2265
|
#[test]
|
|
@@ -11,7 +11,6 @@ use anyhow::{Context, Result, bail, ensure};
|
|
|
11
11
|
use indexmap::IndexMap;
|
|
12
12
|
use url::Url;
|
|
13
13
|
|
|
14
|
-
use crate::candidates;
|
|
15
14
|
use crate::js::{JsObject, JsValue, Options, Payload, Window, json};
|
|
16
15
|
use crate::listing::fold::DECODER_CELL;
|
|
17
16
|
use crate::listing::query::Store;
|
|
@@ -22,6 +21,7 @@ use crate::stage2::payload::{
|
|
|
22
21
|
};
|
|
23
22
|
use crate::stage2::queries;
|
|
24
23
|
use crate::vm::{Constant, Effect, Listing, Term};
|
|
24
|
+
use crate::{candidates, challenge};
|
|
25
25
|
|
|
26
26
|
/// Widget parameters passed to `turnstile.render`, excluding callbacks.
|
|
27
27
|
const RENDER_PARAMETERS: [&str; 8] = [
|
|
@@ -322,7 +322,7 @@ impl Reader<'_> {
|
|
|
322
322
|
})
|
|
323
323
|
}
|
|
324
324
|
|
|
325
|
-
/// Recovers outcome fields from callbacks. Expiry writes
|
|
325
|
+
/// Recovers outcome fields from callbacks. Expiry writes its status and clears the token;
|
|
326
326
|
/// success writes the token. Both call shared finishing functions.
|
|
327
327
|
fn outcome(&self, callbacks: &BTreeMap<String, u32>, started: &str) -> Result<SettledFields> {
|
|
328
328
|
let entry = |name: &str| {
|
|
@@ -334,9 +334,16 @@ impl Reader<'_> {
|
|
|
334
334
|
let expired: Vec<&Store> = self
|
|
335
335
|
.stores_in(self.index.function_at(entry("expired-callback")?))
|
|
336
336
|
.collect();
|
|
337
|
+
// The status is `"expired"`, or in builds with hex names, one of those names.
|
|
337
338
|
let status = expired
|
|
338
339
|
.iter()
|
|
339
|
-
.find(|store|
|
|
340
|
+
.find(|store| {
|
|
341
|
+
matches!(
|
|
342
|
+
self.origin(store.pc, &store.value),
|
|
343
|
+
Some(Expression::Literal { value: JsValue::String(text) })
|
|
344
|
+
if text == "expired" || challenge::is_hex_name(&text)
|
|
345
|
+
)
|
|
346
|
+
})
|
|
340
347
|
.context("expiry callback writes no status")?
|
|
341
348
|
.key
|
|
342
349
|
.clone();
|
|
@@ -4,7 +4,6 @@ use anyhow::{Context, Result};
|
|
|
4
4
|
use chrono::{DateTime, Local, NaiveDate, NaiveTime, TimeZone, Utc};
|
|
5
5
|
use indexmap::IndexMap;
|
|
6
6
|
use serde::{Deserialize, Serialize};
|
|
7
|
-
use std::collections::BTreeMap;
|
|
8
7
|
|
|
9
8
|
use crate::engine::Engine;
|
|
10
9
|
use crate::js::{JsObject, json};
|
|
@@ -14,30 +13,6 @@ use crate::stage2::reference;
|
|
|
14
13
|
/// The historical years the probe parses.
|
|
15
14
|
const YEARS: [i32; 5] = [999, 1060, 1937, 1945, 1989];
|
|
16
15
|
|
|
17
|
-
/// The three current-date outputs follow the last historical year. Newer programs move API
|
|
18
|
-
/// names into captured decoder cells, so reference alignment alone can mispair these fields.
|
|
19
|
-
pub(crate) fn names(
|
|
20
|
-
features: &[String],
|
|
21
|
-
writes: &[String],
|
|
22
|
-
) -> Option<BTreeMap<String, &'static str>> {
|
|
23
|
-
let year = features.iter().position(|text| text == "1989")?;
|
|
24
|
-
let [today, offset, method, zone] = features.get(year + 1..year + 5)? else {
|
|
25
|
-
return None;
|
|
26
|
-
};
|
|
27
|
-
if method != "getTimezoneOffset"
|
|
28
|
-
|| ![today, offset, zone]
|
|
29
|
-
.iter()
|
|
30
|
-
.all(|field| writes.contains(field))
|
|
31
|
-
{
|
|
32
|
-
return None;
|
|
33
|
-
}
|
|
34
|
-
Some(BTreeMap::from([
|
|
35
|
-
(today.clone(), reference::DATES_TODAY),
|
|
36
|
-
(offset.clone(), reference::DATES_OFFSET),
|
|
37
|
-
(zone.clone(), reference::DATES_ZONE),
|
|
38
|
-
]))
|
|
39
|
-
}
|
|
40
|
-
|
|
41
16
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
42
17
|
pub struct Fields {
|
|
43
18
|
/// The field reporting each historical year's parsing difference, in program order.
|
|
@@ -130,25 +105,6 @@ mod tests {
|
|
|
130
105
|
use super::*;
|
|
131
106
|
use crate::js::{self, JsValue};
|
|
132
107
|
|
|
133
|
-
#[test]
|
|
134
|
-
fn pins_date_outputs_after_the_historical_years() {
|
|
135
|
-
let features = [
|
|
136
|
-
"last_year",
|
|
137
|
-
"1989",
|
|
138
|
-
"today",
|
|
139
|
-
"offset",
|
|
140
|
-
"getTimezoneOffset",
|
|
141
|
-
"zone",
|
|
142
|
-
]
|
|
143
|
-
.map(str::to_owned);
|
|
144
|
-
let writes = ["last_year", "today", "offset", "zone"].map(str::to_owned);
|
|
145
|
-
let names = names(&features, &writes).unwrap();
|
|
146
|
-
assert_eq!(names["today"], reference::DATES_TODAY);
|
|
147
|
-
assert_eq!(names["offset"], reference::DATES_OFFSET);
|
|
148
|
-
assert_eq!(names["zone"], reference::DATES_ZONE);
|
|
149
|
-
assert!(super::names(&features, &writes[..3]).is_none());
|
|
150
|
-
}
|
|
151
|
-
|
|
152
108
|
fn fields() -> Fields {
|
|
153
109
|
js::from_value(json!({"years": {"legacy": 999, "old": 1060, "winter": 1989}})).unwrap()
|
|
154
110
|
}
|