Files
shelfmark/tests/download/test_ddg_check_probe_handoff.py
T
CaliBrain 9452ebc70d fix(bypass): stop handing solvers DDoS-Guard's ?check=1 probe URL (#1300)
html_get_page follows Anna's Archive redirects by hand, and DDoS-Guard's
gate
answers /search with a 302 to the same path plus `check=1`. The follower
walks
that handshake by reassigning `current_url`, so every downstream handoff
- the
403 branch, the 503-challenge branch, both redirect-loop rescues -
passed the
*probe* URL to the bypasser rather than the page we actually wanted.

A solver opens that in a fresh browser holding none of the cookies the
probe
exists to collect, so DDoS-Guard cannot verify it automatically and
serves the
manual CAPTCHA page that nothing can solve. The #1292 log is exactly
that: a 403
handed off on `&check=1`, FlareSolverr answering "Challenge solved!",
and a
4721-byte DDOS-GUARD captcha page coming back.

- `_solvable_url()` strips the probe parameter, applied at the single
choke point
in `_run_bypasser` so all four handoffs are covered. Scoped to the hosts
whose
redirects we follow manually; a URL without the parameter is returned by
  identity, so nothing else is re-encoded.

The same reports showed three further defects, all of which stand
whatever the
host was reacting to:

- The external bypasser logged that the solve had not cleared the
protection and
then returned the challenge page as a success. That skipped the one
recovery
left - get_bypassed_page's retry-and-rotate loop, where the next mirror
is a
different DDoS-Guard host - and filed the captcha page's own __ddg
cookies as
that host's clearance, to be replayed on every later request. It now
raises
  ChallengeNotSolvedError before storing anything.

- "Check that the bypasser is reachable and working" was the one piece
of advice
guaranteed to waste the reporter's time: it was reachable, it ran a full
solve,
and it returned a captcha. ChallengeNotSolvedError carries the marker so
the
  search layer can name the host as the cause instead of the bypasser.

- The untabled-page fingerprint logged `attempt_url`, which
html_get_page has
since rotated past. The #1298 bundle reported the page against
annas-archive.gl
when the body had come from .pk - the triage cost #1289 added the line
to
remove. The search now asks for the response URL and logs that. Its
give-up
shape is the tuple ("", url), which is truthy, so the exhaustion check
reads
  the body rather than the response.

Regression fixtures are built from the pages in the reports. The two
behavioural
handoff tests were checked against the unfixed code: both fail there,
reproducing
the reporter's log line verbatim.

Refs #1292
Refs #1298
2026-09-02 16:12:52 -04:00

140 lines
5.0 KiB
Python

