Repository navigation
Expand file tree
/
Copy pathpaths.rs
More file actions
520 lines (466 loc) · 16.5 KB
/
Copy pathpaths.rs
File metadata and controls
520 lines (466 loc) · 16.5 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Path utilities for the Cortex CLI.
//!
//! Provides functions for path manipulation, validation, and security checks.
use std::path::{Path, PathBuf};
/// Get the cortex home directory.
///
/// Uses the following precedence:
/// 1. `CORTEX_HOME` environment variable if set
/// 2. `~/.cortex` as fallback
///
/// # Returns
/// The path to the cortex home directory.
pub fn get_cortex_home() -> PathBuf {
cortex_common::get_cortex_home().unwrap_or_else(|| {
dirs::home_dir()
.map(|h| h.join(".cortex"))
.unwrap_or_else(|| PathBuf::from(".cortex"))
})
}
/// Expand tilde (`~`) in a path to the user's home directory.
///
/// # Arguments
/// * `path` - The path string to expand
///
/// # Returns
/// The expanded path, or the original if home directory cannot be determined.
///
/// # Example
/// ```ignore
/// let expanded = expand_tilde("~/documents/file.txt");
/// // Returns: /home/user/documents/file.txt
/// ```
pub fn expand_tilde(path: &str) -> String {
if path.starts_with("~/")
&& let Some(home) = dirs::home_dir()
{
return home.join(&path[2..]).to_string_lossy().to_string();
}
path.to_string()
}
/// Validate a path for security concerns.
///
/// Checks for:
/// - Path traversal attempts (`..`)
/// - System-critical paths that should not be modified
/// - Device files on Unix systems
///
/// # Arguments
/// * `path` - The path to validate
/// * `base_dir` - Optional base directory for containment check
///
/// # Returns
/// `Ok(())` if the path is safe, or an error message describing the issue.
pub fn validate_path_safety(path: &Path, base_dir: Option<&Path>) -> Result<(), String> {
let path_str = path.to_string_lossy();
// Check for path traversal attempts
if path_str.contains("..") {
return Err("Path contains traversal sequence '..'".to_string());
}
// Check for absolute paths that escape the base directory
if let Some(base) = base_dir
&& path.is_absolute()
{
let canonical_path = path.canonicalize().ok();
let canonical_base = base.canonicalize().ok();
if let (Some(cp), Some(cb)) = (canonical_path, canonical_base)
&& !cp.starts_with(&cb)
{
return Err(format!(
"Path '{}' escapes base directory '{}'",
path.display(),
base.display()
));
}
}
// Block system-critical paths
let critical_paths = [
"/etc", "/usr", "/bin", "/sbin", "/lib", "/lib64", "/boot", "/root", "/var/run",
"/var/lib", "/proc", "/sys", "/dev",
];
for critical in critical_paths {
if path_str.starts_with(critical) && !path_str.starts_with("/etc/cortex") {
return Err(format!(
"Path '{}' is in a protected system directory",
path.display()
));
}
}
Ok(())
}
/// Sensitive system paths that should trigger a security warning when read.
pub const SENSITIVE_PATHS: &[&str] = &[
"/etc/passwd",
"/etc/shadow",
"/etc/group",
"/etc/gshadow",
"/etc/sudoers",
"/etc/ssh/",
"/etc/ssl/",
"/etc/pki/",
"/.ssh/",
"/.gnupg/",
"/.aws/",
"/.azure/",
"/.gcloud/",
"/.kube/",
"/.docker/config.json",
"/.npmrc",
"/.pypirc",
"/.netrc",
"/.gitconfig",
"/id_rsa",
"/id_dsa",
"/id_ecdsa",
"/id_ed25519",
"/.env",
"/credentials",
"/secrets",
"/private",
"/token",
];
/// Check if a path appears to be a sensitive system file.
///
/// # Arguments
/// * `path` - The path to check
///
/// # Returns
/// `true` if the path appears sensitive, `false` otherwise.
pub fn is_sensitive_path(path: &Path) -> bool {
let path_str = path.to_string_lossy().to_lowercase();
for sensitive in SENSITIVE_PATHS {
if path_str.contains(&sensitive.to_lowercase()) {
return true;
}
}
// Also check for common sensitive file extensions
if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
let ext_lower = ext.to_lowercase();
if matches!(
ext_lower.as_str(),
"pem" | "key" | "crt" | "cer" | "pfx" | "p12"
) {
return true;
}
}
false
}
/// Join path components safely, preventing path traversal.
///
/// # Arguments
/// * `base` - The base directory
/// * `path` - The path to join
///
/// # Returns
/// The joined path if safe, or an error if path traversal was detected.
pub fn safe_join(base: &Path, path: &str) -> Result<PathBuf, String> {
// First expand tilde
let expanded = expand_tilde(path);
let joined = base.join(&expanded);
// Validate the result
validate_path_safety(&joined, Some(base))?;
Ok(joined)
}
#[cfg(test)]
mod tests {
use super::*;
use std::env;
#[test]
fn test_expand_tilde() {
// Just test that it doesn't panic on empty or regular paths
assert_eq!(expand_tilde(""), "");
assert_eq!(expand_tilde("/absolute/path"), "/absolute/path");
assert_eq!(expand_tilde("relative/path"), "relative/path");
// Tilde expansion depends on HOME env var, so we just check it doesn't panic
let _ = expand_tilde("~/some/path");
}
#[test]
fn test_path_traversal_detection() {
let path = Path::new("../../../etc/passwd");
let result = validate_path_safety(path, None);
assert!(result.is_err());
assert!(result.unwrap_err().contains("traversal"));
}
#[test]
fn test_sensitive_path_detection() {
assert!(is_sensitive_path(Path::new("/home/user/.ssh/id_rsa")));
assert!(is_sensitive_path(Path::new("/home/user/.aws/credentials")));
assert!(is_sensitive_path(Path::new("secret.pem")));
assert!(!is_sensitive_path(Path::new(
"/home/user/documents/file.txt"
)));
}
// ==================== Additional tests ====================
#[test]
fn test_get_cortex_home_returns_path() {
// Ensure get_cortex_home returns a valid path that either exists or can be created
let home = get_cortex_home();
// The path should contain "cortex" - either from env var or default
let path_str = home.to_string_lossy();
// Just verify it returns a path containing "cortex"
assert!(
path_str.contains("cortex"),
"Expected cortex home to contain 'cortex', got: {}",
path_str
);
}
#[test]
fn test_get_cortex_home_with_env_variable() {
// Save original value
let original = env::var("CORTEX_HOME").ok();
// Set custom CORTEX_HOME
// SAFETY: This test runs in a single-threaded context and we restore the value afterwards
unsafe {
env::set_var("CORTEX_HOME", "/tmp/custom_cortex_home");
}
// Note: cortex_common::get_cortex_home() might cache the value,
// but we test that our function doesn't panic
let home = get_cortex_home();
// The function should return a valid PathBuf
assert!(!home.as_os_str().is_empty());
// Restore original
// SAFETY: This test runs in a single-threaded context
unsafe {
match original {
Some(val) => env::set_var("CORTEX_HOME", val),
None => env::remove_var("CORTEX_HOME"),
}
}
}
#[test]
fn test_expand_tilde_with_tilde_only() {
// Test tilde alone - should remain unchanged (not "~/")
assert_eq!(expand_tilde("~"), "~");
}
#[test]
fn test_expand_tilde_with_tilde_in_middle() {
// Tilde in the middle should NOT be expanded
assert_eq!(expand_tilde("/path/~/file"), "/path/~/file");
}
#[test]
fn test_expand_tilde_with_tilde_prefix_expands() {
// Test that ~/ prefix triggers expansion (when home dir is available)
let result = expand_tilde("~/test/file.txt");
// If home directory is available, the path should be expanded
if let Some(home) = dirs::home_dir() {
let expected = home.join("test/file.txt").to_string_lossy().to_string();
assert_eq!(result, expected);
} else {
// If no home dir, original is returned
assert_eq!(result, "~/test/file.txt");
}
}
#[test]
fn test_validate_path_safety_allows_safe_paths() {
// Safe relative path
let path = Path::new("safe/path/to/file.txt");
assert!(validate_path_safety(path, None).is_ok());
// Safe absolute path outside critical directories
let path = Path::new("/home/user/documents/file.txt");
assert!(validate_path_safety(path, None).is_ok());
}
#[test]
fn test_validate_path_safety_blocks_critical_paths() {
// /etc is critical
let path = Path::new("/etc/hosts");
let result = validate_path_safety(path, None);
assert!(result.is_err());
assert!(result.unwrap_err().contains("protected system directory"));
// /usr is critical
let path = Path::new("/usr/bin/something");
let result = validate_path_safety(path, None);
assert!(result.is_err());
// /dev is critical
let path = Path::new("/dev/null");
let result = validate_path_safety(path, None);
assert!(result.is_err());
}
#[test]
fn test_validate_path_safety_allows_etc_cortex() {
// /etc/cortex is special-cased to be allowed
let path = Path::new("/etc/cortex/config.toml");
assert!(validate_path_safety(path, None).is_ok());
}
#[test]
fn test_validate_path_safety_detects_various_traversal_patterns() {
// Different traversal patterns
let patterns = ["foo/../bar", "...", "foo/bar/../baz", "./foo/../../../etc"];
for pattern in patterns {
let path = Path::new(pattern);
let result = validate_path_safety(path, None);
// Only patterns containing ".." should fail
if pattern.contains("..") {
assert!(
result.is_err(),
"Expected traversal detection for: {}",
pattern
);
}
}
}
#[test]
fn test_is_sensitive_path_detects_ssh_keys() {
assert!(is_sensitive_path(Path::new("/home/user/.ssh/id_rsa")));
assert!(is_sensitive_path(Path::new("/home/user/.ssh/id_dsa")));
assert!(is_sensitive_path(Path::new("/home/user/.ssh/id_ecdsa")));
assert!(is_sensitive_path(Path::new("/home/user/.ssh/id_ed25519")));
}
#[test]
fn test_is_sensitive_path_detects_cloud_credentials() {
assert!(is_sensitive_path(Path::new("/home/user/.aws/credentials")));
assert!(is_sensitive_path(Path::new("/home/user/.azure/config")));
assert!(is_sensitive_path(Path::new(
"/home/user/.gcloud/credentials"
)));
assert!(is_sensitive_path(Path::new("/home/user/.kube/config")));
}
#[test]
fn test_is_sensitive_path_detects_env_files() {
assert!(is_sensitive_path(Path::new("/project/.env")));
assert!(is_sensitive_path(Path::new("/project/.env.local")));
}
#[test]
fn test_is_sensitive_path_detects_certificate_extensions() {
assert!(is_sensitive_path(Path::new("server.pem")));
assert!(is_sensitive_path(Path::new("private.key")));
assert!(is_sensitive_path(Path::new("certificate.crt")));
assert!(is_sensitive_path(Path::new("ca.cer")));
assert!(is_sensitive_path(Path::new("keystore.pfx")));
assert!(is_sensitive_path(Path::new("keystore.p12")));
}
#[test]
fn test_is_sensitive_path_case_insensitive() {
// Extensions should be case-insensitive
assert!(is_sensitive_path(Path::new("cert.PEM")));
assert!(is_sensitive_path(Path::new("cert.Key")));
assert!(is_sensitive_path(Path::new("cert.CRT")));
}
#[test]
fn test_is_sensitive_path_returns_false_for_safe_files() {
assert!(!is_sensitive_path(Path::new(
"/home/user/documents/report.pdf"
)));
assert!(!is_sensitive_path(Path::new("/home/user/code/main.rs")));
assert!(!is_sensitive_path(Path::new("/tmp/data.json")));
assert!(!is_sensitive_path(Path::new("README.md")));
}
#[test]
fn test_safe_join_with_safe_path() {
let base = Path::new("/home/user/project");
let result = safe_join(base, "src/main.rs");
// safe_join should succeed for safe paths within base
// Note: actual success depends on whether the paths exist for canonicalize
// If paths don't exist, we just verify it doesn't fail on traversal check
match result {
Ok(joined) => {
assert!(joined.to_string_lossy().contains("src/main.rs"));
}
Err(e) => {
// If it fails, it should not be due to traversal
assert!(
!e.contains("traversal"),
"Unexpected traversal error for safe path"
);
}
}
}
#[test]
fn test_safe_join_rejects_traversal() {
let base = Path::new("/home/user/project");
let result = safe_join(base, "../../../etc/passwd");
assert!(result.is_err());
assert!(result.unwrap_err().contains("traversal"));
}
#[test]
fn test_safe_join_expands_tilde() {
let base = Path::new("/home/user/project");
// If tilde is used, it gets expanded before joining
let result = safe_join(base, "~/other/file");
// The result should contain the expanded home directory
// But this might fail validation if it escapes base_dir
// The important thing is tilde is expanded
match result {
Ok(path) => {
// If it succeeds, tilde should be expanded
assert!(
!path.to_string_lossy().contains("~"),
"Tilde should be expanded"
);
}
Err(_) => {
// Might fail because expanded path escapes base dir
// That's valid behavior
}
}
}
#[test]
fn test_safe_join_with_empty_path() {
let base = Path::new("/home/user/project");
let result = safe_join(base, "");
// Empty path joined with base should just be the base
match result {
Ok(path) => {
assert_eq!(path, base.join(""));
}
Err(e) => {
// Should not fail with traversal error
assert!(!e.contains("traversal"));
}
}
}
#[test]
fn test_sensitive_paths_constant_includes_expected_entries() {
// Verify the SENSITIVE_PATHS constant contains expected sensitive locations
assert!(SENSITIVE_PATHS.contains(&"/etc/passwd"));
assert!(SENSITIVE_PATHS.contains(&"/etc/shadow"));
assert!(SENSITIVE_PATHS.contains(&"/.ssh/"));
assert!(SENSITIVE_PATHS.contains(&"/.aws/"));
assert!(SENSITIVE_PATHS.contains(&"/.env"));
assert!(SENSITIVE_PATHS.contains(&"/credentials"));
}
#[test]
fn test_is_sensitive_path_detects_secrets_and_private() {
assert!(is_sensitive_path(Path::new("/app/secrets/api_key")));
assert!(is_sensitive_path(Path::new("/data/private/config")));
assert!(is_sensitive_path(Path::new("/home/user/token.txt")));
}
#[test]
fn test_is_sensitive_path_detects_docker_config() {
assert!(is_sensitive_path(Path::new(
"/home/user/.docker/config.json"
)));
}
#[test]
fn test_is_sensitive_path_detects_npm_pypi_config() {
assert!(is_sensitive_path(Path::new("/home/user/.npmrc")));
assert!(is_sensitive_path(Path::new("/home/user/.pypirc")));
assert!(is_sensitive_path(Path::new("/home/user/.netrc")));
}
#[test]
fn test_validate_path_safety_blocks_all_critical_dirs() {
let critical_paths = [
"/etc/hosts",
"/usr/local/bin/tool",
"/bin/bash",
"/sbin/init",
"/lib/libfoo.so",
"/lib64/libbar.so",
"/boot/vmlinuz",
"/root/.bashrc",
"/var/run/pid",
"/var/lib/data",
"/proc/1/status",
"/sys/class/net",
"/dev/sda",
];
for path_str in critical_paths {
let path = Path::new(path_str);
let result = validate_path_safety(path, None);
assert!(
result.is_err(),
"Expected '{}' to be blocked as critical path",
path_str
);
}
}
}