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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! Centralized path safety helpers.
//!
//! `sanitize_name` strips characters that are unsafe as filesystem names,
//! collapses dot sequences, and caps length — use for any caller-supplied
//! skill/directory name before joining it to a base path.
//!
//! `is_path_safe` canonicalizes both inputs and verifies the target stays
//! inside the base directory. Call before any write/delete/copy that
//! consumes a path derived from untrusted input.
use std::path::{Component, Path, PathBuf};
use super::error::AppError;
/// Directory links and Windows reparse points are unsafe deletion boundaries.
pub(crate) fn is_link_or_reparse_point(metadata: &std::fs::Metadata) -> bool {
#[cfg(windows)]
{
use std::os::windows::fs::MetadataExt;
if metadata.file_attributes() & 0x400 != 0 {
return true;
}
}
metadata.file_type().is_symlink()
}
pub(crate) fn ensure_safe_skill_relative_path(skill_relative_path: &str) -> Result<(), AppError> {
if skill_relative_path.trim().is_empty() {
return Err(AppError::invalid_input("Invalid skill directory path"));
}
let mut saw_component = false;
for component in Path::new(skill_relative_path).components() {
if !matches!(component, Component::Normal(_)) {
return Err(AppError::invalid_input("Invalid skill directory path"));
}
saw_component = true;
}
if !saw_component {
return Err(AppError::invalid_input("Invalid skill directory path"));
}
Ok(())
}
pub(crate) fn ensure_dir_within_root(path: &Path, root: &Path) -> Result<(), AppError> {
// First check that the lexical path (before symlink resolution) is under root.
// This ensures the link itself lives where expected.
let abs_path = if path.is_absolute() {
path.to_path_buf()
} else {
std::env::current_dir()?.join(path)
};
let abs_root = if root.is_absolute() {
root.to_path_buf()
} else {
std::env::current_dir()?.join(root)
};
if !abs_path.starts_with(&abs_root) {
return Err(AppError::invalid_input("Invalid skill directory path"));
}
Ok(())
}
/// Refuse equal or nested publication paths. Callers resolve and validate
/// each path first, so comparisons cannot hide link aliases or `..` segments.
pub(crate) fn ensure_disjoint_paths(first: &Path, second: &Path) -> Result<(), AppError> {
if first.starts_with(second) || second.starts_with(first) {
return Err(AppError::invalid_input(
"Project source and deployment paths overlap",
));
}
Ok(())
}
/// Maximum filesystem name length we'll allow. Matches the lowest common
/// denominator (most modern filesystems allow 255 bytes).
const MAX_NAME_LEN: usize = 200;
/// Fallback used when sanitization strips everything out.
const FALLBACK_NAME: &str = "unnamed";
/// Sanitize a filesystem name.
///
/// - Strips path separators and Windows-forbidden characters
/// - Removes null bytes and other control characters
/// - Collapses `..` sequences (no traversal)
/// - Trims leading/trailing dots and whitespace
/// - Truncates to MAX_NAME_LEN bytes
/// - Returns FALLBACK_NAME when input collapses to empty
pub fn sanitize_name(input: &str) -> String {
let mut out = String::with_capacity(input.len());
for ch in input.chars() {
let drop = matches!(
ch,
'/' | '\\' | '\0' | ':' | '*' | '?' | '"' | '<' | '>' | '|'
) || ch.is_control();
if !drop {
out.push(ch);
}
}
// Collapse any run of dots into a single dot so ".." cannot survive,
// even if other characters were stripped between dots.
let mut collapsed = String::with_capacity(out.len());
let mut prev_dot = false;
for ch in out.chars() {
if ch == '.' {
if !prev_dot {
collapsed.push('.');
}
prev_dot = true;
} else {
collapsed.push(ch);
prev_dot = false;
}
}
let trimmed = collapsed.trim_matches(|c: char| c == '.' || c.is_whitespace());
let truncated = if trimmed.len() > MAX_NAME_LEN {
// Cut on a char boundary at or below the byte limit.
let mut end = MAX_NAME_LEN;
while !trimmed.is_char_boundary(end) && end > 0 {
end -= 1;
}
&trimmed[..end]
} else {
trimmed
};
if truncated.is_empty() {
FALLBACK_NAME.to_string()
} else {
truncated.to_string()
}
}
/// Verify that `target` resolves to a location inside `base`.
///
/// Both paths are canonicalized when possible so symlinks and `..` segments
/// are resolved before comparison. When canonicalize fails (e.g. target does
/// not yet exist), falls back to a normalized path comparison.
///
/// Returns `false` when the relationship cannot be established safely —
/// callers should treat that as a refusal.
pub fn is_path_safe(base: &Path, target: &Path) -> bool {
let base_resolved = base.canonicalize().unwrap_or_else(|_| normalize(base));
let target_resolved = target
.canonicalize()
.unwrap_or_else(|_| resolve_non_existing(target));
target_resolved.starts_with(&base_resolved)
}
/// Resolve a path that may not exist yet by walking from the nearest
/// existing ancestor.
fn resolve_non_existing(path: &Path) -> PathBuf {
for anc in path.ancestors() {
if let Ok(real) = anc.canonicalize() {
let mut out = real;
if let Ok(rest) = path.strip_prefix(anc) {
out.push(rest);
}
return normalize(&out);
}
}
normalize(path)
}
/// Resolve `.` and `..` segments without touching the filesystem.
pub(crate) fn normalize(path: &Path) -> PathBuf {
let mut out = PathBuf::new();
for comp in path.components() {
match comp {
Component::ParentDir => {
out.pop();
}
Component::CurDir => {}
other => out.push(other.as_os_str()),
}
}
out
}
#[cfg(test)]
mod tests;