Skip to content

Commit 483ffb4

Browse files
Python: fix(python): Pin HttpPlugin requests to validated DNS addresses (#14649)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> `HttpPlugin` validates the DNS addresses for allow-listed hosts before sending a request, but previously discarded those validated addresses before opening the connection. The HTTP transport could therefore resolve the hostname independently at connection time. This change keeps validation and connection behavior consistent without redesigning the plugin or changing its explicit unrestricted/private-network opt-ins. No public issue is linked. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> - Retain the IPv4/IPv6 addresses returned by the existing public-network validator. - Supply `aiohttp` with a host-scoped resolver that returns only those validated addresses. - Keep the original normalized URL for the request so Host headers, TLS SNI, and certificate validation retain the hostname identity. - Apply the same pinned connector path to GET, POST, PUT, and DELETE. - Preserve existing behavior for literal IP URLs and the explicit `allow_all_domains` / `allow_private_network_access` opt-ins. - Add regression coverage for every HTTP method and a real connection test proving a later hostname lookup is not used. Validation: - `uv build` — passed; source distribution and wheel built successfully. - `uv run --python 3.12 --frozen ruff check semantic_kernel\core_plugins\http_plugin.py tests\unit\core_plugins\test_http_plugin.py` — passed. - `uv run --python 3.12 --frozen pytest -q tests\unit\core_plugins\test_http_plugin.py` — passed (226 tests). - `uv run --python 3.12 --frozen pytest -q tests\unit\connectors\openapi_plugin\test_server_url_validator.py tests\unit\connectors\openapi_plugin\test_openapi_runner_dns_pinning.py` — passed (91 tests). - `uv run --python 3.12 --frozen mypy semantic_kernel\core_plugins\http_plugin.py` — passed. - The repository pre-commit wrapper was attempted, but local hook-environment initialization did not complete; the changed Python files passed the direct Ruff formatting/lint checks above. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: Copilot App <[email protected]>
1 parent a08e050 commit 483ffb4

2 files changed

Lines changed: 156 additions & 17 deletions

File tree

‎python/semantic_kernel/core_plugins/http_plugin.py‎

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Copyright (c) Microsoft. All rights reserved.
22

3+
import ipaddress
34
import json
45
import logging
6+
import socket
7+
from collections.abc import Sequence
58
from typing import Annotated, Any, ClassVar
69
from urllib.parse import urlparse
710

@@ -15,6 +18,56 @@
1518

1619
logger = logging.getLogger(__name__)
1720

21+
_NetworkAddress = ipaddress.IPv4Address | ipaddress.IPv6Address
22+
23+
24+
class _ValidatedAddressResolver(aiohttp.abc.AbstractResolver):
25+
"""Resolve one validated host only to addresses that were already vetted."""
26+
27+
def __init__(self, host: str, addresses: Sequence[_NetworkAddress]) -> None:
28+
self._host = host
29+
self._addresses = addresses
30+
31+
async def resolve(
32+
self,
33+
host: str,
34+
port: int = 0,
35+
family: socket.AddressFamily = socket.AF_INET,
36+
) -> list[Any]:
37+
"""Return vetted addresses without performing another DNS lookup."""
38+
if host != self._host:
39+
raise OSError(f"Refusing to resolve host '{host}'; only '{self._host}' was validated.")
40+
41+
results: list[Any] = []
42+
for address in self._addresses:
43+
address_family = socket.AF_INET6 if isinstance(address, ipaddress.IPv6Address) else socket.AF_INET
44+
if family not in (socket.AF_UNSPEC, address_family):
45+
continue
46+
results.append({
47+
"hostname": host,
48+
"host": str(address),
49+
"port": port,
50+
"family": address_family,
51+
"proto": socket.IPPROTO_TCP,
52+
"flags": socket.AI_NUMERICHOST,
53+
})
54+
return results
55+
56+
async def close(self) -> None:
57+
"""Release resolver resources."""
58+
59+
60+
def _create_client_session(request_url: URL, validated_addresses: Sequence[_NetworkAddress]) -> aiohttp.ClientSession:
61+
if not validated_addresses:
62+
return aiohttp.ClientSession()
63+
64+
host = request_url.raw_host
65+
if host is None:
66+
raise FunctionExecutionException("The request URL does not contain a valid host.")
67+
68+
resolver = _ValidatedAddressResolver(host, validated_addresses)
69+
return aiohttp.ClientSession(connector=aiohttp.TCPConnector(resolver=resolver))
70+
1871

1972
class HttpPlugin(KernelBaseModel):
2073
"""A plugin that provides HTTP functionality.
@@ -45,11 +98,11 @@ class HttpPlugin(KernelBaseModel):
4598
to permit additional ports. Port validation is skipped when
4699
``allow_all_domains`` is True.
47100
- Allowed hosts are normalized using aiohttp's IDNA rules before DNS validation,
48-
and the same normalized URL is used for the request. Non-public addresses and
49-
DNS failures are blocked unless ``allow_private_network_access`` is True.
50-
``allow_all_domains=True`` also bypasses this check.
51-
- This pre-request check does not pin the connection's IP address and does not
52-
protect against DNS changing between validation and connection.
101+
and the same normalized URL is used for the request. Connections are pinned to
102+
the validated DNS addresses so the host cannot resolve to a different address
103+
when the connection is opened. Non-public addresses and DNS failures are blocked
104+
unless ``allow_private_network_access`` is True. ``allow_all_domains=True`` also
105+
bypasses this check.
53106
"""
54107

55108
allowed_domains: set[str] | None = None
@@ -141,7 +194,7 @@ def _is_uri_allowed(self, url: str) -> bool:
141194
# Default: deny all
142195
return False
143196

144-
async def _validate_url(self, url: str) -> URL:
197+
async def _validate_url(self, url: str) -> tuple[URL, list[_NetworkAddress]]:
145198
"""Validate and normalize the URL before sending a request.
146199
147200
Always checks that the URL is non-empty, uses an allowed scheme, and has a
@@ -153,7 +206,7 @@ async def _validate_url(self, url: str) -> URL:
153206
url: The URL to validate.
154207
155208
Returns:
156-
The normalized URL to use for the request.
209+
The normalized URL and any DNS addresses that must be pinned for the request.
157210
158211
Raises:
159212
FunctionExecutionException: If the URL is empty, uses a disallowed scheme,
@@ -167,13 +220,14 @@ async def _validate_url(self, url: str) -> URL:
167220
raise FunctionExecutionException("Sending requests to the provided location is not allowed.")
168221

169222
request_url = URL(url)
223+
validated_addresses: list[_NetworkAddress] = []
170224
if not self.allow_all_domains and not self.allow_private_network_access:
171-
await ensure_public_host(
225+
validated_addresses = await ensure_public_host(
172226
urlparse(str(request_url)),
173227
configuration_hint="To allow trusted internal endpoints, set allow_private_network_access=True.",
174228
)
175229

176-
return request_url
230+
return request_url, validated_addresses
177231

178232
@kernel_function(description="Makes a GET request to a url", name="getAsync")
179233
async def get(self, url: Annotated[str, "The URL to send the request to."]) -> str:
@@ -185,10 +239,10 @@ async def get(self, url: Annotated[str, "The URL to send the request to."]) -> s
185239
Returns:
186240
The response body as a string.
187241
"""
188-
request_url = await self._validate_url(url)
242+
request_url, validated_addresses = await self._validate_url(url)
189243

190244
async with (
191-
aiohttp.ClientSession() as session,
245+
_create_client_session(request_url, validated_addresses) as session,
192246
session.get(request_url, raise_for_status=True, allow_redirects=self._allow_redirects) as response,
193247
):
194248
return await response.text()
@@ -207,12 +261,12 @@ async def post(
207261
returns:
208262
The response body as a string.
209263
"""
210-
request_url = await self._validate_url(url)
264+
request_url, validated_addresses = await self._validate_url(url)
211265

212266
headers = {"Content-Type": "application/json"}
213267
data = json.dumps(body) if body is not None else None
214268
async with (
215-
aiohttp.ClientSession() as session,
269+
_create_client_session(request_url, validated_addresses) as session,
216270
session.post(
217271
request_url, headers=headers, data=data, raise_for_status=True, allow_redirects=self._allow_redirects
218272
) as response,
@@ -234,12 +288,12 @@ async def put(
234288
Returns:
235289
The response body as a string.
236290
"""
237-
request_url = await self._validate_url(url)
291+
request_url, validated_addresses = await self._validate_url(url)
238292

239293
headers = {"Content-Type": "application/json"}
240294
data = json.dumps(body) if body is not None else None
241295
async with (
242-
aiohttp.ClientSession() as session,
296+
_create_client_session(request_url, validated_addresses) as session,
243297
session.put(
244298
request_url, headers=headers, data=data, raise_for_status=True, allow_redirects=self._allow_redirects
245299
) as response,
@@ -256,10 +310,10 @@ async def delete(self, url: Annotated[str, "The URI to send the request to."]) -
256310
Returns:
257311
The response body as a string.
258312
"""
259-
request_url = await self._validate_url(url)
313+
request_url, validated_addresses = await self._validate_url(url)
260314

261315
async with (
262-
aiohttp.ClientSession() as session,
316+
_create_client_session(request_url, validated_addresses) as session,
263317
session.delete(request_url, raise_for_status=True, allow_redirects=self._allow_redirects) as response,
264318
):
265319
return await response.text()

‎python/tests/unit/core_plugins/test_http_plugin.py‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright (c) Microsoft. All rights reserved.
22

3+
import asyncio
4+
import ipaddress
35
import logging
46
import socket
57
from unittest.mock import AsyncMock, patch
@@ -576,6 +578,89 @@ async def test_public_dns_addresses_are_allowed(method, mock_dns):
576578
mock_dns.assert_awaited_once()
577579

578580

581+
@pytest.mark.parametrize("method", ["get", "post", "put", "delete"])
582+
async def test_http_methods_pin_validated_dns_addresses(method, mock_dns):
583+
mock_dns.return_value = dns_response("93.184.216.34", "2606:4700:4700::1111")
584+
plugin = HttpPlugin(allowed_domains={"example.com"})
585+
586+
with (
587+
patch("aiohttp.TCPConnector") as connector_type,
588+
patch("aiohttp.ClientSession") as session_type,
589+
):
590+
session = session_type.return_value
591+
session.__aenter__.return_value = session
592+
request = getattr(session, method)
593+
request.return_value.__aenter__.return_value.text.return_value = "public response"
594+
595+
kwargs = {"body": {"key": "value"}} if method in {"post", "put"} else {}
596+
assert await getattr(plugin, method)("http://example.com/", **kwargs) == "public response"
597+
598+
session_type.assert_called_once_with(connector=connector_type.return_value)
599+
resolver = connector_type.call_args.kwargs["resolver"]
600+
assert await resolver.resolve("example.com", 80, socket.AF_UNSPEC) == [
601+
{
602+
"hostname": "example.com",
603+
"host": "93.184.216.34",
604+
"port": 80,
605+
"family": socket.AF_INET,
606+
"proto": socket.IPPROTO_TCP,
607+
"flags": socket.AI_NUMERICHOST,
608+
},
609+
{
610+
"hostname": "example.com",
611+
"host": "2606:4700:4700::1111",
612+
"port": 80,
613+
"family": socket.AF_INET6,
614+
"proto": socket.IPPROTO_TCP,
615+
"flags": socket.AI_NUMERICHOST,
616+
},
617+
]
618+
with pytest.raises(OSError, match="only 'example.com' was validated"):
619+
await resolver.resolve("rebound.example", 80, socket.AF_UNSPEC)
620+
mock_dns.assert_awaited_once_with("example.com", None, type=socket.SOCK_STREAM)
621+
622+
623+
async def test_request_pins_connection_to_validated_address_under_dns_rebinding(mock_dns):
624+
response_body = b"validated address response"
625+
626+
async def handle_request(reader, writer):
627+
await reader.readuntil(b"\r\n\r\n")
628+
writer.write(
629+
b"HTTP/1.1 200 OK\r\n"
630+
+ f"Content-Length: {len(response_body)}\r\n".encode()
631+
+ b"Connection: close\r\n\r\n"
632+
+ response_body
633+
)
634+
await writer.drain()
635+
writer.close()
636+
await writer.wait_closed()
637+
638+
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
639+
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
640+
listener.bind(("127.0.0.1", 0))
641+
listener.listen()
642+
listener.setblocking(False)
643+
port = listener.getsockname()[1]
644+
server = await asyncio.start_server(handle_request, sock=listener)
645+
646+
mock_dns.return_value = dns_response("127.0.0.2")
647+
plugin = HttpPlugin(allowed_domains={"rebind.example"}, allowed_ports={port})
648+
649+
try:
650+
with patch(
651+
"semantic_kernel.core_plugins.http_plugin.ensure_public_host",
652+
new_callable=AsyncMock,
653+
return_value=[ipaddress.ip_address("127.0.0.1")],
654+
) as validate_host:
655+
assert await plugin.get(f"http://rebind.example:{port}/") == response_body.decode()
656+
finally:
657+
server.close()
658+
await server.wait_closed()
659+
660+
validate_host.assert_awaited_once()
661+
mock_dns.assert_not_awaited()
662+
663+
579664
@pytest.mark.parametrize("method", ["get", "post", "put", "delete"])
580665
async def test_idna_hostname_resolving_to_loopback_is_blocked(method, mock_dns):
581666
def resolve(host, *args, **kwargs):

0 commit comments

Comments
 (0)