Repository navigation
fix(security): unified sensitive path protection across shell and file tools #1713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,121 @@ | |
| //! attacks and ensure paths stay within allowed sandboxes. | ||
|
|
||
| use std::path::{Path, PathBuf}; | ||
| use std::sync::LazyLock; | ||
|
|
||
| use crate::tools::tool::ToolError; | ||
|
|
||
| /// Paths that contain credentials, secrets, or private keys. | ||
| /// Used by both file tools (exact path check) and shell tool (substring scan). | ||
| /// Keep sorted by category for readability. | ||
| static SENSITIVE_PATH_PATTERNS: LazyLock<Vec<&'static str>> = LazyLock::new(|| { | ||
| vec![ | ||
| // SSH | ||
| "/.ssh/", | ||
| "/id_rsa", | ||
| "/id_ed25519", | ||
| "/id_ecdsa", | ||
| "/id_dsa", | ||
| "/authorized_keys", | ||
| "/known_hosts", | ||
| // GPG | ||
| "/.gnupg/", | ||
| // AWS | ||
| "/.aws/credentials", | ||
| "/.aws/config", | ||
| // Kubernetes | ||
| "/.kube/config", | ||
| // Cloud providers | ||
| "/.azure/", | ||
| "/.gcloud/", | ||
| "/.config/gcloud/", | ||
| // Terraform | ||
| "/.terraform.d/credentials.tfrc.json", | ||
| // GitHub CLI | ||
| "/.config/gh/hosts.yml", | ||
| // Docker | ||
| "/.docker/config.json", | ||
| // Vault | ||
| "/.vault-token", | ||
| // Shell history | ||
| "/.bash_history", | ||
| "/.zsh_history", | ||
| "/.histfile", | ||
| // Env files (may contain secrets) | ||
| "/.env", | ||
| // Git credentials | ||
| "/.git-credentials", | ||
| "/.netrc", | ||
| "/.pgpass", | ||
| // IronClaw's own secrets | ||
| "/.ironclaw/secrets/", | ||
| // System | ||
| "/etc/shadow", | ||
| "/etc/gshadow", | ||
| ] | ||
| }); | ||
|
|
||
| /// File extensions that are always sensitive regardless of location. | ||
| static SENSITIVE_EXTENSIONS: LazyLock<Vec<&'static str>> = LazyLock::new(|| { | ||
| vec![".pem", ".key", ".p12", ".pfx", ".jks", ".keystore"] | ||
| }); | ||
|
|
||
| /// Suffixes that indicate a file is safe despite matching a sensitive pattern | ||
| /// (e.g., `.env.example`, `.env.sample`). | ||
| static SAFE_SUFFIXES: LazyLock<Vec<&'static str>> = LazyLock::new(|| { | ||
| vec![".example", ".sample", ".template", ".dist", ".bak.example"] | ||
| }); | ||
|
|
||
| /// Check if a resolved file path points to a sensitive location. | ||
| /// Used by file tools (read, write, list_dir, apply_patch). | ||
| pub fn is_sensitive_path(path: &Path) -> bool { | ||
| let path_str = match path.canonicalize() { | ||
| Ok(p) => p.to_string_lossy().to_string(), | ||
| Err(_) => path.to_string_lossy().to_string(), | ||
| }; | ||
|
|
||
| // Safe suffixes override sensitive patterns | ||
| let lower = path_str.to_lowercase(); | ||
| if SAFE_SUFFIXES.iter().any(|s| lower.ends_with(s)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check sensitive path patterns | ||
| if SENSITIVE_PATH_PATTERNS.iter().any(|p| path_str.contains(p)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HIGH (H3): Pattern check is case-sensitive but Fix: Change |
||
| return true; | ||
| } | ||
|
|
||
| // Check sensitive file extensions | ||
| if let Some(ext) = path.extension().and_then(|e| e.to_str()) { | ||
| let dot_ext = format!(".{}", ext.to_lowercase()); | ||
| if SENSITIVE_EXTENSIONS.iter().any(|e| *e == dot_ext) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| false | ||
| } | ||
|
|
||
| /// Scan a shell command string for references to sensitive paths. | ||
| /// Returns the first matched pattern, or None if the command is clean. | ||
| /// Used by the shell tool to block `cat ~/.ssh/id_rsa` etc. | ||
| pub fn command_references_sensitive_path(command: &str) -> Option<&'static str> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HIGH (H1): This function does pure substring matching with no safe-suffix exemption, unlike Fix: Apply |
||
| let normalized = command.to_lowercase(); | ||
|
|
||
| for pattern in SENSITIVE_PATH_PATTERNS.iter() { | ||
| // For path patterns, check case-insensitively | ||
| if normalized.contains(&pattern.to_lowercase()) { | ||
| return Some(pattern); | ||
| } | ||
|
Comment on lines
+123
to
+127
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation uses simple substring containment to detect sensitive paths, which leads to false positives (e.g., matching 'id_rsa' inside other words). Per repository security guidelines, please replace simple substring checks with token-based or word-boundary checks to improve precision and reduce false positives. References
|
||
| } | ||
|
|
||
| // Check for sensitive extensions in file arguments | ||
| SENSITIVE_EXTENSIONS | ||
| .iter() | ||
| .find(|ext| normalized.contains(*ext)) | ||
| .copied() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HIGH (H2): Extension matching via Fix: Split the command into whitespace-delimited tokens and check file extensions on each token individually, or use a regex like |
||
| } | ||
|
|
||
| /// Normalize a path by resolving `.` and `..` components lexically (no filesystem access). | ||
| /// | ||
| /// This is critical for security: `std::fs::canonicalize` only works on paths that exist, | ||
|
|
@@ -236,4 +348,90 @@ mod tests { | |
| let result = validate_path("a/b/../c.txt", Some(dir.path())); | ||
| assert!(result.is_ok()); | ||
| } | ||
|
|
||
| // ── sensitive path tests ── | ||
|
|
||
| #[test] | ||
| fn test_is_sensitive_path_blocks_ssh() { | ||
| assert!(is_sensitive_path(Path::new("/home/user/.ssh/id_rsa"))); | ||
| assert!(is_sensitive_path(Path::new("/home/user/.ssh/authorized_keys"))); | ||
| assert!(is_sensitive_path(Path::new("/root/.ssh/config"))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_is_sensitive_path_blocks_cloud_credentials() { | ||
| assert!(is_sensitive_path(Path::new("/home/user/.aws/credentials"))); | ||
| assert!(is_sensitive_path(Path::new("/home/user/.kube/config"))); | ||
| assert!(is_sensitive_path(Path::new("/home/user/.azure/some_token"))); | ||
| assert!(is_sensitive_path(Path::new( | ||
| "/home/user/.config/gh/hosts.yml" | ||
| ))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_is_sensitive_path_blocks_system_secrets() { | ||
| assert!(is_sensitive_path(Path::new("/etc/shadow"))); | ||
| assert!(is_sensitive_path(Path::new("/etc/gshadow"))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_is_sensitive_path_blocks_key_files_by_extension() { | ||
| assert!(is_sensitive_path(Path::new("/tmp/server.pem"))); | ||
| assert!(is_sensitive_path(Path::new("/app/certs/private.key"))); | ||
| assert!(is_sensitive_path(Path::new("/home/user/keystore.p12"))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_is_sensitive_path_allows_safe_suffixes() { | ||
| assert!(!is_sensitive_path(Path::new("/app/.env.example"))); | ||
| assert!(!is_sensitive_path(Path::new("/app/.env.sample"))); | ||
| assert!(!is_sensitive_path(Path::new("/app/.env.template"))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_is_sensitive_path_allows_normal_files() { | ||
| assert!(!is_sensitive_path(Path::new("/app/src/main.rs"))); | ||
| assert!(!is_sensitive_path(Path::new("/home/user/README.md"))); | ||
| assert!(!is_sensitive_path(Path::new("/tmp/output.json"))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_is_sensitive_path_blocks_env_files() { | ||
| assert!(is_sensitive_path(Path::new("/app/.env"))); | ||
| assert!(is_sensitive_path(Path::new("/app/.env.local"))); | ||
| assert!(is_sensitive_path(Path::new("/app/.env.production"))); | ||
| } | ||
|
|
||
| // ── command scanning tests ── | ||
|
|
||
| #[test] | ||
| fn test_command_references_sensitive_path_catches_cat_ssh() { | ||
| assert!(command_references_sensitive_path("cat ~/.ssh/id_rsa").is_some()); | ||
| assert!(command_references_sensitive_path("head -n 5 /home/user/.ssh/authorized_keys").is_some()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_command_references_sensitive_path_catches_aws() { | ||
| assert!(command_references_sensitive_path("cat ~/.aws/credentials").is_some()); | ||
| assert!(command_references_sensitive_path("grep key ~/.aws/config").is_some()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_command_references_sensitive_path_catches_etc_shadow() { | ||
| assert!(command_references_sensitive_path("cat /etc/shadow").is_some()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_command_references_sensitive_path_catches_key_extensions() { | ||
| assert!(command_references_sensitive_path("cp server.pem /tmp/").is_some()); | ||
| assert!(command_references_sensitive_path("cat private.key").is_some()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_command_references_sensitive_path_allows_safe_commands() { | ||
| assert!(command_references_sensitive_path("ls -la").is_none()); | ||
| assert!(command_references_sensitive_path("cargo build").is_none()); | ||
| assert!(command_references_sensitive_path("git status").is_none()); | ||
| assert!(command_references_sensitive_path("cat README.md").is_none()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When canonicalization fails,
is_sensitive_pathfalls back to the unresolved input path, which allows symlink aliases to hide sensitive destinations for new-file writes. For example, if/tmp/linkis a symlink to~/.ssh, writing/tmp/link/new_configis treated as non-sensitive (no/.ssh/in the alias string) even thoughwrite_filewill create the file inside~/.ssh, leaving a bypass for protected directories.Useful? React with 👍 / 👎.