dort-core 0.3.0

UI-independent planning and guarded filesystem services for Dort
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
//! Installs one exact npm package in an explicit scope.
use super::*;
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, path::Path};

pub const WARNING: &str = "Installs one exact npm package with lifecycle scripts off. Native dependencies and bin/man links can change the prefix and add commands to PATH. npm can read configuration and contact package sources. No sandbox or rollback. A 10-minute deadline can leave partial effects.";

#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Catalog {
    pub query: inspection::Query,
    pub executable: PathBuf,
    pub search_dirs: Vec<PathBuf>,
    pub npm_binding: Option<inspection::NpmBinding>,
    pub tools: BTreeSet<String>,
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Scope {
    pub binding: inspection::NpmBinding,
    pub catalogs: Vec<Catalog>,
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct Sources {
    pub programs: Vec<Entry>,
    pub directories: Vec<Entry>,
    pub tool_directories: Vec<Entry>,
}
pub(super) fn valid(scope: &super::Scope) -> bool {
    let Some(npm) = &scope.npm else {
        return false;
    };
    if scope.brew.is_some()
        || npm.catalogs.is_empty()
        || npm.catalogs.len() > 16
        || npm.binding.node != scope.executable
        || npm.binding.prefix == scope.home
        || npm.binding.prefix.ancestors().count() > 32
        || !npm.binding.prefix.starts_with(&scope.home)
        || !scope.bound_tools.contains(&npm.binding.runtime_tool)
    {
        return false;
    }
    let mut ids = BTreeSet::new();
    for c in &npm.catalogs {
        if c.tools.is_empty()
            || c.tools.len() > 256
            || c.search_dirs.len() > 256
            || c.preview(&scope.home).is_err()
            || c.tools.iter().any(|id| !ids.insert(id.clone()))
        {
            return false;
        }
        if c.tools.contains(&scope.tool)
            && (c.npm_binding.as_ref() != Some(&npm.binding) || c.search_dirs != scope.search_dirs)
        {
            return false;
        }
    }
    ids == scope.bound_tools
}
impl Catalog {
    pub(crate) fn preview(&self, home: &Path) -> Result<inspection::Preview> {
        let result = if self.query == inspection::Query::NpmGlobals {
            let binding = self
                .npm_binding
                .clone()
                .ok_or_else(|| invalid("npm catalog binding missing"))?;
            if binding.node != self.executable {
                return Err(invalid("npm catalog executable differs from selected Node"));
            }
            inspection::Preview::npm_probe(binding, home.to_owned(), self.search_dirs.clone())
        } else {
            if self.npm_binding.is_some() {
                return Err(invalid("non-npm catalog cannot have an npm binding"));
            }
            inspection::Preview::new(
                self.query,
                self.executable.clone(),
                home.to_owned(),
                self.search_dirs.clone(),
            )
        };
        result.map_err(|_| invalid("invalid explicit installation catalog scope"))
    }
    pub(super) fn matches(&self, home: &Path, r: &inspection::Report) -> bool {
        let d = &r.description;
        d.query == self.query
            && d.executable == self.executable
            && d.home == home
            && d.trusted_search_path == self.search_dirs
            && d.npm_binding == self.npm_binding
    }
}
/// Process-free reconstruction from explicit catalog bindings. Serialized command
/// descriptions are not accepted. The application must validate snapshot provenance.
pub fn prepare(
    host: &HostContext,
    request: Request,
    npm: Scope,
    reports: &[&inspection::Report],
) -> Result<Preview> {
    prepare_inner(host, request, npm, reports, false)
}

pub(crate) fn prepare_environment(
    host: &HostContext,
    request: Request,
    npm: Scope,
    reports: &[&inspection::Report],
) -> Result<Preview> {
    prepare_inner(host, request, npm, reports, true)
}

fn prepare_inner(
    host: &HostContext,
    request: Request,
    npm: Scope,
    reports: &[&inspection::Report],
    environment: bool,
) -> Result<Preview> {
    let (manifest, scope, sources) = super::sources(host, &request, Some(&npm), None)?;
    // The durable intent budget applies before creation. Newly pinned parents
    // expand in-memory fingerprints, not the immutable persisted intent.
    super::bounded_metadata(&scope, &sources)?;
    if !valid(&scope) {
        return Err(invalid("invalid explicit npm installation scope"));
    }
    validate_catalogs(&manifest, &scope, &npm, reports)?;
    let row = super::evaluate_catalogs(host, &manifest, &scope, reports)?;
    if !super::reviewable(&row, environment) {
        return Err(invalid(
            "only an observed missing exact npm package with satisfied explicit runtime and prerequisites can be reviewed",
        ));
    }
    Ok(Preview {
        request,
        scope,
        sources,
    })
}
fn validate_catalogs(
    manifest: &Manifest,
    scope: &super::Scope,
    npm: &Scope,
    reports: &[&inspection::Report],
) -> Result<()> {
    if npm.catalogs.is_empty() || npm.catalogs.len() > 16 || reports.len() != npm.catalogs.len() {
        return Err(invalid("installation requires bounded explicit catalogs"));
    }
    let mut bound = BTreeMap::new();
    for (catalog, report) in npm.catalogs.iter().zip(reports) {
        catalog.preview(&scope.home)?;
        if catalog.tools.is_empty() || !catalog.matches(&scope.home, report) {
            return Err(invalid("selected installation catalog scope differs"));
        }
        for id in &catalog.tools {
            if !scope.bound_tools.contains(id) || bound.insert(id.clone(), catalog).is_some() {
                return Err(invalid(
                    "duplicate or inactive installation catalog binding",
                ));
            }
        }
    }
    if bound.len() != scope.bound_tools.len()
        || !bound.get(&scope.tool).is_some_and(|c| {
            c.query == inspection::Query::NpmGlobals && c.npm_binding.as_ref() == Some(&npm.binding)
        })
        || !bound.contains_key(&npm.binding.runtime_tool)
        || !manifest
            .tools
            .iter()
            .any(|t| t.id == npm.binding.runtime_tool)
    {
        return Err(invalid(
            "explicit target and runtime catalog bindings required",
        ));
    }
    Ok(())
}
pub(super) fn catalogs<'a>(
    scope: &super::Scope,
    npm: &Scope,
    reports: &[&'a inspection::Report],
) -> Result<BTreeMap<String, &'a inspection::Report>> {
    if npm.catalogs.len() != reports.len() {
        return Err(invalid("installation catalog count differs"));
    }
    let mut catalogs = BTreeMap::new();
    for (c, r) in npm.catalogs.iter().zip(reports) {
        if !c.matches(&scope.home, r) {
            return Err(invalid("installation catalog scope differs"));
        }
        for id in &c.tools {
            if catalogs.insert(id.clone(), *r).is_some() {
                return Err(invalid("duplicate installation catalog binding"));
            }
        }
    }
    Ok(catalogs)
}
pub(super) fn arguments(scope: &super::Scope) -> Vec<String> {
    let binding = &scope.npm.as_ref().expect("npm scope").binding;
    [
        binding.npm_cli.to_str().expect("validated path").to_owned(),
        "install".into(),
        "--global".into(),
        "--prefix".into(),
        binding.prefix.to_str().expect("validated path").into(),
        "--ignore-scripts".into(),
        "--audit=false".into(),
        "--fund=false".into(),
        "--update-notifier=false".into(),
        "--logs-max=0".into(),
        "--loglevel=silent".into(),
        "--registry=https://registry.npmjs.org/".into(),
        "--userconfig=/dev/null".into(),
        "--globalconfig=/dev/null".into(),
        format!("{}@{}", scope.package, scope.version),
    ]
    .into()
}
pub(super) fn program(path: &Path, executable: bool) -> Result<Entry> {
    let e = observation::inspect(path, false)?;
    if e.kind != Kind::Regular
        || e.fingerprint
            .as_ref()
            .is_none_or(|f| f.links != 1 || (executable && f.mode & 0o111 == 0))
    {
        return Err(invalid(
            "installation requires a physical regular trusted program",
        ));
    }
    Ok(e)
}
pub(super) fn sources(scope: &super::Scope) -> Result<Sources> {
    let npm = scope.npm.as_ref().expect("npm scope");
    let b = &npm.binding;
    if !inspection::absolute(&b.prefix)
        || b.prefix == scope.home
        || !b.prefix.starts_with(&scope.home)
    {
        return Err(invalid(
            "npm installation prefix must be strictly inside HOME",
        ));
    }
    for root in ["lib/node_modules", "bin", "share/man"] {
        let root = b.prefix.join(root);
        if b.node.starts_with(&root) || b.npm_cli.starts_with(&root) {
            return Err(invalid(
                "npm write roots must not contain selected Node or npm CLI",
            ));
        }
    }
    let mut programs = vec![program(&b.node, true)?, program(&b.npm_cli, false)?];
    for c in &npm.catalogs {
        c.preview(&scope.home)?;
        programs.push(program(&c.executable, true)?);
        if let Some(b) = &c.npm_binding {
            programs.push(program(&b.npm_cli, false)?);
        }
    }
    let mut paths = BTreeSet::new();
    let mut roots = vec![
        b.prefix.clone(),
        b.prefix.join("lib/node_modules"),
        b.prefix.join("bin"),
        b.prefix.join("share/man"),
    ];
    if let Some((scope, _)) = scope
        .package
        .strip_prefix('@')
        .and_then(|p| p.split_once('/'))
    {
        roots.push(b.prefix.join("lib/node_modules").join(format!("@{scope}")));
    }
    for path in roots {
        for p in path.ancestors().take_while(|p| p.starts_with(&scope.home)) {
            paths.insert(p.to_owned());
        }
    }
    let mut directories = Vec::new();
    for path in paths {
        let e = observation::inspect(&path, false)?;
        if !matches!(e.kind, Kind::Missing | Kind::Directory)
            || e.fingerprint.as_ref().is_some_and(|f| {
                f.uid != rustix::process::geteuid().as_raw() || f.mode & 0o022 != 0
            })
            || (path == scope.home && e.kind != Kind::Directory)
        {
            return Err(invalid(
                "npm prefix parents must be physical owned directories without group/world write access",
            ));
        }
        directories.push(e);
    }
    let tool_directories = scope
        .tool_search_dirs
        .iter()
        .map(|p| {
            let e = observation::inspect(p, false)?;
            if e.kind != Kind::Directory {
                return Err(invalid("tool search directory unavailable or unsafe"));
            }
            Ok(e)
        })
        .collect::<Result<Vec<_>>>()?;
    Ok(Sources {
        programs,
        directories,
        tool_directories,
    })
}
pub(super) fn same_directory(a: &Entry, b: &Entry) -> bool {
    a.path == b.path
        && a.kind == b.kind
        && match (&a.fingerprint, &b.fingerprint) {
            (Some(a), Some(b)) => adoption::parent_identity(a, b),
            (None, None) => true,
            _ => false,
        }
        && a.parents.len() == b.parents.len()
        && a.parents.iter().zip(&b.parents).all(|(a, b)| {
            a.path == b.path && adoption::parent_identity(&a.fingerprint, &b.fingerprint)
        })
}
pub(super) fn effect_sources(sources: &Sources) -> Sources {
    let mut stable = sources.clone();
    for program in &mut stable.programs {
        *program = program.clone().stable_parents();
    }
    for entry in stable
        .directories
        .iter_mut()
        .chain(&mut stable.tool_directories)
    {
        *entry = entry.clone().stable_parents();
        if let Some(fingerprint) = &mut entry.fingerprint {
            fingerprint.modified = (0, 0);
            fingerprint.changed = (0, 0);
            fingerprint.links = 0;
            fingerprint.size = 0;
        }
    }
    stable
}

