33import ipaddress
44import socket
55
6+ import httpx
67import pytest
78
89from 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)
337378async 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+
364439async 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