Skip to content

Commit 402c752

Browse files
fix(python): normalize allowlist paths to httpx wire targets
1 parent ac870c6 commit 402c752

3 files changed

Lines changed: 120 additions & 6 deletions

File tree

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ def _normalize_path(path: str, decode_percent_encoded: bool = False) -> str:
134134
# Protect only the leading root segment; internal empty segments can be popped.
135135
continue
136136
resolved.append(segment)
137-
if segments[-1] in (".", ".."):
138-
resolved.append("")
139137
# Absolute paths keep a leading empty segment from the initial "/".
140138
if not resolved:
141139
return "/"

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,47 @@ async def test_run_operation_enforces_allowed_base_url_path_case_before_authenti
387387
runner.http_client.request.assert_not_awaited()
388388

389389

390+
@pytest.mark.parametrize(
391+
("allowed_base_url", "url", "is_allowed"),
392+
[
393+
("https://api.example.com/api", "https://api.example.com/api/.", True),
394+
("https://api.example.com/api", "https://api.example.com/api/users/..", True),
395+
("https://api.example.com/api/", "https://api.example.com/api/.", False),
396+
("https://api.example.com/api/", "https://api.example.com/api/users/..", False),
397+
("https://api.example.com/api/", "https://api.example.com/api//.", True),
398+
("https://api.example.com/api//", "https://api.example.com/api//.", False),
399+
],
400+
)
401+
async def test_run_operation_enforces_allowed_base_url_with_wire_targets(allowed_base_url, url, is_allowed):
402+
runner = OpenApiRunner(
403+
{},
404+
server_url_validation_options=ServerUrlValidationOptions(allowed_base_urls=[allowed_base_url]),
405+
)
406+
operation = MagicMock()
407+
operation.method = "GET"
408+
operation.build_headers.return_value = {}
409+
operation.responses = OrderedDict()
410+
runner.build_operation_url = MagicMock(return_value=url)
411+
runner.build_operation_payload = MagicMock(return_value=(None, None))
412+
response = MagicMock()
413+
response.text = "response text"
414+
runner.http_client = AsyncMock()
415+
runner.http_client.headers = {}
416+
runner.http_client.request = AsyncMock(return_value=response)
417+
runner.auth_callback = AsyncMock(return_value={"Authorization": "******"})
418+
419+
if is_allowed:
420+
result = await runner.run_operation(operation)
421+
assert result == "response text"
422+
runner.auth_callback.assert_awaited_once_with()
423+
runner.http_client.request.assert_awaited_once()
424+
else:
425+
with pytest.raises(FunctionExecutionException, match="allowed base URLs"):
426+
await runner.run_operation(operation)
427+
runner.auth_callback.assert_not_awaited()
428+
runner.http_client.request.assert_not_awaited()
429+
430+
390431
async def test_run_operation_blocks_disallowed_url_before_request():
391432
runner = OpenApiRunner({})
392433
operation = MagicMock()

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

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import ipaddress
44
import socket
55

6+
import httpx
67
import pytest
78

89
from semantic_kernel.connectors.openapi_plugin.server_url_validator import (
@@ -319,19 +320,59 @@ async def test_validate_server_url_rejects_path_that_only_matches_after_decoding
319320
[
320321
"https://api.example.com/api/.",
321322
"https://api.example.com/api/users/..",
323+
"https://api.example.com/api//..",
322324
],
323325
)
324-
async def test_validate_server_url_preserves_trailing_slash_after_normalization(url):
326+
async def test_validate_server_url_rejects_terminal_dot_segments_under_trailing_slash_allowlist(url):
327+
"""httpx sends terminal dot segments as bare paths, so an /api/ allowlist must reject them."""
328+
options = ServerUrlValidationOptions(allowed_base_urls=["https://api.example.com/api/"])
329+
330+
with pytest.raises(FunctionExecutionException, match="allowed base URLs"):
331+
await validate_server_url(url, options)
332+
333+
334+
@pytest.mark.parametrize(
335+
"url",
336+
[
337+
"https://api.example.com/api//.",
338+
"https://api.example.com/api//../",
339+
],
340+
)
341+
async def test_validate_server_url_allows_terminal_dot_segments_normalizing_to_trailing_slash(url):
342+
"""httpx sends /api//. and /api//../ as /api/, matching an /api/ allowlist."""
325343
options = ServerUrlValidationOptions(allowed_base_urls=["https://api.example.com/api/"])
326344

