Skip to content

Commit b05ec52

Browse files
committed
refactor(utils): polish UrlValidator API and matching semantics
Self-review follow-ups for the #1023 draft: - expose the address union as public UrlAddress instead of a private alias (it appears in the public ResolvedUrl annotation); - match BlockPrivateNetworks allow_hosts case-insensitively: urlparse lowercases parsed hostnames, so configured exemptions must be normalized too; - deduplicate resolved addresses while preserving resolver order (getaddrinfo repeats addresses across CNAME chains and record families). Signed-off-by: andy <linux2011@qq.com>
1 parent 1401c5c commit b05ec52

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

‎src/a2a/utils/url_validation.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
logger = logging.getLogger(__name__)
3030

31-
_Address = ipaddress.IPv4Address | ipaddress.IPv6Address
31+
UrlAddress = ipaddress.IPv4Address | ipaddress.IPv6Address
3232

3333
Resolver = Callable[[str, int], Awaitable[list[tuple]]]
3434

@@ -43,15 +43,15 @@ class ResolvedUrl:
4343

4444
raw: str
4545
parsed: urllib.parse.SplitResult
46-
addresses: tuple[_Address, ...]
46+
addresses: tuple[UrlAddress, ...]
4747

4848
@property
4949
def hostname(self) -> str:
5050
"""The lowercased host from the parsed URL (may be empty)."""
5151
return self.parsed.hostname or ''
5252

5353

54-
def _is_non_public(address: _Address) -> bool:
54+
def _is_non_public(address: UrlAddress) -> bool:
5555
"""Whether an address is not a public unicast destination."""
5656
return (
5757
address.is_private
@@ -106,12 +106,14 @@ def __init__(
106106
allow_hosts: Sequence[str] = (),
107107
allow_cidrs: Sequence[str] = (),
108108
) -> None:
109-
self._allow_hosts = frozenset(allow_hosts)
109+
# Hostnames are matched case-insensitively: urlparse lowercases
110+
# parsed hostnames, so the exemptions are normalized too.
111+
self._allow_hosts = frozenset(host.lower() for host in allow_hosts)
110112
self._allow_cidrs = tuple(
111113
ipaddress.ip_network(cidr, strict=False) for cidr in allow_cidrs
112114
)
113115

114-
def _is_allowed(self, url: ResolvedUrl, address: _Address) -> bool:
116+
def _is_allowed(self, url: ResolvedUrl, address: UrlAddress) -> bool:
115117
if url.hostname in self._allow_hosts:
116118
return True
117119
return any(address in network for network in self._allow_cidrs)
@@ -165,15 +167,15 @@ async def validate(self, url: str) -> ResolvedUrl:
165167
if not host:
166168
raise InvalidUrlError(f'URL has no hostname: {url}')
167169

168-
addresses: tuple[_Address, ...] = ()
170+
addresses: tuple[UrlAddress, ...] = ()
169171
if self._resolve:
170172
try:
171173
infos = await self._resolver(host, port)
172174
except OSError as e:
173175
raise InvalidUrlError(
174176
f'Host {host!r} could not be resolved: {url}'
175177
) from e
176-
resolved: list[_Address] = []
178+
resolved: list[UrlAddress] = []
177179
for info in infos:
178180
raw_ip = str(info[4][0]).split('%', maxsplit=1)[0]
179181
try:
@@ -182,7 +184,10 @@ async def validate(self, url: str) -> ResolvedUrl:
182184
raise InvalidUrlError(
183185
f'Host {host!r} resolved to an unparseable address'
184186
) from e
185-
addresses = tuple(resolved)
187+
# getaddrinfo can return the same address several times (CNAME
188+
# chains, multi-record families); deduplicate while keeping the
189+
# resolver's order.
190+
addresses = tuple(dict.fromkeys(resolved))
186191

187192
resolved_url = ResolvedUrl(raw=url, parsed=parsed, addresses=addresses)
188193
for rule in self._rules:

‎tests/utils/test_url_validation.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,30 @@ async def test_block_private_networks_allow_cidrs_still_blocks_outside():
145145
)
146146

147147

148+
@pytest.mark.asyncio
149+
async def test_block_private_networks_allow_hosts_is_case_insensitive():
150+
"""Allowlist matching must survive host casing: parsed hostnames are
151+
lowercased by urlparse, so configured exemptions are normalized too."""
152+
rules = (
153+
RequireScheme(('http', 'https')),
154+
BlockPrivateNetworks(allow_hosts=('Internal.Corp',)),
155+
)
156+
resolved = await _validate(
157+
'http://INTERNAL.corp/api', rules=rules, ips=('10.1.2.3',)
158+
)
159+
160+
assert resolved.hostname == 'internal.corp'
161+
162+
163+
@pytest.mark.asyncio
164+
async def test_duplicate_resolved_addresses_are_deduplicated():
165+
resolved = await _validate(
166+
'http://example.com/hook', ips=(PUBLIC_IP, PUBLIC_IP)
167+
)
168+
169+
assert [str(a) for a in resolved.addresses] == [PUBLIC_IP]
170+
171+
148172
@pytest.mark.asyncio
149173
async def test_block_private_networks_ignores_public_addresses():
150174
resolved = await _validate('http://example.com/hook')

0 commit comments

Comments
 (0)