cfsolve 0.1.3 → 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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -0
  3. data/README.md +30 -5
  4. data/UPSTREAM.md +24 -3
  5. data/lib/cfsolve/profiles/safari-26-ios/answers.json +805 -797
  6. data/lib/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +909 -899
  7. data/lib/cfsolve/version.rb +2 -2
  8. data/lib/cfsolve.rb +21 -10
  9. data/vendor/cfsolve/examples/listing_dump.rs +24 -0
  10. data/vendor/cfsolve/examples/probe_similarity.rs +37 -0
  11. data/vendor/cfsolve/examples/rebase_reference.rs +96 -0
  12. data/vendor/cfsolve/examples/reread_capture.rs +31 -0
  13. data/vendor/cfsolve/examples/turnstile_smoke.rs +79 -0
  14. data/vendor/cfsolve/profiles/safari-26-ios/answers.json +805 -797
  15. data/vendor/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +909 -899
  16. data/vendor/cfsolve/src/capture/sessions.rs +88 -4
  17. data/vendor/cfsolve/src/capture.rs +1 -0
  18. data/vendor/cfsolve/src/deobfuscate/ast.rs +16 -0
  19. data/vendor/cfsolve/src/deobfuscate/control_flow.rs +1 -1
  20. data/vendor/cfsolve/src/deobfuscate/dead_branches.rs +33 -0
  21. data/vendor/cfsolve/src/deobfuscate/merged_functions.rs +24 -4
  22. data/vendor/cfsolve/src/embedding_page/extra_params.rs +3 -3
  23. data/vendor/cfsolve/src/embedding_page/page.rs +76 -65
  24. data/vendor/cfsolve/src/stage2/analysis/discovery.rs +7 -4
  25. data/vendor/cfsolve/src/stage2/benchmark.rs +29 -8
  26. data/vendor/cfsolve/src/stage2/call_stack.rs +134 -95
  27. data/vendor/cfsolve/src/stage2/challenge_page.rs +10 -3
  28. data/vendor/cfsolve/src/stage2/probes/parent_info.rs +345 -61
  29. data/vendor/cfsolve/src/stage2/probes/performance.rs +42 -5
  30. data/vendor/cfsolve/src/stage2/probes/stack.rs +41 -0
  31. data/vendor/cfsolve/src/stage2/probes/websocket_echo.rs +11 -2
  32. data/vendor/cfsolve/src/stage2/reference.json +1 -1
  33. data/vendor/cfsolve/src/stage2/reference.rs +70 -110
  34. data/vendor/cfsolve/src/stage2/registry.rs +4 -3
  35. data/vendor/cfsolve/src/stage2/trace.rs +1 -1
  36. data/vendor/cfsolve/src/vm/dataflow.rs +15 -6
  37. data/vendor/cfsolve/src/vm/disassemble.rs +2 -1
  38. data/vendor/cfsolve/src/vm/encoded_strings.rs +1553 -0
  39. data/vendor/cfsolve/src/vm/extract.rs +205 -152
  40. data/vendor/cfsolve/src/vm/interpret/access.rs +0 -2
  41. data/vendor/cfsolve/src/vm/interpret/builtin.rs +0 -13
  42. data/vendor/cfsolve/src/vm/interpret/intrinsic.rs +30 -7
  43. data/vendor/cfsolve/src/vm/interpret/mix32.rs +216 -86
  44. data/vendor/cfsolve/src/vm/interpret/native_dispatch.rs +42 -21
  45. data/vendor/cfsolve/src/vm/interpret/pattern.rs +268 -67
  46. data/vendor/cfsolve/src/vm/interpret/register_helpers.rs +261 -30
  47. data/vendor/cfsolve/src/vm/machine.rs +14 -12
  48. data/vendor/cfsolve/src/vm/output.rs +4 -1
  49. data/vendor/cfsolve/src/vm/registers.rs +17 -23
  50. data/vendor/cfsolve/src/vm/session.rs +9 -4
  51. data/vendor/cfsolve/src/vm.rs +1 -0
  52. metadata +7 -1
@@ -8,21 +8,21 @@
8
8
  //! Each session directory holds the original and rewritten HTML, the full log `session.json`,
