moesniper 0.7.12

Escape-proof precision file editor for LLM agents. Hex-encoded content, line-range splicing, atomic writes.
Documentation
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
//! Path security validation and sanitization.
//!
//! This module provides defense-in-depth path validation to prevent:
//! - Path traversal attacks (../../../etc/passwd)
//! - Symlink attacks
//! - Directory escape attempts

use std::fs;
use std::path::{Component, Path, PathBuf};

/// Security policy configuration for path validation.
///
/// Controls which path patterns are rejected and whether base directory
/// containment is enforced. Use `SecurityPolicy::default()` for the
/// standard policy that rejects parent references without base restriction.
#[derive(Debug, Clone)]
pub struct SecurityPolicy {
    /// Base directory that all paths must be within.
    /// When set, `validate_path` rejects any path that resolves outside
    /// this directory. When None, base directory containment is NOT enforced.
    pub base_dir: Option<PathBuf>,
    /// Whether to reject paths containing parent references (..).
    /// When true, any path component equal to `..` triggers
    /// `PathSecurityError::ParentReferenceNotAllowed`.
    pub reject_parent_refs: bool,
}

impl Default for SecurityPolicy {
    fn default() -> Self {
        Self {
            base_dir: None,           // No base restriction by default (backward compatible)
            reject_parent_refs: true, // Always reject parent refs
        }
    }
}

/// Path validation error types.
///
/// Produced by `validate_path` when a path violates the security policy.
/// Each variant identifies the specific violation for targeted error handling.
#[derive(Debug, Clone, PartialEq)]
pub enum PathSecurityError {
    /// Path contains a parent directory reference (..) and policy rejects it.
    /// The `component` field contains the offending path component string.
    ParentReferenceNotAllowed { component: String },
    /// Path resolves outside the allowed base directory after canonicalization.
    /// Contains both the resolved `path` and the configured `base` for diagnostics.
    EscapesBaseDirectory { path: PathBuf, base: PathBuf },
    /// Filesystem I/O error occurred during path resolution or canonicalization.
    /// The string contains the OS error description.
    IoError(String),
}

impl std::fmt::Display for PathSecurityError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PathSecurityError::ParentReferenceNotAllowed { component } => {
                write!(f, "Parent reference not allowed: {}", component)
            }
            PathSecurityError::EscapesBaseDirectory { path, base } => {
                write!(
                    f,
                    "Path escapes base directory: {:?} (base: {:?})",
                    path, base
                )
            }
            PathSecurityError::IoError(e) => {
                write!(f, "IO error: {}", e)
            }
        }
    }
}

impl std::error::Error for PathSecurityError {}

/// Validates and sanitizes a path according to the security policy.
pub fn validate_path<P: AsRef<Path>>(
    path: P,
    policy: &SecurityPolicy,
) -> Result<PathBuf, PathSecurityError> {
    let path = path.as_ref();

    // Explicit null-byte guard: C truncation can hide embedded NULs.
    if path.as_os_str().as_encoded_bytes().contains(&0) {
        return Err(PathSecurityError::IoError(
            "path contains a null byte".to_string(),
        ));
    }

    // Layer 1: Check for parent references
    for component in path.components() {
        if component == Component::ParentDir && policy.reject_parent_refs {
            return Err(PathSecurityError::ParentReferenceNotAllowed {
                component: "..".to_string(),
            });
        }
    }

    // Layer 2: Base directory containment (only if configured)
    let base_dir = policy.base_dir.as_ref();

    let canonical = if path.exists() {
        fs::canonicalize(path).map_err(|e| PathSecurityError::IoError(e.to_string()))?
    } else if let Some(base) = base_dir {
        // For non-existent files with base_dir, resolve against base
        let resolved = base.join(path);
        clean_path(&resolved)
    } else {
        // For non-existent files without base_dir, use clean_path
        clean_path(path)
    };

    if let Some(base) = base_dir {
        let canonical_base = base.canonicalize().map_err(|e| {
            PathSecurityError::IoError(format!("Failed to canonicalize base directory: {}", e))
        })?;

        if !canonical.starts_with(&canonical_base) {
            return Err(PathSecurityError::EscapesBaseDirectory {
                path: canonical,
                base: canonical_base,
            });
        }
    }

    Ok(canonical)
}

