From 56cd0fe345d8fc7a9035935c30d9c42f43128fa6 Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Sun, 15 Nov 2026 17:19:24 +0530 Subject: [PATCH 1/7] test(sessions): add failing test for uploading directory path in SessionsPythonTool --- .../core_plugins/test_sessions_python_plugin.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/python/tests/unit/core_plugins/test_sessions_python_plugin.py b/python/tests/unit/core_plugins/test_sessions_python_plugin.py index dc47b5a82a81..514a16d922d9 100644 --- a/python/tests/unit/core_plugins/test_sessions_python_plugin.py +++ b/python/tests/unit/core_plugins/test_sessions_python_plugin.py @@ -885,6 +885,21 @@ async def test_upload_file_path_traversal_blocked(aca_python_sessions_unit_test_ await plugin.upload_file(local_file_path=traversal_path) +async def test_upload_file_denied_when_path_is_allowed_directory(aca_python_sessions_unit_test_env, tmp_path): + """Test that upload_file raises an exception when path is the allowed directory itself.""" + allowed_dir = tmp_path / "allowed" + allowed_dir.mkdir() + + plugin = SessionsPythonTool( + auth_callback=lambda: "sample_token", + enable_dangerous_file_uploads=True, + allowed_upload_directories={str(allowed_dir)}, + ) + + with pytest.raises(FunctionExecutionException, match="Access denied"): + await plugin.upload_file(local_file_path=str(allowed_dir)) + + @patch("httpx.AsyncClient.get") @patch("httpx.AsyncClient.post") async def test_upload_file_succeeds_within_allowed_directory( From 345da50254423f256976ef1d725fe856f5a07824 Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Sun, 15 Nov 2026 17:19:59 +0530 Subject: [PATCH 2/7] fix(sessions): reject allowed directory itself in _validate_local_path_for_upload --- .../core_plugins/sessions_python_tool/sessions_python_plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py b/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py index 7d9ff5978b70..d953f55ade06 100644 --- a/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py +++ b/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py @@ -217,7 +217,7 @@ def _validate_local_path_for_upload(self, local_file_path: str) -> str: allowed_canonical = os.path.realpath(allowed_dir) try: common = os.path.commonpath([allowed_canonical, canonical_path]) - if common == allowed_canonical: + if common == allowed_canonical and canonical_path != allowed_canonical: return canonical_path except ValueError: continue # Different drives on Windows From e9f7e195220b6112000a67102cb1c6a817022720 Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Sun, 15 Nov 2026 17:21:01 +0530 Subject: [PATCH 3/7] test(sessions): verify subdirectories are rejected in _validate_local_path_for_upload --- .../core_plugins/test_sessions_python_plugin.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/python/tests/unit/core_plugins/test_sessions_python_plugin.py b/python/tests/unit/core_plugins/test_sessions_python_plugin.py index 514a16d922d9..c694fa17be9f 100644 --- a/python/tests/unit/core_plugins/test_sessions_python_plugin.py +++ b/python/tests/unit/core_plugins/test_sessions_python_plugin.py @@ -900,6 +900,23 @@ async def test_upload_file_denied_when_path_is_allowed_directory(aca_python_sess await plugin.upload_file(local_file_path=str(allowed_dir)) +async def test_upload_file_denied_when_path_is_subdirectory(aca_python_sessions_unit_test_env, tmp_path): + """Test that upload_file raises an exception when path is a subdirectory inside allowed directory.""" + allowed_dir = tmp_path / "allowed" + allowed_dir.mkdir() + sub_dir = allowed_dir / "subdir" + sub_dir.mkdir() + + plugin = SessionsPythonTool( + auth_callback=lambda: "sample_token", + enable_dangerous_file_uploads=True, + allowed_upload_directories={str(allowed_dir)}, + ) + + with pytest.raises(FunctionExecutionException, match="Access denied"): + await plugin.upload_file(local_file_path=str(sub_dir)) + + @patch("httpx.AsyncClient.get") @patch("httpx.AsyncClient.post") async def test_upload_file_succeeds_within_allowed_directory( From b7540f212df60d7e51eed7b4a580bd9dfb94c453 Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Sun, 15 Nov 2026 17:21:54 +0530 Subject: [PATCH 4/7] fix(sessions): reject subdirectory paths in _validate_local_path_for_upload --- .../sessions_python_tool/sessions_python_plugin.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py b/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py index d953f55ade06..02eda7942e38 100644 --- a/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py +++ b/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py @@ -217,7 +217,11 @@ def _validate_local_path_for_upload(self, local_file_path: str) -> str: allowed_canonical = os.path.realpath(allowed_dir) try: common = os.path.commonpath([allowed_canonical, canonical_path]) - if common == allowed_canonical and canonical_path != allowed_canonical: + if ( + common == allowed_canonical + and canonical_path != allowed_canonical + and not os.path.isdir(canonical_path) + ): return canonical_path except ValueError: continue # Different drives on Windows From 24d0430592d18792b96b5a4789ef6d3744ddcebd Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Sun, 15 Nov 2026 17:22:53 +0530 Subject: [PATCH 5/7] test(sessions): verify symlink pointing to a directory is rejected in upload validation --- .../test_sessions_python_plugin.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/python/tests/unit/core_plugins/test_sessions_python_plugin.py b/python/tests/unit/core_plugins/test_sessions_python_plugin.py index c694fa17be9f..cf820925b3f8 100644 --- a/python/tests/unit/core_plugins/test_sessions_python_plugin.py +++ b/python/tests/unit/core_plugins/test_sessions_python_plugin.py @@ -917,6 +917,25 @@ async def test_upload_file_denied_when_path_is_subdirectory(aca_python_sessions_ await plugin.upload_file(local_file_path=str(sub_dir)) +async def test_upload_file_denied_through_symlink_to_directory(aca_python_sessions_unit_test_env, tmp_path): + """Test that a symlink pointing to a directory is rejected in upload validation.""" + allowed_dir = tmp_path / "allowed" + allowed_dir.mkdir() + sub_dir = allowed_dir / "subdir" + sub_dir.mkdir() + link = allowed_dir / "link_to_dir" + _symlink_or_skip(link, sub_dir) + + plugin = SessionsPythonTool( + auth_callback=lambda: "sample_token", + enable_dangerous_file_uploads=True, + allowed_upload_directories={str(allowed_dir)}, + ) + + with pytest.raises(FunctionExecutionException, match="Access denied"): + await plugin.upload_file(local_file_path=str(link)) + + @patch("httpx.AsyncClient.get") @patch("httpx.AsyncClient.post") async def test_upload_file_succeeds_within_allowed_directory( From 7535bdfc616d425f9296aef70ab623ac6141d09e Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Sun, 15 Nov 2026 17:23:14 +0530 Subject: [PATCH 6/7] docs(sessions): clarify upload path validation docstring in SessionsPythonTool --- .../sessions_python_tool/sessions_python_plugin.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py b/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py index 02eda7942e38..62184938e284 100644 --- a/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py +++ b/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py @@ -191,7 +191,10 @@ def _build_url_with_version(self, base_url, endpoint, params): return f"{base_url}{endpoint}?{query_string}" def _validate_local_path_for_upload(self, local_file_path: str) -> str: - """Validate local path is within allowed upload directories. + """Validate local path is within allowed upload directories and is not a directory. + + The path is canonicalized to ensure it resides inside an allowed directory. + Paths that resolve to an allowed directory itself or any subdirectory are rejected. Args: local_file_path: The path to validate. @@ -200,7 +203,8 @@ def _validate_local_path_for_upload(self, local_file_path: str) -> str: str: The canonicalized absolute path. Raises: - FunctionExecutionException: If file operations are disabled or path is not within allowed directories. + FunctionExecutionException: If file operations are disabled, path is not within allowed + directories, or path resolves to a directory. """ if not self.enable_dangerous_file_uploads: raise FunctionExecutionException( From 079c12f725d70c4a233569684e7f2a9f9b5f42e1 Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Sun, 15 Nov 2026 17:24:33 +0530 Subject: [PATCH 7/7] test(sessions): verify valid file uploads within allowed directory continue to succeed --- .../test_sessions_python_plugin.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/python/tests/unit/core_plugins/test_sessions_python_plugin.py b/python/tests/unit/core_plugins/test_sessions_python_plugin.py index cf820925b3f8..17d4e4e40504 100644 --- a/python/tests/unit/core_plugins/test_sessions_python_plugin.py +++ b/python/tests/unit/core_plugins/test_sessions_python_plugin.py @@ -995,6 +995,66 @@ async def async_return(result): mock_post.assert_awaited_once() +@patch("httpx.AsyncClient.get") +@patch("httpx.AsyncClient.post") +async def test_upload_file_nested_file_succeeds_within_allowed_directory( + mock_post, mock_get, aca_python_sessions_unit_test_env, tmp_path +): + """Test that upload_file succeeds for a file nested in a subdirectory within allowed directories.""" + + async def async_return(result): + return result + + allowed_dir = tmp_path / "allowed" + sub_dir = allowed_dir / "subdir" + sub_dir.mkdir(parents=True) + test_file = sub_dir / "nested_test.txt" + test_file.write_text("nested content") + + with patch( + "semantic_kernel.core_plugins.sessions_python_tool.sessions_python_plugin.SessionsPythonTool._ensure_auth_token", + return_value="test_token", + ): + mock_request = httpx.Request(method="POST", url="https://example.com/files/upload?identifier=None") + mock_response = httpx.Response( + status_code=200, + json={"$id": "1", "value": []}, + request=mock_request, + ) + mock_post.return_value = await async_return(mock_response) + + mock_get_request = httpx.Request(method="GET", url="https://example.com/files?identifier=None") + mock_get_response = httpx.Response( + status_code=200, + json={ + "$id": "1", + "value": [ + { + "$id": "2", + "properties": { + "$id": "3", + "filename": "nested_test.txt", + "size": 14, + "lastModifiedTime": "2024-07-02T19:29:23.4369699Z", + }, + }, + ], + }, + request=mock_get_request, + ) + mock_get.return_value = await async_return(mock_get_response) + + plugin = SessionsPythonTool( + auth_callback=lambda: "sample_token", + enable_dangerous_file_uploads=True, + allowed_upload_directories={str(allowed_dir)}, + ) + + result = await plugin.upload_file(local_file_path=str(test_file)) + assert result.filename == "nested_test.txt" + mock_post.assert_awaited_once() + + @patch("httpx.AsyncClient.get") async def test_download_file_works_without_allowed_directories(mock_get, aca_python_sessions_unit_test_env, tmp_path): """Test that download_file works without restrictions when allowed_download_directories is None."""