fix(bypass): pre-resolve Chrome host rules with the system resolver's address family (#1743)

> This PR was investigated, written and opened by an AI agent acting
autonomously on behalf of the fork owner.

## Problem

On a host without IPv4 connectivity (for example an IPv6-only Kubernetes
cluster that reaches IPv4 sites through NAT64/DNS64), every Anna's
Archive search via the internal bypasser comes back empty and the AA
mirrors get quarantined with "responded without an Anna's Archive page".

The page the bypasser hands back is Chromium's own error page:

```
Search page has no results table: https://annas-archive.pk/search?... (bytes=252838, title='annas-archive.pk', aa_markers=none, challenge_marker=None, over_challenge_size_cap=True)
```

Its body is "This site can't be reached ... ERR_ADDRESS_UNREACHABLE".
Because it is over the 64 KB challenge cap and carries no AA markers, it
is classified as "not AA" and the mirror is rotated/quarantined, so the
user only ever sees "No books found".

## Cause

`_build_host_resolver_rules` pre-resolves AA hostnames with
`socket.getaddrinfo(hostname, 443, socket.AF_INET)` and pins the result
into Chrome with `--host-resolver-rules=MAP <host> <ip>`. Forcing
`AF_INET` returns the raw A record even when the system resolver would
hand back a reachable address:

```
annas-archive.pk AF_INET: 186.2.163.174 | default: 64:ff9b::ba02:a3ae
```

So Chrome is told to connect to an IPv4 address the host has no route
to. Plain `requests` traffic is unaffected because it uses the default
lookup.

## Fix

Resolve without restricting the address family and use the first address
the system resolver returns, bracketing IPv6 literals as Chrome's `MAP`
rule syntax requires. Behaviour on IPv4 hosts is unchanged (the first
result is still the A record there).

## Verification

- New unit test
`test_host_resolver_rules_use_the_system_resolvers_address_family`
covers both an IPv6 (NAT64) and an IPv4 answer; it fails on `main` and
passes with the fix.
- `pytest tests/ -m "not integration and not e2e"`: 6110 passed. `ruff
check`, `ruff format --check` and `basedpyright` (source and tests) are
clean.
- End to end on the affected IPv6-only deployment (v1.5.1 image, with
the patched function applied in-process): Chrome reached
`annas-archive.pk`, the DDoS-Guard challenge cleared itself, and the
returned page was `Elantris - Search - Anna's Archive` with a results
table. Before the patch the same call returned the
`ERR_ADDRESS_UNREACHABLE` page.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Improved host resolution to support IPv6 addresses alongside IPv4 when
configuring browser connections.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: Joshua Tag Howard <git@jthoward.dev>
This commit is contained in:
Tag HowardandJoshua Tag Howard authored and GitHub committed 2026-11-20 23:42:11 -05:00
1 parent d3045c40b4
commit 4ed85f9782
2 files changed
+35 -3

No files matched your search

+5 -3
View File
@@ -846,10 +846,12 @@ def _build_host_resolver_rules() -> list[str]:
continue
try:
results = socket.getaddrinfo(hostname, 443, socket.AF_INET)
results = socket.getaddrinfo(hostname, 443, type=socket.SOCK_STREAM)
if results:
ip = results[0][4][0]
host_rules.append(f"MAP {hostname} {ip}")
family, _, _, _, sockaddr = results[0]
ip = str(sockaddr[0])
target = f"[{ip}]" if family == socket.AF_INET6 else ip
host_rules.append(f"MAP {hostname} {target}")
logger.debug("Chrome: Pre-resolved %s -> %s", hostname, ip)
else:
logger.warning("Chrome: No addresses returned for %s", hostname)
+30
View File
@@ -747,3 +747,33 @@ def test_helper_watchdog_waits_while_its_parent_is_alive(monkeypatch):
internal_bypasser._watch_parent_process(999, interval=0.5)
assert sleeps == [0.5, 0.5]
def test_host_resolver_rules_use_the_system_resolvers_address_family(monkeypatch):
"""An IPv6-only host (e.g. NAT64) must not be pinned to an unreachable IPv4 address."""
import socket
import shelfmark.bypass.internal_bypasser as internal_bypasser
answers = {
"aa.v6": [(socket.AF_INET6, socket.SOCK_STREAM, 6, "", ("64:ff9b::ba02:a3ae", 443, 0, 0))],
"aa.v4": [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("186.2.163.174", 443))],
}
calls: list[dict[str, object]] = []
def _getaddrinfo(host, port, *args, **kwargs):
calls.append({"args": args, **kwargs})
return answers[host]
monkeypatch.setattr(
internal_bypasser.network,
"get_available_aa_urls",
lambda: ["https://aa.v6", "https://aa.v4"],
)
monkeypatch.setattr(internal_bypasser.socket, "getaddrinfo", _getaddrinfo)
assert internal_bypasser._build_host_resolver_rules() == [
"MAP aa.v6 [64:ff9b::ba02:a3ae]",
"MAP aa.v4 186.2.163.174",
]
assert all(socket.AF_INET not in call["args"] and "family" not in call for call in calls)