Skip to content

The agent that grows with you

fix: scope the no-cf-cache-status block signal to our R2 host; keep it on 429s

Tekniumec5fa1e437a8
diff --git a/pm/downloader.py b/pm/downloader.py
index 24e33613f5b..3c7dfed6275 100644
--- a/pm/downloader.py
+++ b/pm/downloader.py
@@ -137,8 +137,8 @@ class DownloadTransportError(DownloadError):
self.fallback_allowed = (
self.status in (401, 403, 404, 410) or unresolved or is_transient(cause)
)
- refused = edge_block(cause) or "the host refused access"
- reason = f"{cause}; {refused}" if self.status in (401, 403) else str(cause)
+ refused = edge_block(cause) or ("the host refused access" if self.status in (401, 403) else None)
+ reason = f"{cause}; {refused}" if refused else str(cause)
super().__init__(f"download failed from {url}: {reason}")
diff --git a/pm/network.py b/pm/network.py
index 3baab827036..aa399a07642 100644
--- a/pm/network.py
+++ b/pm/network.py
@@ -18,6 +18,7 @@ _T = TypeVar("_T")
_ATTEMPTS = 4
_MAX_DELAY = 30.0
_HTTP_RETRY = frozenset({408, 429, 500, 502, 503, 504})
+_R2_HOSTS = frozenset({"hermes-assets.nousresearch.com"})
_NETWORK_ERRNOS = frozenset({
errno.ECONNABORTED, errno.ECONNREFUSED, errno.ECONNRESET,
errno.ENETDOWN, errno.ENETRESET, errno.ENETUNREACH,
@@ -45,22 +46,25 @@ def edge_block(exc: BaseException) -> str | None:
"""Explain a 403/429 that a CDN firewall sent instead of the server, else None.
A bare "HTTP 403" from a regional block reads as a broken install or a missing
- file (#128295). Cloudflare marks challenges with ``cf-mitigated``, and on an R2
- custom domain a response without ``cf-cache-status`` never reached the bucket
- (Cloudflare R2 troubleshooting docs); Vercel's firewall sets ``x-vercel-mitigated``.
+ file (#128295). Cloudflare marks challenges with ``cf-mitigated`` and Vercel's
+ firewall sets ``x-vercel-mitigated``. Cloudflare's own block page carries
+ neither, but on our R2 custom domain a response without ``cf-cache-status``
+ never reached the bucket (Cloudflare R2 troubleshooting docs); other hosts
+ behind Cloudflare make no such promise, so that fallback is R2-only.
"""
if not isinstance(exc, urllib.error.HTTPError) or exc.code not in (403, 429):
return None
headers = exc.headers or {}
+ host = urllib.parse.urlsplit(exc.filename or "").hostname or ""
if headers.get("x-vercel-mitigated"):
cdn = "Vercel"
elif headers.get("cf-mitigated") or (
- (headers.get("server") or "").lower() == "cloudflare" and not headers.get("cf-cache-status")):
+ host in _R2_HOSTS and (headers.get("server") or "").lower() == "cloudflare"
+ and not headers.get("cf-cache-status")):
cdn = "Cloudflare"
else:
return None
- host = urllib.parse.urlsplit(exc.filename or "").hostname or "the server"
- return (f"{host}'s CDN firewall ({cdn}) refused this network. Your install is fine; "
+ return (f"{host or 'the server'}'s CDN firewall ({cdn}) refused this network. Your install is fine; "
"retry from another network, or with your VPN or proxy turned off")
diff --git a/tests/hermes_cli/test_release_channels.py b/tests/hermes_cli/test_release_channels.py
index a25b63e5432..2a24284ce74 100644
--- a/tests/hermes_cli/test_release_channels.py
+++ b/tests/hermes_cli/test_release_channels.py
@@ -141,12 +141,13 @@ def test_passive_reads_and_missing_objects_make_one_attempt(monkeypatch):
assert waits == []
[email protected](("headers", "blocked"), [
- ({"Server": "cloudflare", "Content-Type": "text/html"}, True),
- ({"Server": "cloudflare", "cf-cache-status": "DYNAMIC"}, False),
[email protected](("base", "headers", "blocked"), [
+ ("https://hermes-assets.nousresearch.com", {"Server": "cloudflare", "Content-Type": "text/html"}, True),
+ ("https://hermes-assets.nousresearch.com", {"Server": "cloudflare", "cf-cache-status": "DYNAMIC"}, False),
+ ("https://other-r2.example", {"Server": "cloudflare", "Content-Type": "text/html"}, False),
])
-def test_cdn_firewall_403_names_the_network_not_the_install(headers, blocked):
- """#128295: a Cloudflare block that never reached the bucket must not read as a bare HTTP 403."""
+def test_cdn_firewall_403_names_the_network_not_the_install(base, headers, blocked):
+ """#128295: a Cloudflare block that never reached our bucket must not read as a bare HTTP 403."""
from email.message import Message
from urllib.error import HTTPError
from hermes_cli.release_channels import ChannelError, ChannelReader
@@ -159,8 +160,8 @@ def test_cdn_firewall_403_names_the_network_not_the_install(headers, blocked):
raise HTTPError(request.full_url, 403, "Forbidden", hdrs, None)
with pytest.raises(ChannelError) as raised:
- ChannelReader("https://hermes-assets.example", opener=opener).read_bytes("releases/channels/pm-preview.json")
- assert ("hermes-assets.example's CDN firewall (Cloudflare) refused this network" in str(raised.value)) is blocked
+ ChannelReader(base, opener=opener).read_bytes("releases/channels/pm-preview.json")
+ assert ("CDN firewall (Cloudflare) refused this network" in str(raised.value)) is blocked
assert str(raised.value).startswith("Channel read unavailable: HTTP 403")
diff --git a/tests/pm/test_network.py b/tests/pm/test_network.py
index dd4a1aa092c..06164c54742 100644
--- a/tests/pm/test_network.py
+++ b/tests/pm/test_network.py
@@ -95,18 +95,22 @@ def test_retry_after_is_bounded_and_interruptible(monkeypatch, retry_after, mini
@pytest.mark.parametrize("status,headers,cdn", [
pytest.param(403, {"Server": "Vercel", "x-vercel-mitigated": "deny"}, "Vercel", id="vercel-deny"),
+ pytest.param(429, {"Server": "Vercel", "x-vercel-mitigated": "challenge"}, "Vercel", id="vercel-challenge-429"),
pytest.param(403, {"Server": "cloudflare", "cf-mitigated": "challenge", "cf-cache-status": "DYNAMIC"},
"Cloudflare", id="cloudflare-challenge"),
+ pytest.param(403, {"Server": "cloudflare"}, None, id="cloudflare-origin-not-r2"),
pytest.param(404, {"Server": "cloudflare"}, None, id="missing-object"),
pytest.param(403, {"Server": "AmazonS3"}, None, id="origin-403"),
])
-def test_edge_block_only_names_a_cdn_firewall_refusal(status, headers, cdn):
+def test_download_failure_names_only_a_cdn_firewall_refusal(status, headers, cdn):
+ from pm.downloader import DownloadTransportError
+
hdrs = Message()
for key, value in headers.items():
hdrs[key] = value
- error = urllib.error.HTTPError("https://portal.example/api/x", status, "failed", hdrs, None)
- message = network.edge_block(error)
+ url = "https://mirror.example/x"
+ message = str(DownloadTransportError(url, urllib.error.HTTPError(url, status, "failed", hdrs, None)))
if cdn is None:
- assert message is None
+ assert "CDN firewall" not in message
else:
- assert (message or "").startswith(f"portal.example's CDN firewall ({cdn})"), message
+ assert f"mirror.example's CDN firewall ({cdn}) refused this network" in message, message