9
9
  //! and `capture.json`, which `cargo run --example build_profile` turns into `answers.json`.
10
10
 
11
- use std::fs;
12
11
  use std::path::{Path, PathBuf};
13
12
  use std::sync::{Arc, Mutex, MutexGuard};
13
+ use std::{fs, str};
14
14
 
15
- use anyhow::{Context, Result};
15
+ use anyhow::{Context, Result, ensure};
16
16
  use num_bigint::BigUint;
17
17
  use oxc::ast::ast::Program;
18
18
  use oxc::ast_visit::Visit;
19
19
  use serde::{Serialize, Serializer};
20
- use serde_json::Value;
20
+ use serde_json::{Value, json};
21
21
  use tokio::task::JoinSet;
22
22
  use url::Url;
23
23
 
24
24
  use super::rsa::{ProxyKey, Rewrapped};
25
- use crate::challenge::codec::{Codec, KEY_LENGTH};
25
+ use crate::challenge::codec::{Codec, Envelope, KEY_LENGTH, SEED_LENGTH};
26
26
  use crate::challenge::program::{self, PreparedSession};
27
27
  use crate::deobfuscate::Encoded;
28
28
  use crate::js::ast;
@@ -355,3 +355,87 @@ pub(super) fn scan_alphabet(program: &Program<'_>, strings: &[Encoded]) -> Resul
355
355
  candidates.extend(strings.iter().map(|string| string.decoded.clone()));
356
356
  program::base64_alphabet(candidates)
357
357
  }
358
+
359
+ /// Reads the payloads of a session the proxy logged without them, as when extraction failed
360
+ /// during capture, using each POST's logged seed and the parameters extraction recovers from
361
+ /// `original.html`. Writes the completed session to `out_dir` and returns how many payloads were
362
+ /// read.
363
+ pub fn reread(session_dir: &Path, out_dir: &Path) -> Result<usize> {
364
+ let read = |name: &str| {
365
+ let path = session_dir.join(name);
366
+ fs::read_to_string(&path).with_context(|| format!("reading {}", path.display()))
367
+ };
368
+ let original = read("original.html")?;
369
+ let mut log: Value = serde_json::from_str(&read("session.json")?)?;
370
+ let mut capture: Value = serde_json::from_str(&read("capture.json")?)?;
371
+ let codec = extract_codec(&original)?;
372
+ let logged_modulus = log["realModulus"]
373
+ .as_str()
374
+ .context("the log has no real modulus")?;
375
+ ensure!(
376
+ format!("{:x}", codec.modulus) == logged_modulus,
377
+ "extraction finds another modulus than the one the proxy swapped"
378
+ );
379
+ let engine = challenge::codec::engine(&codec.alphabet_text)?;
380
+ let mut payloads = Vec::new();
381
+ let mut notes = Vec::new();
382
+ for post in log["posts"]
383
+ .as_array_mut()
384
+ .context("the log has no posts")?
385
+ {
386
+ let url = post["url"].as_str().unwrap_or_default().to_owned();
387
+ let read_post = || -> Result<Value> {
388
+ let seed = post["seed"]
389
+ .as_str()
390
+ .context("the POST has no logged seed")?;
391
+ let body = post["encryptedBody"]
392
+ .as_str()
393
+ .context("the POST has no body")?;
394
+ Envelope::decode(body, &engine)?.payload(&seed_bytes(seed)?, &codec.secret)
395
+ };
396
+ match read_post() {
397
+ Ok(payload) => {
398
+ post["payload"] = payload.clone();
399
+ payloads.push(json!({"url": url, "payload": payload}));
400
+ }
401
+ Err(error) => notes.push(format!("POST {url}: {error:#}")),
402
+ }
403
+ }
404
+ let count = payloads.len();
405
+ log["alphabet"] = Value::from(codec.alphabet_text);
406
+ log["secret"] = Value::from(String::from_iter(codec.secret.map(char::from)));
407
+ log["complete"] = Value::from(notes.is_empty());
408
+ log["notes"] = Value::from(notes);
409
+ capture["payloads"] = Value::from(payloads);
410
+ fs::create_dir_all(out_dir).with_context(|| format!("creating {}", out_dir.display()))?;
411
+ for name in ["original.html", "rewritten.html"] {
412
+ let from = session_dir.join(name);
413
+ if from.exists() {
414
+ fs::copy(&from, out_dir.join(name))
415
+ .with_context(|| format!("copying {}", from.display()))?;
416
+ }
417
+ }
418
+ write_file(
419
+ &out_dir.join("session.json"),
420
+ serde_json::to_string_pretty(&log)?,
421
+ )?;
422
+ write_file(
423
+ &out_dir.join("capture.json"),
424
+ serde_json::to_string_pretty(&capture)?,
425
+ )?;
426
+ Ok(count)
427
+ }
428
+
429
+ /// Parses a seed the log holds as lowercase hex.
430
+ fn seed_bytes(hex: &str) -> Result<[u8; SEED_LENGTH]> {
431
+ ensure!(
432
+ hex.len() == 2 * SEED_LENGTH,
433
+ "a {SEED_LENGTH}-byte value needs {} hex digits",
434
+ 2 * SEED_LENGTH
435
+ );
436
+ let mut seed = [0; SEED_LENGTH];
437
+ for (byte, pair) in seed.iter_mut().zip(hex.as_bytes().chunks(2)) {
438
+ *byte = u8::from_str_radix(str::from_utf8(pair)?, 16)?;
439
+ }
440
+ Ok(seed)
441
+ }
@@ -19,4 +19,5 @@ pub use ca::CertificateAuthority;
19
19
  pub use profile::fill as fill_profile;
