Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix(security): percent-decode host before IPv4 classification

Saurav Pandab6ef0e28888f
diff --git a/browser_use/browser/watchdogs/security_watchdog.py b/browser_use/browser/watchdogs/security_watchdog.py
index 22ae50161..55bd7b82d 100644
--- a/browser_use/browser/watchdogs/security_watchdog.py
+++ b/browser_use/browser/watchdogs/security_watchdog.py
@@ -141,17 +141,33 @@ class SecurityWatchdog(BaseWatchdog):
Recognizes non-standard IPv4 representations that Chromium resolves but
`ipaddress.ip_address()` rejects — decimal (`2130706433`), hex
(`0x7f000001`), octal (`0177.0.0.1`), and short forms (`127.1`,
- `127.0.1`). Without this canonicalization, `block_ip_addresses=True`
- can be bypassed by simply re-encoding the IP in one of these forms.
+ `127.0.1`). Also percent-decodes the host first, since Chromium decodes
+ `%XX` escapes before applying its IPv4 parser; without that step,
+ `http://%30x7f000001/` (decodes to `0x7f000001` → 127.0.0.1) and
+ `http://%31%32%37.0.0.1/` (decodes to `127.0.0.1`) would still bypass
+ `block_ip_addresses`.
+
See GHSA-xrfv-gg9f-wwjp / GHSA-g27c-8gp4-28cv.
"""
import ipaddress
import socket
+ from urllib.parse import unquote
# Strip IPv6 bracket wrapping defensively; urlparse usually strips it,
# but a malformed url could leak it through.
bare = host.strip('[]')
+ # Percent-decode the host before parsing. Browsers decode `%XX` escapes
+ # in the host component, so an unencoded check would miss
+ # `%30x7f000001` (→ '0x7f000001') and `%31%32%37.0.0.1` (→ '127.0.0.1').
+ # `unquote` is forgiving (leaves malformed `%`-sequences as-is) but we
+ # still wrap in try/except so any future encoding edge case can never
+ # crash the navigation security check.
+ try:
+ bare = unquote(bare)
+ except Exception:
+ pass
+
# Standard dotted-quad IPv4 and full IPv6.
try:
ipaddress.ip_address(bare)
diff --git a/tests/ci/security/test_ip_blocking.py b/tests/ci/security/test_ip_blocking.py
index 2f408c558..2182306b1 100644
--- a/tests/ci/security/test_ip_blocking.py
+++ b/tests/ci/security/test_ip_blocking.py
@@ -588,3 +588,33 @@ class TestNonStandardIPv4Representations:
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
+
+ def test_percent_encoded_ipv4_blocked(self):
+ """Percent-encoded hostnames that decode to IPs must be blocked.
+
+ Chromium percent-decodes the host before resolving — so
+ `http://%30x7f000001/` decodes to `0x7f000001` → `127.0.0.1`, and
+ `http://%31%32%37.0.0.1/` decodes to `127.0.0.1`. Without
+ percent-decoding inside the classifier, the IP block is bypassed
+ whenever the URL contains any `%`-encoded host bytes.
+ """
+ watchdog = self._watchdog()
+ # Mixed encoding: %30 = '0', rest literal → '0x7f000001'
+ assert watchdog._is_url_allowed('http://%30x7f000001/') is False
+ # Fully encoded canonical form: %31%32%37 = '127'
+ assert watchdog._is_url_allowed('http://%31%32%37.0.0.1/') is False
+ # Fully encoded decimal form: → '2130706433'
+ assert watchdog._is_url_allowed('http://%32%31%33%30%37%30%36%34%33%33/') is False
+ # Direct classifier checks for the same decoded forms.
+ assert watchdog._is_ip_address('%30x7f000001') is True
+ assert watchdog._is_ip_address('%31%32%37.0.0.1') is True
+ assert watchdog._is_ip_address('%32%31%33%30%37%30%36%34%33%33') is True
+
+ def test_malformed_percent_encoding_does_not_crash(self):
+ """Hostnames with malformed `%` escapes must not crash the classifier."""
+ watchdog = self._watchdog()
+ # `unquote` leaves bad `%`-sequences as-is; we must still treat the
+ # result as a non-IP rather than blowing up.
+ assert watchdog._is_ip_address('%') is False
+ assert watchdog._is_ip_address('%zz') is False
+ assert watchdog._is_ip_address('%2') is False