pub(super) fn same(a: &Sources, b: &Sources) -> bool {
    a.tool_directories.len() == b.tool_directories.len()
        && a.tool_directories
            .iter()
            .zip(&b.tool_directories)
            .all(|(a, b)| same_directory(a, b))
        && a.programs.len() == b.programs.len()
        && a.programs
            .iter()
            .zip(&b.programs)
            .all(|(a, b)| same_source(a, b))
        && a.directories.len() == b.directories.len()
        && a.directories
            .iter()
            .zip(&b.directories)
            .all(|(a, b)| same_directory(a, b))
}
/// Called only after immutable intent and final guards. Create only approved
/// mutation parents, descriptor-relative, then pin the resulting identities.
pub(super) fn create_directories(scope: &super::Scope, old: &Sources) -> Result<Sources> {
    if !same(old, &sources(scope)?) {
        return Err(invalid("npm prefix changed before creation"));
    }
    let mut created = Vec::new();
    for e in &old.directories {
        if e.kind != Kind::Missing {
            continue;
        }
        let handle = observation::parent_handle(&e.path)?;
        if handle.missing {
            return Err(invalid("npm prefix creation parent unavailable"));
        }
        rustix::fs::mkdirat(
            &handle.directory,
            &handle.name,
            rustix::fs::Mode::from_raw_mode(0o755),
        )
        .map_err(|_| invalid("npm prefix creation failed; inspect pending attempt"))?;
        handle
            .directory
            .sync_all()
            .map_err(|_| invalid("npm prefix parent sync failed"))?;
        let made = observation::parent_handle(&e.path.join(".dort-parent-check"))?;
        made.directory
            .sync_all()
            .map_err(|_| invalid("npm prefix directory sync failed"))?;
        // Pin each new directory immediately, not only at the end of creation.
        let entry = observation::inspect(&e.path, false)?;
        if entry.kind != Kind::Directory {
            return Err(invalid("new npm directory changed"));
        }
        created.push(entry);
    }
    let fresh = sources(scope)?;
    if old.tool_directories.len() != fresh.tool_directories.len()
        || old
            .tool_directories
            .iter()
            .zip(&fresh.tool_directories)
            .any(|(a, b)| !same_directory(a, b))
        || created.iter().any(|a| {
            fresh
                .directories
                .iter()
                .find(|b| b.path == a.path)
                .is_none_or(|b| !same_directory(a, b))
        })
        || old
            .programs
            .iter()
            .zip(&fresh.programs)
            .any(|(a, b)| !same_source(a, b))
        || old
            .directories
            .iter()
            .zip(&fresh.directories)
            .any(|(a, b)| a.kind != Kind::Missing && !same_directory(a, b))
    {
        return Err(invalid(
            "npm prefix changed while creating approved parents",
        ));
    }
    Ok(fresh)
}