Skip to content

Commit c5c8db7

Browse files
committed
fix: pin validated OpenAPI DNS addresses
1 parent b39d95a commit c5c8db7

4 files changed

Lines changed: 101 additions & 10 deletions

File tree

‎python/semantic_kernel/connectors/openapi_plugin/openapi_runner.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from semantic_kernel.connectors.openapi_plugin.models.rest_api_operation import RestApiOperation
1818
from semantic_kernel.connectors.openapi_plugin.models.rest_api_payload import RestApiPayload
1919
from semantic_kernel.connectors.openapi_plugin.models.rest_api_run_options import RestApiRunOptions
20+
from semantic_kernel.connectors.openapi_plugin.pinned_http_transport import PinnedDnsTransport
2021
from semantic_kernel.connectors.openapi_plugin.server_url_validator import (
2122
ServerUrlValidationOptions,
2223
validate_server_url,
@@ -143,7 +144,7 @@ async def run_operation(
143144
server_url_override=options.server_url_override if options else None,
144145
api_host_url=options.api_host_url if options else None,
145146
)
146-
await validate_server_url(url, self.server_url_validation_options)
147+
pinned_hosts = await validate_server_url(url, self.server_url_validation_options)
147148
headers = operation.build_headers(arguments=arguments)
148149
payload, _ = self.build_operation_payload(operation=operation, arguments=arguments)
149150

@@ -183,7 +184,10 @@ async def make_request(client: httpx.AsyncClient):
183184

184185
if hasattr(self, "http_client") and self.http_client is not None:
185186
return await make_request(self.http_client)
186-
async with httpx.AsyncClient(timeout=timeout) as client:
187+
async with httpx.AsyncClient(
188+
timeout=timeout,
189+
transport=PinnedDnsTransport(pinned_hosts) if pinned_hosts else None,
190+
) as client:
187191
return await make_request(client)
188192

189193
return await fetch()
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
from collections.abc import Mapping
4+
from typing import Any
5+
6+
import httpcore
7+
import httpx
8+
9+
10+
class _PinnedNetworkBackend(httpcore.AsyncNetworkBackend):
11+
def __init__(self, pinned_hosts: Mapping[str, str]):
12+
self._pinned_hosts = {host.lower(): address for host, address in pinned_hosts.items()}
13+
self._backend = httpcore.AnyIOBackend()
14+
15+
async def connect_tcp(self, host, port, timeout=None, local_address=None, socket_options=None):
16+
return await self._backend.connect_tcp(
17+
self._pinned_hosts.get(host.lower(), host), port, timeout, local_address, socket_options
18+
)
19+
20+
21+
class PinnedDnsTransport(httpx.AsyncBaseTransport):
22+
"""HTTP transport that connects validated hostnames to fixed IP addresses."""
23+
24+
def __init__(self, pinned_hosts: Mapping[str, str], **kwargs: Any):
25+
"""Initialize a transport using fixed addresses for validated hosts."""
26+
ssl_context = httpx.create_ssl_context(
27+
verify=kwargs.pop("verify", True), cert=kwargs.pop("cert", None), trust_env=kwargs.pop("trust_env", True)
28+
)
29+
limits = kwargs.pop("limits", httpx.Limits())
30+
if kwargs:
31+
unexpected = ", ".join(sorted(kwargs))
32+
raise TypeError(f"Unexpected transport options: {unexpected}")
33+
self._pool = httpcore.AsyncConnectionPool(
34+
ssl_context=ssl_context,
35+
max_connections=limits.max_connections,
36+
max_keepalive_connections=limits.max_keepalive_connections,
37+
keepalive_expiry=limits.keepalive_expiry,
38+
http1=kwargs.pop("http1", True),
39+
http2=kwargs.pop("http2", False),
40+
network_backend=_PinnedNetworkBackend(pinned_hosts),
41+
)
42+
43+
async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
44+
"""Send a request through the pinned connection pool."""
45+
req = httpcore.Request(
46+
method=request.method,
47+
url=httpcore.URL(
48+
scheme=request.url.raw_scheme,
49+
host=request.url.raw_host,
50+
port=request.url.port,
51+
target=request.url.raw_path,
52+
),
53+
headers=request.headers.raw,
54+
content=request.stream,
55+
extensions=request.extensions,
56+
)
57+
response = await self._pool.handle_async_request(req)
58+
return httpx.Response(
59+
status_code=response.status,
60+
headers=response.headers,
61+
stream=_HttpcoreResponseStream(response.stream),
62+
extensions=response.extensions,
63+
request=request,
64+
)
65+
66+
async def aclose(self) -> None:
67+
"""Close all pooled connections."""
68+
await self._pool.aclose()
69+
70+
71+
class _HttpcoreResponseStream(httpx.AsyncByteStream):
72+
def __init__(self, stream: Any):
73+
self._stream = stream
74+
75+
async def __aiter__(self):
76+
async for chunk in self._stream:
77+
yield chunk
78+
79+
async def aclose(self) -> None:
80+
await self._stream.aclose()

‎python/semantic_kernel/connectors/openapi_plugin/server_url_validator.py‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ async def validate_server_url(
3333
url: str,
3434
options: ServerUrlValidationOptions | None = None,
3535
dns_resolver: DnsResolver | None = None,
36-
) -> None:
37-
"""Validate a fully resolved OpenAPI operation URL against the supplied policy."""
36+
) -> dict[str, str]:
37+
"""Validate a URL and return the hostname-to-address mapping used for pinning.
38+
39+
An empty mapping means that pinning is not required because an explicitly
40+
allowed base URL or private-network opt-in bypassed public-host validation.
41+
"""
3842
options = options or ServerUrlValidationOptions()
3943
try:
4044
parsed_url = _parse_absolute_url(url)
@@ -44,7 +48,7 @@ async def validate_server_url(
4448
) from exc
4549