20
20
  pub use proxy::{CA_HOST, CA_PATH, CHALLENGE_HOST, CaptureProxy, Config};
21
21
  pub use recorded::{CapturedSession, Recording};
22
+ pub use sessions::reread as reread_session;
22
23
  pub use upstream::{Credentials, UpstreamProxy};
@@ -266,6 +266,22 @@ pub(super) enum LabeledContinue {
266
266
  Inner,
267
267
  }
268
268
 
269
+ /// Checks whether `statement` always ends with `return` or `throw`, or, with `jumps`, also with
270
+ /// `break` or `continue`. A labeled statement may be the target of its own `break`, so it never
271
+ /// counts.
272
+ pub(super) fn abrupt(statement: &Statement<'_>, jumps: bool) -> bool {
273
+ match statement {
274
+ Statement::ReturnStatement(_) | Statement::ThrowStatement(_) => true,
275
+ Statement::BreakStatement(_) | Statement::ContinueStatement(_) => jumps,
276
+ Statement::BlockStatement(block) => block.body.iter().any(|inner| abrupt(inner, jumps)),
277
+ Statement::IfStatement(branch) => {
278
+ abrupt(&branch.consequent, jumps)
279
+ && (branch.alternate.as_ref()).is_some_and(|alternate| abrupt(alternate, jumps))
280
+ }
281
+ _ => false,
282
+ }
283
+ }
284
+
269
285
  /// Detects jumps whose loop or switch target would be removed when `statements` are copied out
270
286
  /// of a case. Labeled breaks keep their explicit target, which still encloses the copy.
