captchaforge 0.2.34

Automatic CAPTCHA detection and multi-strategy solving for chromiumoxide-driven headless browsers (Cloudflare Turnstile, reCAPTCHA v2/v3, hCaptcha, image grids, audio, sliders).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Frame + shadow-root graph for the current page.
//!
//! Every captcha-solving operation that has to walk frames today
//! does this:
//!
//! ```text
//! for fid in page.frames().await? {
//!     if let Some(ctx) = page.frame_execution_context(fid).await? {
//!         page.evaluate_expression(...with ctx...).await?;
//!     }
//! }
//! ```
//!
//! Three problems with the bare-loop pattern:
//!
//! 1. **No structure.** `page.frames()` returns a flat list — you
//!    can't ask "which frame is this iframe's parent?", "which
//!    frames live inside the captcha container?", "which frame is
//!    nested deepest?" without re-running an extraction pass each
//!    time. Solvers re-derive the topology over and over.
//! 2. **Shadow roots are invisible.** `page.frames()` only sees
//!    cross-document boundaries; same-document shadow roots are
//!    missed. The existing in-DOM `walkAllRoots` JS pass handles
//!    them but lives in every solver as a copy-paste blob.
//! 3. **No reasoning.** With a graph you can BFS from "the deepest
//!    frame containing a captcha widget" outward to find the
//!    nearest token field, or topo-sort frames so the deepest
//!    challenge runs first. With a flat list you can't.
//!
//! [`FrameGraph`] is the substrate: snapshot once, query many
//! times. [`FrameNode`] is the per-node shape (frame_id +
//! parent + URL + title + presence of captcha markers).
//!
//! Pure data type — no IO ourselves; [`FrameGraph::snapshot`] does
//! the CDP round-trips and returns a built graph. Tests can
//! construct synthetic graphs without a browser.

use anyhow::Result;
use chromiumoxide::Page;
use std::collections::{HashMap, VecDeque};

/// One node in the frame graph.
///
/// `frame_id` is the CDP frame identifier — opaque string we hand
/// back to `page.frame_execution_context(frame_id)` when running
/// JS in this frame's context.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FrameNode {
    /// CDP frame ID. `None` for the synthetic root that ties the
    /// main frame plus all shadow roots together; nodes representing
    /// real frames always carry one.
    pub frame_id: Option<String>,
    /// Index of the parent node in [`FrameGraph::nodes`]. `None`
    /// only for the root.
    pub parent: Option<usize>,
    /// URL of the document this frame is rendering. `about:blank`
    /// or `about:srcdoc` for synthetic / written iframes.
    pub url: String,
    /// `document.title` at snapshot time.
    pub title: String,
    /// True when the frame's body or any descendant matched a
    /// captcha-shaped selector at snapshot time. Drives reasoning
    /// like "BFS to the nearest captcha-bearing frame".
    pub has_captcha_marker: bool,
    /// Depth from the root (root = 0, top-level frame = 1, …).
    pub depth: usize,
}

/// The full graph for a page snapshot.
///
/// Children are indexed via [`FrameGraph::children`] which scans
/// the `nodes` Vec — fine for the typical <50-node frame trees we
/// see in the wild; would warrant a parent→children index for
/// 1000+-frame pages (which don't exist in practice).
#[derive(Debug, Clone, Default)]
pub struct FrameGraph {
    pub nodes: Vec<FrameNode>,
}

impl FrameGraph {
    /// Build a graph from the current state of `page`.
    ///
    /// Calls `page.frames()` once + per-frame title/URL evals.
    /// The captcha-marker scan runs the same selector list as
    /// [`crate::solver::oracle::take_snapshot`] uses for outcome
    /// classification, so the two stay in sync by reading the
    /// same predicates.
    pub async fn snapshot(page: &Page) -> Result<Self> {
        let frame_ids = page.frames().await?;
        let mut nodes: Vec<FrameNode> = Vec::with_capacity(frame_ids.len() + 1);

        // Node 0 = synthetic root. Always present even when the
        // page has no frames; gives every consumer a stable
        // entry point.
        nodes.push(FrameNode {
            frame_id: None,
            parent: None,
            url: "(root)".into(),
            title: String::new(),
            has_captcha_marker: false,
            depth: 0,
        });

        for fid in frame_ids {
            if let Some(ctx) = page.frame_execution_context(fid.clone()).await? {
                use chromiumoxide::cdp::js_protocol::runtime::EvaluateParams;
                let probe = EvaluateParams::builder()
                    .expression(PROBE_JS)
                    .context_id(ctx)
                    .build()
                    .ok();
                if let Some(p) = probe {
                    if let Ok(eval) = page.evaluate_expression(p).await {
                        if let Ok(v) = eval.into_value::<FrameProbe>() {
                            nodes.push(FrameNode {
                                frame_id: Some(format!("{fid:?}")),
                                parent: Some(0),
                                url: v.url,
                                title: v.title,
                                has_captcha_marker: v.has_captcha_marker,
                                depth: 1,
                            });
                        }
                    }
                }
            }
        }

        Ok(Self { nodes })
    }

