Python: Add directory allowlist configuration for SessionsPythonTool … · alliscode/semantic-kernel@e696dc7 · GitHub
Skip to content

Commit e696dc7

Browse files
authored
Python: Add directory allowlist configuration for SessionsPythonTool file (microsoft#13467)
### Motivation and Context When `SessionsPythonTool` is used with an AI model, providing explicit directory boundaries for file operations gives developers fine-grained control over which parts of the filesystem the plugin can access. This follows the pattern established by `HttpPlugin.allowed_domains`. - Add `allowed_upload_directories` parameter to control which local directories can be used for file uploads - Add `allowed_download_directories` parameter for optional restrictions on file download destinations - Implement path canonicalization to resolve symlinks and normalize paths Usage: ```python tool = SessionsPythonTool( credential=credential, allowed_upload_directories={"/app/data/uploads", "/app/user_files"}, allowed_download_directories={"/app/data/downloads"}, # optional ) ``` <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description - `upload_file` now requires `allowed_upload_directories` to be configured (deny-by-default) - `download_file` optionally supports `allowed_download_directories` (permissive-by-default, since it's not exposed as a kernel function) - Both parameters accept `set[str]` or `list[str]` for convenience - Paths are canonicalized using `os.path.realpath()` before validation <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
1 parent f987064 commit e696dc7

3 files changed

Lines changed: 408 additions & 48 deletions

File tree

‎python/samples/getting_started_with_agents/chat_completion/step12_chat_completion_agent_code_interpreter.py‎

Lines changed: 9 additions & 2 deletions

‎python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py‎

Lines changed: 116 additions & 7 deletions

0 commit comments

Comments
 (0)