agd-core 0.1.0

Org-mode parsing and task operations for agd
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
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
use std::collections::BTreeSet;
use std::path::{Component, Path, PathBuf};

use globset::{Glob, GlobSet, GlobSetBuilder};
use ignore::WalkBuilder;

use crate::AgdError;

/// agd's configuration directory: `$XDG_CONFIG_HOME/agd`, else
/// `~/.config/agd`, on every Unix including macOS; the platform
/// configuration directory on Windows.
pub fn config_dir() -> Result<PathBuf, AgdError> {
    base_dir("XDG_CONFIG_HOME", ".config", dirs::config_dir).map(|base| base.join("agd"))
}

/// agd's data directory, which holds the journal: `$XDG_DATA_HOME/agd`, else
/// `~/.local/share/agd`, on every Unix including macOS; the platform local
/// data directory on Windows.
pub fn data_dir() -> Result<PathBuf, AgdError> {
    base_dir("XDG_DATA_HOME", ".local/share", dirs::data_local_dir).map(|base| base.join("agd"))
}

fn base_dir(
    variable: &str,
    home_relative: &str,
    windows: fn() -> Option<PathBuf>,
) -> Result<PathBuf, AgdError> {
    if cfg!(windows) {
        return windows().ok_or_else(|| {
            AgdError::new(
                "config",
                format!("cannot determine the Windows directory for {variable}"),
            )
        });
    }
    // The XDG Base Directory spec says a relative value is invalid and must be ignored.
    if let Some(value) = std::env::var_os(variable) {
        let path = PathBuf::from(value);
        if path.is_absolute() {
            return Ok(path);
        }
    }
    let home = dirs::home_dir().ok_or_else(|| {
        AgdError::new(
            "config",
            format!("cannot determine the home directory for ~/{home_relative}; set {variable}"),
        )
    })?;
    Ok(home.join(home_relative))
}

#[derive(Clone, Debug)]
pub struct RootSet {
    roots: Vec<PathBuf>,
    agenda: Vec<String>,
}

impl RootSet {
    /// `roots` are the only directories agd may read or write; `agenda`
    /// holds the agenda file patterns (files, directories or globs).
    pub fn new(roots: &[String], agenda: &[String]) -> Result<Self, AgdError> {
        let configured_roots = roots;
        let mut roots = Vec::with_capacity(configured_roots.len());
        for configured in configured_roots {
            let expanded = shellexpand::tilde(configured).into_owned();
            let canonical = PathBuf::from(&expanded).canonicalize().map_err(|error| {
                AgdError::with_source(
                    "invalid_root",
                    format!("cannot resolve approved root '{configured}': {error}"),
                    error,
                )
            })?;
            if !canonical.is_dir() {
                return Err(AgdError::new(
                    "invalid_root",
                    format!("approved root is not a directory: {configured}"),
                ));
            }
            roots.push(canonical);
        }
        roots.sort();
        roots.dedup();
        if roots.is_empty() {
            return Err(AgdError::new(
                "invalid_root",
                "at least one root is required",
            ));
        }
        Ok(Self {
            roots,
            agenda: agenda.to_vec(),
        })
    }

    pub fn roots(&self) -> &[PathBuf] {
        &self.roots
    }

    pub fn contains(&self, path: &Path) -> bool {
        self.roots.iter().any(|root| path.starts_with(root))
    }

    pub fn resolve_read(&self, requested: &Path) -> Result<PathBuf, AgdError> {
        reject_parent_segments(requested)?;
        if requested.is_absolute() {
            return self.validate_existing_read(requested);
        }

        let mut matches = BTreeSet::new();
        for root in &self.roots {
            let candidate = root.join(requested);
            if candidate.exists() {
                matches.insert(self.validate_existing_read(&candidate)?);
            }
        }
        match matches.len() {
            0 => Err(AgdError::new(
                "not_found",
                format!(
                    "file is not present under an approved root: {}",
                    requested.display()
                ),
            )),
            1 => Ok(matches.into_iter().next().expect("one path")),
            _ => Err(AgdError::new(
                "ambiguous_path",
                format!(
                    "relative path matches multiple approved roots: {}",
                    requested.display()
                ),
            )),
        }
    }