271
287
  pub(super) fn escapes(statements: &[Statement<'_>], labeled_continue: LabeledContinue) -> bool {
@@ -136,7 +136,7 @@ fn flatten<'a>(
136
136
  Some(Statement::ContinueStatement(jump)) if jump.label.is_none() => {
137
137
  body.pop();
138
138
  }
139
- Some(Statement::ReturnStatement(_) | Statement::ThrowStatement(_)) => {}
139
+ Some(last) if ast::abrupt(last, false) => {}
140
140
  _ => return None,
141
141
  }
142
142
  if ast::escapes(&body, LabeledContinue::Inner) || cases.insert(label, body).is_some() {
@@ -58,6 +58,18 @@ impl<'a> VisitMut<'a> for Dead<'_, 'a> {
58
58
  fn visit_statements(&mut self, statements: &mut Vec<'a, Statement<'a>>) {
59
59
  walk_mut::walk_statements(self, statements);
60
60
  let before = statements.len();
61
+ // Statements after one that always completes abruptly never run. Declarations stay:
62
+ // `var` and functions hoist, and `let`, `const`, and classes shadow outer bindings.
63
+ if let Some(end) = statements
64
+ .iter()
65
+ .position(|statement| ast::abrupt(statement, true))
66
+ {
67
+ let mut index = 0;
68
+ statements.retain(|statement| {
69
+ index += 1;
70
+ index <= end + 1 || statement.is_declaration() || declares(statement)
71
+ });
72
+ }
61
73
  statements.retain(|statement| match statement {
62
74
  Statement::EmptyStatement(_) => false,
63
75
  Statement::ExpressionStatement(statement) => match &statement.expression {
@@ -114,4 +126,25 @@ mod tests {
114
126
  );
115
127
  testing::assert_contains(&output, &["var x", "function g"]);
116
128
  }
129
+
130
+ #[test]
131
+ fn removes_statements_after_abrupt_completions() {
132
+ let transform = |source: &str| testing::rewrite(source, super::run);
133
+ // A split merged function keeps its case's trailing `return;`.
134
+ let output = transform(
135
+ "function f(r, i) { if (r.J[i] === -4) { return r.H[i]; } else { return g(r, i); } return; }",
136
+ );
137
+ testing::assert_contains(&output, &["return r.H[i]", "return g(r, i)"]);
138
+ testing::assert_lacks(&output, &["return;"]);
139
+ let output = transform(
140
+ "function f() { { throw e; } a(); var x = b(); function g() {} let y; return x + y; }",
141
+ );
142
+ testing::assert_lacks(&output, &["a()", "return x"]);
143
+ testing::assert_contains(&output, &["var x = b()", "function g", "let y"]);
144
+ // Code after a one-armed `if` or a labeled block that breaks itself still runs.
145
+ let output = transform("function f() { if (c) return; a(); l: { break l; } b(); }");
146
+ testing::assert_contains(&output, &["a()", "b()"]);
147
+ let output = transform("function f() { while (c) { if (d) break; else continue; a(); } }");
148
+ testing::assert_lacks(&output, &["a()"]);
149
+ }
117
150
  }
@@ -126,10 +126,12 @@ fn selection<'a>(function: &'a Function<'a>, analysis: &Analysis<'a>) -> Option<
126
126
  .collect::<Option<_>>()?;
127
127
  let mut declarations = vec![];
128
128
  for case in &switch.cases {
129
- if !matches!(
130
- case.consequent.last(),
131
- Some(Statement::ReturnStatement(_) | Statement::ThrowStatement(_))
132
- ) || ast::escapes(&case.consequent, LabeledContinue::Any)
129
+ // Dead branch removal drops a trailing `return;` after an `if` whose branches return.
130
+ if !case
131
+ .consequent
132
+ .last()
133
+ .is_some_and(|last| ast::abrupt(last, false))
134
+ || ast::escapes(&case.consequent, LabeledContinue::Any)
133
135
  {
134
136
  return None;
135
137
  }
@@ -395,4 +397,22 @@ mod tests {
395
397
  assert!(output.contains("switch"), "{output}");
396
398
  }
397
399
  }
400
+
401
+ #[test]
402
+ fn splits_cases_ending_in_branches_that_return() {
403
+ // Dead branch removal leaves no `return;` after the `if`.
404
+ let transform = |source: &str| testing::rewrite(source, super::run);
405
+ let output = transform(
406
+ "function m(s, a) { switch (s) { case 1: if (a) { return 1; } else { return 2; } \
407
+ case 2: throw a; } } h(m(1, p), m(2, q));",
408
+ );
409
+ testing::assert_contains(&output, &["function m_0(a) {", "h(m_0(p), m_1(q));"]);
410
+ testing::assert_lacks(&output, &["switch"]);
411
+ // A branch that breaks out of the switch falls out of the case.
412
+ let output = transform(
413
+ "function m(s, a) { switch (s) { case 1: if (a) { return 1; } else { break; } \
414
+ case 2: return; } } h(m(1, p), m(2, q));",
415
+ );
416
+ testing::assert_contains(&output, &["switch"]);
417
+ }
398
418
  }
@@ -291,17 +291,17 @@ pub(crate) struct ExtraParams<'page> {
291
291
  pub retry_interval: i64,
292
292
  /// Both sides check this before reading a message.
293
293
  pub source: &'static str,
294
+ #[serde(flatten)]
295
+ pub times: ElapsedTimes,
294
296
  pub upgrade_attempts: u32,