/// Returns true when `path` is an existing regular file.
///
/// Useful as a quick pre-read guard to reject FIFOs, pipes, and other
/// special files that would otherwise block or behave unexpectedly.
pub fn is_regular_file<P: AsRef<Path>>(path: P) -> bool {
    fs::metadata(path.as_ref())
        .map(|m| m.is_file())
        .unwrap_or(false)
}

/// Validate that a file target is safe and suitable for editing.
///
/// Performs three pre-edit safety checks used by all entry points
/// (CLI, Python bindings, and any future consumers):
///
/// 1. **Regular file check**: Rejects FIFOs, pipes, devices, and other
///    non-regular files that would block or behave unexpectedly.
/// 2. **Read-only guard**: Rejects non-empty read-only files to prevent
///    overwrite failures during atomic rename.
/// 3. **Null-byte scan**: Reads the first 4 KB of the file and returns
///    a warning string if null bytes are found (binary file heuristic).
///    This check is non-blocking — only the first two produce errors.
///
/// # Arguments
/// * `filepath` — Target file path (`AsRef<Path>`). Must exist and be a
///   regular file for the blocking checks to pass.
///
/// # Returns
/// * `Ok(Some(warning))` — file is safe; an optional null-byte warning.
/// * `Ok(None)` — file is safe; no warnings.
/// * `Err(String)` — blocking safety violation (not a regular file, or
///   read-only non-empty).
pub fn validate_edit_target<P: AsRef<Path>>(filepath: P) -> Result<Option<String>, String> {
    let path = filepath.as_ref();

    // Guard 1: block writes into special files (FIFOs, devices, etc.).
    if !is_regular_file(path) {
        return Err(
            "target path is not a regular file (FIFOs, pipes, and devices are not supported)"
                .into(),
        );
    }
    // Guard 2: refuse to overwrite read-only files via atomic rename.
    if let Ok(meta) = fs::metadata(path) {
        if meta.permissions().readonly() && meta.len() > 0 {
            return Err(format!(
                "file is read-only: {}. Refusing to overwrite via atomic rename",
                path.display()
            ));
        }
    }
    // Guard 3: Scan first 4KB for null bytes (binary file heuristic).
    // Non-blocking — returns a warning string, not an error.
    let null_warning = if let Ok(mut f) = fs::File::open(path) {
        let mut buf = [0u8; 4096];
        if let Ok(n) = std::io::Read::read(&mut f, &mut buf) {
            if buf[..n].contains(&0) {
                Some(format!(
                    "{:?} contains null bytes — may be binary or corrupted",
                    path
                ))
            } else {
                None
            }
        } else {
            None
        }
    } else {
        None
    };
    Ok(null_warning)
}

/// Clean a path by removing redundant components (. and ..).
fn clean_path(path: &Path) -> PathBuf {
    let mut components: Vec<Component> = Vec::new();

    for component in path.components() {
        match component {
            Component::CurDir => {
                continue;
            }
            Component::ParentDir => {
                if let Some(Component::Normal(_)) = components.last() {
                    components.pop();
                } else if components.is_empty() {
                    // Leading .. - keep it for absolute paths
                    components.push(component);
                }
            }
            other => {
                components.push(other);
            }
        }
    }

    components.iter().collect()
}

