cfsolve 0.1.3 → 0.1.4

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 (50) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.md +8 -3
  4. data/UPSTREAM.md +8 -2
  5. data/lib/cfsolve/profiles/chrome-157-macos/answers.json +4777 -0
  6. data/lib/cfsolve/profiles/chrome-157-macos.json +613 -0
  7. data/lib/cfsolve/version.rb +2 -2
  8. data/lib/cfsolve.rb +1 -1
  9. data/vendor/cfsolve/README.md +53 -5
  10. data/vendor/cfsolve/examples/turnstile_smoke.rs +58 -0
  11. data/vendor/cfsolve/profiles/chrome-157-macos/answers.json +4777 -0
  12. data/vendor/cfsolve/profiles/chrome-157-macos.json +613 -0
  13. data/vendor/cfsolve/src/embedding_page/extra_params.rs +110 -7
  14. data/vendor/cfsolve/src/embedding_page/page.rs +41 -1
  15. data/vendor/cfsolve/src/session/initial.rs +7 -6
  16. data/vendor/cfsolve/src/session/probes.rs +1 -0
  17. data/vendor/cfsolve/src/stage2/analysis/analyze.rs +33 -4
  18. data/vendor/cfsolve/src/stage2/analysis/discovery.rs +13 -2
  19. data/vendor/cfsolve/src/stage2/benchmark.rs +59 -18
  20. data/vendor/cfsolve/src/stage2/call_stack.rs +169 -36
  21. data/vendor/cfsolve/src/stage2/fixtures/encoded_benchmark_worker.js +1 -0
  22. data/vendor/cfsolve/src/stage2/probes/dates.rs +44 -0
  23. data/vendor/cfsolve/src/stage2/probes/parent_info_array.rs +326 -0
  24. data/vendor/cfsolve/src/stage2/probes/stack.rs +117 -4
  25. data/vendor/cfsolve/src/stage2/probes/websocket_echo.rs +80 -2
  26. data/vendor/cfsolve/src/stage2/probes.rs +1 -0
  27. data/vendor/cfsolve/src/stage2/reference.rs +5 -0
  28. data/vendor/cfsolve/src/stage2/registry.rs +8 -2
  29. data/vendor/cfsolve/src/stage2/replay.rs +34 -3
  30. data/vendor/cfsolve/src/stage2/stylesheets.rs +35 -0
  31. data/vendor/cfsolve/src/stage2/value_labels.rs +185 -0
  32. data/vendor/cfsolve/src/stage2/vocabulary.rs +111 -6
  33. data/vendor/cfsolve/src/stage2.rs +1 -0
  34. data/vendor/cfsolve/src/vm/disassemble.rs +68 -4
  35. data/vendor/cfsolve/src/vm/encoded_names.rs +635 -0
  36. data/vendor/cfsolve/src/vm/extract.rs +246 -43
  37. data/vendor/cfsolve/src/vm/fixtures/free_functions/README.md +9 -0
  38. data/vendor/cfsolve/src/vm/fixtures/free_functions/machine.js +7438 -0
  39. data/vendor/cfsolve/src/vm/fixtures/free_functions/program.base64 +1 -0
  40. data/vendor/cfsolve/src/vm/interpret/access.rs +8 -2
  41. data/vendor/cfsolve/src/vm/interpret/intrinsic.rs +39 -1
  42. data/vendor/cfsolve/src/vm/interpret/pattern.rs +33 -4
  43. data/vendor/cfsolve/src/vm/interpret/register_helpers.rs +14 -6
  44. data/vendor/cfsolve/src/vm/interpret/run.rs +4 -0
  45. data/vendor/cfsolve/src/vm/machine.rs +63 -5
  46. data/vendor/cfsolve/src/vm/output.rs +6 -1
  47. data/vendor/cfsolve/src/vm/registers.rs +35 -21
  48. data/vendor/cfsolve/src/vm/session.rs +6 -3
  49. data/vendor/cfsolve/src/vm.rs +1 -0
  50. metadata +13 -1
