Likhith Sai Seemala
Deploy IntentForge v2 to HF Spaces
0ca462b
Raw History Blame Contribute Delete
6.65 kB
//! Cloudflare challenge detection
//!
//! Identifies when a response is a Cloudflare challenge page rather than
//! actual content, so we can skip it or escalate to a solver.
/// Types of Cloudflare challenges
#[derive(Debug, Clone, PartialEq)]
pub enum ChallengeType {
/// No challenge detected — response is real content
None,
/// "Just a moment..." JS challenge page
JsChallenge,
/// Turnstile interactive CAPTCHA widget
Turnstile,
/// HTTP 403 with Cloudflare headers
AccessDenied,
/// Rate limited (HTTP 429 or 1015)
RateLimited,
/// HTTP 1020 — site-specific WAF rule
WafBlocked,
}
pub struct CloudflareDetector;
impl CloudflareDetector {
/// Inspect a response body and status to detect Cloudflare challenges.
/// Returns the challenge type, or `None` if the page is real content.
pub fn detect(status: u16, headers: &[(String, String)], body: &str) -> ChallengeType {
// Check server header for cloudflare
let is_cf = headers.iter().any(|(k, v)| {
k.eq_ignore_ascii_case("server") && v.to_lowercase().contains("cloudflare")
});
// Check for cf-mitigated header (CF 2024+)
let is_cf_mitigated = headers.iter().any(|(k, v)| {
k.eq_ignore_ascii_case("cf-mitigated") && v.to_lowercase().contains("challenge")
});
// 1020 = Access Denied (site WAF rule)
if status == 1020 || (status == 403 && body.contains("error code: 1020")) {
return ChallengeType::WafBlocked;
}
// 429 or 1015 = rate limited
if status == 429 || (status == 403 && body.contains("error code: 1015")) {
return ChallengeType::RateLimited;
}
// 403 with Cloudflare = access denied
if status == 403 && is_cf {
// Check if it's a challenge page vs plain denial
if body.contains("Just a moment...") || body.contains("cf_chl_opt") {
return ChallengeType::JsChallenge;
}
return ChallengeType::AccessDenied;
}
// 403/503 with cf-mitigated header
if (status == 403 || status == 503) && is_cf_mitigated {
return ChallengeType::JsChallenge;
}
// 503 with Cloudflare = usually JS challenge
if status == 503
&& is_cf
&& (body.contains("Just a moment...") || body.contains("Checking your browser"))
{
return ChallengeType::JsChallenge;
}
// 200 but body is a Cloudflare challenge page
// (Cloudflare sometimes returns 200 with challenge HTML)
if is_cf && Self::is_challenge_html(body) {
if body.contains("turnstile") || body.contains("cf-turnstile") {
return ChallengeType::Turnstile;
}
return ChallengeType::JsChallenge;
}
ChallengeType::None
}
/// Check if an HTML body is a Cloudflare challenge page
fn is_challenge_html(body: &str) -> bool {
// Cloudflare challenge page indicators
let indicators = [
"Just a moment...",
"Checking your browser",
"cf_chl_opt",
"challenge-platform",
"__cf_chl_f_tk",
"managed-challenge",
"cf-turnstile",
"Attention Required! | Cloudflare",
"Ray ID:",
"cf-browser-verification",
];
// Only check first 5KB — challenge pages are detected early
let snippet = &body[..body.len().min(5120)];
indicators.iter().any(|i| snippet.contains(i))
}
/// Check if body is the "Just a moment..." Cloudflare interstitial
pub fn is_just_a_moment(body: &str) -> bool {
body.contains("Just a moment...") && body.contains("cloudflare")
}
/// Check if a URL's response is usable content (not a challenge)
pub fn is_usable_content(status: u16, body: &str) -> bool {
if status != 200 {
return false;
}
// Reject if body is too short (likely challenge/error)
if body.len() < 200 {
return false;
}
// Reject if it's a Cloudflare challenge page
if Self::is_challenge_html(body) {
return false;
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_detect_just_a_moment() {
let body = r#"<html><head><title>Just a moment...</title></head>
<body>Checking your browser... <div id="challenge-platform"></div></body></html>"#;
let headers = vec![
("server".to_string(), "cloudflare".to_string()),
("cf-mitigated".to_string(), "challenge".to_string()),
];
assert_eq!(
CloudflareDetector::detect(403, &headers, body),
ChallengeType::JsChallenge
);
}
#[test]
fn test_detect_turnstile() {
let body = r#"<html><div class="cf-turnstile" data-sitekey="abc"></div></html>"#;
let headers = vec![("server".to_string(), "cloudflare".to_string())];
assert_eq!(
CloudflareDetector::detect(200, &headers, body),
ChallengeType::Turnstile
);
}
#[test]
fn test_detect_rate_limited() {
let body = "error code: 1015 You are being rate limited";
let headers = vec![("server".to_string(), "cloudflare".to_string())];
assert_eq!(
CloudflareDetector::detect(403, &headers, body),
ChallengeType::RateLimited
);
}
#[test]
fn test_detect_waf_blocked() {
let body = "error code: 1020";
let headers = vec![("server".to_string(), "cloudflare".to_string())];
assert_eq!(
CloudflareDetector::detect(403, &headers, body),
ChallengeType::WafBlocked
);
}
#[test]
fn test_no_challenge_normal_page() {
let body = "<html><head><title>My Blog</title></head><body><p>Content</p></body></html>";
let headers = vec![];
assert_eq!(
CloudflareDetector::detect(200, &headers, body),
ChallengeType::None
);
}
#[test]
fn test_is_usable_content() {
assert!(CloudflareDetector::is_usable_content(
200,
&("<html><body>".to_string() + &"x".repeat(200) + "</body></html>")
));
assert!(!CloudflareDetector::is_usable_content(403, "blocked"));
assert!(!CloudflareDetector::is_usable_content(
200,
"Just a moment... cloudflare"
));
assert!(!CloudflareDetector::is_usable_content(200, "short"));
}
}