295
297
  pub upgrade_completed_count: u32,
296
298
  pub url: &'page str,
297
299
  #[serde(rename = "wPr")]
298
300
  pub signals: PageSignals,
299
301
  pub widget_id: &'page str,
300
- #[serde(flatten)]
301
- pub times: ElapsedTimes,
302
302
  }
303
303
 
304
- /// Elapsed loader times reported at the end of [`ExtraParams`], in milliseconds.
304
+ /// Elapsed loader times reported in [`ExtraParams`], in milliseconds.
305
305
  #[derive(Debug, Clone, Deserialize, Serialize)]
306
306
  #[serde(rename_all = "camelCase")]
307
307
  pub(crate) struct ElapsedTimes {
@@ -9,10 +9,10 @@ use ego_tree::{NodeId, NodeRef};
9
9
  use html5ever::{Attribute, QualName, ns};
10
10
  use scraper::node::Element;
11
11
  use scraper::{ElementRef, Html, Node, Selector};
12
- use serde::{Deserialize, Serialize};
12
+ use serde::{Deserialize, Serialize, Serializer};
13
13
  use url::Url;
14
14
 
15
- use crate::js::JsObject;
15
+ use crate::js::{JsObject, JsValue, json};
16
16
 
17
17
  /// Implicit render collects containers with this class.
18
18
  const CONTAINER_CLASS: &str = "cf-turnstile";
@@ -194,88 +194,107 @@ pub(crate) struct RenderedPage {
194
194
  parent: ParentInfo,
195
195
  }
196
196
 
197
- /// `wPr`: what `api.js` reports about the page around the widget. Fields serialize in the
198
- /// order `api.js` writes them, which the message keeps.
199
- #[derive(Clone, Debug, Serialize)]
197
+ /// `wPr`: what `api.js` reports about the page around the widget, sent as the array
198
+ /// [`Self::elements`].
199
+ #[derive(Clone, Debug)]
200
200
  pub(crate) struct PageSignals {
201
- #[serde(rename = "d.cT")]
202
201
  pub custom_element_tags: Vec<String>,
203
- #[serde(rename = "ht.atrs")]
204
202
  pub root_attributes: Vec<String>,
205
- #[serde(rename = "pg.ref")]
206
203
  pub referrer: String,
207
- #[serde(rename = "pi")]
208
204
  pub parent: ParentInfo,
209
- #[serde(rename = "w.iW")]
210
205
  pub inner_width: u32,
211
206
  }
212
207
 
213
- /// `pi`: the widget's place in the page. The parent info probe copies these properties by
214
- /// their `api.js` names.
215
- #[derive(Clone, Debug, Serialize)]
208
+ impl PageSignals {
209
+ /// The array `api.js` sends. Its order is that of the named fields earlier builds sent,
210
+ /// noted beside each element. `pac` and `pad` are `undefined` when `api.js` leaves them out.
211
+ #[must_use]
212
+ pub(crate) fn elements(&self) -> Vec<JsValue> {
213
+ let parent = &self.parent;
214
+ let viewport = &parent.viewport;
215
+ let optional = |value: Option<String>| value.map_or(JsValue::Undefined, JsValue::String);
216
+ vec![
217
+ json!(self.custom_element_tags), // d.cT
218
+ json!(self.root_attributes), // ht.atrs
219
+ json!(self.referrer), // pg.ref
220
+ json!(parent.form), // pi.ffp
221
+ json!(parent.in_iframe), // pi.ii
222
+ json!(parent.location), // pi.lH
223
+ json!(parent.meta), // pi.mL
224
+ optional(parent.pac.clone()), // pi.pac
225
+ optional(parent.pad.clone()), // pi.pad
226
+ json!(parent.fingerprint), // pi.pfp
227
+ json!(parent.scripts), // pi.sL
228
+ json!(parent.same_realm), // pi.sR
229
+ json!(parent.stylesheets), // pi.ssL
230
+ json!(parent.title), // pi.t
231
+ json!(parent.elements), // pi.tL
232
+ json!(parent.appearance), // pi.vDa
233
+ json!(parent.error_handled), // pi.vDeh
234
+ json!(parent.host), // pi.vDhp
235
+ json!(parent.hidden_reason), // pi.vDhr
236
+ json!(parent.implicit), // pi.vDie
237
+ json!(parent.mode), // pi.vDmd
238
+ json!(parent.mount), // pi.vDmt
239
+ json!(parent.reasons), // pi.vDrs
240
+ json!(parent.user_hidden), // pi.vDuh
241
+ json!(viewport.height), // pi.vDvp.height
242
+ json!(viewport.visibility_state), // pi.vDvp.visibilityState
243
+ json!(viewport.width), // pi.vDvp.width
244
+ json!(parent.widget_position), // pi.wp
245
+ json!(parent.xpath), // pi.xp
246
+ json!(self.inner_width), // w.iW
247
+ ]
248
+ }
249
+ }
250
+
251
+ /// Writes [`PageSignals::elements`], with `undefined` as `null`.
252
+ impl Serialize for PageSignals {
253
+ fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
254
+ JsValue::Array(self.elements()).serialize(serializer)
255
+ }
256
+ }
257
+
258
+ /// `pi`: the widget's place in the page.
259
+ #[derive(Clone, Debug)]
216
260
  pub(crate) struct ParentInfo {
217
261
  /// The enclosing form's method and controls, or `nf` if there is no form.
218
- #[serde(rename = "ffp")]
219
262
  pub form: String,
220
- #[serde(rename = "ii")]
221
263
  pub in_iframe: bool,
222
- #[serde(rename = "lH")]
223
264
  pub location: String,
224
- #[serde(rename = "mL")]
225
265
  pub meta: usize,
226
- #[serde(skip_serializing_if = "Option::is_none")]
227
266
  pub pac: Option<String>,
228
- #[serde(skip_serializing_if = "Option::is_none")]
229
267
  pub pad: Option<String>,
230
- #[serde(rename = "pfp")]
231
268
  pub fingerprint: String,
232
- #[serde(rename = "sL")]
233
269
  pub scripts: usize,
234
- #[serde(rename = "sR")]
235
270
  pub same_realm: bool,
236
- #[serde(rename = "ssL")]
237
271
  pub stylesheets: usize,
238
272
  /// The title's UTF-16 length and djb2 hash.
239
- #[serde(rename = "t")]
240
273
  pub title: String,
241
- #[serde(rename = "tL")]
242
274
  pub elements: usize,
243
- #[serde(rename = "vDa")]
244
275
  pub appearance: String,
245
- #[serde(rename = "vDeh")]
246
276
  pub error_handled: bool,
247
- #[serde(rename = "vDhp")]
248
277
  pub host: &'static str,
249
278
  /// Why the widget is hidden; `null` while it shows.
250
- #[serde(rename = "vDhr")]
251
279
  pub hidden_reason: Option<String>,
252
280
  /// Whether the loader rendered the widget from the page's markup.
253
- #[serde(rename = "vDie")]
254
281
  pub implicit: bool,
255
282
  /// The challenge mode the iframe reported.
256
- #[serde(rename = "vDmd")]
257
283
  pub mode: Option<String>,
258
- #[serde(rename = "vDmt")]
259
284
  pub mount: &'static str,
260
285
  /// Why the widget was re-rendered; empty on a first render.
261
- #[serde(rename = "vDrs")]
262
286
  pub reasons: Vec<String>,
263
- #[serde(rename = "vDuh")]
264
287
  pub user_hidden: bool,
265
- #[serde(rename = "vDvp")]
266
288
  pub viewport: Viewport,
267
289
  /// The wrapper's top and right edges.
268
- #[serde(rename = "wp")]
269
290
  pub widget_position: String,
270
- #[serde(rename = "xp")]
271
291
  pub xpath: String,
272
292
  }
