Skip to content

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

fix(security): NFKC-normalize host before IPv4 classification

Saurav Pandae34d2cc6c21d
diff --git a/browser_use/browser/watchdogs/security_watchdog.py b/browser_use/browser/watchdogs/security_watchdog.py
index 55bd7b82d..603f3967d 100644
--- a/browser_use/browser/watchdogs/security_watchdog.py
+++ b/browser_use/browser/watchdogs/security_watchdog.py
@@ -138,19 +138,27 @@ class SecurityWatchdog(BaseWatchdog):
def _is_ip_address(self, host: str) -> bool:
"""Check if a hostname is an IP address (IPv4 or IPv6).
- 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`). 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`.
+ Mirrors the host canonicalization the browser performs before applying
+ its IPv4 parser, so the classifier sees the same string the network
+ stack would resolve:
+
+ 1. Strip IPv6 brackets.
+ 2. Percent-decode (`%30x7f000001` → `0x7f000001`).
+ 3. NFKC-normalize (fullwidth `127.0.0.1` → ASCII `127.0.0.1`;
+ circled `①②⑦.⓪.⓪.①` → ASCII `127.0.0.1`).
+ 4. Try `ipaddress.ip_address` (dotted-quad / IPv6).
+ 5. Fall back to `socket.inet_aton` for non-standard IPv4 forms —
+ decimal (`2130706433`), hex (`0x7f000001`), octal (`0177.0.0.1`),
+ and short forms (`127.1`, `127.0.1`).
+
+ Without these steps `block_ip_addresses=True` is trivially bypassed by
+ re-encoding the IP in any of those forms.
See GHSA-xrfv-gg9f-wwjp / GHSA-g27c-8gp4-28cv.
"""
import ipaddress
import socket
+ import unicodedata
from urllib.parse import unquote
# Strip IPv6 bracket wrapping defensively; urlparse usually strips it,
@@ -168,6 +176,15 @@ class SecurityWatchdog(BaseWatchdog):
except Exception:
pass
+ # NFKC-normalize so Unicode digit variants the browser canonicalizes to
+ # ASCII (fullwidth `127`, circled `①②⑦`, etc.) reach the IP parsers
+ # in their ASCII form. NFKC is a stdlib operation that does not raise
+ # for malformed input (surrogates pass through unchanged).
+ try:
+ bare = unicodedata.normalize('NFKC', 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 2182306b1..d76c99ba0 100644
--- a/tests/ci/security/test_ip_blocking.py
+++ b/tests/ci/security/test_ip_blocking.py
@@ -618,3 +618,35 @@ class TestNonStandardIPv4Representations:
assert watchdog._is_ip_address('%') is False
assert watchdog._is_ip_address('%zz') is False
assert watchdog._is_ip_address('%2') is False
+
+ def test_unicode_normalized_ipv4_blocked(self):
+ """Hostnames using fullwidth, circled, or other Unicode digit variants
+ that NFKC/IDNA-normalize to ASCII IPv4 literals must be blocked.
+
+ WHATWG URL canonicalization maps `127.0.0.1` (fullwidth digits),
+ `0x7f000001` (fullwidth zero + ASCII hex), and `①②⑦.⓪.⓪.①` (circled
+ digits) all to `127.0.0.1`. Without NFKC normalization in the
+ classifier, the new non-standard IPv4 blocking can be bypassed with
+ any equivalent Unicode digit form.
+ """
+ watchdog = self._watchdog()
+ # Fullwidth digits (U+FF10..U+FF19) — NFKC → ASCII digits.
+ assert watchdog._is_url_allowed('http://127.0.0.1/') is False
+ # Fullwidth zero + ASCII hex 7f000001.
+ assert watchdog._is_url_allowed('http://0x7f000001/') is False
+ # Circled digits (U+2460+, U+24EA for zero).
+ assert watchdog._is_url_allowed('http://①②⑦.⓪.⓪.①/') is False
+ # Direct classifier checks.
+ assert watchdog._is_ip_address('127.0.0.1') is True
+ assert watchdog._is_ip_address('0x7f000001') is True
+ assert watchdog._is_ip_address('①②⑦.⓪.⓪.①') is True
+
+ def test_idn_domains_not_misclassified_as_ip(self):
+ """Defense against false positives from the new normalization step:
+ legitimate IDN domains (Unicode letters / punycode) MUST NOT be
+ classified as IPs after NFKC."""
+ watchdog = self._watchdog()
+ assert watchdog._is_ip_address('café.example') is False
+ assert watchdog._is_ip_address('xn--caf-dma.example') is False
+ assert watchdog._is_ip_address('日本.example') is False
+ assert watchdog._is_ip_address('xn--wgv71a.example') is False