Skip to content

Commit 1b63d53

Browse files
voidstackloopclaude
andcommitted
Add an eighth check: bot-protection/CAPTCHA detection
Requested as this session's next large feature. Many .gov sites sit behind Cloudflare Turnstile, reCAPTCHA, or hCaptcha. Right now that silently breaks every other check ("No <form> element found") with no indication of why — a real, misleading finding that implies the form is broken when the actual cause is automated testing getting blocked before it ever reached the real page. check_bot_protection detects: - reCAPTCHA / hCaptcha / Cloudflare Turnstile via their standard widget markup, iframe/script src patterns, or global JS objects (window.grecaptcha / window.hcaptcha). - A full-page Cloudflare JS-challenge interstitial ("Just a moment...", "Checking your browser..."). - Generic "verify you're human" / "are you a robot" wording as a fallback when none of the above match anything specific. Always Warn, never Fail: none of this is evidence the *form* itself is broken (a real person, unlike a headless browser, passes a CAPTCHA fine) — it's a heuristic limit on what formwatch could verify, exactly what Warn already means elsewhere in this codebase. Distinguishes "challenge widget on an otherwise-present, testable form" (submission just can't be tested with --submit) from "full-page block, no form visible at all" (testing never got anywhere near the real page), since those call for different reactions from whoever reads the report. Added as the first entry in run_all's result vec (before Submission flow) since "is this even the real page" is conceptually the first question. Gets a screenshot on Warn for free via run_safely, same as every other check. Verified all three outcomes (widget-on-real-form Warn, full-page-block Warn, ordinary-form Pass) are meaningful by mutating check_bot_protection three separate ways — forcing blocking to false, to true, and forcing `found` to always be non-empty — and confirming each predicted test failure occurred exactly as expected, before restoring the real logic. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 8f98d6d commit 1b63d53

6 files changed

Lines changed: 183 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ project doesn't have a release yet, so everything below is grouped under
4646
before reporting "Page load: Fail" — a transient DNS blip or dropped
4747
connection against a real site no longer gets mistaken for the form
4848
itself being down.
49+
- An eighth check, **Bot protection**: detects reCAPTCHA, hCaptcha,
50+
Cloudflare Turnstile, or a generic "verify you're human" interstitial.
51+
Never a Fail — none of this is evidence the form itself is broken, a
52+
real person sails through a CAPTCHA fine — but without it, a page
53+
stuck behind a challenge just looked like "No `<form>` element
54+
found," a real finding with the wrong explanation.
4955

5056
### Fixed
5157

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ for why it's built the way it is.
6161
checks the value is still there and whether the page started showing
6262
session-timeout wording. A real 15-30 minute timeout isn't waited out by
6363
default; pass a longer `--wait` to test that for real.
64+
- **Bot protection** — detects reCAPTCHA, hCaptcha, Cloudflare Turnstile, or
65+
a generic "verify you're human" challenge. Never a Fail — none of this
66+
is evidence the *form* is broken, a real person sails through a CAPTCHA
67+
fine — but without this check, a page stuck behind Cloudflare's "Just a
68+
moment..." interstitial just looks like "no `<form>` found," a real
69+
finding with the wrong explanation.
6470

6571
## Commands
6672

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head><meta charset="utf-8"><title>Just a moment...</title></head>
4+
<body>
5+
<!-- A full-page Cloudflare JS-challenge interstitial — no real form at
6+
all, since it's blocking access to the site entirely. This is what
7+
an automated tool actually sees when a site under Cloudflare's "I'm
8+
Under Attack Mode" (or similar) blocks it before the real page. -->
9+
<p>Checking your browser before accessing the site.</p>
10+
<p>This process is automatic. You will be redirected once the check is complete.</p>
11+
</body>
12+
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head><meta charset="utf-8"><title>Permit Application</title></head>
4+
<body>
5+
<!-- A real form that also embeds a reCAPTCHA widget — common on real
6+
government forms. The form itself is present and testable, but
7+
real submission (--submit) would be blocked by the challenge. -->
8+
<form>
9+
<label for="fullname">Full name</label>
10+
<input id="fullname" name="fullname" type="text">
11+
12+
<div class="g-recaptcha" data-sitekey="fake-key-for-testing"></div>
13+
14+
<button type="submit">Submit</button>
15+
</form>
16+
</body>
17+
</html>

‎src/checks.rs‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,84 @@ pub async fn check_autofill_hints(page: &Page) -> Result<CheckResult> {
904904
))
905905
}
906906