273
293
 
274
294
  /// `vDvp`: the page's viewport.
275
- #[derive(Clone, Debug, Default, Serialize)]
295
+ #[derive(Clone, Debug, Default)]
276
296
  pub(crate) struct Viewport {
277
297
  pub height: u32,
278
- #[serde(rename = "visibilityState")]
279
298
  pub visibility_state: String,
280
299
  pub width: u32,
281
300
  }
@@ -987,26 +1006,15 @@ mod tests {
987
1006
  ..Layout::default()
988
1007
  };
989
1008
  let signals = embedding_page::tests::page().signals(&layout, Some("managed"));
990
- // Recorded from a real browser rendering exactly this page.
991
- let expected = json!({
992
- "d.cT": [],
993
- "ht.atrs": [],
994
- "pg.ref": "",
995
- "pi": {
996
- "ffp": "nf", "ii": false, "lH": "http://127.0.0.1:8000/index.html", "mL": 1,
997
- "pac": "n", "pad": "n|n|n|0|0|0|0|",
998
- "pfp": "htm>hea>met_ch>tit>-tscr_sr_as_de>-tbod>h1>p>-tdiv_cl_da_da_da>-t",
999
- "sL": 1, "sR": true, "ssL": 0, "t": "19|3058264090", "tL": 12,
1000
- "vDa": "always", "vDeh": false, "vDhp": "wrapper", "vDhr": null,
1001
- "vDie": true, "vDmd": "managed", "vDmt": "wrapper", "vDrs": [],
1002
- "vDuh": false,
1003
- "vDvp": {"height": 813, "visibilityState": "hidden", "width": 1280},
1004
- "wp": "113.875|1272", "xp": "/div[1]/div[1]"
1005
- },
1006
- "w.iW": 1280
1007
- });
1008
- // Field order is part of the message, so compare the serialized text.
1009
- assert_eq!(json!(signals).stringify(), expected.stringify());
1009
+ // Recorded from a real browser rendering exactly this page. Order is part of the message,
1010
+ // so compare the serialized text.
1011
+ let expected = concat!(
1012
+ r#"[[],[],"","nf",false,"http://127.0.0.1:8000/index.html",1,"n","n|n|n|0|0|0|0|","#,
1013
+ r#""htm>hea>met_ch>tit>-tscr_sr_as_de>-tbod>h1>p>-tdiv_cl_da_da_da>-t",1,true,0,"#,
1014
+ r#""19|3058264090",12,"always",false,"wrapper",null,true,"managed","wrapper",[],"#,
1015
+ r#"false,813,"hidden",1280,"113.875|1272","/div[1]/div[1]",1280]"#,
1016
+ );
1017
+ assert_eq!(json!(signals).stringify().as_deref(), Some(expected));
1010
1018
  }