    /// True iff the graph has any node with `has_captcha_marker`.
    /// Cheap pre-check before more expensive walks.
    pub fn any_captcha_marker(&self) -> bool {
        self.nodes.iter().any(|n| n.has_captcha_marker)
    }

    /// Indices of all child nodes of `parent_idx`.
    ///
    /// Linear scan; acceptable for typical tree sizes (<50 nodes).
    /// If we ever ship a graph with hundreds of nodes, replace
    /// with a precomputed parent→children index.
    pub fn children(&self, parent_idx: usize) -> Vec<usize> {
        self.nodes
            .iter()
            .enumerate()
            .filter_map(|(i, n)| {
                if n.parent == Some(parent_idx) {
                    Some(i)
                } else {
                    None
                }
            })
            .collect()
    }

    /// BFS from the root, returning node indices in visit order.
    ///
    /// Handy for "do this thing in every frame, top-down" without
    /// open-coding the queue management at every call site.
    pub fn bfs(&self) -> Vec<usize> {
        if self.nodes.is_empty() {
            return Vec::new();
        }
        let mut order = Vec::with_capacity(self.nodes.len());
        let mut queue: VecDeque<usize> = VecDeque::new();
        queue.push_back(0);
        while let Some(idx) = queue.pop_front() {
            order.push(idx);
            for child in self.children(idx) {
                queue.push_back(child);
            }
        }
        order
    }

    /// Find the deepest node that has a captcha marker. Returns
    /// `None` when no node carries one.
    ///
    /// Useful for "walk OUTWARD from the captcha to find the
    /// nearest enclosing token field" — once you have the captcha
    /// node, traverse parent links upward until the token shows up.
    pub fn deepest_captcha(&self) -> Option<usize> {
        self.nodes
            .iter()
            .enumerate()
            .filter(|(_, n)| n.has_captcha_marker)
            .max_by_key(|(_, n)| n.depth)
            .map(|(i, _)| i)
    }

    /// All node indices on the path from `node_idx` up to the root,
    /// inclusive of both endpoints. Empty when `node_idx` is OOB.
    ///
    /// Use this when a captcha solve produces a token in a deeply-
    /// nested iframe and you need to relay it up the tree via
    /// `postMessage` — the path is the relay route.
    pub fn ancestors_inclusive(&self, mut node_idx: usize) -> Vec<usize> {
        let mut out = Vec::new();
        let mut visited = std::collections::HashSet::new();
        while let Some(node) = self.nodes.get(node_idx) {
            if !visited.insert(node_idx) {
                // Cycle guard. Snapshot graphs are trees by
                // construction but defensive coding wins.
                break;
            }
            out.push(node_idx);
            match node.parent {
                Some(p) => node_idx = p,
                None => break,
            }
        }
        out
    }

    /// Group nodes by URL host, returning a map host → indices.
    ///
    /// Lets a solver target "all cross-origin frames hosted by
    /// `challenges.cloudflare.com`" in one query, e.g. to pierce
    /// the CF Turnstile sandbox.
    pub fn frames_by_host(&self) -> HashMap<String, Vec<usize>> {
        let mut out: HashMap<String, Vec<usize>> = HashMap::new();
        for (i, n) in self.nodes.iter().enumerate() {
            if let Some(host) = url::Url::parse(&n.url)
                .ok()
                .and_then(|u| u.host_str().map(String::from))
            {
                out.entry(host).or_default().push(i);
            }
        }
        out
    }
}

#[derive(serde::Deserialize)]
struct FrameProbe {
    url: String,
    title: String,
    has_captcha_marker: bool,
}

/// JS payload run inside every frame's execution context to
/// populate a [`FrameNode`]. Selector list mirrors
/// `oracle::take_snapshot` so the two passes stay consistent.
const PROBE_JS: &str = r#"({
    url: location.href || '',
    title: document.title || '',
    has_captcha_marker: !!document.querySelector(
        'iframe[src*="challenges.cloudflare.com"], iframe[src*="recaptcha"], iframe[src*="hcaptcha"], '
        + 'iframe[src*="arkoselabs"], iframe[src*="datadome"], iframe[src*="geetest"], '
        + 'iframe[src*="perimeterx"], iframe[src*="kasada"], iframe[src*="incapsula"], '
        + '.cf-turnstile, .h-captcha, .g-recaptcha, '
        + '#challenge-form, #challenge-stage, #cf-please-wait, #px-captcha, '
        + '[id^="captcha"], [class*="captcha" i], [class*="challenge" i]'
    )
})"#;