907+
/// Detects a bot-protection/CAPTCHA challenge (reCAPTCHA, hCaptcha,
908+
/// Cloudflare Turnstile, or a generic "verify you're human" interstitial)
909+
/// on the page. Never a Fail: none of this is evidence the *form* is
910+
/// broken — a real human, unlike a headless browser, sails through a
911+
/// CAPTCHA fine — it's a heuristic limit on what formwatch itself could
912+
/// verify, exactly the kind of thing `Status::Warn` exists for. Without
913+
/// this, a page sitting behind Cloudflare's "Just a moment..." challenge
914+
/// just looks like "No `<form>` element found": a real, misleading
915+
/// finding with the wrong explanation.
916+
pub async fn check_bot_protection(page: &Page) -> Result<CheckResult> {
917+
let detail: String = page
918+
.evaluate(
919+
r#"(() => {
920+
const title = document.title.toLowerCase();
921+
const bodyText = document.body.innerText.toLowerCase();
922+
923+
const found = [];
924+
if (document.querySelector('.g-recaptcha, iframe[src*="recaptcha"], script[src*="recaptcha"]')
925+
|| typeof window.grecaptcha !== 'undefined') found.push('reCAPTCHA');
926+
if (document.querySelector('.h-captcha, iframe[src*="hcaptcha"], script[src*="hcaptcha"]')
927+
|| typeof window.hcaptcha !== 'undefined') found.push('hCaptcha');
928+
if (document.querySelector('.cf-turnstile, iframe[src*="challenges.cloudflare.com"]')) found.push('Cloudflare Turnstile');
929+
const cloudflareChallengePage = title.includes('just a moment')
930+
|| bodyText.includes('checking your browser')
931+
|| bodyText.includes('checking if the site connection is secure');
932+
if (cloudflareChallengePage) found.push('Cloudflare challenge page');
933+
if (found.length === 0 && /verify you.{0,3}re (a )?human|are you a robot|prove you.{0,3}re not a robot|complete the (security check|challenge)/.test(bodyText)) {
934+
found.push('generic bot-check wording');
935+
}
936+
937+
if (found.length === 0) return 'none';
938+
const formStillPresent = document.querySelector('form') !== null;
939+
const blocking = cloudflareChallengePage || !formStillPresent;
940+
return JSON.stringify({ found, blocking });
941+
})()"#,
942+
)
943+
.await?
944+
.into_value()?;
945+
946+
if detail == "none" {
947+
return Ok(result(
948+
"Bot protection",
949+
Status::Pass,
950+
"No bot-protection challenge detected.",
951+
));
952+
}
953+
954+
let parsed: serde_json::Value = serde_json::from_str(&detail)?;
955+
let found: Vec<String> = parsed["found"]
956+
.as_array()
957+
.map(|a| {
958+
a.iter()
959+
.filter_map(|v| v.as_str().map(str::to_string))
960+
.collect()
961+
})
962+
.unwrap_or_default();
963+
let blocking = parsed["blocking"].as_bool().unwrap_or(false);
964+
let names = found.join(", ");
965+
966+
Ok(result(
967+
"Bot protection",
968+
Status::Warn,
969+
if blocking {
970+
format!(
971+
"{names} detected, with no form visible — automated testing was likely blocked \
972+
before it ever reached the real form. This isn't evidence the form itself is \
973+
broken; a real user (unlike formwatch) would pass this challenge."
974+
)
975+
} else {
976+
format!(
977+
"{names} detected on the page. The form itself was still found and checked, but \
978+
real submission (--submit) will be blocked by the challenge — formwatch can't \
979+
and shouldn't try to solve it."
980+
)
981+
},
982+
))
983+
}
984+
907985
/// Default ceiling for a single check — generous for a slow real-world
908986
/// page, but bounded. `check_submission_flow` gets its own longer
909987
/// ceiling (an 8-step wizard can legitimately take a while) and
@@ -1033,6 +1111,13 @@ pub async fn run_all(page: &Page, allow_submit: bool, wait_secs: u64) -> Vec<Che
10331111
}
10341112

10351113
vec![
1114+
run_safely(
1115+
page,
1116+
"Bot protection",
1117+
CHECK_TIMEOUT,
1118+
check_bot_protection(page),
1119+
)
1120+
.await,
10361121
submission,
10371122
run_safely(
10381123
page,

‎tests/checks_test.rs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,3 +644,60 @@ async fn closed_shadow_root_degrades_to_a_clean_no_form_found_not_a_crash() {
644644
result.detail
645645
);
646646
}
647+
648+
#[tokio::test]
649+
async fn recaptcha_widget_on_a_real_form_warns_but_still_finds_the_form() {
650+
// fixtures/recaptcha-widget-form.html has a real, testable form that
651+
// also embeds a reCAPTCHA widget — common on real government forms.
652+
// This should Warn (not Fail: it's not evidence the form is broken)
653+
// and note the form itself was still found.
654+
let (_browser, page, _handle) = open_fixture("recaptcha-widget-form.html").await;
655+
let result = checks::check_bot_protection(&page)
656+
.await
657+
.expect("check_bot_protection");
658+
assert_eq!(result.status, checks::Status::Warn, "got: {result:?}");
659+
assert!(
660+
result.detail.contains("reCAPTCHA"),
661+
"got: {}",
662+
result.detail
663+
);
664+
assert!(
665+
result.detail.contains("form itself was still found"),
666+
"got: {}",
667+
result.detail
668+
);
669+
}
670+
671+
#[tokio::test]
672+
async fn cloudflare_challenge_page_is_reported_as_blocking_not_a_broken_form() {
673+
// fixtures/cloudflare-challenge-page.html mimics the full-page "Just
674+
// a moment..." interstitial Cloudflare shows instead of the real
675+
// site — no <form> at all, since access itself is blocked. Without
676+
// this check, check_submission_flow's "No <form> element found"
677+
// would be the only signal, misleadingly implying the form is broken
678+
// rather than "automated testing got blocked before reaching it."
679+
let (_browser, page, _handle) = open_fixture("cloudflare-challenge-page.html").await;
680+
let result = checks::check_bot_protection(&page)
681+
.await
682+
.expect("check_bot_protection");
683+
assert_eq!(result.status, checks::Status::Warn, "got: {result:?}");
684+
assert!(
685+
result.detail.contains("Cloudflare challenge page"),
686+
"got: {}",
687+
result.detail
688+
);
689+
assert!(
690+
result.detail.contains("no form visible"),
691+
"got: {}",
692+
result.detail
693+
);
694+
}
695+
696+
#[tokio::test]
697+
async fn ordinary_form_with_no_challenge_passes_bot_protection() {
698+
let (_browser, page, _handle) = open_fixture("test-form.html").await;
699+
let result = checks::check_bot_protection(&page)
700+
.await
701+
.expect("check_bot_protection");
702+
assert_eq!(result.status, checks::Status::Pass, "got: {result:?}");
703+
}

0 commit comments

Comments
 (0)