327345
assert await validate_server_url(url, options) == []
328346

329347

330348
@pytest.mark.parametrize(
331-
"allowed_base_url",
349+
"url",
332350
[
333351
"https://api.example.com/api/.",
352+
"https://api.example.com/api/users/..",
353+
"https://api.example.com/api//.",
354+
"https://api.example.com/api//..",
355+
],
356+
)
357+
async def test_validate_server_url_allows_terminal_dot_segments_under_bare_allowlist(url):
358+
options = ServerUrlValidationOptions(allowed_base_urls=["https://api.example.com/api"])
359+
360+
assert await validate_server_url(url, options) == []
361+
362+
363+
async def test_validate_server_url_rejects_terminal_dot_segment_under_double_slash_allowlist():
364+
"""httpx sends /api//. as /api/, so an /api// allowlist must reject it."""
365+
options = ServerUrlValidationOptions(allowed_base_urls=["https://api.example.com/api//"])
366+
367+
with pytest.raises(FunctionExecutionException, match="allowed base URLs"):
368+
await validate_server_url("https://api.example.com/api//.", options)
369+
370+
371+
@pytest.mark.parametrize(
372+
"allowed_base_url",
373+
[
334374
"https://api.example.com/api/",
375+
"https://api.example.com/api//.",
335376
],
336377
)
337378
async def test_validate_server_url_trailing_slash_allowlist_does_not_allow_bare_path(allowed_base_url):
@@ -341,15 +382,24 @@ async def test_validate_server_url_trailing_slash_allowlist_does_not_allow_bare_
341382
await validate_server_url("https://api.example.com/api", options)
342383

343384

385+
async def test_validate_server_url_terminal_dot_allowlist_allows_bare_path():
386+
"""An allowlist ending in /api/. normalizes to bare /api in httpx, so it allows bare /api."""
387+
options = ServerUrlValidationOptions(allowed_base_urls=["https://api.example.com/api/."])
388+
389+
assert await validate_server_url("https://api.example.com/api", options) == []
390+
391+
344392
@pytest.mark.parametrize("decode_percent_encoded", [False, True])
345393
@pytest.mark.parametrize(
346394
("path", "expected"),
347395
[
348396
("/api//../../admin", "/admin"),
349397
("/api///../../../admin", "/admin"),
350-
("/api//..", "/api/"),
398+
("/api//..", "/api"),
351399
("/api//../", "/api/"),
352-
("/api//.", "/api//"),
400+
("/api//.", "/api/"),
401+
("/api/.", "/api"),
402+
("/api/users/..", "/api"),
353403
("/api//users", "/api//users"),
354404
("/api///users/", "/api///users/"),
355405
("/api//../../..", "/"),
@@ -361,6 +411,31 @@ def test_normalize_path_pops_internal_empty_segments_but_preserves_root(path, ex
361411
assert _normalize_path(path, decode_percent_encoded) == expected
362412

363413

414+
@pytest.mark.parametrize(
415+
"path",
416+
[
417+
"/api/.",
418+
"/api/users/..",
419+
"/api//.",
420+
"/api//..",
421+
"/api//../",
422+
"/api//users",
423+
"/api///users/",
424+
"/api//../../admin",
425+
"/api///../../../admin",
426+
"/api/../admin",
427+
"/api/./users",
428+
"/..",
429+
"/../..",
430+
"//../..",
431+
"/api//../../..",
432+
],
433+
)
434+
def test_normalize_path_matches_httpx_wire_path(path):
435+
httpx_wire_path = httpx.URL(f"https://api.example.com{path}").raw_path.decode("ascii").partition("?")[0]
436+
assert _normalize_path(path, decode_percent_encoded=False) == httpx_wire_path
437+
438+
364439
async def test_validate_server_url_preserves_empty_segment_in_allowlist():
365440
options = ServerUrlValidationOptions(allowed_base_urls=["https://api.example.com/api//"])
366441

0 commit comments

Comments
 (0)