1011
1019
 
1012
1020
  /// Checks `ffp`, `pac`, and `pad` for forms and nearby controls. Without a surrounding
@@ -1044,8 +1052,8 @@ mod tests {
1044
1052
  cases.push((nested(depth), if depth <= 3 { newsletter } else { none }));
1045
1053
  }
1046
1054
  for (html, expected) in cases {
1047
- let pi = &signals(&html)["pi"];
1048
- assert_eq!([&pi["ffp"], &pi["pac"], &pi["pad"]], expected, "{html}");
1055
+ let signals = signals(&html);
1056
+ assert_eq!([&signals[3], &signals[7], &signals[8]], expected, "{html}");
1049
1057
  }
1050
1058
  }
1051
1059
 
@@ -1061,9 +1069,12 @@ mod tests {
1061
1069
  json!(page.unwrap().signals(&Layout::default(), None))
1062
1070
  };
1063
1071
  let challenge = rendered("data");
1064
- assert!(challenge["pi"].get("pac").is_none());
1065
- assert!(challenge["pi"].get("pad").is_none());
1066
- assert_eq!(rendered("")["pi"]["pac"], "n");
1072
+ assert_eq!(
1073
+ [&challenge[7], &challenge[8]],
1074
+ [&JsValue::Null; 2],
1075
+ "`undefined` in place"
1076
+ );
1077
+ assert_eq!(rendered("")[7], "n");
1067
1078
  }
1068
1079
 
1069
1080
  #[test]
@@ -1143,7 +1154,7 @@ mod tests {
1143
1154
  let html = format!(
1144
1155
  r#"<html><body><div{attributes}></div><div class="cf-turnstile" data-sitekey="k"></div></body></html>"#
1145
1156
  );
1146
- let shape = signals(&html)["pi"]["pfp"].as_str().unwrap().to_owned();
1157
+ let shape = signals(&html)[9].as_str().unwrap().to_owned();
1147
1158
  assert_eq!(shape.encode_utf16().count(), FINGERPRINT_LIMIT);
1148
1159
  }