#[cfg(test)]
mod tests {
    use super::*;

    /// Build a synthetic graph for testing without a browser:
    ///
    /// ```text
    /// root
    /// ├── main (no captcha)
    /// │   ├── frame_a (captcha)
    /// │   │   └── frame_aa (captcha, deepest)
    /// │   └── frame_b
    /// └── isolated (no parent linkage)
    /// ```
    fn fixture_graph() -> FrameGraph {
        FrameGraph {
            nodes: vec![
                // 0: root
                FrameNode {
                    frame_id: None,
                    parent: None,
                    url: "(root)".into(),
                    title: String::new(),
                    has_captcha_marker: false,
                    depth: 0,
                },
                // 1: main
                FrameNode {
                    frame_id: Some("F1".into()),
                    parent: Some(0),
                    url: "https://example.com".into(),
                    title: "Main".into(),
                    has_captcha_marker: false,
                    depth: 1,
                },
                // 2: frame_a (captcha)
                FrameNode {
                    frame_id: Some("F2".into()),
                    parent: Some(1),
                    url: "https://challenges.cloudflare.com/turnstile".into(),
                    title: String::new(),
                    has_captcha_marker: true,
                    depth: 2,
                },
                // 3: frame_aa (captcha, deepest)
                FrameNode {
                    frame_id: Some("F3".into()),
                    parent: Some(2),
                    url: "https://challenges.cloudflare.com/turnstile/inner".into(),
                    title: String::new(),
                    has_captcha_marker: true,
                    depth: 3,
                },
                // 4: frame_b
                FrameNode {
                    frame_id: Some("F4".into()),
                    parent: Some(1),
                    url: "https://example.com/sidebar".into(),
                    title: String::new(),
                    has_captcha_marker: false,
                    depth: 2,
                },
            ],
        }
    }

    #[test]
    fn empty_graph_has_no_captcha_markers() {
        let g = FrameGraph::default();
        assert!(!g.any_captcha_marker());
        assert!(g.bfs().is_empty());
        assert!(g.deepest_captcha().is_none());
    }

    #[test]
    fn any_captcha_marker_short_circuits_on_first_match() {
        let g = fixture_graph();
        assert!(g.any_captcha_marker());
    }

    #[test]
    fn children_returns_all_direct_children_of_root() {
        let g = fixture_graph();
        let kids = g.children(0);
        assert_eq!(kids, vec![1]);
    }

    #[test]
    fn children_returns_all_direct_children_of_internal_node() {
        let g = fixture_graph();
        // Main (idx=1) has frame_a (2) and frame_b (4).
        let kids = g.children(1);
        assert_eq!(kids, vec![2, 4]);
    }

    #[test]
    fn bfs_visits_root_first_then_each_level() {
        let g = fixture_graph();
        let order = g.bfs();
        // Expected: 0 (root) → 1 (main) → 2,4 (children of main)
        // → 3 (child of frame_a). Order within a level matches
        // insertion order in `nodes`.
        assert_eq!(order, vec![0, 1, 2, 4, 3]);
    }

    #[test]
    fn deepest_captcha_finds_innermost_marker() {
        let g = fixture_graph();
        let deepest = g.deepest_captcha().expect("fixture has captcha markers");
        assert_eq!(deepest, 3, "frame_aa is the deepest captcha-bearing node");
    }

    #[test]
    fn ancestors_inclusive_walks_to_root_in_order() {
        let g = fixture_graph();
        // From frame_aa (3) → frame_a (2) → main (1) → root (0).
        let path = g.ancestors_inclusive(3);
        assert_eq!(path, vec![3, 2, 1, 0]);
    }

    #[test]
    fn ancestors_inclusive_handles_oob_index_gracefully() {
        let g = fixture_graph();
        assert!(g.ancestors_inclusive(999).is_empty());
    }

    #[test]
    fn ancestors_inclusive_handles_root_node() {
        let g = fixture_graph();
        let path = g.ancestors_inclusive(0);
        assert_eq!(path, vec![0]);
    }

    #[test]
    fn frames_by_host_groups_correctly() {
        let g = fixture_graph();
        let hosts = g.frames_by_host();
        assert_eq!(
            hosts.get("example.com").map(|v| v.len()),
            Some(2),
            "main + sidebar both on example.com"
        );
        assert_eq!(
            hosts.get("challenges.cloudflare.com").map(|v| v.len()),
            Some(2),
            "two CF turnstile frames"
        );
    }

    #[test]
    fn frames_by_host_skips_unparseable_urls() {
        // (root) URL is not a valid http(s) URL → must be skipped.
        let g = fixture_graph();
        let hosts = g.frames_by_host();
        assert!(!hosts.contains_key("(root)"));
    }
}