|
3 | 3 | import ipaddress |
4 | 4 | import socket |
5 | 5 |
|
| 6 | +import httpx |
6 | 7 | import pytest |
7 | 8 |
|
8 | 9 | from semantic_kernel.connectors.openapi_plugin.server_url_validator import ( |
9 | 10 | ServerUrlValidationOptions, |
| 11 | + _normalize_path, |
10 | 12 | validate_server_url, |
11 | 13 | ) |
12 | 14 | from semantic_kernel.exceptions import FunctionExecutionException |
@@ -256,3 +258,187 @@ async def test_validate_server_url_returns_no_addresses_for_allowed_base_url(): |
256 | 258 | async def test_validate_server_url_returns_no_addresses_when_private_access_is_allowed(): |
257 | 259 | options = ServerUrlValidationOptions(allow_private_network_access=True) |
258 | 260 | assert await validate_server_url("https://internal.example/api", options) == [] |
| 261 | + |
| 262 | + |
| 263 | +@pytest.mark.parametrize( |
| 264 | + "url", |
| 265 | + [ |
| 266 | + "https://api.example.com/api/../admin", |
| 267 | + "https://api.example.com/api/%2e%2e/admin", |
| 268 | + "https://api.example.com/api/%2E%2E/admin", |
| 269 | + "https://api.example.com/api/foo/../../admin", |
| 270 | + "https://api.example.com/api//../../admin", |
| 271 | + "https://api.example.com/api//%2e%2e/%2E%2E/admin", |
| 272 | + "https://api.example.com/api//.%2e/%2E./admin", |
| 273 | + "https://api.example.com/api///../../../admin", |
| 274 | + "https://api.example.com/api//../../..", |
| 275 | + "https://api.example.com/api/%2f../../admin", |
| 276 | + ], |
| 277 | +) |
| 278 | +async def test_validate_server_url_rejects_path_prefix_escape(url): |
| 279 | + """Dot-segment escapes must not satisfy an /api allowlist prefix.""" |
| 280 | + options = ServerUrlValidationOptions(allowed_base_urls=["https://api.example.com/api"]) |
| 281 | + |
| 282 | + with pytest.raises(FunctionExecutionException, match="allowed base URLs"): |
| 283 | + await validate_server_url(url, options) |
| 284 | + |
| 285 | + |
| 286 | +@pytest.mark.parametrize( |
| 287 | + "url", |
| 288 | + [ |
| 289 | + "https://api.example.com/api/./users", |
| 290 | + "https://api.example.com/api//users", |
| 291 | + "https://api.example.com/api//../users", |
| 292 | + "https://api.example.com/api//%2e%2e/users", |
| 293 | + "https://api.example.com/api/users", |
| 294 | + "https://api.example.com/api/", |
| 295 | + "https://api.example.com/api", |
| 296 | + ], |
| 297 | +) |
| 298 | +async def test_validate_server_url_allows_normalized_paths_under_prefix(url): |
| 299 | + options = ServerUrlValidationOptions(allowed_base_urls=["https://api.example.com/api"]) |
| 300 | + |
| 301 | + assert await validate_server_url(url, options) == [] |
| 302 | + |
| 303 | + |
| 304 | +async def test_validate_server_url_still_rejects_path_that_only_shares_prefix_chars(): |
| 305 | + options = ServerUrlValidationOptions(allowed_base_urls=["https://api.example.com/api"]) |
| 306 | + |
| 307 | + with pytest.raises(FunctionExecutionException, match="allowed base URLs"): |
| 308 | + await validate_server_url("https://api.example.com/apiusers", options) |
| 309 | + |
| 310 | + |
| 311 | +async def test_validate_server_url_rejects_path_that_only_matches_after_decoding(): |
| 312 | + options = ServerUrlValidationOptions(allowed_base_urls=["https://api.example.com/api"]) |
| 313 | + |
| 314 | + with pytest.raises(FunctionExecutionException, match="allowed base URLs"): |
| 315 | + await validate_server_url("https://api.example.com/admin/%2e%2e/api", options) |
| 316 | + |
| 317 | + |
| 318 | +@pytest.mark.parametrize( |
| 319 | + "url", |
| 320 | + [ |
| 321 | + "https://api.example.com/api/.", |
| 322 | + "https://api.example.com/api/users/..", |
| 323 | + "https://api.example.com/api//..", |
| 324 | + ], |
| 325 | +) |
| 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.""" |
| 343 | + options = ServerUrlValidationOptions(allowed_base_urls=["https://api.example.com/api/"]) |
| 344 | + |
| 345 | + assert await validate_server_url(url, options) == [] |
| 346 | + |
| 347 | + |
| 348 | +@pytest.mark.parametrize( |
| 349 | + "url", |
| 350 | + [ |
| 351 | + "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 | + [ |
| 374 | + "https://api.example.com/api/", |
| 375 | + "https://api.example.com/api//.", |
| 376 | + ], |
| 377 | +) |
| 378 | +async def test_validate_server_url_trailing_slash_allowlist_does_not_allow_bare_path(allowed_base_url): |
| 379 | + options = ServerUrlValidationOptions(allowed_base_urls=[allowed_base_url]) |
| 380 | + |
| 381 | + with pytest.raises(FunctionExecutionException, match="allowed base URLs"): |
| 382 | + await validate_server_url("https://api.example.com/api", options) |
| 383 | + |
| 384 | + |
| 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 | + |
| 392 | +@pytest.mark.parametrize("decode_percent_encoded", [False, True]) |
| 393 | +@pytest.mark.parametrize( |
| 394 | + ("path", "expected"), |
| 395 | + [ |
| 396 | + ("/api//../../admin", "/admin"), |
| 397 | + ("/api///../../../admin", "/admin"), |
| 398 | + ("/api//..", "/api"), |
| 399 | + ("/api//../", "/api/"), |
| 400 | + ("/api//.", "/api/"), |
| 401 | + ("/api/.", "/api"), |
| 402 | + ("/api/users/..", "/api"), |
| 403 | + ("/api//users", "/api//users"), |
| 404 | + ("/api///users/", "/api///users/"), |
| 405 | + ("/api//../../..", "/"), |
| 406 | + ("//../..", "/"), |
| 407 | + ("/../../admin", "/admin"), |
| 408 | + ], |
| 409 | +) |
| 410 | +def test_normalize_path_pops_internal_empty_segments_but_preserves_root(path, expected, decode_percent_encoded): |
| 411 | + assert _normalize_path(path, decode_percent_encoded) == expected |
| 412 | + |
| 413 | + |
| 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 | + |
| 439 | +async def test_validate_server_url_preserves_empty_segment_in_allowlist(): |
| 440 | + options = ServerUrlValidationOptions(allowed_base_urls=["https://api.example.com/api//"]) |
| 441 | + |
| 442 | + assert await validate_server_url("https://api.example.com/api//users", options) == [] |
| 443 | + with pytest.raises(FunctionExecutionException, match="allowed base URLs"): |
| 444 | + await validate_server_url("https://api.example.com/api/users", options) |
0 commit comments