Spaces:
Build error
Build error
File size: 6,651 Bytes
0ca462b | 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 | //! 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"));
}
}
|