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
//! Shared low-level file-edit I/O primitives (epic #1008).
//!
//! Extracted from `ctx_edit` so the anchored editor (`ctx_patch`) reuses the
//! *exact same* TOCTOU-safe read→verify→atomic-write path instead of growing a
//! second, drifting copy of this security-critical code:
//!
//! * symlink rejection (`reject_symlink` + `O_NOFOLLOW`),
//! * read-size cap + UTF-8 validation,
//! * whole-file preimage fingerprint (size + mtime + BLAKE3) for the TOCTOU
//! guard ([`ensure_preimage_still_matches`]),
//! * permission-preserving crash-atomic `rename`, with the read-only-directory
//! in-place fallback (#459),
//! * the read-only-roots write choke point (#475).
//!
//! One implementation = one audited boundary. `ctx_edit` (`str_replace`) and
//! `ctx_patch` (anchored) both apply through these functions, so a fix here
//! protects both tools at once.
//!
//! **Known limitation (#960):** the TOCTOU guard is a point-in-time check,
//! not a held lock — see [`ensure_preimage_still_matches`] for the residual
//! window between that check and the atomic write it gates.
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
/// Size + mtime + content hash of a file, used as a TOCTOU fingerprint.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct FileFingerprint {
pub(crate) size: u64,
pub(crate) mtime_ms: u64,
pub(crate) md5: String,
}
/// A file read for editing: its fingerprint, permissions, raw bytes, decoded
/// text and whether it uses CRLF line endings.
#[derive(Clone, Debug)]
pub(crate) struct FilePreimage {
pub(crate) fp: FileFingerprint,
pub(crate) permissions: std::fs::Permissions,
pub(crate) bytes: Vec<u8>,
pub(crate) text: String,
pub(crate) uses_crlf: bool,
}
pub(crate) fn system_time_to_millis(t: SystemTime) -> u64 {
t.duration_since(UNIX_EPOCH)
.map_or(0, |d| d.as_millis() as u64)
}
/// Rejects symlinks at `path` (TOCTOU protection, same boundary as
/// `core::io_boundary::read_file_nofollow`): a symlink planted inside the jail
/// after the jail check could otherwise read or overwrite files outside it.
pub(crate) fn reject_symlink(path: &Path) -> Result<(), String> {
if let Ok(meta) = std::fs::symlink_metadata(path) {
// Windows: also covers NTFS junctions/reparse points (GL#442).
if crate::core::pathutil::is_symlink_or_reparse(&meta) {
return Err(format!(
"ERROR: {} is a symlink — refusing to edit through it (TOCTOU protection). \
Edit the symlink target directly via its real path.",
path.display()
));
}
}
Ok(())
}
pub(crate) fn read_file_bytes_limited(
path: &Path,
cap: usize,
) -> Result<(Vec<u8>, std::fs::Metadata), String> {
reject_symlink(path)?;
if let Ok(meta) = std::fs::metadata(path)
&& meta.len() > cap as u64
{
return Err(format!(
"ERROR: file too large ({} bytes, cap {} via LCTX_MAX_READ_BYTES): {}",
meta.len(),
cap,
path.display()
));
}
let mut opts = std::fs::OpenOptions::new();
opts.read(true);
#[cfg(unix)]
{
// Defense in depth alongside `reject_symlink`: O_NOFOLLOW closes the
// race between the lstat check and the open.
use std::os::unix::fs::OpenOptionsExt;
opts.custom_flags(libc::O_NOFOLLOW);
}
let mut file = opts.open(path).map_err(|e| {
#[cfg(unix)]
if e.raw_os_error() == Some(libc::ELOOP) {
return format!(
"ERROR: {} is a symlink — refusing to edit through it (TOCTOU protection).",
path.display()
);
}
format!("ERROR: cannot open {}: {e}", path.display())
})?;
use std::io::Read;
let mut raw: Vec<u8> = Vec::new();
let mut limited = (&mut file).take((cap as u64).saturating_add(1));
limited
.read_to_end(&mut raw)
.map_err(|e| format!("ERROR: cannot read {}: {e}", path.display()))?;
if raw.len() > cap {
return Err(format!(
"ERROR: file too large (cap {} via LCTX_MAX_READ_BYTES): {}",
cap,
path.display()
));
}
let meta = file
.metadata()
.map_err(|e| format!("ERROR: cannot stat {}: {e}", path.display()))?;
Ok((raw, meta))
}
pub(crate) fn fingerprint_from_bytes(bytes: &[u8], meta: &std::fs::Metadata) -> FileFingerprint {
FileFingerprint {
size: bytes.len() as u64,
mtime_ms: meta.modified().map_or(0, system_time_to_millis),
md5: crate::core::hasher::hash_hex(bytes),
}
}
pub(crate) fn read_preimage(
path: &Path,
cap: usize,
allow_lossy_utf8: bool,
) -> Result<FilePreimage, String> {
let (bytes, meta) = read_file_bytes_limited(path, cap)?;
let permissions = meta.permissions();
let fp = fingerprint_from_bytes(&bytes, &meta);
let text = if allow_lossy_utf8 {
String::from_utf8_lossy(&bytes).into_owned()
} else {
String::from_utf8(bytes.clone()).map_err(|_| {
format!(
"ERROR: file is not valid UTF-8 (binary/encoding). Refusing to edit: {}",
path.display()
)
})?
};
let uses_crlf = text.contains("\r\n");
Ok(FilePreimage {
fp,
permissions,
bytes,
text,
uses_crlf,
})
}
/// Re-reads the file and confirms its fingerprint still equals `expected`
/// (TOCTOU guard): a concurrent writer between the preimage read and the write
/// is detected here so the edit aborts instead of clobbering newer bytes.
///
/// **Residual window (#960, accepted limitation):** this is a point-in-time
/// check, not a held lock — it only detects a write that already happened
/// *before* this call returns. Callers still compute `new_content` and hand
/// it to [`write_atomic_bytes_with_permissions`] afterward; nothing guards
/// that gap. An external editor writing to `path` between this check
/// succeeding and the temp+rename completing is not detected, and its write
/// is silently overwritten. Acceptable for lean-ctx's single-writer-daemon
/// model — the MCP server is the only intended writer of files it edits — but
/// a real gap if an external editor (or a second lean-ctx instance) writes to
/// the same file concurrently. Closing it fully would need a held file lock
/// (e.g. `flock`) spanning check-through-rename, more machinery than the
/// mono-writer case warrants today.
pub(crate) fn ensure_preimage_still_matches(
path: &Path,
expected: &FileFingerprint,
cap: usize,
) -> Result<(), String> {
let (bytes, meta) = read_file_bytes_limited(path, cap)?;
let now = fingerprint_from_bytes(&bytes, &meta);
if &now != expected {
return Err(format!(
"ERROR: file changed since read (TOCTOU guard). Re-read and retry: {}\nexpected: size={}, mtime_ms={}, md5={}\nactual: size={}, mtime_ms={}, md5={}",
path.display(),
expected.size,
expected.mtime_ms,
expected.md5,
now.size,
now.mtime_ms,
now.md5
));
}
Ok(())
}
pub(crate) fn default_backup_path(path: &Path) -> Option<PathBuf> {
let parent = path.parent()?;
let filename = path.file_name()?.to_string_lossy();
let pid = std::process::id();
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |d| d.as_nanos());
Some(parent.join(format!("{filename}.lean-ctx.bak.{pid}.{nanos}")))
}
pub(crate) fn write_atomic_bytes_with_permissions(
path: &Path,
bytes: &[u8],
permissions: Option<&std::fs::Permissions>,
) -> Result<(), String> {
// Read-only-roots choke point (#475). Every edit write — replace, create,
// and the pre-edit backup — funnels here, including a backup whose raw
// `backup_path` bypasses the dispatch jail, so this single guard makes the
// whole tool default-deny inside a read-only root before any byte is written
// or temp/dir created.
crate::core::pathjail::enforce_writable(path)?;
// The rename below would *replace* a symlink at `path` (safe), but the edit
// pipeline read through this path moments ago — a symlink here means the
// read/write pair straddles two different files. Reject for consistency
// with the read-side O_NOFOLLOW boundary.
reject_symlink(path)?;
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
// Mechanics — temp+rename with the read-only-directory in-place fallback
// (#459) — are shared with `config_io` via `core::atomic_fs`. The symlink /
// TOCTOU / read-only-root policy above stays here, so the edit tools keep
// their audited boundary while the durable-write dance lives in one place.
crate::core::atomic_fs::write_bytes_with_fallback(path, bytes, permissions)
.map_err(|e| format!("ERROR: {e}"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detects_toctou_via_preimage_guard() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("toctou.txt");
std::fs::write(&path, "aaa\n").unwrap();
let cap = crate::core::limits::max_read_bytes();
let pre = read_preimage(&path, cap, false).unwrap();
std::fs::write(&path, "bbb\n").unwrap();
let err = ensure_preimage_still_matches(&path, &pre.fp, cap).unwrap_err();
assert!(err.contains("TOCTOU guard"), "unexpected error: {err}");
}
#[test]
fn read_preimage_rejects_invalid_utf8_by_default() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("bin.dat");
std::fs::write(&path, [0xff, 0xfe, 0xfd]).unwrap();
let cap = crate::core::limits::max_read_bytes();
let err = read_preimage(&path, cap, false).unwrap_err();
assert!(err.contains("not valid UTF-8"), "got: {err}");
// Lossy mode tolerates it.
assert!(read_preimage(&path, cap, true).is_ok());
}
#[test]
fn fingerprint_is_content_addressed() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("fp.txt");
std::fs::write(&path, "hello\n").unwrap();
let cap = crate::core::limits::max_read_bytes();
let a = read_preimage(&path, cap, false).unwrap().fp;
let b = read_preimage(&path, cap, false).unwrap().fp;
assert_eq!(a, b, "same bytes → same fingerprint");
assert_eq!(a.md5, crate::core::hasher::hash_hex(b"hello\n"));
}
}