Skip to content

misterdownloadmanager: The ultimate open-source, clean-room download manager written from scratch in pure Rust for Windows, Mac, and Linux

A site's bot check: say so instead of "403 Forbidden" (#376) (#390)

Ahmed Nefzaouia6ed6a19dbfd
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bc19c900..56df3058 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see
### Fixed
+- A download refused by a site's bot check (Cloudflare's challenge) says so and how to get the file, instead of "the server answered 403 Forbidden" ([#376](https://github.com/anefzaoui/misterdownloadmanager/issues/376)).
- **Open folder** works on a torrent that is still downloading ([#354](https://github.com/anefzaoui/misterdownloadmanager/issues/354)).
- **Alt** and **F10** open the menu bar while a button or the list has the keyboard focus, not only when nothing does ([#328](https://github.com/anefzaoui/misterdownloadmanager/issues/328)).
- Windows: no more crash on a computer where Direct3D can't start, when a window opened during the switch to OpenGL ([#337](https://github.com/anefzaoui/misterdownloadmanager/issues/337)).
diff --git a/ROADMAP.md b/ROADMAP.md
index 07eac297..9bd17337 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -463,6 +463,7 @@ These ship in a later version, when they're ready.
### Bugs being looked into
+- πŸ” A site's bot check refusing MDM's request (testfile.org, a Cloudflare challenge): the\n message now says so ([#376](https://github.com/anefzaoui/misterdownloadmanager/issues/376)); a way to hand the download back to the browser is still to decide
- πŸ”„ A download starting again by itself after Stop, when it is in a running queue\n ([#341](https://github.com/anefzaoui/misterdownloadmanager/issues/341)): fix in review
- πŸ”„ A crash when removing the last feed in the RSS feeds window
([#321](https://github.com/anefzaoui/misterdownloadmanager/issues/321)): fix in review
diff --git a/crates/app/src/errors.rs b/crates/app/src/errors.rs
index f19d7280..2dc37ed6 100644
--- a/crates/app/src/errors.rs
+++ b/crates/app/src/errors.rs
@@ -15,6 +15,8 @@ pub fn describe(e: &Error) -> String {
t("the server answered {status}"),
&[("status", &ltr(&s.to_string()))],
),
+ // i18n: why a download failed: the site's bot check (Cloudflare) lets browsers through, not programs. Alt+click: the link clicked with the Alt key held, which leaves the download to the browser
+ Error::BotCheck => t("this site lets only browsers through; Alt+click the link").to_owned(),
Error::Throttled(s, _) => fmt(
t("the server is busy ({status}); try again later"),
&[("status", &ltr(&s.to_string()))],
diff --git a/crates/engine/src/download.rs b/crates/engine/src/download.rs
index b01d2215..86c198c0 100644
--- a/crates/engine/src/download.rs
+++ b/crates/engine/src/download.rs
@@ -245,6 +245,10 @@ pub enum Error {
Http(Arc<reqwest::Error>),
#[error("server answered {0}")]
Status(StatusCode),
+ /// The site's bot check refused the request: it lets browsers through, not programs (#376).
+ /// Cloudflare says so in its answer (`cf-mitigated: challenge`).
+ #[error("the site's bot check refused the download")]
+ BotCheck,
/// 429 / 503: the server is busy; retry after its `Retry-After` when it gives one.
#[error("server busy ({0})")]
Throttled(StatusCode, Option<Duration>),
@@ -336,7 +340,7 @@ impl Error {
/// The address no longer serves this file (or refuses us): retrying won't help.
fn link_dead(&self) -> bool {
matches!(self, Error::Status(s) if matches!(s.as_u16(), 401 | 403 | 404 | 410 | 416))
- || matches!(self, Error::GotWebPage | Error::LinkExpired)
+ || matches!(self, Error::GotWebPage | Error::LinkExpired | Error::BotCheck)
// A site's refusal; a service's, unless waiting helps (#44).
|| matches!(self, Error::Refused(r) if !matches!(r, Refusal::Service { fault, .. } if fault.retry_wait().is_some()))
// Login refused; no such file (or no permission); a name the server won't take.
@@ -2297,6 +2301,7 @@ async fn get(
s if s == StatusCode::TOO_MANY_REQUESTS || s == StatusCode::SERVICE_UNAVAILABLE => {
return Err(Error::Throttled(s, retry_after(resp.headers())));
}
+ s if is_bot_check(s, resp.headers()) => return Err(Error::BotCheck),
s => return Err(Error::Status(s)),
}
// Where the HTTP redirects ended: the headers' credentials follow as reqwest's did.
@@ -2909,6 +2914,13 @@ fn header_str(h: &HeaderMap, name: header::HeaderName) -> Option<&str> {
h.get(name).and_then(|v| v.to_str().ok())
}
+/// A site's bot check turning the request away (#376), as Cloudflare marks it.
+fn is_bot_check(status: StatusCode, h: &HeaderMap) -> bool {
+ status == StatusCode::FORBIDDEN
+ && h.get("cf-mitigated")
+ .is_some_and(|v| v.as_bytes().eq_ignore_ascii_case(b"challenge"))
+}
+
fn is_web_page(h: &HeaderMap) -> bool {
h.get(header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
diff --git a/crates/engine/tests/post.rs b/crates/engine/tests/post.rs
index a44373dc..2d1d5a7a 100644
--- a/crates/engine/tests/post.rs
+++ b/crates/engine/tests/post.rs
@@ -216,6 +216,40 @@ async fn fetch(s: &Server, path: &str, opts: Options, want: &[u8]) -> String {
out.path.file_name().unwrap().to_string_lossy().into_owned()
}
+/// #376: Cloudflare's challenge, as it marks it, is reported as the site's bot check, with no
+/// retries; another 403 stays a 403.
+#[tokio::test]
+async fn a_bot_check_is_named() {
+ let s = Server::start(|seen| {
+ let headers = if seen.path.starts_with("/challenge") {
+ vec![("Cf-Mitigated".into(), "challenge".into())]
+ } else {
+ Vec::new()
+ };
+ (403, headers, b"forbidden".to_vec())
+ })
+ .await;
+ let dir = tempfile::tempdir().unwrap();
+ let attempt = |path: &str| {
+ Download::start(
+ Request {
+ url: s.url(path),
+ headers: HeaderMap::new(),
+ dest: Destination::Dir(dir.path().to_owned()),
+ },
+ Options::default(),
+ )
+ };
+ let challenge = attempt("/challenge/f.dat").wait().await.unwrap_err();
+ assert!(
+ matches!(challenge, mdm_engine::Error::BotCheck),
+ "{challenge:?}"
+ );
+ assert_eq!(s.seen.lock().unwrap().len(), 1, "a bot check isn't retried");
+ let plain = attempt("/plain/f.dat").wait().await.unwrap_err();
+ assert!(matches!(plain, mdm_engine::Error::Status(_)), "{plain:?}");
+}
+
fn is_form_post(r: &Seen) -> bool {
r.method == "POST"
&& r.body == FORM
diff --git a/i18n/en.tsv b/i18n/en.tsv
index cc729934..57f710f8 100644
--- a/i18n/en.tsv
+++ b/i18n/en.tsv
@@ -3388,6 +3388,9 @@
#. error: a program chosen as yt-dlp or ffmpeg didn't answer as one; {name} = yt-dlp or ffmpeg
#. {name} = a file name (keep the braces as they are)
this program isn't {name} this program isn't {name}
+#. text in errors.rs
+#. why a download failed: the site's bot check (Cloudflare) lets browsers through, not programs. Alt+click: the link clicked with the Alt key held, which leaves the download to the browser
+ this site lets only browsers through; Alt+click the link this site lets only browsers through; Alt+click the link
#. text in a download's progress window
This system has no dial-up connections to hang up This system has no dial-up connections to hang up
#. text in errors.rs