4650
if _matches_allowed_base_url(parsed_url, options.allowed_base_urls):
47-
return
51+
return {}
4852

4953
if options.allowed_base_urls:
5054
raise FunctionExecutionException(
@@ -59,9 +63,9 @@ async def validate_server_url(
5963
)
6064

6165
if options.allow_private_network_access:
62-
return
66+
return {}
6367

64-
await _ensure_public_host(parsed_url, dns_resolver)
68+
return await _ensure_public_host(parsed_url, dns_resolver)
6569

6670

6771
def try_categorize_non_public_address(
@@ -127,7 +131,7 @@ def _matches_path_prefix(url_path: str, base_path: str) -> bool:
127131
return url_path.lower().startswith(base_path_with_slash.lower())
128132

129133

130-
async def _ensure_public_host(parsed_url: ParseResult, dns_resolver: DnsResolver | None) -> None:
134+
async def _ensure_public_host(parsed_url: ParseResult, dns_resolver: DnsResolver | None) -> dict[str, str]:
131135
host = parsed_url.hostname
132136
if host is None:
133137
raise FunctionExecutionException(f"The request URI '{parsed_url.geturl()}' does not contain a valid host.")
@@ -138,7 +142,7 @@ async def _ensure_public_host(parsed_url: ParseResult, dns_resolver: DnsResolver
138142
addresses = await _resolve_host(host, dns_resolver)
139143
else:
140144
_ensure_public_address(parsed_url.geturl(), ip_address)
141-
return
145+
return {host: str(ip_address)}
142146

143147
if not addresses:
144148
raise FunctionExecutionException(
@@ -148,6 +152,7 @@ async def _ensure_public_host(parsed_url: ParseResult, dns_resolver: DnsResolver
148152

149153
for address in addresses:
150154
_ensure_public_address(parsed_url.geturl(), address)
155+
return {host: str(addresses[0])}
151156

152157

153158
async def _resolve_host(

‎python/tests/unit/connectors/openapi_plugin/test_server_url_validator.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ async def fake_resolver(host: str):
149149
assert host == "api.example.com"
150150
return ["93.184.216.34"]
151151

152-
await validate_server_url("https://api.example.com/", dns_resolver=fake_resolver)
152+
assert await validate_server_url("https://api.example.com/", dns_resolver=fake_resolver) == {
153+
"api.example.com": "93.184.216.34"
154+
}
153155

154156

155157
async def test_validate_server_url_blocks_dns_resolution_failure():

0 commit comments

Comments
 (0)