/// Secure version of normalize_path.
pub fn normalize_path_secure(path: &str, base_dir: Option<&Path>) -> Result<PathBuf, String> {
    let policy = SecurityPolicy {
        base_dir: base_dir.map(|p| p.to_path_buf()),
        ..SecurityPolicy::default()
    };

    validate_path(path, &policy).map_err(|e| e.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_valid_path() {
        let dir = TempDir::new().unwrap();
        let file = dir.path().join("test.txt");
        fs::write(&file, "test").unwrap();

        let policy = SecurityPolicy::default();
        let result = validate_path(&file, &policy);
        assert!(result.is_ok());
    }

    #[test]
    fn test_path_traversal_blocked() {
        let policy = SecurityPolicy::default();
        let malicious = "../../../etc/passwd";

        let result = validate_path(malicious, &policy);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            PathSecurityError::ParentReferenceNotAllowed { .. }
        ));
    }

    #[test]
    fn test_escapes_base_directory() {
        let dir = TempDir::new().unwrap();
        let outside = dir.path().parent().unwrap().join("outside.txt");
        fs::write(&outside, "content").unwrap();

        let policy = SecurityPolicy {
            base_dir: Some(dir.path().to_path_buf()),
            ..SecurityPolicy::default()
        };

        let result = validate_path(&outside, &policy);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            PathSecurityError::EscapesBaseDirectory { .. }
        ));
    }

    #[test]
    fn test_clean_path() {
        let messy = PathBuf::from("/tmp/subdir/../file.txt");
        let cleaned = clean_path(&messy);
        assert_eq!(cleaned, PathBuf::from("/tmp/file.txt"));
    }

    #[test]
    fn test_normalize_path_secure_rejects_traversal() {
        let dir = TempDir::new().unwrap();

        let result = normalize_path_secure("../../../etc/passwd", Some(dir.path()));
        assert!(result.is_err());
    }

    #[test]
    fn test_new_file_path_validation() {
        let dir = TempDir::new().unwrap();
        let new_file = dir.path().join("subdir").join("new.txt");
        fs::create_dir_all(new_file.parent().unwrap()).unwrap();

        let policy = SecurityPolicy::default();
        // This should succeed even though file doesn't exist
        let result = validate_path(&new_file, &policy);
        assert!(result.is_ok());
    }

    #[test]
    fn test_base_dir_nonexistent_file_inside() {
        let dir = TempDir::new().unwrap();
        let base = dir.path().to_path_buf();
        let target = base.join("nonexistent.txt");

        let policy = SecurityPolicy {
            base_dir: Some(base),
            reject_parent_refs: true,
        };

        let result = validate_path(&target, &policy);
        assert!(result.is_ok());
    }

    #[test]
    fn test_base_dir_nonexistent_file_escapes_via_parent_ref() {
        let dir = TempDir::new().unwrap();
        let base = dir.path().to_path_buf();
        // Create a path that looks like it's in base but escapes it via parent refs
        // We set reject_parent_refs = false to test the base_dir escape logic specifically
        let target = base.join("..").join("outside.txt");

        let policy = SecurityPolicy {
            base_dir: Some(base),
            reject_parent_refs: false,
        };

        let result = validate_path(&target, &policy);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            PathSecurityError::EscapesBaseDirectory { .. }
        ));
    }

    #[test]
    fn test_base_dir_nonexistent_absolute_path_escapes() {
        let dir = TempDir::new().unwrap();
        let base = dir.path().to_path_buf();

        #[cfg(unix)]
        let target = PathBuf::from("/tmp/some_random_nonexistent_file.txt");

        #[cfg(windows)]
        let target = PathBuf::from("C:\\temp\\some_random_nonexistent_file.txt");

        let policy = SecurityPolicy {
            base_dir: Some(base),
            reject_parent_refs: true,
        };

        let result = validate_path(&target, &policy);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            PathSecurityError::EscapesBaseDirectory { .. }
        ));
    }

    #[test]
    fn test_table_driven_traversal() {
        let policy = SecurityPolicy::default();
        let cases = vec![
            "../etc/passwd",
            "foo/bar/../../../etc/passwd",
            "foo/../bar",
            "../",
            "..",
            "a/b/c/..",
        ];

        for case in cases {
            let result = validate_path(case, &policy);
            assert!(result.is_err(), "Expected {} to fail", case);
            assert!(
                matches!(
                    result.unwrap_err(),
                    PathSecurityError::ParentReferenceNotAllowed { .. }
                ),
                "Expected ParentReferenceNotAllowed for {}",
                case
            );
        }
    }

    #[test]
    fn test_null_byte_rejected() {
        let policy = SecurityPolicy::default();
        // Use OsStr to embed an actual null byte that Rust's C layer would truncate.
        use std::ffi::OsStr;
        use std::os::unix::ffi::OsStrExt;
        let with_nul = OsStr::from_bytes(b"file.txt\x00.txt");
        let result = validate_path(with_nul, &policy);
        assert!(result.is_err(), "Path with null byte must be rejected");
        let err = result.unwrap_err();
        assert!(
            err.to_string().contains("null byte"),
            "Error should mention null byte: {}",
            err
        );
    }

    #[test]
    fn test_is_regular_file() {
        let dir = TempDir::new().unwrap();
        let file = dir.path().join("regular.txt");
        fs::write(&file, "content").unwrap();
        assert!(is_regular_file(&file));
        assert!(!is_regular_file(dir.path().join("nonexistent.txt")));
        assert!(!is_regular_file(dir.path()));
    }
}