Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
fix(security): catch non-OSError failures from inet_aton
Saurav Pandab0d543fff4c6
diff --git a/browser_use/browser/watchdogs/security_watchdog.py b/browser_use/browser/watchdogs/security_watchdog.pyindex 1f76d4ffb..22ae50161 100644--- a/browser_use/browser/watchdogs/security_watchdog.py+++ b/browser_use/browser/watchdogs/security_watchdog.py@@ -158,14 +158,22 @@ class SecurityWatchdog(BaseWatchdog):return Trueexcept (ValueError, TypeError):pass+ except Exception:+ # Be defensively conservative: any parser-internal failure means+ # "we can't recognize this as a standard IP", not "crash the+ # navigation security check". Falls through to inet_aton.+ pass# Non-standard IPv4 forms that the kernel resolver (and browsers) accept:# decimal, hex, octal, and short-form. `inet_aton` is IPv4-only and# accepts all of these; OSError means "not a parseable IPv4 in any form".+ # It can also raise other exceptions (e.g. `UnicodeEncodeError` for+ # lone-surrogate hostnames from URL-decoded malformed UTF-8) — treat+ # any failure as "not a recognizable IP", never propagate.try:socket.inet_aton(bare)return True- except OSError:+ except Exception:return Falsedef _is_url_allowed(self, url: str) -> bool:diff --git a/tests/ci/security/test_ip_blocking.py b/tests/ci/security/test_ip_blocking.pyindex 3bdc84a15..2f408c558 100644--- a/tests/ci/security/test_ip_blocking.py+++ b/tests/ci/security/test_ip_blocking.py@@ -572,3 +572,19 @@ class TestNonStandardIPv4Representations:assert watchdog._is_url_allowed('http://0x7f000001/') is False# Sanity: legitimate domains still allowed.assert watchdog._is_url_allowed('https://example.com/') is True++ def test_malformed_unicode_hostnames_do_not_crash_classifier(self):+ """Hostnames with lone surrogates (e.g. `\\udcff` from URL-decoded+ malformed UTF-8) must not crash `_is_ip_address` — `socket.inet_aton`+ raises `UnicodeEncodeError` (not `OSError`) for surrogates, and the+ classifier must treat that as "not an IP" rather than propagating.++ Pre-existing defensive behavior in the original code (caught+ `Exception`) — must be preserved when extending with `inet_aton`.+ """+ watchdog = self._watchdog()+ # Lone surrogates — common in URLs containing percent-encoded malformed UTF-8.+ assert watchdog._is_ip_address('\udcff') is False+ assert watchdog._is_ip_address('\ud800') is False+ assert watchdog._is_ip_address('caf\udce9.local') is False+ assert watchdog._is_ip_address('\udcff.example.com') is False