"""A solver must never be handed DDoS-Guard's ?check=1 probe URL.
Regression for #1292. `html_get_page` follows Anna's Archive redirects by hand, and
DDoS-Guard's gate answers /search with a 302 to the same path plus `check=1`. Because
the follower walks that handshake by reassigning `current_url`, every downstream handoff
- the 403 branch, the 503-challenge branch, the redirect-loop rescues - passed the
*probe* URL to the bypasser rather than the page we wanted.
A solver opens that in a fresh browser holding none of the cookies the probe exists to
collect, so DDoS-Guard cannot verify it automatically and serves the manual CAPTCHA page
that nothing can solve. The reporter's log is exactly that: a 403 handed off on a
`&check=1` URL, FlareSolverr answering "Challenge solved!", and a 4.7 KB DDOS-GUARD
CAPTCHA page coming back.
"""
import requests
class _FakeResponse:
"""Minimal stand-in for requests.Response covering what html_get_page touches."""
def __init__(
self,
status_code: int,
*,
url: str,
text: str = "",
headers: dict[str, str] | None = None,
cookies: dict[str, str] | None = None,
) -> None:
self.status_code = status_code
self.url = url
self.text = text
self.cookies = cookies or {}
self.headers = {"Content-Type": "text/html;charset=utf-8", **(headers or {})}
self.is_redirect = 300 <= status_code < 400
def raise_for_status(self) -> None:
if self.status_code >= 400:
error = requests.exceptions.HTTPError(f"{self.status_code} Error")
error.response = self
raise error
def _aa_http(monkeypatch):
"""Import http with the network stubbed out and AA treated as an AA host."""
import shelfmark.download.http as http
monkeypatch.setattr(http, "_apply_cf_bypass", lambda _url, _headers: {})
monkeypatch.setattr(http, "get_proxies", lambda _url: {})
monkeypatch.setattr(http, "get_ssl_verify", lambda _url: True)
monkeypatch.setattr(http.time, "sleep", lambda _seconds: None)
monkeypatch.setattr(http, "_is_cf_bypass_enabled", lambda: True)
monkeypatch.setattr(http.network, "should_rotate_dns_for_url", lambda _url: True)
monkeypatch.setattr(http.network, "is_aa_auto_mode", lambda: True)
return http
SEARCH_URL = "https://annas-archive.pk/search?index=&display=table&q=Ken+follett"
PROBE_URL = f"{SEARCH_URL}&check=1"
def test_403_on_the_check_probe_hands_over_the_pre_probe_url(monkeypatch):
"""The reporter's exact sequence: 302 to ?check=1, then 403 on the probe."""
http = _aa_http(monkeypatch)
bypassed: list[str] = []
def fake_get(url: str, **_kwargs):
if "check=1" not in url:
return _FakeResponse(302, url=url, headers={"Location": PROBE_URL})
return _FakeResponse(403, url=url)
monkeypatch.setattr(http.requests, "get", fake_get)
monkeypatch.setattr(
http, "get_bypassed_page", lambda url, *_a, **_k: bypassed.append(url) or "<html>ok</html>"
)
html = http.html_get_page(SEARCH_URL, retry=1, success_delay=0)
assert html == "<html>ok</html>"
# The page we wanted, not the handshake hop we happened to be standing on.
assert bypassed == [SEARCH_URL]
def test_redirect_loop_hands_over_the_pre_probe_url(monkeypatch):
"""Stale clearance turns the gate into an endless ?check=1 bounce."""
http = _aa_http(monkeypatch)
bypassed: list[str] = []
monkeypatch.setattr(
http.requests,
"get",
lambda url, **_kwargs: _FakeResponse(302, url=url, headers={"Location": PROBE_URL}),
)
monkeypatch.setattr(
http, "get_bypassed_page", lambda url, *_a, **_k: bypassed.append(url) or "<html>ok</html>"
)
html = http.html_get_page(SEARCH_URL, retry=1, success_delay=0)
assert html == "<html>ok</html>"
assert bypassed == [SEARCH_URL]
def test_only_the_check_parameter_is_dropped(monkeypatch):
"""Everything else about the URL survives - it is still the search we asked for."""
http = _aa_http(monkeypatch)
url = (
"https://annas-archive.pk/search?index=&page=1&display=table&acc=aa_download"
"&acc=external_download&ext=epub&q=Ken+follett&check=1"
)
assert http._solvable_url(url) == (
"https://annas-archive.pk/search?index=&page=1&display=table&acc=aa_download"
"&acc=external_download&ext=epub&q=Ken+follett"
)
def test_a_url_without_the_probe_is_returned_untouched(monkeypatch):
"""No rewriting, no re-encoding: an unrelated URL must come back identical."""
http = _aa_http(monkeypatch)
url = "https://annas-archive.pk/md5/abc?q=a%20b&empty="
assert http._solvable_url(url) is url
def test_non_aa_hosts_keep_their_check_parameter(monkeypatch):
"""Elsewhere `check` is an ordinary query parameter and none of our business."""
import shelfmark.download.http as http
monkeypatch.setattr(http.network, "should_rotate_dns_for_url", lambda _url: False)
url = "https://example.com/api?check=1"
assert http._solvable_url(url) == url