1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149//! Workspace filesystem sandboxing.
//!
//! Confines agent file operations to their workspace directory.
//! Prevents path traversal, symlink escapes, and access outside the sandbox.
use std::path::{Path, PathBuf};
/// Resolve a user-supplied path within a workspace sandbox.
///
/// - Rejects `..` components outright.
/// - Relative paths are joined with `workspace_root`.
/// - Absolute paths are checked against the workspace root after canonicalization.
/// - For new files: canonicalizes the parent directory and appends the filename.
/// - The final canonical path must start with the canonical workspace root.
pub fn resolve_sandbox_path(user_path: &str, workspace_root: &Path) -> Result<PathBuf, String> {
let path = Path::new(user_path);
// Reject any `..` components
for component in path.components() {
if matches!(component, std::path::Component::ParentDir) {
return Err("Path traversal denied: '..' components are forbidden".to_string());
}
}
// Build the candidate path
let candidate = if path.is_absolute() {
path.to_path_buf()
} else {
workspace_root.join(path)
};
// Canonicalize the workspace root
let canon_root = workspace_root
.canonicalize()
.map_err(|e| format!("Failed to resolve workspace root: {e}"))?;
// Canonicalize the candidate (or its parent for new files)
let canon_candidate = if candidate.exists() {
candidate
.canonicalize()
.map_err(|e| format!("Failed to resolve path: {e}"))?
} else {
// For new files: canonicalize the parent and append the filename
let parent = candidate
.parent()
.ok_or_else(|| "Invalid path: no parent directory".to_string())?;
let filename = candidate
.file_name()
.ok_or_else(|| "Invalid path: no filename".to_string())?;
let canon_parent = parent
.canonicalize()
.map_err(|e| format!("Failed to resolve parent directory: {e}"))?;
canon_parent.join(filename)
};
// Verify the canonical path is inside the workspace
if !canon_candidate.starts_with(&canon_root) {
return Err(format!(
"Access denied: path '{}' resolves outside workspace. \
If you have an MCP filesystem server configured, use the \
mcp_filesystem_* tools (e.g. mcp_filesystem_read_file, \
mcp_filesystem_list_directory) to access files outside \
the workspace.",
user_path
));
}
Ok(canon_candidate)
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_relative_path_inside_workspace() {
let dir = TempDir::new().unwrap();
let data_dir = dir.path().join("data");
std::fs::create_dir_all(&data_dir).unwrap();
std::fs::write(data_dir.join("test.txt"), "hello").unwrap();
let result = resolve_sandbox_path("data/test.txt", dir.path());
assert!(result.is_ok());
let resolved = result.unwrap();
assert!(resolved.starts_with(dir.path().canonicalize().unwrap()));
}
#[test]
fn test_absolute_path_inside_workspace() {
let dir = TempDir::new().unwrap();
std::fs::write(dir.path().join("file.txt"), "ok").unwrap();
let abs_path = dir.path().join("file.txt");
let result = resolve_sandbox_path(abs_path.to_str().unwrap(), dir.path());
assert!(result.is_ok());
}
#[test]
fn test_absolute_path_outside_workspace_blocked() {
let dir = TempDir::new().unwrap();
let outside = std::env::temp_dir().join("outside_test.txt");
std::fs::write(&outside, "nope").unwrap();
let result = resolve_sandbox_path(outside.to_str().unwrap(), dir.path());
assert!(result.is_err());
assert!(result.unwrap_err().contains("Access denied"));
let _ = std::fs::remove_file(&outside);
}
#[test]
fn test_dotdot_component_blocked() {
let dir = TempDir::new().unwrap();
let result = resolve_sandbox_path("../../../etc/passwd", dir.path());
assert!(result.is_err());
assert!(result.unwrap_err().contains("Path traversal denied"));
}
#[test]
fn test_nonexistent_file_with_valid_parent() {
let dir = TempDir::new().unwrap();
let data_dir = dir.path().join("data");
std::fs::create_dir_all(&data_dir).unwrap();
let result = resolve_sandbox_path("data/new_file.txt", dir.path());
assert!(result.is_ok());
let resolved = result.unwrap();
assert!(resolved.starts_with(dir.path().canonicalize().unwrap()));
assert!(resolved.ends_with("new_file.txt"));
}
#[cfg(unix)]
#[test]
fn test_symlink_escape_blocked() {
let dir = TempDir::new().unwrap();
let outside = TempDir::new().unwrap();
std::fs::write(outside.path().join("secret.txt"), "secret").unwrap();
// Create a symlink inside the workspace pointing outside
let link_path = dir.path().join("escape");
std::os::unix::fs::symlink(outside.path(), &link_path).unwrap();
let result = resolve_sandbox_path("escape/secret.txt", dir.path());
assert!(result.is_err());
assert!(result.unwrap_err().contains("Access denied"));
}
}