@@ -1,17 +1,18 @@
1
1
  //! Builds the `extraParams` reply `api.js` sends to the iframe's `requestExtraParams`: widget
2
2
  //! configuration, elapsed times, loader resource timing, and the page summary.
3
3
 
4
- use anyhow::{Context, Result};
4
+ use anyhow::{Context, Result, ensure};
5
+ use oxc::ast::AstKind;
5
6
  use oxc::ast::ast::{CallExpression, Expression, ObjectPropertyKind};
6
7
  use oxc::ast_visit::{Visit, walk};
7
8
  use serde::{Deserialize, Serialize};
8
9
  use url::Url;
9
10
 
10
11
  use crate::challenge::reduced_timing::{Entry, ResourceTiming};
11
- use crate::embedding_page::page::{Layout, PageSignals, RenderedPage};
12
+ use crate::embedding_page::page::{PageSignals, RenderedPage};
12
13
  use crate::embedding_page::render_sample::RenderSample;
13
14
  use crate::engine::Engine;
14
- use crate::js::ast::{self, ParsedScript};
15
+ use crate::js::ast::{self, ParsedScript, Script};
15
16
  use crate::js::{JsValue, json};
16
17
  use crate::resource_timing::{NEXT_HOP_PROTOCOL, Phases, ResourceMeasurement};
17
18
  use crate::{candidates, random_id};
@@ -55,6 +56,7 @@ pub(crate) struct ApiScript {
55
56
  pub render_sample: RenderSample,
56
57
  /// The loader's source, for [`RenderSample::explicit`] once a render's caller is known.
57
58
  pub source: String,
59
+ positional_signals: bool,
58
60
  }
59
61
 
60
62
  /// Resource timing information for the loader response.