1149
1160
  }
@@ -284,10 +284,13 @@ fn collection_strings(
284
284
  }
285
285
  },
286
286
  );
287
- (
288
- without_inline_descriptors(strings.into_values().flatten()),
289
- outputs,
290
- )
287
+ // Text the program decodes with its own decoders stands for the decoded text that follows.
288
+ let encoded = &index.listing().encoded;
289
+ let strings = strings
290
+ .into_values()
291
+ .flatten()
292
+ .filter(|text| !encoded.contains(text));
293
+ (without_inline_descriptors(strings), outputs)
291
294
  }
292
295
 
293
296
  /// Drops string descriptors the program decodes in place, keeping the decoded text that follows
@@ -7,7 +7,9 @@ use std::time::Duration;
7
7
  use anyhow::{Context, Result};
8
8
  use base64::Engine as _;
9
9
  use base64::engine::general_purpose::STANDARD;
10
- use oxc::ast::ast::{CallExpression, Expression, MemberExpression, VariableDeclarator};
10
+ use oxc::ast::ast::{
11
+ CallExpression, Expression, MemberExpression, StringLiteral, VariableDeclarator,
12
+ };
11
13
  use oxc::ast_visit::{Visit, walk};
12
14
  use oxc::str::Ident;
13
15
  use serde::{Deserialize, Serialize};
@@ -166,8 +168,15 @@ pub(crate) fn recognize(probe: &ProbeView<'_>) -> Option<Fields> {
166
168
  });
167
169
  }
168
170
  }
169
- // Programs that decode strings in place also hold the source as a descriptor.
170
- let parsed = strings
171
+ // Programs that decode strings in place also hold the source as a descriptor. Some builds
172
+ // hide the worker's own strings behind a decoder in its source, which deobfuscation inlines.
173
+ let sources: Vec<String> = strings
174
+ .iter()
175
+ .filter(|text| text.contains("WebAssembly"))
176
+ .filter_map(|source| crate::deobfuscate::deobfuscate(source.clone()).ok())
177
+ .map(|deobfuscated| deobfuscated.text)
178
+ .collect();
179
+ let parsed = sources
171
180
  .iter()
172
181
  .filter(|text| text.contains("WebAssembly.instantiate"))
173
182
  .find_map(|source| ParsedScript::parse("benchmark worker", source).ok())?;
@@ -307,18 +316,30 @@ impl<'a> Worker<'a> {
307
316
  plan_fields: Vec::new(),
308
317
  }
309
318
  }
310
- }
311
319
 
312
- impl<'a> Visit<'a> for Worker<'a> {
313
- fn visit_variable_declarator(&mut self, declaration: &VariableDeclarator<'a>) {
314
- if let Some(value) = declaration.init.as_ref().and_then(ast::static_str)
315
- && let Ok(bytes) = STANDARD.decode(value)
320
+ /// Takes `text` as the module if it is a module's base64.
321
+ fn module_text(&mut self, text: &str) {
322
+ if let Ok(bytes) = STANDARD.decode(text)
316
323
  && bytes.starts_with(b"\0asm")
317
324
  {
318
325
  self.module = Some(bytes);
319
326
  }
327
+ }
328
+ }
329
+
330
+ impl<'a> Visit<'a> for Worker<'a> {
331
+ fn visit_variable_declarator(&mut self, declaration: &VariableDeclarator<'a>) {
332
+ if let Some(value) = declaration.init.as_ref().and_then(ast::static_str) {
333
+ self.module_text(value);
334
+ }
320
335
  walk::walk_variable_declarator(self, declaration);
321
336
  }
337
+ /// Deobfuscation inlines a module used once, as in `instantiate(decode("AGFzbQ..."))`.
338
+ fn visit_string_literal(&mut self, literal: &StringLiteral<'a>) {
339
+ if let Some(text) = literal.value.as_str() {
340
+ self.module_text(text);
341
+ }
342
+ }
322
343
  fn visit_member_expression(&mut self, member: &MemberExpression<'a>) {
323
344
  if let MemberExpression::StaticMemberExpression(field) = member
324
345
  && let Expression::Identifier(object) = &field.object