Stealth Chromium that passes every bot detection test. Drop-in Playwright replacement with source-level fingerprint patches. 50+ tests passed.
Python ai-agentsanti-detectantidetect-browserbot-detectionbrowser-automationcaptcha-bypasschromiumcloudflarecloudflare-bypassfingerprintheadless-browserplaywrightpuppeteerpythonrecaptchaseleniumstealth-browserundetectedweb-scrapingwebscraping
fix(geoip): don't fall back to a private proxy address (#631)
Alexandr Kazmin94c972500147
diff --git a/cloakbrowser/geoip.py b/cloakbrowser/geoip.pyindex f834d75..1a890c9 100644--- a/cloakbrowser/geoip.py+++ b/cloakbrowser/geoip.py@@ -123,9 +123,14 @@ def resolve_proxy_geo_with_ip(# a DB hiccup would let WebRTC fall back to the real IP behind a proxy while# the connection shows the proxy IP — a real deanonymization.ip = _resolve_exit_ip(proxy_url, timeout=_remaining_seconds(deadline))- # Hostname fallback only applies to a proxy; no proxy → echo services only+ # Hostname fallback only applies to a proxy; no proxy → echo services only.+ # A private address (a local forwarding proxy, a LAN gateway) is never the+ # exit, so it would only produce a misleading lookup error.if ip is None and proxy_url and not _deadline_expired(deadline):ip = _resolve_proxy_ip(proxy_url)+ if ip is not None and _is_private_ip(ip):+ logger.debug("Proxy host resolves to private address %s, not using it as the exit", ip)+ ip = Noneif ip is None or _deadline_expired(deadline):if deadline is not None and _deadline_expired(deadline):raise RuntimeError(f"GeoIP resolution timed out after {timeout:.1f}s")diff --git a/dotnet/src/CloakBrowser/GeoIp.cs b/dotnet/src/CloakBrowser/GeoIp.csindex ee8e00c..1bb681d 100644--- a/dotnet/src/CloakBrowser/GeoIp.cs+++ b/dotnet/src/CloakBrowser/GeoIp.cs@@ -112,8 +112,14 @@ public static class GeoIp// while the connection shows the proxy IP - a real deanonymization.var ip = await ResolveExitIpAsync(proxyUrl, RemainingSeconds(deadline), ct).ConfigureAwait(false);// Hostname fallback only applies to a proxy; no proxy -> echo services only.+ // A private address (a local forwarding proxy, a LAN gateway) is never the+ // exit, so it would only produce a misleading lookup error.if (ip == null && !string.IsNullOrEmpty(proxyUrl) && !DeadlineExpired(deadline))+ {ip = ResolveProxyIp(proxyUrl);+ if (ip != null && IsPrivateIp(ip))+ ip = null;+ }if (ip == null || DeadlineExpired(deadline)){if (deadline != null && DeadlineExpired(deadline))diff --git a/js/src/geoip.ts b/js/src/geoip.tsindex e5e5248..b00ad01 100644--- a/js/src/geoip.ts+++ b/js/src/geoip.ts@@ -106,8 +106,13 @@ export async function resolveProxyGeo(// a DB hiccup would let WebRTC fall back to the real IP behind a proxy while// the connection shows the proxy IP — a real deanonymization.let ip = await resolveExitIp(proxyUrl, remainingMs(deadline));- // Hostname fallback only applies to a proxy; no proxy → echo services only- if (!ip && proxyUrl && !deadlineExpired(deadline)) ip = await resolveProxyIp(proxyUrl);+ // Hostname fallback only applies to a proxy; no proxy → echo services only.+ // A private address (a local forwarding proxy, a LAN gateway) is never the+ // exit, so it would only produce a misleading lookup error.+ if (!ip && proxyUrl && !deadlineExpired(deadline)) {+ ip = await resolveProxyIp(proxyUrl);+ if (ip && isPrivateIp(ip)) ip = null;+ }if (!ip || deadlineExpired(deadline)) {if (deadlineExpired(deadline)) {throw new Error(`GeoIP resolution timed out after ${timeoutMs / 1000}s`);@@ -187,6 +192,23 @@ export async function resolveProxyIp(}}+const PRIVATE_NETWORKS = new net.BlockList();+PRIVATE_NETWORKS.addSubnet("10.0.0.0", 8, "ipv4");+PRIVATE_NETWORKS.addSubnet("172.16.0.0", 12, "ipv4");+PRIVATE_NETWORKS.addSubnet("192.168.0.0", 16, "ipv4");+PRIVATE_NETWORKS.addSubnet("127.0.0.0", 8, "ipv4");+PRIVATE_NETWORKS.addSubnet("169.254.0.0", 16, "ipv4");+PRIVATE_NETWORKS.addAddress("::1", "ipv6");+PRIVATE_NETWORKS.addSubnet("fc00::", 7, "ipv6");+PRIVATE_NETWORKS.addSubnet("fe80::", 10, "ipv6");++/** @internal Exported for testing. Private/internal, not routable on the internet. */+export function isPrivateIp(ip: string): boolean {+ const family = net.isIP(ip);+ if (!family) return false;+ return PRIVATE_NETWORKS.check(ip, family === 6 ? "ipv6" : "ipv4");+}+const IP_ECHO_URLS = ["https://api.ipify.org","https://checkip.amazonaws.com",diff --git a/js/tests/geoip.test.ts b/js/tests/geoip.test.tsindex cc7942d..1ca78ad 100644--- a/js/tests/geoip.test.ts+++ b/js/tests/geoip.test.ts@@ -6,6 +6,7 @@ import path from "node:path";import {COUNTRY_LOCALE_MAP,DEFAULT_GEOIP_TIMEOUT_MS,+ isPrivateIp,maybeResolveGeoip,resolveProxyGeo,resolveProxyIp,@@ -58,6 +59,17 @@ describe("resolveProxyIp", () => {});});+describe("isPrivateIp", () => {+ it("classifies loopback, private and link-local ranges", () => {+ for (const ip of ["127.0.0.1", "10.0.0.1", "172.16.5.4", "192.168.1.1", "169.254.1.1", "::1", "fd00::1", "fe80::1"]) {+ expect(isPrivateIp(ip), ip).toBe(true);+ }+ for (const ip of ["8.8.8.8", "172.32.0.1", "2001:4860:4860::8888", "not-an-ip"]) {+ expect(isPrivateIp(ip), ip).toBe(false);+ }+ });+});+describe("maybeResolveGeoip", () => {it("uses a 20-second default timeout", () => {expect(DEFAULT_GEOIP_TIMEOUT_MS).toBe(20_000);@@ -165,6 +177,25 @@ describe("maybeResolveGeoip", () => {await expect(resolveProxyGeo(null)).rejects.toThrow("database is unavailable");});+ it("does not fall back to a private proxy address when the exit IP is unknown", async () => {+ const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), "cloak-geoip-private-proxy-"));+ tempDirs.push(cacheDir);+ const geoipDir = path.join(cacheDir, "geoip");+ fs.mkdirSync(geoipDir, { recursive: true });+ fs.writeFileSync(path.join(geoipDir, "GeoLite2-City.mmdb"), "stub");+ process.env.CLOAKBROWSER_CACHE_DIR = cacheDir;++ // A local forwarding proxy that is not listening: exit-IP discovery fails.+ const server = http.createServer();+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));+ const { port } = server.address() as { port: number };+ await new Promise<void>((resolve) => server.close(() => resolve()));++ await expect(resolveProxyGeo(`http://127.0.0.1:${port}`)).rejects.toThrow(+ "could not discover the egress IP"+ );+ });+it("throws when the GeoIP database lookup fails", async () => {const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), "cloak-geoip-lookup-failure-"));tempDirs.push(cacheDir);diff --git a/tests/test_geoip.py b/tests/test_geoip.pyindex 9d3b8f1..2d38fed 100644--- a/tests/test_geoip.py+++ b/tests/test_geoip.py@@ -117,6 +117,34 @@ def test_resolve_geo_raises_when_exit_ip_missing():resolve_proxy_geo(None)+def test_resolve_geo_does_not_fall_back_to_a_private_proxy_address():+ """A local forwarding proxy is not the exit: report the egress failure instead."""+ mock_geoip2 = type("module", (), {"database": type("db", (), {"Reader": None})})()+ with patch.dict("sys.modules", {"geoip2": mock_geoip2, "geoip2.database": mock_geoip2.database}):+ with patch("cloakbrowser.geoip._ensure_geoip_db", return_value=object()):+ with patch("cloakbrowser.geoip._resolve_exit_ip", return_value=None):+ from cloakbrowser.geoip import resolve_proxy_geo+ with pytest.raises(RuntimeError, match="could not discover the egress IP"):+ resolve_proxy_geo("http://127.0.0.1:8080")+++def test_resolve_geo_still_falls_back_to_a_public_proxy_address():+ """A public proxy host is still a usable guess when exit discovery fails."""+ resp = MagicMock()+ resp.location.time_zone = "Europe/Berlin"+ resp.country.iso_code = "DE"+ reader = MagicMock()+ reader.return_value.__enter__.return_value.city.return_value = resp+ mock_geoip2 = type("module", (), {"database": type("db", (), {"Reader": reader})})()+ with patch.dict("sys.modules", {"geoip2": mock_geoip2, "geoip2.database": mock_geoip2.database}):+ with patch("cloakbrowser.geoip._ensure_geoip_db", return_value=object()):+ with patch("cloakbrowser.geoip._resolve_exit_ip", return_value=None):+ from cloakbrowser.geoip import resolve_proxy_geo_with_ip+ assert resolve_proxy_geo_with_ip("http://5.6.7.8:8080") == (+ "Europe/Berlin", "de-DE", "5.6.7.8",+ )++def test_resolve_geo_raises_when_db_missing():"""A database failure must abort requested GeoIP resolution."""mock_geoip2 = type("module", (), {"database": type("db", (), {"Reader": None})})()