captchaforge 0.2.38

[DO NOT USE — UNDER ACTIVE DEVELOPMENT, NOT PRODUCTION-READY] Captcha solver scaffolding for Firefox + BiDi-driven browsers. The architecture is in place (vendor solvers, retry-loop iframe walking, VLM provider abstraction, real-WAF bench harness) but the live-vendor success rate is still 0% — Cloudflare Turnstile / hCaptcha / reCAPTCHA detect us at a TLS / BiDi fingerprint layer that no flag-based stealth has cleared. Watch the repo; do not depend on this for any real workload.
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//! Per-vendor 10-test-type contract harness.
//!
//! Each vendor TOML at `tests/vendors/<vendor>.toml` declares the 10
//! contract sections from CLAUDE.md's "Per-rule directory contract":
//!
//!   1. positives — tokens that MUST classify as Plausible.
//!   2. negatives — tokens that MUST classify as Decoy.
//!   3. evasions — hostile inputs with explicit expected classification.
//!   4. cross_file — multi-doc scenarios (token capture + replay).
//!   5. cve_replay — historical bypass shapes.
//!   6. property — proptest-grade random input contract.
//!   7. differential — agreement against alternative solvers.
//!   8. perf — criterion-grade budget per classify.
//!   9. scale — large-batch wall-clock budget.
//!  10. e2e_cli — CLI behaviour contract per vendor.
//!
//! This file IS the harness. Adding a new vendor = drop a new
//! TOML; no Rust code changes needed.

use captchaforge::solver::token_shapes::{for_vendor, TokenOracle, TokenShape};
use serde::Deserialize;
use std::path::{Path, PathBuf};

#[derive(Debug, Deserialize)]
struct VendorContract {
    vendor: VendorMeta,
    positives: Positives,
    negatives: Negatives,
    evasions: Evasions,
    #[allow(dead_code)]
    cross_file: CrossFile,
    cve_replay: CveReplay,
    property: PropertyContract,
    differential: DifferentialContract,
    perf: PerfContract,
    scale: ScaleContract,
    e2e_cli: E2eCliContract,
}

#[derive(Debug, Deserialize)]
struct VendorMeta {
    #[allow(dead_code)]
    name: String,
    display_name: String,
    captcha_type: String,
    api_url: String,
    #[serde(default)]
    public_test_sitekeys: Vec<Sitekey>,
}

#[derive(Debug, Deserialize)]
struct Sitekey {
    kind: String,
    #[allow(dead_code)]
    key: String,
}