    pub fn resolve_write(&self, requested: &Path, create: bool) -> Result<PathBuf, AgdError> {
        reject_parent_segments(requested)?;
        let lexical = if requested.is_absolute() {
            requested.to_path_buf()
        } else if self.roots.len() == 1 {
            self.roots[0].join(requested)
        } else {
            let existing: Vec<PathBuf> = self
                .roots
                .iter()
                .map(|root| root.join(requested))
                .filter(|path| path.exists())
                .collect();
            match existing.as_slice() {
                [path] => path.clone(),
                [] if create => {
                    return Err(AgdError::new(
                        "ambiguous_path",
                        "capture into multiple roots requires an absolute target",
                    ));
                }
                [] => {
                    return Err(AgdError::new(
                        "not_found",
                        format!(
                            "file is not present under an approved root: {}",
                            requested.display()
                        ),
                    ));
                }
                _ => {
                    return Err(AgdError::new(
                        "ambiguous_path",
                        format!(
                            "relative path matches multiple approved roots: {}",
                            requested.display()
                        ),
                    ));
                }
            }
        };

        if let Ok(metadata) = std::fs::symlink_metadata(&lexical)
            && metadata.file_type().is_symlink()
        {
            return Err(AgdError::new(
                "symlink_write_refused",
                format!("writes to symlinks are refused: {}", lexical.display()),
            ));
        }

        let parent = lexical.parent().ok_or_else(|| {
            AgdError::new(
                "invalid_path",
                format!("path has no parent: {}", lexical.display()),
            )
        })?;
        let canonical_parent = nearest_existing(parent)?.canonicalize().map_err(|error| {
            AgdError::with_source(
                "invalid_path",
                format!("cannot resolve parent of {}: {error}", lexical.display()),
                error,
            )
        })?;
        if !self.contains(&canonical_parent) {
            return Err(AgdError::new(
                "outside_roots",
                format!("path escapes approved roots: {}", lexical.display()),
            ));
        }
        if lexical.exists() && !lexical.is_file() {
            return Err(AgdError::new(
                "invalid_path",
                format!("write target is not a regular file: {}", lexical.display()),
            ));
        }
        Ok(lexical)
    }

    pub fn agenda_files(&self) -> Result<Vec<PathBuf>, AgdError> {
        let patterns = if self.agenda.is_empty() {
            vec!["**/*.org".to_string()]
        } else {
            self.agenda.clone()
        };
        self.org_files_matching(&patterns)
    }

    pub fn all_org_files(&self) -> Result<Vec<PathBuf>, AgdError> {
        self.org_files_matching(&[String::from("**/*.org")])
    }

    fn org_files_matching(&self, patterns: &[String]) -> Result<Vec<PathBuf>, AgdError> {
        let mut relative_patterns = Vec::new();
        let mut files = BTreeSet::new();
        for pattern in patterns {
            let path = Path::new(pattern);
            reject_parent_segments(path)?;
            if path.is_absolute() {
                if pattern.contains(['*', '?', '[', ']']) {
                    return Err(AgdError::new(
                        "invalid_agenda_pattern",
                        format!("absolute agenda entries must be exact files: {pattern}"),
                    ));
                }
                let resolved = self.validate_existing_read(path)?;
                if resolved
                    .extension()
                    .is_none_or(|extension| extension != "org")
                {
                    return Err(AgdError::new(
                        "invalid_agenda_file",
                        format!("agenda file does not have an .org extension: {pattern}"),
                    ));
                }
                files.insert(resolved);
            } else {
                relative_patterns.push(pattern.clone());
            }
        }
        if relative_patterns.is_empty() {
            return Ok(files.into_iter().collect());
        }

        let matcher = build_globset(&relative_patterns)?;
        for root in &self.roots {
            for entry in WalkBuilder::new(root).follow_links(true).build() {
                let entry = match entry {
                    Ok(entry) => entry,
                    Err(error) if is_emacs_lock(&error) => continue,
                    Err(error) => {
                        return Err(AgdError::new(
                            "walk_error",
                            format!("cannot walk {}: {error}", root.display()),
                        ));
                    }
                };
                if !entry.file_type().is_some_and(|kind| kind.is_file()) {
                    continue;
                }
                let path = entry.path();
                let relative = path.strip_prefix(root).unwrap_or(path);
                if path.extension().is_some_and(|extension| extension == "org")
                    && matcher.is_match(relative)
                {
                    files.insert(self.validate_existing_read(path)?);
                }
            }
        }
        Ok(files.into_iter().collect())
    }

    fn validate_existing_read(&self, path: &Path) -> Result<PathBuf, AgdError> {
        let canonical = path.canonicalize().map_err(|error| {
            AgdError::with_source(
                "not_found",
                format!("cannot resolve file {}: {error}", path.display()),
                error,
            )
        })?;
        if !self.contains(&canonical) {
            return Err(AgdError::new(
                "outside_roots",
                format!("resolved path escapes approved roots: {}", path.display()),
            ));
        }
        if !canonical.is_file() {
            return Err(AgdError::new(
                "invalid_path",
                format!("path is not a regular file: {}", path.display()),
            ));
        }
        Ok(canonical)
    }
}

