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
//! `fs_read` / `fs_walk` / `fs_write` effects: `fs.*` dispatch and the per-op path allow-list checks.
use super::*;
impl DefaultHandler {
pub(super) fn dispatch_fs(&mut self, op: &str, args: Vec<Value>) -> Result<Value, String> {
match op {
"exists" => {
let path = expect_str(args.first())?.to_string();
if let Err(e) = self.ensure_fs_walk_path(&path) {
return Ok(err(Value::Str(e.into())));
}
Ok(Value::Bool(std::path::Path::new(&path).exists()))
}
"is_file" => {
let path = expect_str(args.first())?.to_string();
if let Err(e) = self.ensure_fs_walk_path(&path) {
return Ok(err(Value::Str(e.into())));
}
Ok(Value::Bool(std::path::Path::new(&path).is_file()))
}
"is_dir" => {
let path = expect_str(args.first())?.to_string();
if let Err(e) = self.ensure_fs_walk_path(&path) {
return Ok(err(Value::Str(e.into())));
}
Ok(Value::Bool(std::path::Path::new(&path).is_dir()))
}
"stat" => {
let path = expect_str(args.first())?.to_string();
if let Err(e) = self.ensure_fs_walk_path(&path) {
return Ok(err(Value::Str(e.into())));
}
match std::fs::metadata(&path) {
Ok(md) => {
let mtime = md.modified()
.ok()
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
.map(|d| d.as_secs() as i64)
.unwrap_or(0);
let mut rec = indexmap::IndexMap::new();
rec.insert("size".into(), Value::Int(md.len() as i64));
rec.insert("mtime".into(), Value::Int(mtime));
rec.insert("is_dir".into(), Value::Bool(md.is_dir()));
rec.insert("is_file".into(), Value::Bool(md.is_file()));
Ok(ok(Value::record_dynamic(rec)))
}
Err(e) => Ok(err(Value::Str(format!("fs.stat `{path}`: {e}").into()))),
}
}
"list_dir" => {
let path = expect_str(args.first())?.to_string();
if let Err(e) = self.ensure_fs_walk_path(&path) {
return Ok(err(Value::Str(e.into())));
}
match std::fs::read_dir(&path) {
Ok(rd) => {
let mut entries: Vec<Value> = Vec::new();
for ent in rd {
match ent {
Ok(e) => {
let p = e.path();
entries.push(Value::Str(p.to_string_lossy().into_owned().into()));
}
Err(e) => return Ok(err(Value::Str(format!("fs.list_dir: {e}").into()))),
}
}
Ok(ok(Value::List(entries.into())))
}
Err(e) => Ok(err(Value::Str(format!("fs.list_dir `{path}`: {e}").into()))),
}
}
"walk" => {
let path = expect_str(args.first())?.to_string();
if let Err(e) = self.ensure_fs_walk_path(&path) {
return Ok(err(Value::Str(e.into())));
}
let mut paths: Vec<Value> = Vec::new();
for ent in walkdir::WalkDir::new(&path) {
match ent {
Ok(e) => paths.push(Value::Str(
e.path().to_string_lossy().into_owned().into())),
Err(e) => return Ok(err(Value::Str(format!("fs.walk: {e}").into()))),
}
}
Ok(ok(Value::List(paths.into())))
}
"glob" => {
let pattern = expect_str(args.first())?.to_string();
// Glob patterns can't be path-scoped at parse time
// (`**/*.rs` doesn't pin a directory); we filter the
// per-result paths after expansion against
// `--allow-fs-read`.
let entries = match glob::glob(&pattern) {
Ok(e) => e,
Err(e) => return Ok(err(Value::Str(format!("fs.glob: {e}").into()))),
};
let mut paths: Vec<Value> = Vec::new();
for ent in entries {
match ent {
Ok(p) => {
let s = p.to_string_lossy().into_owned();
if self.policy.allow_fs_read.is_empty()
|| self.policy.allow_fs_read.iter().any(|root| p.starts_with(root))
{
paths.push(Value::Str(s.into()));
}
}
Err(e) => return Ok(err(Value::Str(format!("fs.glob: {e}").into()))),
}
}
Ok(ok(Value::List(paths.into())))
}
// Content read/write. These are the reason `[fs_read]` /
// `[fs_write]` exist as effect names at all: the same
// operations under `io.read` / `io.write` declare `[io]`,
// which reads as "console" and tells a reviewer nothing
// about filesystem reach (alpibrusl/lex-lang#882).
"read_to_string" => {
let path = expect_str(args.first())?.to_string();
let resolved = self.ensure_fs_read_content_path(&path)?;
match std::fs::read_to_string(&resolved) {
Ok(s) => Ok(ok(Value::Str(s.into()))),
Err(e) => Ok(err(Value::Str(format!("{e}").into()))),
}
}
"write" => {
let path = expect_str(args.first())?.to_string();
let contents = expect_str(args.get(1))?.to_string();
self.ensure_fs_write_content_path(&path)?;
match std::fs::write(&path, contents) {
Ok(_) => Ok(ok(Value::Unit)),
Err(e) => Ok(err(Value::Str(format!("{e}").into()))),
}
}
// Append, so a log does not have to be rewritten to grow.
//
// Opened with `append(true).create(true)`, so appending to a path
// that does not exist yet behaves like `write` rather than
// failing — a log's first line should not be a special case.
//
// ATOMICITY. One call issues one `write(2)` to an O_APPEND
// descriptor. On a local filesystem the kernel makes the seek and
// the write atomic, so concurrent appenders cannot interleave
// within a single call for payloads up to the platform's
// guarantee (PIPE_BUF, 4 KiB on Linux). A larger payload may be
// split, and NFS does not guarantee it at all. Callers that need
// a record to arrive whole should keep records small — which a
// line-oriented log does anyway — and callers that need more
// should be using a database.
"append" => {
let path = expect_str(args.first())?.to_string();
let contents = expect_str(args.get(1))?.to_string();
self.ensure_fs_write_content_path(&path)?;
match std::fs::OpenOptions::new()
.append(true)
.create(true)
.open(&path)
{
Ok(mut f) => {
use std::io::Write as _;
match f.write_all(contents.as_bytes()) {
Ok(_) => Ok(ok(Value::Unit)),
Err(e) => Ok(err(Value::Str(format!("{e}").into()))),
}
}
Err(e) => Ok(err(Value::Str(format!("{e}").into()))),
}
}
"mkdir_p" => {
let path = expect_str(args.first())?.to_string();
if let Err(e) = self.ensure_fs_write_path(&path) {
return Ok(err(Value::Str(e.into())));
}
match std::fs::create_dir_all(&path) {
Ok(_) => Ok(ok(Value::Unit)),
Err(e) => Ok(err(Value::Str(format!("fs.mkdir_p `{path}`: {e}").into()))),
}
}
"remove" => {
let path = expect_str(args.first())?.to_string();
if let Err(e) = self.ensure_fs_write_path(&path) {
return Ok(err(Value::Str(e.into())));
}
let p = std::path::Path::new(&path);
let result = if p.is_dir() {
std::fs::remove_dir_all(p)
} else {
std::fs::remove_file(p)
};
match result {
Ok(_) => Ok(ok(Value::Unit)),
Err(e) => Ok(err(Value::Str(format!("fs.remove `{path}`: {e}").into()))),
}
}
"copy" => {
let src = expect_str(args.first())?.to_string();
let dst = expect_str(args.get(1))?.to_string();
if let Err(e) = self.ensure_fs_walk_path(&src) {
return Ok(err(Value::Str(e.into())));
}
if let Err(e) = self.ensure_fs_write_path(&dst) {
return Ok(err(Value::Str(e.into())));
}
match std::fs::copy(&src, &dst) {
Ok(_) => Ok(ok(Value::Unit)),
Err(e) => Ok(err(Value::Str(format!("fs.copy {src} -> {dst}: {e}").into()))),
}
}
other => Err(format!("unsupported fs.{other}")),
}
}
}
impl DefaultHandler {
/// Path scope for walk-style operations. `[fs_walk]` reuses the
/// `--allow-fs-read` allowlist — listing a directory is an
/// information disclosure on the same path tree as reading file
/// content, so the same scope applies. Empty allowlist = any path.
pub(super) fn ensure_fs_walk_path(&self, path: &str) -> Result<(), String> {
if self.policy.allow_fs_read.is_empty() {
return Ok(());
}
let p = std::path::Path::new(path);
if self.policy.allow_fs_read.iter().any(|a| p.starts_with(a)) {
Ok(())
} else {
Err(format!("fs path `{path}` outside --allow-fs-read"))
}
}
}
impl DefaultHandler {
/// Path scope for mutating operations. `[fs_write]` uses the
/// existing `--allow-fs-write` allowlist.
pub(super) fn ensure_fs_write_path(&self, path: &str) -> Result<(), String> {
if self.policy.allow_fs_write.is_empty() {
return Ok(());
}
let p = std::path::Path::new(path);
if self.policy.allow_fs_write.iter().any(|a| p.starts_with(a)) {
Ok(())
} else {
Err(format!("fs path `{path}` outside --allow-fs-write"))
}
}
}
impl DefaultHandler {
/// Path scope for CONTENT reads (`fs.read_to_string`, and the legacy
/// `io.read`). Distinct from `ensure_fs_walk_path` in one way that
/// matters: it returns the *resolved* path, because `read_root` may
/// rebase it for tests, and the caller must read the same path the
/// check approved.
pub(super) fn ensure_fs_read_content_path(&self, path: &str) -> Result<PathBuf, String> {
let resolved = self.resolve_read_path(path);
if !self.policy.allow_fs_read.is_empty()
&& !self.policy.allow_fs_read.iter().any(|a| resolved.starts_with(a))
{
return Err(format!("read of `{path}` outside --allow-fs-read"));
}
Ok(resolved)
}
/// Path scope for CONTENT writes (`fs.write`, and the legacy
/// `io.write`). Canonicalises both sides so platform path aliases
/// (macOS `/tmp` -> `/private/tmp`) compare correctly; `canonicalize`
/// fails on a file that does not exist yet, so a new write falls back
/// to canonicalising the parent.
///
/// NOTE: `ensure_fs_write_path` (used by mkdir_p / remove / copy) does
/// a plain prefix comparison with no canonicalisation, so the two
/// disagree on aliased paths. That divergence predates this function
/// and is deliberately not changed here.
pub(super) fn ensure_fs_write_content_path(&self, path: &str) -> Result<(), String> {
if self.policy.allow_fs_write.is_empty() {
return Ok(());
}
let raw = std::env::current_dir()
.map(|cwd| cwd.join(path))
.unwrap_or_else(|_| PathBuf::from(path));
let p = std::fs::canonicalize(&raw).unwrap_or_else(|_| {
raw.parent()
.and_then(|par| std::fs::canonicalize(par).ok())
.map(|par| par.join(raw.file_name().unwrap_or_default()))
.unwrap_or(raw)
});
let allowed = self.policy.allow_fs_write.iter().any(|a| {
let ca = std::fs::canonicalize(a).unwrap_or_else(|_| a.clone());
p.starts_with(&ca)
});
if allowed {
Ok(())
} else {
Err(format!("write to `{path}` outside --allow-fs-write"))
}
}
}