Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import re
from typing import Any, Final
from urllib.parse import ParseResult, ParseResultBytes, urlencode, urljoin, urlparse, urlunparse
from urllib.parse import ParseResult, ParseResultBytes, quote, urlencode, urljoin, urlparse, urlunparse

from semantic_kernel.connectors.openapi_plugin.models.rest_api_expected_response import (
RestApiExpectedResponse,
Expand Down Expand Up @@ -288,7 +288,7 @@ def build_path(self, path_template: str, arguments: dict[str, Any]) -> str:
f"required parameter of the operation - `{self.id}`."
)
continue
path_template = path_template.replace(f"{{{parameter.name}}}", str(argument))
path_template = path_template.replace(f"{{{parameter.name}}}", quote(str(argument), safe=""))
Comment thread
SergeyMenshykh marked this conversation as resolved.
Comment thread
SergeyMenshykh marked this conversation as resolved.
return path_template

def build_query_string(self, arguments: dict[str, Any]) -> str:
Expand Down
28 changes: 28 additions & 0 deletions python/tests/unit/connectors/openapi_plugin/test_sk_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,34 @@ def test_build_path_with_optional_and_required_parameters():
assert operation.build_path(operation.path, arguments) == expected_path


def test_build_path_encodes_special_characters():
parameters = [RestApiParameter(name="id", type="string", location=RestApiParameterLocation.PATH, is_required=True)]
operation = RestApiOperation(
id="test", method="GET", servers=["https://example.com/"], path="/resource/{id}", params=parameters
)
# Characters like /, ?, #, and spaces must be percent-encoded to prevent traversal
arguments = {"id": "foo/bar?q=1#frag data"}
result = operation.build_path(operation.path, arguments)
encoded_part = result.split("/resource/")[1]
assert "/" not in encoded_part
assert "?" not in encoded_part
assert "#" not in encoded_part
assert " " not in encoded_part
# Python's quote(safe="") encodes all except unreserved chars (letters, digits, _, ., -, ~)
assert result == "/resource/foo%2Fbar%3Fq%3D1%23frag%20data"


def test_build_path_prevents_path_traversal():
parameters = [RestApiParameter(name="id", type="string", location=RestApiParameterLocation.PATH, is_required=True)]
operation = RestApiOperation(
id="test", method="GET", servers=["https://example.com/"], path="/resource/{id}", params=parameters
)
arguments = {"id": "../../admin"}
result = operation.build_path(operation.path, arguments)
# The slashes must be encoded so ../../admin becomes a single path segment, not a traversal
assert result == "/resource/..%2F..%2Fadmin"
Comment thread
SergeyMenshykh marked this conversation as resolved.


def test_build_query_string_with_required_parameter():
parameters = [
RestApiParameter(name="query", type="string", location=RestApiParameterLocation.QUERY, is_required=True)
Expand Down
Loading