fn reject_parent_segments(path: &Path) -> Result<(), AgdError> {
    if path
        .components()
        .any(|component| component == Component::ParentDir)
    {
        return Err(AgdError::new(
            "path_traversal",
            format!("'..' is not allowed in paths: {}", path.display()),
        ));
    }
    Ok(())
}

fn nearest_existing(path: &Path) -> Result<&Path, AgdError> {
    let mut current = path;
    loop {
        if current.exists() {
            return Ok(current);
        }
        current = current.parent().ok_or_else(|| {
            AgdError::new(
                "invalid_path",
                format!("no existing ancestor for {}", path.display()),
            )
        })?;
    }
}

fn build_globset(patterns: &[String]) -> Result<GlobSet, AgdError> {
    let mut builder = GlobSetBuilder::new();
    for pattern in patterns {
        reject_parent_segments(Path::new(pattern))?;
        if Path::new(pattern).is_absolute() {
            return Err(AgdError::new(
                "invalid_agenda_pattern",
                format!("agenda pattern must be relative to an approved root: {pattern}"),
            ));
        }
        builder.add(Glob::new(pattern).map_err(|error| {
            AgdError::new(
                "invalid_agenda_pattern",
                format!("invalid agenda pattern '{pattern}': {error}"),
            )
        })?);
    }
    builder.build().map_err(|error| {
        AgdError::new(
            "invalid_agenda_pattern",
            format!("cannot build agenda patterns: {error}"),
        )
    })
}

/// Emacs marks a buffer with unsaved changes by a dangling `.#name`
/// symlink; following it fails, but it is never an agenda file.
fn is_emacs_lock(error: &ignore::Error) -> bool {
    match error {
        ignore::Error::WithPath { path, err } => {
            path.file_name()
                .is_some_and(|name| name.to_string_lossy().starts_with(".#"))
                || is_emacs_lock(err)
        }
        ignore::Error::WithDepth { err, .. } | ignore::Error::WithLineNumber { err, .. } => {
            is_emacs_lock(err)
        }
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn roots(temp: &tempfile::TempDir) -> RootSet {
        RootSet::new(&[temp.path().to_string_lossy().into_owned()], &[]).unwrap()
    }

    #[test]
    fn rejects_parent_traversal() {
        let temp = tempfile::tempdir().unwrap();
        let error = roots(&temp)
            .resolve_read(Path::new("../secret.org"))
            .unwrap_err();
        assert_eq!(error.code(), "path_traversal");
    }

    #[test]
    fn accepts_exact_absolute_agenda_files_inside_roots() {
        let temp = tempfile::tempdir().unwrap();
        let file = temp.path().join("tasks.org");
        std::fs::write(&file, "* TODO Safe\n").unwrap();
        let roots = RootSet::new(
            &[temp.path().to_string_lossy().into_owned()],
            &[file.to_string_lossy().into_owned()],
        )
        .unwrap();
        assert_eq!(
            roots.agenda_files().unwrap(),
            vec![std::fs::canonicalize(file).unwrap()]
        );
    }

    #[cfg(unix)]
    #[test]
    fn emacs_lock_symlinks_do_not_break_discovery() {
        use std::os::unix::fs::symlink;

        let temp = tempfile::tempdir().unwrap();
        let file = temp.path().join("tasks.org");
        std::fs::write(&file, "* TODO Edited in Emacs\n").unwrap();
        symlink("user@host.1234:1700000000", temp.path().join(".#tasks.org")).unwrap();
        assert_eq!(
            roots(&temp).agenda_files().unwrap(),
            vec![std::fs::canonicalize(file).unwrap()]
        );
    }

    #[cfg(unix)]
    #[test]
    fn follows_only_root_contained_read_symlinks_and_refuses_symlink_writes() {
        use std::os::unix::fs::symlink;

        let temp = tempfile::tempdir().unwrap();
        let target = temp.path().join("target.org");
        std::fs::write(&target, "* TODO Safe\n").unwrap();
        symlink(&target, temp.path().join("link.org")).unwrap();
        let roots = roots(&temp);
        assert_eq!(
            roots.resolve_read(Path::new("link.org")).unwrap(),
            std::fs::canonicalize(target).unwrap()
        );
        let error = roots
            .resolve_write(Path::new("link.org"), false)
            .unwrap_err();
        assert_eq!(error.code(), "symlink_write_refused");

        let outside = tempfile::tempdir().unwrap();
        let outside_file = outside.path().join("outside.org");
        std::fs::write(&outside_file, "* TODO Secret\n").unwrap();
        symlink(&outside_file, temp.path().join("escape.org")).unwrap();
        let error = roots.resolve_read(Path::new("escape.org")).unwrap_err();
        assert_eq!(error.code(), "outside_roots");
    }
}