Skip to content

Personal AI, On Personal Devices

Python

fix(security): protect sensitive files from symlink aliases (#964)

Haotian Zheng056000bbc7dc
diff --git a/rust/crates/openjarvis-security/src/file_policy.rs b/rust/crates/openjarvis-security/src/file_policy.rs
index 58d81840..811042d0 100644
--- a/rust/crates/openjarvis-security/src/file_policy.rs
+++ b/rust/crates/openjarvis-security/src/file_policy.rs
@@ -2,7 +2,7 @@
use once_cell::sync::Lazy;
use std::collections::HashSet;
-use std::path::Path;
+use std::path::{Path, PathBuf};
static SENSITIVE_PATTERNS: Lazy<HashSet<&'static str>> = Lazy::new(|| {
HashSet::from([
@@ -16,18 +16,42 @@ static SENSITIVE_PATTERNS: Lazy<HashSet<&'static str>> = Lazy::new(|| {
])
});
-static SENSITIVE_EXTENSIONS: Lazy<Vec<&'static str>> = Lazy::new(|| {
- vec![
- ".pem", ".key", ".p12", ".pfx", ".jks", ".secrets",
- ]
-});
+static SENSITIVE_EXTENSIONS: Lazy<Vec<&'static str>> =
+ Lazy::new(|| vec![".pem", ".key", ".p12", ".pfx", ".jks", ".secrets"]);
-static SENSITIVE_PREFIXES: Lazy<Vec<&'static str>> = Lazy::new(|| {
- vec![".env.", "credentials."]
-});
+static SENSITIVE_PREFIXES: Lazy<Vec<&'static str>> = Lazy::new(|| vec![".env.", "credentials."]);
+
+/// Resolve a path for policy checks without requiring the final target to exist.
+///
+/// Preserve sensitive intermediate names before canonicalizing, including
+/// dangling targets that `canonicalize` cannot resolve.
+fn path_for_policy(path: &Path) -> PathBuf {
+ let mut candidate = path.to_path_buf();
+ for _ in 0..40 {
+ let Ok(target) = std::fs::read_link(&candidate) else {
+ break;
+ };
+ candidate = if target.is_absolute() {
+ target
+ } else {
+ candidate
+ .parent()
+ .unwrap_or_else(|| Path::new(""))
+ .join(target)
+ };
+ if matches_sensitive_name(&candidate) {
+ return candidate;
+ }
+ }
+ std::fs::canonicalize(&candidate).unwrap_or(candidate)
+}
-/// Return `true` if path matches a sensitive file pattern.
+/// Return `true` if path or its resolved target matches a sensitive pattern.
pub fn is_sensitive_file(path: &Path) -> bool {
+ matches_sensitive_name(path) || matches_sensitive_name(&path_for_policy(path))
+}
+
+fn matches_sensitive_name(path: &Path) -> bool {
let name = match path.file_name().and_then(|n| n.to_str()) {
Some(n) => n,
None => return false,
@@ -65,6 +89,119 @@ pub fn filter_sensitive_paths<'a>(paths: &'a [&'a Path]) -> Vec<&'a Path> {
mod tests {
use super::*;
+ fn symlink_file(target: &Path, alias: &Path) -> bool {
+ #[cfg(unix)]
+ let result = std::os::unix::fs::symlink(target, alias);
+ #[cfg(windows)]
+ let result = std::os::windows::fs::symlink_file(target, alias);
+ if let Err(error) = result {
+ #[cfg(windows)]
+ if error.kind() == std::io::ErrorKind::PermissionDenied {
+ return false;
+ }
+ panic!("failed to create test symlink: {error}");
+ }
+ true
+ }
+
+ #[test]
+ fn test_sensitive_symlink_name_to_ordinary_target() {
+ for sensitive_name in [".env", "credentials.json", "server.pem"] {
+ for target_exists in [false, true] {
+ let dir = tempfile::tempdir().unwrap();
+ let target = dir.path().join("notes.txt");
+ if target_exists {
+ std::fs::write(&target, "SENSITIVE-SENTINEL").unwrap();
+ }
+ let alias = dir.path().join(sensitive_name);
+ if !symlink_file(Path::new("notes.txt"), &alias) {
+ return;
+ }
+
+ assert!(
+ is_sensitive_file(&alias),
+ "{sensitive_name}, exists={target_exists}"
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn test_relative_symlink_chain() {
+ for target_name in [".env", "notes.txt"] {
+ for target_exists in [false, true] {
+ let dir = tempfile::tempdir().unwrap();
+ let target = dir.path().join(target_name);
+ if target_exists {
+ std::fs::write(&target, "SENTINEL").unwrap();
+ }
+ let intermediate = dir.path().join("intermediate.txt");
+ let alias = dir.path().join("alias.txt");
+ if !symlink_file(Path::new(target_name), &intermediate)
+ || !symlink_file(Path::new("intermediate.txt"), &alias)
+ {
+ return;
+ }
+
+ assert_eq!(is_sensitive_file(&alias), target_name == ".env");
+ }
+ }
+ }
+
+ #[test]
+ fn test_sensitive_intermediate_symlink_to_ordinary_target() {
+ for target_exists in [false, true] {
+ let dir = tempfile::tempdir().unwrap();
+ let target = dir.path().join("notes.txt");
+ if target_exists {
+ std::fs::write(&target, "SENTINEL").unwrap();
+ }
+ let intermediate = dir.path().join(".env");
+ let alias = dir.path().join("alias.txt");
+ if !symlink_file(Path::new("notes.txt"), &intermediate)
+ || !symlink_file(Path::new(".env"), &alias)
+ {
+ return;
+ }
+
+ assert!(is_sensitive_file(&alias));
+ }
+ }
+
+ #[test]
+ fn test_sensitive_symlink_loop() {
+ let dir = tempfile::tempdir().unwrap();
+ let alias = dir.path().join(".env");
+ if !symlink_file(Path::new(".env"), &alias) {
+ return;
+ }
+
+ assert!(is_sensitive_file(&alias));
+ }
+
+ #[test]
+ fn test_filter_sensitive_aliases() {
+ let dir = tempfile::tempdir().unwrap();
+ let target = dir.path().join("notes.txt");
+ std::fs::write(&target, "SENTINEL").unwrap();
+ let protected_alias = dir.path().join(".env");
+ let ordinary_alias = dir.path().join("alias.txt");
+ let secret = dir.path().join("server.pem");
+ std::fs::write(&secret, "SENTINEL").unwrap();
+ if !symlink_file(Path::new("notes.txt"), &protected_alias)
+ || !symlink_file(Path::new("server.pem"), &ordinary_alias)
+ {
+ return;
+ }
+
+ let paths = [
+ protected_alias.as_path(),
+ ordinary_alias.as_path(),
+ target.as_path(),
+ ];
+ assert_eq!(filter_sensitive_paths(&paths), vec![target.as_path()]);
+ }
+
#[test]
fn test_sensitive_files() {
assert!(is_sensitive_file(Path::new(".env")));
@@ -81,4 +218,39 @@ mod tests {
assert!(!is_sensitive_file(Path::new("README.md")));
assert!(!is_sensitive_file(Path::new("config.toml")));
}
+
+ #[test]
+ fn test_sensitive_symlink_alias() {
+ let dir = tempfile::tempdir().unwrap();
+ let sensitive = dir.path().join(".env");
+ std::fs::write(&sensitive, "SENSITIVE-SENTINEL").unwrap();
+ let alias = dir.path().join("notes.txt");
+
+ #[cfg(unix)]
+ let link_result = std::os::unix::fs::symlink(&sensitive, &alias);
+ #[cfg(windows)]
+ let link_result = std::os::windows::fs::symlink_file(&sensitive, &alias);
+ if link_result.is_err() {
+ return;
+ }
+
+ assert!(is_sensitive_file(&alias));
+ }
+
+ #[test]
+ fn test_sensitive_symlink_alias_to_missing_target() {
+ let dir = tempfile::tempdir().unwrap();
+ let alias = dir.path().join("notes.txt");
+ let sensitive = dir.path().join(".env");
+
+ #[cfg(unix)]
+ let link_result = std::os::unix::fs::symlink(&sensitive, &alias);
+ #[cfg(windows)]
+ let link_result = std::os::windows::fs::symlink_file(&sensitive, &alias);
+ if link_result.is_err() {
+ return;
+ }
+
+ assert!(is_sensitive_file(&alias));
+ }
}
diff --git a/src/openjarvis/security/file_policy.py b/src/openjarvis/security/file_policy.py
index 350f499a..9e547dc4 100644
--- a/src/openjarvis/security/file_policy.py
+++ b/src/openjarvis/security/file_policy.py
@@ -3,7 +3,7 @@
from __future__ import annotations
from pathlib import Path
-from typing import Iterable, List, Union
+from typing import Iterable, Iterator, List, Union
DEFAULT_SENSITIVE_PATTERNS: frozenset[str] = frozenset(
{
@@ -30,17 +30,41 @@ DEFAULT_SENSITIVE_PATTERNS: frozenset[str] = frozenset(
def is_sensitive_file(path: Union[str, Path]) -> bool:
"""Return ``True`` if *path* matches a sensitive file pattern.
- Checks both the filename and the full name against
- ``DEFAULT_SENSITIVE_PATTERNS`` using :func:`fnmatch.fnmatch`.
+ Checks the original path, symlink targets, and resolved path against
+ ``DEFAULT_SENSITIVE_PATTERNS``. A symlink must not bypass the policy by
+ hiding a sensitive name anywhere in its chain.
Uses the Rust implementation when available, falls back to Python.
"""
try:
from openjarvis._rust_bridge import get_rust_module
_rust = get_rust_module()
- return _rust.is_sensitive_file(str(path))
+ check = _rust.is_sensitive_file
except ImportError:
- return _is_sensitive_file_py(str(path))
+ check = _is_sensitive_file_py
+
+ return any(check(str(candidate)) for candidate in _paths_for_policy(Path(path)))
+
+
+def _paths_for_policy(path: Path) -> Iterator[Path]:
+ """Keep sensitive names that full resolution would discard."""
+ yield path
+ candidate = path
+ for _ in range(40):
+ try:
+ target = candidate.readlink()
+ except OSError:
+ break
+ candidate = target if target.is_absolute() else candidate.parent / target
+ yield candidate
+
+ try:
+ # Also resolve symlinks in parent directories and normalize the target.
+ yield path.resolve(strict=False)
+ except (OSError, RuntimeError):
+ # Missing files and loops retain the checks above. The filesystem
+ # operation will report its own error when a path is not usable.
+ return
def _is_sensitive_file_py(path_str: str) -> bool:
diff --git a/tests/security/test_file_policy.py b/tests/security/test_file_policy.py
index cd6b7cd2..39b6973c 100644
--- a/tests/security/test_file_policy.py
+++ b/tests/security/test_file_policy.py
@@ -4,9 +4,21 @@ from __future__ import annotations
from pathlib import Path
+import pytest
+
from openjarvis.security.file_policy import filter_sensitive_paths, is_sensitive_file
[email protected](params=["default", "python"])
+def policy_backend(request, monkeypatch):
+ if request.param == "python":
+
+ def unavailable():
+ raise ImportError("exercise the Python fallback")
+
+ monkeypatch.setattr("openjarvis._rust_bridge.get_rust_module", unavailable)
+
+
class TestIsSensitiveFile:
def test_sensitive_env(self) -> None:
assert is_sensitive_file(".env") is True
@@ -69,8 +81,110 @@ class TestIsSensitiveFile:
assert is_sensitive_file(Path("server.pem")) is True
assert is_sensitive_file(Path("main.py")) is False
+ @pytest.mark.parametrize(
+ "sensitive_name", [".env", "credentials.json", "server.pem"]
+ )
+ def test_sensitive_symlink_alias(self, tmp_path: Path, sensitive_name: str) -> None:
+ sensitive = tmp_path / sensitive_name
+ sensitive.write_text("SENSITIVE-SENTINEL", encoding="utf-8")
+ alias = tmp_path / "notes.txt"
+ try:
+ alias.symlink_to(sensitive)
+ except OSError:
+ pytest.skip("filesystem does not permit creating symlinks")
+
+ assert is_sensitive_file(alias) is True
+
+ def test_sensitive_symlink_alias_to_missing_target(self, tmp_path: Path) -> None:
+ alias = tmp_path / "notes.txt"
+ try:
+ alias.symlink_to(tmp_path / ".env")
+ except OSError:
+ pytest.skip("filesystem does not permit creating symlinks")
+
+ assert is_sensitive_file(alias) is True
+
+ @pytest.mark.parametrize(
+ "sensitive_name", [".env", "credentials.json", "server.pem"]
+ )
+ @pytest.mark.parametrize("target_exists", [True, False])
+ def test_sensitive_symlink_name_to_ordinary_target(
+ self, tmp_path: Path, sensitive_name: str, target_exists: bool, policy_backend
+ ) -> None:
+ target = tmp_path / "notes.txt"
+ if target_exists:
+ target.write_text("SENSITIVE-SENTINEL", encoding="utf-8")
+ alias = tmp_path / sensitive_name
+ try:
+ alias.symlink_to(target.name)
+ except OSError:
+ pytest.skip("filesystem does not permit creating symlinks")
+
+ assert is_sensitive_file(alias) is True
+
+ @pytest.mark.parametrize("target_name", [".env", "notes.txt"])
+ @pytest.mark.parametrize("target_exists", [True, False])
+ def test_relative_symlink_chain(
+ self, tmp_path: Path, target_name: str, target_exists: bool, policy_backend
+ ) -> None:
+ target = tmp_path / target_name
+ if target_exists:
+ target.write_text("SENTINEL", encoding="utf-8")
+ intermediate = tmp_path / "intermediate.txt"
+ alias = tmp_path / "alias.txt"
+ try:
+ intermediate.symlink_to(target.name)
+ alias.symlink_to(intermediate.name)
+ except OSError:
+ pytest.skip("filesystem does not permit creating symlinks")
+
+ assert is_sensitive_file(alias) is (target_name == ".env")
+
+ def test_sensitive_symlink_loop(self, tmp_path: Path, policy_backend) -> None:
+ alias = tmp_path / ".env"
+ try:
+ alias.symlink_to(alias.name)
+ except OSError:
+ pytest.skip("filesystem does not permit creating symlinks")
+
+ assert is_sensitive_file(alias) is True
+
+ @pytest.mark.parametrize("target_exists", [True, False])
+ def test_sensitive_intermediate_symlink_to_ordinary_target(
+ self, tmp_path: Path, target_exists: bool, policy_backend
+ ) -> None:
+ target = tmp_path / "notes.txt"
+ if target_exists:
+ target.write_text("SENTINEL", encoding="utf-8")
+ intermediate = tmp_path / ".env"
+ alias = tmp_path / "alias.txt"
+ try:
+ intermediate.symlink_to(target.name)
+ alias.symlink_to(intermediate.name)
+ except OSError:
+ pytest.skip("filesystem does not permit creating symlinks")
+
+ assert is_sensitive_file(alias) is True
+
class TestFilterSensitivePaths:
+ def test_filter_sensitive_aliases(self, tmp_path: Path, policy_backend) -> None:
+ target = tmp_path / "notes.txt"
+ target.write_text("SENTINEL", encoding="utf-8")
+ protected_alias = tmp_path / ".env"
+ ordinary_alias = tmp_path / "alias.txt"
+ secret = tmp_path / "server.pem"
+ secret.write_text("SENTINEL", encoding="utf-8")
+ try:
+ protected_alias.symlink_to(target.name)
+ ordinary_alias.symlink_to(secret.name)
+ except OSError:
+ pytest.skip("filesystem does not permit creating symlinks")
+
+ assert filter_sensitive_paths([protected_alias, ordinary_alias, target]) == [
+ target
+ ]
+
def test_filter_sensitive_paths(self) -> None:
paths = [
"main.py",
diff --git a/tests/tools/test_apply_patch.py b/tests/tools/test_apply_patch.py
index 734c0b81..af137219 100644
--- a/tests/tools/test_apply_patch.py
+++ b/tests/tools/test_apply_patch.py
@@ -2,6 +2,8 @@
from __future__ import annotations
+import pytest
+
from openjarvis.tools.apply_patch import ApplyPatchTool
@@ -133,6 +135,31 @@ class TestApplyPatchTool:
assert result.success is False
assert "sensitive" in result.content.lower()
+ @pytest.mark.parametrize(
+ "alias_name,target_name", [("notes.txt", ".env"), (".env", "notes.txt")]
+ )
+ @pytest.mark.parametrize("explicit_path", [True, False])
+ def test_blocks_symlink_alias_to_sensitive_file(
+ self, tmp_path, alias_name, target_name, explicit_path
+ ):
+ sensitive = tmp_path / target_name
+ sensitive.write_text("SECRET=foo\n", encoding="utf-8")
+ alias = tmp_path / alias_name
+ try:
+ alias.symlink_to(sensitive)
+ except OSError:
+ pytest.skip("filesystem does not permit creating symlinks")
+ patch = f"--- {alias}\n+++ {alias}\n@@ -1 +1 @@\n-SECRET=foo\n+SECRET=bar\n"
+
+ result = ApplyPatchTool().execute(
+ patch=patch, path=str(alias) if explicit_path else None
+ )
+
+ assert result.success is False
+ assert "sensitive" in result.content.lower()
+ assert sensitive.read_text(encoding="utf-8") == "SECRET=foo\n"
+ assert not alias.with_suffix(alias.suffix + ".bak").exists()
+
def test_auto_detect_path_from_patch_header(self, tmp_path):
f = tmp_path / "auto.txt"
f.write_text("one\ntwo\nthree\n", encoding="utf-8")
diff --git a/tests/tools/test_file_read.py b/tests/tools/test_file_read.py
index d6dba7a5..8bfb07ac 100644
--- a/tests/tools/test_file_read.py
+++ b/tests/tools/test_file_read.py
@@ -2,6 +2,8 @@
from __future__ import annotations
+import pytest
+
from openjarvis.tools.file_read import FileReadTool
@@ -87,6 +89,26 @@ class TestFileReadTool:
assert result.success is False
assert "sensitive" in result.content.lower()
+ @pytest.mark.parametrize(
+ "alias_name,target_name", [("notes.txt", ".env"), (".env", "notes.txt")]
+ )
+ def test_blocks_symlink_alias_to_sensitive_file(
+ self, tmp_path, alias_name, target_name
+ ):
+ sensitive = tmp_path / target_name
+ sensitive.write_text("SECRET=foo", encoding="utf-8")
+ alias = tmp_path / alias_name
+ try:
+ alias.symlink_to(sensitive)
+ except OSError:
+ pytest.skip("filesystem does not permit creating symlinks")
+
+ result = FileReadTool().execute(path=str(alias))
+
+ assert result.success is False
+ assert "sensitive" in result.content.lower()
+ assert "SECRET=foo" not in result.content
+
def test_allows_normal_py_files(self, tmp_path):
f = tmp_path / "main.py"
f.write_text("print('hello')", encoding="utf-8")
diff --git a/tests/tools/test_file_write.py b/tests/tools/test_file_write.py
index 002de3e7..210da56f 100644
--- a/tests/tools/test_file_write.py
+++ b/tests/tools/test_file_write.py
@@ -2,6 +2,8 @@
from __future__ import annotations
+import pytest
+
from openjarvis.tools.file_write import FileWriteTool
@@ -83,6 +85,34 @@ class TestFileWriteTool:
assert result.success is False
assert "sensitive" in result.content.lower()
+ @pytest.mark.parametrize(
+ "alias_name,target_name", [("notes.txt", ".env"), (".env", "notes.txt")]
+ )
+ @pytest.mark.parametrize("target_exists", [True, False])
+ @pytest.mark.parametrize("mode", ["write", "append"])
+ def test_blocks_symlink_alias_to_sensitive_file(
+ self, tmp_path, alias_name, target_name, target_exists, mode
+ ):
+ sensitive = tmp_path / target_name
+ if target_exists:
+ sensitive.write_text("SECRET=foo", encoding="utf-8")
+ alias = tmp_path / alias_name
+ try:
+ alias.symlink_to(sensitive)
+ except OSError:
+ pytest.skip("filesystem does not permit creating symlinks")
+
+ result = FileWriteTool().execute(
+ path=str(alias), content="SECRET=bar", mode=mode
+ )
+
+ assert result.success is False
+ assert "sensitive" in result.content.lower()
+ if target_exists:
+ assert sensitive.read_text(encoding="utf-8") == "SECRET=foo"
+ else:
+ assert not sensitive.exists()
+
def test_allowed_dirs_blocks(self, tmp_path):
f = tmp_path / "test.txt"
tool = FileWriteTool(allowed_dirs=["/some/other/dir"])