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 | """Block accidental upload of local files from well-known secret/credential locations.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import re |
| 6 | import typing as t |
| 7 | from pathlib import Path |
| 8 | |
| 9 | # Path components that indicate a sensitive directory anywhere in a resolved local path. |
| 10 | BUILTIN_FILE_UPLOAD_PATH_DENY_SEGMENTS: t.Tuple[str, ...] = ( |
| 11 | ".ssh", |
| 12 | ".aws", |
| 13 | ".azure", |
| 14 | ".gnupg", |
| 15 | ".kube", |
| 16 | ".docker", |
| 17 | ".claude", # may contain API keys and project context read by assistants |
| 18 | ".password-store", |
| 19 | "keychains", |
| 20 | ) |
| 21 | |
| 22 | _SECRET_LIKE_BASENAME = re.compile(r"^(\.env(\.|$)|\.netrc$|\.pgpass$)", re.IGNORECASE) |
| 23 | _DEFAULT_PRIVATE_KEY_BASENAME = re.compile( |
| 24 | r"^id_(rsa|ed25519|ecdsa|dsa|ecdsa_sk)(\.old)?$", re.IGNORECASE |
| 25 | ) |
| 26 | |
| 27 | |
| 28 | def _path_segments(path: Path) -> list[str]: |
| 29 | parts = [part for part in path.as_posix().split("/") if part] |
| 30 | if parts and parts[0].endswith(":"): |
| 31 | # Windows path: "C:/Users/..." -> drop drive letter segment |
| 32 | return parts[1:] |
| 33 | return parts |
| 34 | |
| 35 | |
| 36 | def _normalize_path_segments( |
| 37 | file_path: str | Path, |
| 38 | ) -> tuple[list[str], list[str]]: |
| 39 | """Return resolved and written segments so neither can hide a denied name.""" |
| 40 | written = Path(file_path).expanduser().absolute() |
| 41 | try: |
| 42 | resolved = written.resolve() |
| 43 | except OSError: |
| 44 | resolved = written |
| 45 | return _path_segments(resolved), _path_segments(written) |
| 46 | |
| 47 | |
| 48 | def _get_block_reason( |
| 49 | file_path: t.Union[str, Path], |
| 50 | additional_deny_segments: t.Optional[t.Sequence[str]] = None, |
| 51 | ) -> t.Optional[str]: |
| 52 | extra = [s.strip() for s in (additional_deny_segments or []) if s and s.strip()] |
| 53 | deny: t.Set[str] = {s.lower() for s in BUILTIN_FILE_UPLOAD_PATH_DENY_SEGMENTS} |
| 54 | deny.update(s.lower() for s in extra) |
| 55 | |
| 56 | resolved_segments, written_segments = _normalize_path_segments(file_path) |
| 57 | for segments in (resolved_segments, written_segments): |
| 58 | # Case-insensitive segment match: lower each path component once per check. |
| 59 | for seg, key in zip(segments, (s.lower() for s in segments), strict=True): |
| 60 | if key in deny: |
| 61 | return f'path segment "{seg}" is in the sensitive file upload denylist' |
| 62 | |
| 63 | for segments in (resolved_segments, written_segments): |
| 64 | if not segments: |
| 65 | continue |
| 66 | basename = segments[-1] |
| 67 | if _SECRET_LIKE_BASENAME.search( |
| 68 | basename |
| 69 | ) or _DEFAULT_PRIVATE_KEY_BASENAME.match(basename): |
| 70 | return f'file name "{basename}" looks like a credential, env, or private key file' |
| 71 | if basename.lower() == "credentials": |
| 72 | return ( |
| 73 | 'file name "credentials" is often used for cloud/API credential stores' |
| 74 | ) |
| 75 | return None |
| 76 | |
| 77 | |
| 78 | def is_blocked_sensitive_file_upload_path( |
| 79 | file_path: t.Union[str, Path], |
| 80 | additional_deny_segments: t.Optional[t.Sequence[str]] = None, |
| 81 | ) -> bool: |
| 82 | return _get_block_reason(file_path, additional_deny_segments) is not None |
| 83 | |
| 84 | |
| 85 | def assert_safe_local_file_upload_path( |
| 86 | file_path: t.Union[str, Path], |
| 87 | *, |
| 88 | enabled: bool = True, |
| 89 | additional_deny_segments: t.Optional[t.Sequence[str]] = None, |
| 90 | ) -> None: |
| 91 | """Raise SensitiveFilePathBlockedError if *file_path* matches the denylist.""" |
| 92 | if not enabled: |
| 93 | return |
| 94 | reason = _get_block_reason(file_path, additional_deny_segments) |
| 95 | if reason: |
| 96 | from composio.exceptions import SensitiveFilePathBlockedError |
| 97 | |
| 98 | raise SensitiveFilePathBlockedError( |
| 99 | f"Refusing to upload: {reason}. " |
| 100 | "Set sensitive_file_upload_protection=False on Composio if you must " |
| 101 | "(not recommended), or use a copy outside sensitive locations." |
| 102 | ) |