Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | """Tests for composio.utils.sensitive_file_upload_paths.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import tempfile |
| 6 | from pathlib import Path |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | from composio.exceptions import SensitiveFilePathBlockedError |
| 11 | from composio.utils.sensitive_file_upload_paths import ( |
| 12 | assert_safe_local_file_upload_path, |
| 13 | is_blocked_sensitive_file_upload_path, |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | def test_allows_normal_project_files() -> None: |
| 18 | p = Path("/tmp") / "composio-test" / "document.pdf" |
| 19 | assert is_blocked_sensitive_file_upload_path(p) is False |
| 20 | assert_safe_local_file_upload_path(p) |
| 21 | |
| 22 | |
| 23 | def test_blocks_common_credential_directory_segments() -> None: |
| 24 | home = Path.home() |
| 25 | assert is_blocked_sensitive_file_upload_path(home / ".aws" / "credentials") is True |
| 26 | assert is_blocked_sensitive_file_upload_path(home / ".ssh" / "id_ed25519") is True |
| 27 | assert ( |
| 28 | is_blocked_sensitive_file_upload_path(home / ".claude" / "settings.json") |
| 29 | is True |
| 30 | ) |
| 31 | |
| 32 | |
| 33 | def test_blocks_env_style_basenames() -> None: |
| 34 | assert is_blocked_sensitive_file_upload_path(Path("/app/repo/.env")) is True |
| 35 | assert is_blocked_sensitive_file_upload_path(Path("/app/repo/.env.local")) is True |
| 36 | |
| 37 | |
| 38 | def test_blocks_default_private_key_basenames() -> None: |
| 39 | assert is_blocked_sensitive_file_upload_path(Path("/tmp/id_ed25519")) is True |
| 40 | |
| 41 | |
| 42 | def test_allows_public_key_by_basename() -> None: |
| 43 | assert is_blocked_sensitive_file_upload_path(Path("/tmp/id_ed25519.pub")) is False |
| 44 | |
| 45 | |
| 46 | def test_honors_additional_deny_segments() -> None: |
| 47 | assert ( |
| 48 | is_blocked_sensitive_file_upload_path( |
| 49 | Path("/data/secrets/x.txt"), additional_deny_segments=["secrets"] |
| 50 | ) |
| 51 | is True |
| 52 | ) |
| 53 | assert ( |
| 54 | is_blocked_sensitive_file_upload_path( |
| 55 | Path("/data/ok/x.txt"), additional_deny_segments=["secrets"] |
| 56 | ) |
| 57 | is False |
| 58 | ) |
| 59 | |
| 60 | |
| 61 | def test_blocks_after_symlink_resolves_to_sensitive_dir() -> None: |
| 62 | with tempfile.TemporaryDirectory() as root: |
| 63 | root_p = Path(root) |
| 64 | aws_dir = root_p / "nested" / ".aws" |
| 65 | aws_dir.mkdir(parents=True) |
| 66 | target = aws_dir / "creds" |
| 67 | target.write_text("x", encoding="utf-8") |
| 68 | link = root_p / "innocent-name" |
| 69 | try: |
| 70 | link.symlink_to(target) |
| 71 | except OSError: |
| 72 | pytest.skip("symlinks not supported") |
| 73 | assert is_blocked_sensitive_file_upload_path(link) is True |
| 74 | |
| 75 | |
| 76 | def test_blocks_sensitive_symlinked_directory_with_plain_target(tmp_path: Path) -> None: |
| 77 | store = tmp_path / "state" / "claude" |
| 78 | store.mkdir(parents=True) |
| 79 | (store / "settings.json").write_text("{}", encoding="utf-8") |
| 80 | home = tmp_path / "home" |
| 81 | home.mkdir() |
| 82 | link = home / ".claude" |
| 83 | try: |
| 84 | link.symlink_to(store, target_is_directory=True) |
| 85 | except OSError: |
| 86 | pytest.skip("symlinks not supported") |
| 87 | |
| 88 | sensitive_path = link / "settings.json" |
| 89 | assert is_blocked_sensitive_file_upload_path(sensitive_path) is True |
| 90 | with pytest.raises(SensitiveFilePathBlockedError): |
| 91 | assert_safe_local_file_upload_path(sensitive_path) |
| 92 | |
| 93 | |
| 94 | def test_blocks_sensitive_symlinked_basename_with_plain_target(tmp_path: Path) -> None: |
| 95 | target = tmp_path / "plain-config" |
| 96 | target.write_text("SECRET=1", encoding="utf-8") |
| 97 | link = tmp_path / ".env" |
| 98 | try: |
| 99 | link.symlink_to(target) |
| 100 | except OSError: |
| 101 | pytest.skip("symlinks not supported") |
| 102 | |
| 103 | assert is_blocked_sensitive_file_upload_path(link) is True |
| 104 | |
| 105 | |
| 106 | def test_allows_ordinary_file_through_symlinked_directory(tmp_path: Path) -> None: |
| 107 | store = tmp_path / "store" |
| 108 | store.mkdir() |
| 109 | (store / "document.pdf").write_text("x", encoding="utf-8") |
| 110 | link = tmp_path / "docs" |
| 111 | try: |
| 112 | link.symlink_to(store, target_is_directory=True) |
| 113 | except OSError: |
| 114 | pytest.skip("symlinks not supported") |
| 115 | |
| 116 | assert is_blocked_sensitive_file_upload_path(link / "document.pdf") is False |
| 117 | |
| 118 | |
| 119 | def test_assert_safe_raises() -> None: |
| 120 | p = Path.home() / ".ssh" / "id_rsa" |
| 121 | with pytest.raises(SensitiveFilePathBlockedError): |
| 122 | assert_safe_local_file_upload_path(p) |