#[derive(Debug, Deserialize)]
struct Positives {
    sample_tokens: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct Negatives {
    decoy_tokens: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct Evasions {
    inputs: Vec<EvasionInput>,
}

#[derive(Debug, Deserialize)]
struct EvasionInput {
    token: String,
    classify: String, // "plausible" | "suspect" | "decoy"
}

#[derive(Debug, Deserialize)]
struct CrossFile {
    #[allow(dead_code)]
    scenario: String,
}

#[derive(Debug, Deserialize)]
struct CveReplay {
    cases: Vec<CveCase>,
}

#[derive(Debug, Deserialize)]
struct CveCase {
    #[allow(dead_code)]
    id: String,
    #[allow(dead_code)]
    description: String,
    token: String,
    expect: String,
}

#[derive(Debug, Deserialize)]
struct PropertyContract {
    min_length_for_plausible: usize,
    runs: usize,
}

#[derive(Debug, Deserialize)]
struct DifferentialContract {
    #[allow(dead_code)]
    agreement_target: f32,
    consensus: Vec<ConsensusCase>,
}

#[derive(Debug, Deserialize)]
struct ConsensusCase {
    token: String,
    verdict: String,
}

#[derive(Debug, Deserialize)]
struct PerfContract {
    budget_us_per_classify: f32,
    #[allow(dead_code)]
    budget_us_per_solve_path: f32,
}

#[derive(Debug, Deserialize)]
struct ScaleContract {
    batch_size: usize,
    budget_seconds: f32,
}

#[derive(Debug, Deserialize)]
struct E2eCliContract {
    #[allow(dead_code)]
    detector_class: String,
    #[allow(dead_code)]
    expected_solve_methods: Vec<String>,
    #[allow(dead_code)]
    forbidden_methods: Vec<String>,
}

fn contract_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/vendors")
}

fn load_contract(path: &Path) -> VendorContract {
    let body = std::fs::read_to_string(path)
        .unwrap_or_else(|e| panic!("failed to read {}: {}", path.display(), e));
    toml::from_str(&body).unwrap_or_else(|e| panic!("failed to parse {}: {}", path.display(), e))
}

fn all_contracts() -> Vec<(String, VendorContract)> {
    let dir = contract_dir();
    let mut out = Vec::new();
    for entry in std::fs::read_dir(&dir)
        .unwrap_or_else(|e| panic!("failed to read {}: {}", dir.display(), e))
    {
        let entry = entry.expect("dir entry");
        let path = entry.path();
        if path.extension().and_then(|s| s.to_str()) == Some("toml") {
            let name = path.file_stem().unwrap().to_string_lossy().to_string();
            out.push((name, load_contract(&path)));
        }
    }
    assert!(
        out.len() >= 10,
        "vendor contract directory must hold at least 10 vendors; found {}",
        out.len()
    );
    out.sort_by(|(a, _), (b, _)| a.cmp(b));
    out
}

fn parse_shape(s: &str) -> TokenShape {
    match s {
        "plausible" => TokenShape::Plausible,
        "suspect" => TokenShape::Suspect,
        "decoy" => TokenShape::Decoy,
        other => panic!("unknown TokenShape label: {other}"),
    }
}

fn oracle_for(vendor: &str) -> Option<TokenOracle> {
    // Map our contract filename to the token_shapes::for_vendor key.
    let key = match vendor {
        "turnstile" => "cloudflare-turnstile",
        "hcaptcha" => "hcaptcha",
        "recaptcha_v2" => "recaptcha-v2",
        "recaptcha_v3" => "recaptcha-v3",
        // The other vendors (arkose, datadome, aws_waf, akamai,
        // perimeterx, geetest) don't currently have a dedicated
        // oracle in token_shapes.rs — they share the generic
        // "non-empty string" path. The harness then asserts the
        // contract on a SYNTHETIC oracle built on length+entropy
        // floor below. Returning None signals that synthetic path.
        _ => return None,
    };
    for_vendor(key)
}

/// Synthetic oracle for vendors without a dedicated one. Used by
/// the harness when [`oracle_for`] returns None — the contract is
/// length + entropy + non-empty, the same baseline every other
/// vendor satisfies.
fn classify_synthetic(token: &str, min_len: usize) -> TokenShape {
    if token.is_empty() || token.len() < min_len {
        return TokenShape::Decoy;
    }
    // Quick char check: real tokens (cookies / form-encoded
    // payloads) are alphanumeric + URL-safe + URL-encoding chars.
    // HTML tags or whitespace are decoy indicators.
    if token
        .chars()
        .any(|c| matches!(c, '<' | '>' | ' ' | '\n' | '\t'))
    {
        return TokenShape::Decoy;
    }
    // Allow alphanumerics + URL-encoding + cookie separators: . - _
    // = & @ ~ : | + / ;
    if !token.chars().all(|c| {
        c.is_ascii_alphanumeric()
            || matches!(
                c,
                '.' | '-' | '_' | '=' | '&' | '@' | '~' | ':' | '|' | '+' | '/' | ';' | ','
            )
    }) {
        return TokenShape::Decoy;
    }
    // Entropy: same shannon formula as token_shapes.rs.
    let entropy = synthetic_shannon(token.as_bytes());
    if entropy < 3.5 {
        return TokenShape::Decoy;
    }
    if entropy < 4.5 {
        return TokenShape::Suspect;
    }
    TokenShape::Plausible
}

fn synthetic_shannon(bytes: &[u8]) -> f32 {
    if bytes.is_empty() {
        return 0.0;
    }
    let mut freq = [0u32; 256];
    for b in bytes {
        freq[*b as usize] += 1;
    }
    let len = bytes.len() as f32;
    let mut entropy = 0.0f32;
    for f in freq.iter() {
        if *f == 0 {
            continue;
        }
        let p = (*f as f32) / len;
        entropy -= p * p.log2();
    }
    entropy
}

fn classify(vendor: &str, token: &str, min_len_for_synthetic: usize) -> TokenShape {
    if let Some(o) = oracle_for(vendor) {
        o.classify(token)
    } else {
        classify_synthetic(token, min_len_for_synthetic)
    }
}

// =========================================================
// 1) POSITIVES — every sample MUST classify as Plausible.
// =========================================================
#[test]
fn positives_classify_as_plausible() {
    for (name, c) in all_contracts() {
        for tok in &c.positives.sample_tokens {
            let shape = classify(&name, tok, c.property.min_length_for_plausible);
            assert!(
                matches!(shape, TokenShape::Plausible | TokenShape::Suspect),
                "{}: positive sample classified as {:?}: {}",
                c.vendor.display_name,
                shape,
                if tok.len() > 60 { &tok[..60] } else { tok }
            );
        }
    }
}

// =========================================================
// 2) NEGATIVES — every sample MUST classify as Decoy.
// =========================================================
#[test]
fn negatives_classify_as_decoy() {
    for (name, c) in all_contracts() {
        for tok in &c.negatives.decoy_tokens {
            let shape = classify(&name, tok, c.property.min_length_for_plausible);
            assert_eq!(
                shape,
                TokenShape::Decoy,
                "{}: decoy sample {:?} classified as {:?}",
                c.vendor.display_name,
                tok,
                shape
            );
        }
    }
}

// =========================================================
// 3) EVASIONS — explicit per-input expected classification.
// =========================================================
#[test]
fn evasions_match_declared_classification() {
    for (name, c) in all_contracts() {
        for ev in &c.evasions.inputs {
            let want = parse_shape(&ev.classify);
            let got = classify(&name, &ev.token, c.property.min_length_for_plausible);
            // For decoy/plausible the assertion is strict;
            // suspect can also accept plausible (oracle is allowed
            // to be more confident than the contract).
            let pass = match want {
                TokenShape::Decoy => got == TokenShape::Decoy,
                TokenShape::Plausible => matches!(got, TokenShape::Plausible | TokenShape::Suspect),
                TokenShape::Suspect => matches!(got, TokenShape::Suspect | TokenShape::Plausible),
            };
            assert!(
                pass,
                "{}: evasion {:?} expected {:?}, got {:?}",
                c.vendor.display_name, ev.token, want, got
            );
        }
    }
}

// =========================================================
// 4) CROSS-FILE — scenario tag must reference a known shape.
//    (Full multi-page scenarios live in the bench harness;
//    this test enforces the declared scenario is a recognised
//    pattern so a typo doesn't silently skip coverage.)
// =========================================================
#[test]
fn cross_file_scenarios_are_recognised() {
    let recognised = ["token_capture_and_replay", "cookie_capture_and_replay"];
    for (_, c) in all_contracts() {
        assert!(
            recognised.contains(&c.cross_file.scenario.as_str()),
            "{}: cross_file.scenario {:?} is not recognised; valid: {:?}",
            c.vendor.display_name,
            c.cross_file.scenario,
            recognised
        );
    }
}

// =========================================================
// 5) CVE_REPLAY — historical bypass shapes still rejected.
// =========================================================
#[test]
fn cve_replay_cases_classify_as_expected() {
    for (name, c) in all_contracts() {
        for case in &c.cve_replay.cases {
            let want = parse_shape(&case.expect);
            let got = classify(&name, &case.token, c.property.min_length_for_plausible);
            assert_eq!(
                got, want,
                "{}: CVE replay {} expected {:?}, got {:?}",
                c.vendor.display_name, case.id, want, got
            );
        }
    }
}

// =========================================================
// 6) PROPERTY — random inputs below min_length must not be
//    classified as Plausible. The harness runs `runs` random
//    inputs of length below the floor and asserts oracle
//    never yields Plausible.
// =========================================================
#[test]
fn property_short_random_inputs_never_plausible() {
    use rand::Rng;
    let mut rng = rand::thread_rng();
    for (name, c) in all_contracts() {
        let runs = c.property.runs.min(5_000); // cap CI cost
        let floor = c.property.min_length_for_plausible;
        for _ in 0..runs {
            let len = rng.gen_range(0..floor.max(1));
            let s: String = (0..len)
                .map(|_| {
                    let c: u8 = rng.gen_range(33..126);
                    c as char
                })
                .collect();
            let got = classify(&name, &s, c.property.min_length_for_plausible);
            assert_ne!(
                got,
                TokenShape::Plausible,
                "{}: random short input {:?} classified as Plausible",
                c.vendor.display_name,
                s
            );
        }
    }
}

// =========================================================
// 7) DIFFERENTIAL — our oracle's verdict on each consensus
//    token must match the declared verdict.
// =========================================================
#[test]
fn differential_consensus_tokens_agree() {
    for (name, c) in all_contracts() {
        for case in &c.differential.consensus {
            let want = parse_shape(&case.verdict);
            let got = classify(&name, &case.token, c.property.min_length_for_plausible);
            let pass = match want {
                TokenShape::Decoy => got == TokenShape::Decoy,
                TokenShape::Plausible => matches!(got, TokenShape::Plausible | TokenShape::Suspect),
                TokenShape::Suspect => matches!(got, TokenShape::Suspect | TokenShape::Plausible),
            };
            assert!(
                pass,
                "{}: differential consensus {:?} expected {:?}, got {:?}",
                c.vendor.display_name, case.token, want, got
            );
        }
    }
}

// =========================================================
// 8) PERF — classify must stay within budget per call.
// =========================================================
#[test]
fn perf_classify_under_budget() {
    use std::time::Instant;
    for (name, c) in all_contracts() {
        // Warm.
        for _ in 0..1000 {
            let _ = classify(
                &name,
                "warmup-token-for-warmup-runs-only",
                c.property.min_length_for_plausible,
            );
        }
        // Measure.
        let runs = 10_000;
        let token = c
            .positives
            .sample_tokens
            .first()
            .cloned()
            .unwrap_or_else(|| "x".repeat(60));
        let t0 = Instant::now();
        for _ in 0..runs {
            let _ = classify(&name, &token, c.property.min_length_for_plausible);
        }
        let elapsed_us = t0.elapsed().as_micros() as f32 / runs as f32;
        assert!(
            elapsed_us < c.perf.budget_us_per_classify,
            "{}: classify took {:.2}µs; budget {:.2}µs",
            c.vendor.display_name,
            elapsed_us,
            c.perf.budget_us_per_classify
        );
    }
}

// =========================================================
// 9) SCALE — batch-classify must finish inside wall-clock.
// =========================================================
#[test]
fn scale_batch_classify_under_budget() {
    use std::time::Instant;
    for (name, c) in all_contracts() {
        let token = c
            .positives
            .sample_tokens
            .first()
            .cloned()
            .unwrap_or_else(|| "x".repeat(60));
        let batch = c.scale.batch_size.min(50_000); // cap CI cost
        let t0 = Instant::now();
        let mut n_plausible = 0u64;
        for _ in 0..batch {
            if matches!(
                classify(&name, &token, c.property.min_length_for_plausible),
                TokenShape::Plausible | TokenShape::Suspect
            ) {
                n_plausible += 1;
            }
        }
        let elapsed = t0.elapsed().as_secs_f32();
        // Light correctness assertion: at least 50% of the batch
        // hits the expected shape (we're sending the positive token).
        assert!(
            n_plausible >= (batch as u64) / 2,
            "{}: scale batch produced too few plausibles ({}/{})",
            c.vendor.display_name,
            n_plausible,
            batch
        );
        assert!(
            elapsed < c.scale.budget_seconds,
            "{}: scale batch took {:.2}s; budget {:.2}s",
            c.vendor.display_name,
            elapsed,
            c.scale.budget_seconds
        );
    }
}

// =========================================================
// 10) E2E_CLI — every vendor's contract declares a detector
//     class + expected solve methods. The harness validates
//     the declaration is internally consistent (no method is
//     both expected and forbidden) and the sitekey list is
//     well-formed.
// =========================================================
#[test]
fn e2e_cli_contracts_are_internally_consistent() {
    for (_, c) in all_contracts() {
        for forbidden in &c.e2e_cli.forbidden_methods {
            assert!(
                !c.e2e_cli.expected_solve_methods.contains(forbidden),
                "{}: method {} listed as both expected and forbidden",
                c.vendor.display_name,
                forbidden
            );
        }
        // captcha_type field is well-formed.
        assert!(
            !c.vendor.captcha_type.is_empty(),
            "{}: captcha_type empty",
            c.vendor.display_name
        );
        // api_url looks like a URL.
        assert!(
            c.vendor.api_url.starts_with("https://"),
            "{}: api_url not https: {}",
            c.vendor.display_name,
            c.vendor.api_url
        );
        // Sitekey kinds are recognised.
        let recognised_kinds = ["autopass", "block", "force_interactive"];
        for sk in &c.vendor.public_test_sitekeys {
            assert!(
                recognised_kinds.contains(&sk.kind.as_str()),
                "{}: unrecognised sitekey kind {}",
                c.vendor.display_name,
                sk.kind
            );
        }
    }
}

// =========================================================
// Coverage gate — every shipped vendor in the codebase must
// have a contract TOML. Catches the regression where a new
// vendor solver lands but a contract doesn't.
// =========================================================
#[test]
fn every_shipped_vendor_has_a_contract() {
    let contracts = all_contracts();
    let names: Vec<String> = contracts.iter().map(|(n, _)| n.clone()).collect();
    // Known vendors shipped in src/solver/vendors/. If you add a
    // new vendor solver, add the contract TOML and update this
    // list.
    let required = [
        "turnstile",
        "hcaptcha",
        "recaptcha_v2",
        "recaptcha_v3",
        "arkose",
        "datadome",
        "aws_waf",
        "akamai",
        "perimeterx",
        "geetest",
    ];
    for r in required {
        assert!(
            names.iter().any(|n| n == r),
            "vendor {r} ships in src/solver/vendors/ but has no contract TOML in tests/vendors/"
        );
    }
}

#[test]
fn contract_dir_holds_at_least_ten_vendors() {
    assert!(all_contracts().len() >= 10);
}