@@ -107,6 +109,7 @@ impl ApiScript {
107
109
  .context("loader script names no challenge-platform route")?;
108
110
  let render_sample =
109
111
  RenderSample::from_script(script, stack_url(&url, &response, engine), engine)?;
112
+ let positional_signals = positional_signals(script)?;
110
113
  Ok(Self {
111
114
  url,
112
115
  build,
@@ -115,9 +118,18 @@ impl ApiScript {
115
118
  response,
116
119
  render_sample,
117
120
  source: source.to_owned(),
121
+ positional_signals,
118
122
  })
119
123
  }
120
124
 
125
+ fn signals(&self, signals: &PageSignals) -> JsValue {
126
+ if self.positional_signals {
127
+ signals.positional()
128
+ } else {
129
+ json!(signals)
130
+ }
131
+ }
132
+
121
133
  /// The loader's URL as `engine` names it in stacks.
122
134
  #[must_use]
123
135
  pub(crate) fn stack_url(&self, engine: Engine) -> &Url {
@@ -201,6 +213,70 @@ impl ApiScript {
201
213
  }
202
214
  }
203
215
 
216
+ /// Identify the value actually sent as `wPr`, following its declaration by semantic binding.
217
+ /// Reject unknown array layouts rather than silently submitting an incorrectly ordered record.
218
+ fn positional_signals(script: Script<'_>) -> Result<bool> {
219
+ for node in script.nodes.iter() {
220
+ let AstKind::ObjectProperty(property) = node.kind() else {
221
+ continue;
222
+ };
223
+ if ast::key_str(&property.key, property.computed) != Some("wPr") {
224
+ continue;
225
+ }
226
+ let mut value = property.value.without_parentheses();
227
+ if let Expression::Identifier(reference) = value
228
+ && let Some(symbol) = script.symbol(reference)
229
+ {
230
+ value = script
231
+ .nodes
232
+ .iter()
233
+ .find_map(|node| {
234
+ let AstKind::VariableDeclarator(declaration) = node.kind() else {
235
+ return None;
236
+ };
237
+ let oxc::ast::ast::BindingPattern::BindingIdentifier(identifier) =
238
+ &declaration.id
239
+ else {
240
+ return None;
241
+ };
242
+ (identifier.symbol_id.get() == Some(symbol))
243
+ .then_some(declaration.init.as_ref())
244
+ .flatten()
245
+ })
246
+ .context("the loader's wPr value has no initializer")?
247
+ .without_parentheses();
248
+ }
249
+ if let Expression::ArrayExpression(array) = value {
250
+ let property_at = |index: usize, name: &str| {
251
+ array
252
+ .elements
253
+ .get(index)
254
+ .and_then(|element| element.as_expression())
255
+ .and_then(Expression::as_member_expression)
256
+ .and_then(ast::member_str)
257
+ == Some(name)
258
+ };
259
+ ensure!(
260
+ array.elements.len() == 30
261
+ && property_at(2, "referrer")
262
+ && property_at(5, "href")
263
+ && property_at(23, "unexpectedHidden")
264
+ && property_at(25, "visibilityState")
265
+ && property_at(29, "innerWidth"),
266
+ "the loader uses an unsupported positional page-signals layout"
267
+ );
268
+ return Ok(true);
269
+ }
270
+ ensure!(
271
+ matches!(value, Expression::ObjectExpression(_)),
272
+ "the loader's page-signals format is unknown"
273
+ );
274
+ return Ok(false);
275
+ }
276
+ // Older test loaders omit page signals entirely.
277
+ Ok(false)
278
+ }
279
+
204
280
  /// V8 names the loader in stacks by the URL requested, JavaScriptCore by the final response URL.
205
281
  fn stack_url<'u>(requested: &'u Url, response: &'u LoaderResponse, engine: Engine) -> &'u Url {
206
282
  match engine {
@@ -295,7 +371,7 @@ pub(crate) struct ExtraParams<'page> {
295
371
  pub upgrade_completed_count: u32,
296
372
  pub url: &'page str,
297
373
  #[serde(rename = "wPr")]
298
- pub signals: PageSignals,
374
+ pub signals: JsValue,
299
375
  pub widget_id: &'page str,
300
376
  #[serde(flatten)]
301
377
  pub times: ElapsedTimes,
@@ -359,10 +435,9 @@ impl Timeline {
359
435
  #[must_use]
360
436
  pub(crate) fn collect<'page>(
361
437
  page: &'page RenderedPage,
362
- layout: &Layout,
438
+ signals: &PageSignals,
363
439
  timeline: &Timeline,
364
440
  script: &'page ApiScript,
365
- mode: Option<&str>,
366
441
  ) -> ExtraParams<'page> {
367
442
  let params = &page.params;
368
443
  ExtraParams {
@@ -394,7 +469,7 @@ pub(crate) fn collect<'page>(
394
469
  upgrade_attempts: 0,
395
470
  upgrade_completed_count: 0,
396
471
  url: page.url.as_str(),
397
- signals: page.signals(layout, mode),
472
+ signals: script.signals(signals),
398
473
  widget_id: &page.widget_id,
399
474
  times: timeline.times(),
400
475
  }
@@ -440,6 +515,34 @@ pub(crate) fn widget_id() -> String {
440
515
  mod tests {
441
516
  use super::*;
442
517
 
518
+ #[test]
519
+ fn page_signals_follow_the_sent_binding_and_reject_unknown_array_layouts() {
520
+ let source = |fields: &[String]| {
521
+ format!(
522
+ "function collect() {{ let data=[{}]; function unrelated() {{ let data={{}}; }}; send({{wPr:data}}); }}",
523
+ fields.join(",")
524
+ )
525
+ };
526
+ let mut fields = vec!["null".to_owned(); 30];
527
+ for (index, value) in [
528
+ (2, "document.referrer"),
529
+ (5, "window.location.href"),
530
+ (23, "state.unexpectedHidden"),
531
+ (25, "state.viewport.visibilityState"),
532
+ (29, "window.innerWidth"),
533
+ ] {
534
+ fields[index] = value.to_owned();
535
+ }
536
+ let parsed = ParsedScript::parse("loader", &source(&fields)).unwrap();
537
+ assert!(positional_signals(parsed.script()).unwrap());
538
+ fields.swap(23, 24);
539
+ let parsed = ParsedScript::parse("loader", &source(&fields)).unwrap();
540
+ assert!(positional_signals(parsed.script()).is_err());
541
+ let parsed =
542
+ ParsedScript::parse("legacy", "let data = {pi:{}}; send({wPr:data});").unwrap();
543
+ assert!(!positional_signals(parsed.script()).unwrap());
544
+ }
545
+
443
546
  #[test]
444
547
  fn finds_build_hashes_and_routes_only_where_the_loader_keeps_them() {
445
548
  let literals = |source: &str| {
@@ -12,7 +12,7 @@ use scraper::{ElementRef, Html, Node, Selector};
12
12
  use serde::{Deserialize, Serialize};
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";
@@ -210,6 +210,46 @@ pub(crate) struct PageSignals {
210
210
  pub inner_width: u32,
211
211
  }
212
212
 
213
+ impl PageSignals {
214
+ /// The positional `wPr` format used by newer loaders. Keep absent optional properties as
215
+ /// null array entries, as JSON.stringify does for JavaScript's `undefined`.
216
+ pub(crate) fn positional(&self) -> JsValue {
217
+ let parent = &self.parent;
218
+ json!([
219
+ self.custom_element_tags,
220
+ self.root_attributes,
221
+ self.referrer,
222
+ parent.form,
223
+ parent.in_iframe,
224
+ parent.location,
225
+ parent.meta,
226
+ parent.pac,
227
+ parent.pad,
228
+ parent.fingerprint,
229
+ parent.scripts,
230
+ parent.same_realm,
231
+ parent.stylesheets,
232
+ parent.title,
233
+ parent.elements,
234
+ parent.appearance,
235
+ parent.error_handled,
236
+ parent.host,
237
+ parent.hidden_reason,
238
+ parent.implicit,
239
+ parent.mode,
240
+ parent.mount,
241
+ parent.reasons,
242
+ parent.user_hidden,
243
+ parent.viewport.height,
244
+ parent.viewport.visibility_state,
245
+ parent.viewport.width,
246
+ parent.widget_position,
247
+ parent.xpath,
248
+ self.inner_width,
249
+ ])
250
+ }
251
+ }
252
+
213
253
  /// `pi`: the widget's place in the page. The parent info probe copies these properties by
214
254
  /// their `api.js` names.
215
255
  #[derive(Clone, Debug, Serialize)]
@@ -338,16 +338,17 @@ pub(crate) fn merge(
338
338
  embedding_page.script.build
339
339
  ));
340
340
  }
341
- let message = extra_params::collect(
342
- &embedding_page.page,
341
+ let page_signals = embedding_page.page.signals(
343
342
  &embedding_page.layout,
344
- &page.timeline,
345
- &embedding_page.script,
346
343
  source::mode(&program.request.options),
347
344
  );
345
+ let message = json!(extra_params::collect(
346
+ &embedding_page.page,
347
+ &page_signals,
348
+ &page.timeline,
349
+ &embedding_page.script,
350
+ ));
348
351
  sink.json("extra-params.json", &message)?;
349
- let page_signals = message.signals.clone();
350
- let message = json!(message);
351
352
  let mut unset: Vec<String> = Vec::new();
352
353
  let built = FirstPayload::build(
353
354
  sink,
@@ -115,6 +115,7 @@ pub(crate) async fn prepare(session: &WidgetSession<'_>, solve: &Solve) -> Resul
115
115
  trace: Trace::recover(&session.fetched.source),
116
116
  controller: plan.controller_trace.as_deref(),
117
117
  globals: &globals,
118
+ parent: observed.page_signals.as_ref(),
118
119
  };
119
120
  let records = records(
120
121
  plan,
@@ -146,10 +146,39 @@ pub fn analyze(source: &Source, program: &[u8]) -> Result<Analysis> {
146
146
  .collect();
147
147
  let mut vocabulary = Vocabulary::infer(&session_features);
148
148
  for (position, raw) in discovery.probes.iter().enumerate() {
149
- if vocabulary.partner(position) == Some(AUDIT_PROBE)
150
- && let Some(names) = audit::names(&index, &raw.writes.fields)
151
- {
152
- vocabulary.pin(position, names);
149
+ match vocabulary.partner(position).and_then(Kind::from_reference) {
150
+ Some(Kind::FontEnumeration) => {
151
+ if let Some(labels) = crate::stage2::value_labels::fonts(&index, raw.function.pc) {
152
+ vocabulary.pin(position, labels);
153
+ }
154
+ }
155
+ Some(Kind::Environment) => {
156
+ if let Some(labels) = crate::stage2::value_labels::permissions(&raw.features) {
157
+ vocabulary.pin(position, labels);
158
+ }
159
+ }
160
+ Some(Kind::Stylesheets) => {
161
+ if let Some(field) = crate::stage2::stylesheets::text_field(&index, raw.function.pc)
162
+ {
163
+ vocabulary.pin(
164
+ position,
165
+ [(field, crate::stage2::reference::STYLESHEETS_TEXT)],
166
+ );
167
+ }
168
+ }
169
+ Some(Kind::DateParsingOffsets) => {
170
+ if let Some(names) =
171
+ crate::stage2::probes::dates::names(&raw.features, &raw.writes.fields)
172
+ {
173
+ vocabulary.pin(position, names);
174
+ }
175
+ }
176
+ Some(Kind::EnvironmentAudit) => {
177
+ if let Some(names) = audit::names(&index, &raw.writes.fields) {
178
+ vocabulary.pin(position, names);
179
+ }
180
+ }
181
+ _ => {}
153
182
  }
154
183
  }
155
184
  let mut probes = Vec::new();
@@ -266,12 +266,23 @@ fn collection_strings(
266
266
  outputs.insert(name.clone());
267
267
  }
268
268
  effect.visit_terms(&mut |term| match term {
269
- Term::Const(Constant::String(text)) if text.len() <= FEATURE_LENGTH => {
269
+ Term::Const(Constant::String(text))
270
+ if text.len() <= FEATURE_LENGTH
271
+ && !index
272
+ .listing()
273
+ .decoded_inputs
274
+ .get(&pc)
275
+ .is_some_and(|inputs| inputs.contains(text)) =>
276
+ {
270
277
  if !at_pc.contains(text) {
271
278
  at_pc.push(text.clone());
272
279
  }
273
280
  }
274
- Term::Closure { pc, .. } => pending.push(*pc),
281
+ Term::Closure { pc, .. }
282
+ if !index.listing().string_decoders.contains(pc) =>
283
+ {
284
+ pending.push(*pc)
285
+ }
275
286
  // Exception results are recovered separately from collection paths.
276
287
  _ => {}
277
288
  });
@@ -7,7 +7,7 @@ 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::{CallExpression, Expression, MemberExpression};
11
11
  use oxc::ast_visit::{Visit, walk};
12
12
  use oxc::str::Ident;
13
13
  use serde::{Deserialize, Serialize};
@@ -166,20 +166,8 @@ pub(crate) fn recognize(probe: &ProbeView<'_>) -> Option<Fields> {
166
166
  });
167
167
  }
168
168
  }
169
- // Programs that decode strings in place also hold the source as a descriptor.
170
- let parsed = strings
171
- .iter()
172
- .filter(|text| text.contains("WebAssembly.instantiate"))
173
- .find_map(|source| ParsedScript::parse("benchmark worker", source).ok())?;
174
- let mut worker = Worker::new(parsed.script());
175
- worker.visit_program(parsed.script().program);
176
- // Only the kernels the recorded speeds measure are supported. Field names are recovered
177
- // separately and may change without changing the binary.
178
- if functions_digest(&worker.module?)? != KERNELS {
179
- return None;
180
- }
181
- let [samples, compilation, total]: [String; 3] = worker.outputs.try_into().ok()?;
182
- let [warmup, _, _, rounds]: [String; 4] = worker.plan_fields.try_into().ok()?;
169
+ let ([samples, compilation, total], [warmup, _, _, rounds]) =
170
+ worker_fields(&strings, probe.pool)?;
183
171
  let number = |name: &str| {
184
172
  strings.iter().find_map(|text| {
185
173
  text.strip_prefix(&format!("{name}:"))?
@@ -247,6 +235,29 @@ pub(crate) fn recognize(probe: &ProbeView<'_>) -> Option<Fields> {
247
235
  })
248
236
  }
249
237
 
238
+ /// Ignore unrelated WebAssembly mentions and only accept the complete worker whose
239
+ /// kernels match the captured measurements. Sources may be packed descriptors.
240
+ fn worker_fields(strings: &[String], pool: &str) -> Option<([String; 3], [String; 4])> {
241
+ strings
242
+ .iter()
243
+ .filter(|text| text.contains("WebAssembly"))
244
+ .find_map(|source| {
245
+ let source = crate::deobfuscate::deobfuscate(source.clone())
246
+ .or_else(|_| crate::deobfuscate::deobfuscate(listing::decode_string(source, pool)?))
247
+ .ok()?;
248
+ let parsed = ParsedScript::parse("benchmark worker", &source.text).ok()?;
249
+ let mut worker = Worker::new(parsed.script());
250
+ worker.visit_program(parsed.script().program);
251
+ if functions_digest(&worker.module?)? != KERNELS {
252
+ return None;
253
+ }
254
+ Some((
255
+ worker.outputs.try_into().ok()?,
256
+ worker.plan_fields.try_into().ok()?,
257
+ ))
258
+ })
259
+ }
260
+
250
261
  /// Digest of the kernel functions measured by the profiles; see [`functions_digest`].
251
262
  const KERNELS: &str = "259924f166f0a6ab5c9709a91c80f12f9cfd24cc1827c3a76bf93ac5cbb60a3a";
252
263
 
@@ -310,14 +321,14 @@ impl<'a> Worker<'a> {
310
321
  }
311
322
 
312
323
  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)
324
+ fn visit_expression(&mut self, expression: &Expression<'a>) {
325
+ if let Some(value) = ast::static_str(expression)
315
326
  && let Ok(bytes) = STANDARD.decode(value)
316
327
  && bytes.starts_with(b"\0asm")
317
328
  {
318
329
  self.module = Some(bytes);
319
330
  }
320
- walk::walk_variable_declarator(self, declaration);
331
+ walk::walk_expression(self, expression);
321
332
  }
322
333
  fn visit_member_expression(&mut self, member: &MemberExpression<'a>) {
323
334
  if let MemberExpression::StaticMemberExpression(field) = member
@@ -360,6 +371,36 @@ mod tests {
360
371
  use super::*;
361
372
  use crate::js;
362
373
 
374
+ #[test]
375
+ fn recovers_the_encoded_workers_inlined_module_and_plan() {
376
+ let (outputs, plan_fields) = worker_fields(
377
+ &[
378
+ "WebAssembly".to_owned(),
379
+ "const irrelevant = WebAssembly;".to_owned(),
380
+ include_str!("fixtures/encoded_benchmark_worker.js").to_owned(),
381
+ ],
382
+ "",
383
+ )
384
+ .unwrap();
385
+ assert_eq!(
386
+ outputs,
387
+ [
388
+ "_e712758a2abf0550c8c03caf",
389
+ "_330c1c3a2512b4edd9f7a483",
390
+ "_6e07b0330abb107d89bcb031"
391
+ ]
392
+ );
393
+ assert_eq!(
394
+ plan_fields,
395
+ [
396
+ "_0edcdf5edd841b2aaa59320c",
397
+ "_5789c64e096112b22c52e80f",
398
+ "_7f8289304361962296ebf3f6",
399
+ "_447c0ac98523cf3d6a2324d9"
400
+ ]
401
+ );
402
+ }
403
+
363
404
  fn sample() -> Fields {
364
405
  js::from_value(json!({
365
406
  "samples": "samples", "compilation": "compile", "total": "total",