Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(python): block sensitive paths hidden by symlinks
jkomyno43545ec8891c
diff --git a/python/composio/utils/sensitive_file_upload_paths.py b/python/composio/utils/sensitive_file_upload_paths.pyindex a5d411803..2de2c9e7c 100644--- a/python/composio/utils/sensitive_file_upload_paths.py+++ b/python/composio/utils/sensitive_file_upload_paths.py@@ -25,19 +25,26 @@ _DEFAULT_PRIVATE_KEY_BASENAME = re.compile()-def _normalize_path_segments(file_path: t.Union[str, Path]) -> t.List[str]:- p = Path(file_path).expanduser()- try:- resolved = p.resolve()- except OSError:- resolved = p- parts = [part for part in resolved.as_posix().split("/") if part]+def _path_segments(path: Path) -> list[str]:+ parts = [part for part in path.as_posix().split("/") if part]if parts and parts[0].endswith(":"):# Windows path: "C:/Users/..." -> drop drive letter segment- parts = parts[1:]+ return parts[1:]return parts+def _normalize_path_segments(+ file_path: str | Path,+) -> tuple[list[str], list[str]]:+ """Return resolved and written segments so neither can hide a denied name."""+ written = Path(file_path).expanduser().absolute()+ try:+ resolved = written.resolve()+ except OSError:+ resolved = written+ return _path_segments(resolved), _path_segments(written)++def _get_block_reason(file_path: t.Union[str, Path],additional_deny_segments: t.Optional[t.Sequence[str]] = None,@@ -46,23 +53,25 @@ def _get_block_reason(deny: t.Set[str] = {s.lower() for s in BUILTIN_FILE_UPLOAD_PATH_DENY_SEGMENTS}deny.update(s.lower() for s in extra)- segments = _normalize_path_segments(file_path)- # Case-insensitive segment match: lower each path component once per check.- for seg, key in zip(segments, (s.lower() for s in segments), strict=True):- if key in deny:- return f'path segment "{seg}" is in the sensitive file upload denylist'-- if not segments:- return None- basename = segments[-1]- if _SECRET_LIKE_BASENAME.search(basename) or _DEFAULT_PRIVATE_KEY_BASENAME.match(- basename- ):- return (- f'file name "{basename}" looks like a credential, env, or private key file'- )- if basename.lower() == "credentials":- return 'file name "credentials" is often used for cloud/API credential stores'+ resolved_segments, written_segments = _normalize_path_segments(file_path)+ for segments in (resolved_segments, written_segments):+ # Case-insensitive segment match: lower each path component once per check.+ for seg, key in zip(segments, (s.lower() for s in segments), strict=True):+ if key in deny:+ return f'path segment "{seg}" is in the sensitive file upload denylist'++ for segments in (resolved_segments, written_segments):+ if not segments:+ continue+ basename = segments[-1]+ if _SECRET_LIKE_BASENAME.search(+ basename+ ) or _DEFAULT_PRIVATE_KEY_BASENAME.match(basename):+ return f'file name "{basename}" looks like a credential, env, or private key file'+ if basename.lower() == "credentials":+ return (+ 'file name "credentials" is often used for cloud/API credential stores'+ )return Nonediff --git a/python/tests/test_sensitive_file_upload_paths.py b/python/tests/test_sensitive_file_upload_paths.pyindex 7580c05fb..ddf653f08 100644--- a/python/tests/test_sensitive_file_upload_paths.py+++ b/python/tests/test_sensitive_file_upload_paths.py@@ -73,6 +73,49 @@ def test_blocks_after_symlink_resolves_to_sensitive_dir() -> None:assert is_blocked_sensitive_file_upload_path(link) is True+def test_blocks_sensitive_symlinked_directory_with_plain_target(tmp_path: Path) -> None:+ store = tmp_path / "state" / "claude"+ store.mkdir(parents=True)+ (store / "settings.json").write_text("{}", encoding="utf-8")+ home = tmp_path / "home"+ home.mkdir()+ link = home / ".claude"+ try:+ link.symlink_to(store, target_is_directory=True)+ except OSError:+ pytest.skip("symlinks not supported")++ sensitive_path = link / "settings.json"+ assert is_blocked_sensitive_file_upload_path(sensitive_path) is True+ with pytest.raises(SensitiveFilePathBlockedError):+ assert_safe_local_file_upload_path(sensitive_path)+++def test_blocks_sensitive_symlinked_basename_with_plain_target(tmp_path: Path) -> None:+ target = tmp_path / "plain-config"+ target.write_text("SECRET=1", encoding="utf-8")+ link = tmp_path / ".env"+ try:+ link.symlink_to(target)+ except OSError:+ pytest.skip("symlinks not supported")++ assert is_blocked_sensitive_file_upload_path(link) is True+++def test_allows_ordinary_file_through_symlinked_directory(tmp_path: Path) -> None:+ store = tmp_path / "store"+ store.mkdir()+ (store / "document.pdf").write_text("x", encoding="utf-8")+ link = tmp_path / "docs"+ try:+ link.symlink_to(store, target_is_directory=True)+ except OSError:+ pytest.skip("symlinks not supported")++ assert is_blocked_sensitive_file_upload_path(link / "document.pdf") is False++def test_assert_safe_raises() -> None:p = Path.home() / ".ssh" / "id_rsa"with pytest.raises(SensitiveFilePathBlockedError):