dort-core 0.2.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! Installs one Homebrew core formula. Build and dependency effects are shown first.
use super::*;
use std::{collections::BTreeMap, path::Path, process::Command, time::Duration};

pub const WARNING: &str = "Installs one Homebrew core formula at its current version, not a fixed version. Formula, build and dependency code can run. Dependencies can be installed or upgraded, and links can add commands to PATH. No sandbox or rollback. A 10-minute deadline can leave partial effects.";
const CONTROLS: &[&str] = &[
    "HOMEBREW_NO_AUTO_UPDATE",
    "HOMEBREW_NO_ANALYTICS",
    "HOMEBREW_NO_ENV_HINTS",
    "HOMEBREW_NO_INSTALL_UPGRADE",
    "HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK",
    "HOMEBREW_NO_INSTALL_CLEANUP",
];
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Probe {
    pub required_controls_advertised: bool,
    pub prefix: PathBuf,
    pub repository: PathBuf,
    pub cellar: PathBuf,
    pub stable_version: String,
    pub installed_versions: Vec<String>,
}
impl Probe {
    pub(super) fn valid(&self, b: &Scope) -> bool {
        self.required_controls_advertised
            && self.prefix == b.prefix
            && self.repository == b.repository
            && self.cellar == b.cellar
            && super::super::valid_observed_version(&self.stable_version)
            && self.installed_versions.len() <= 256
            && self
                .installed_versions
                .iter()
                .all(|v| super::super::valid_observed_version(v) && !v.starts_with("HEAD"))
    }
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Scope {
    pub prefix: PathBuf,
    pub repository: PathBuf,
    pub cellar: PathBuf,
    pub policy: VersionPolicy,
    pub catalogs: Vec<npm::Catalog>,
}
/// Typical supported physical layout hint only. Never executes a candidate.
pub fn prefix_hint(executable: &Path) -> Option<PathBuf> {
    let repo = repository(executable)?;
    Some(if repo.file_name()? == "Homebrew" {
        repo.parent()?.to_owned()
    } else {
        repo
    })
}
fn repository(executable: &Path) -> Option<PathBuf> {
    (executable.file_name()? == "brew" && executable.parent()?.file_name()? == "bin")
        .then(|| {
            executable
                .parent()
                .and_then(Path::parent)
                .map(Path::to_owned)
        })
        .flatten()
}
/// Deliberately support the unqualified canonical core spelling only. Qualified
/// tap names retain the pure planner's ambiguous-identity rules, never normalized.
pub fn formula(package: &str) -> bool {
    !package.is_empty()
        && package.len() <= 128
        && package.as_bytes()[0].is_ascii_lowercase()
        && package
            .bytes()
            .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"+._-@".contains(&b))
        && !package.contains("..")
}
pub(super) fn valid(s: &super::Scope) -> bool {
    let Some(b) = &s.brew else {
        return false;
    };
    s.npm.is_none()
        && formula(&s.package)
        && matches!(
            b.policy,
            VersionPolicy::ProviderDefault {} | VersionPolicy::Latest {}
        )
        && s.version
            == if matches!(b.policy, VersionPolicy::Latest {}) {
                "latest"
            } else {
                "provider_default"
            }
        && inspection::absolute(&b.prefix)
        && b.prefix.ancestors().count() <= 32
        && b.prefix != Path::new("/")
        && b.cellar == b.prefix.join("Cellar")
        && repository(&s.executable).as_ref() == Some(&b.repository)
        && (b.repository == b.prefix || b.repository == b.prefix.join("Homebrew"))
        && valid_catalogs(s, b)
}
fn valid_catalogs(s: &super::Scope, b: &Scope) -> bool {
    let mut ids = BTreeSet::new();
    !b.catalogs.is_empty()
        && b.catalogs.len() <= 16
        && b.catalogs.iter().all(|c| {
            !c.tools.is_empty()
                && c.tools.len() <= 256
                && c.search_dirs.len() <= 256
                && c.preview(&s.home).is_ok()
                && c.tools.iter().all(|id| ids.insert(id.clone()))
                && (!c.tools.contains(&s.tool)
                    || (c.query == inspection::Query::BrewFormulae
                        && c.executable == s.executable
                        && c.search_dirs == s.search_dirs))
        })
        && ids == s.bound_tools
}
pub fn prepare(
    host: &HostContext,
    request: Request,
    brew: Scope,
    reports: &[&inspection::Report],
) -> Result<Preview> {
    let (manifest, scope, sources) = super::sources(host, &request, None, Some(&brew))?;
    super::bounded_metadata(&scope, &sources)?;
    catalogs(&scope, &brew, reports)?;
    let row = super::evaluate_catalogs(host, &manifest, &scope, reports)?;
    if row.action != planning::PackageAction::ReviewMissingPackage || !row.blockers.is_empty() {
        return Err(invalid(
            "only an observed missing canonical core formula with clear explicit prerequisites can be reviewed",
        ));
    }
    Ok(Preview {
        request,
        scope,
        sources,
    })
}
pub(super) fn catalogs<'a>(
    s: &super::Scope,
    b: &Scope,
    reports: &[&'a inspection::Report],
) -> Result<BTreeMap<String, &'a inspection::Report>> {
    if b.catalogs.len() != reports.len() {
        return Err(invalid("Homebrew catalog count differs"));
    }
    let mut out = BTreeMap::new();
    for (c, r) in b.catalogs.iter().zip(reports) {
        if !c.matches(&s.home, r) {
            return Err(invalid("Homebrew prerequisite catalog scope differs"));
        }
        for id in &c.tools {
            if out.insert(id.clone(), *r).is_some() {
                return Err(invalid("duplicate Homebrew catalog binding"));
            }
        }
    }
    Ok(out)
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct Sources {
    bindings: npm::Sources,
    parents: Vec<observation::Parent>,
}
pub(super) fn same(a: &Sources, b: &Sources) -> bool {
    npm::same(&a.bindings, &b.bindings)
        && 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)
        })
}
/// Homebrew normally shares its prefix with the caller's own admin group.
/// World-write and foreign-group write are refused; an owner/root prefix must stay
/// owner-writable. This is hardening, not isolation from other members of those groups.
fn writable_root(
    owner: u32,
    group: u32,
    mode: u32,
    uid: u32,
    groups: &[u32],
    prefix: bool,
) -> bool {
    if mode & 0o002 != 0 || (mode & 0o020 != 0 && !groups.contains(&group)) {
        return false;
    }
    (owner == uid || (prefix && owner == 0)) && mode & 0o200 != 0
}
fn safe_parent(fingerprint: &observation::Fingerprint, uid: u32, groups: &[u32]) -> bool {
    if fingerprint.mode & 0o002 != 0
        || (fingerprint.mode & 0o020 != 0 && !groups.contains(&fingerprint.gid))
    {
        return false;
    }
    fingerprint.uid == uid || fingerprint.uid == 0
}
fn caller_groups() -> Vec<u32> {
    let mut groups: Vec<u32> = rustix::process::getgroups()
        .unwrap_or_default()
        .iter()
        .map(|g| g.as_raw())
        .collect();
    groups.push(rustix::process::getegid().as_raw());
    groups.sort_unstable();
    groups.dedup();
    groups
}
pub(super) fn sources(s: &super::Scope) -> Result<Sources> {
    let b = s.brew.as_ref().expect("brew scope");
    let mut programs = vec![npm::program(&s.executable, true)?];
    for c in &b.catalogs {
        programs.push(npm::program(&c.executable, true)?);
        if let Some(n) = &c.npm_binding {
            programs.push(npm::program(&n.npm_cli, false)?);
        }
    }
    // Existing native write roots only: no privilege, group-permission inference
    // or creation before intent. A safe root-owned prefix itself is allowed.
    let mut paths = BTreeSet::from([
        b.prefix.clone(),
        b.repository.clone(),
        b.repository.join("Library"),
    ]);
    for root in [
        "Cellar",
        "bin",
        "etc",
        "include",
        "lib",
        "opt",
        "sbin",
        "share",
        "var",
        "var/homebrew",
    ] {
        paths.insert(b.prefix.join(root));
    }
    let uid = rustix::process::geteuid().as_raw();
    let groups = caller_groups();
    let directories = paths.into_iter().map(|path| {
        let e = observation::inspect(&path, false)?;
        if e.kind != Kind::Directory {
            return Err(scope_invalid(format!(
                "Homebrew folder {path:?} is missing or is not a real folder."
            )));
        }
        let Some(fingerprint) = e.fingerprint.as_ref() else {
            return Err(scope_invalid(format!(
                "Homebrew folder {path:?} cannot be verified."
            )));
        };
        if fingerprint.mode & 0o002 != 0 {
            return Err(scope_invalid(format!(
                "Homebrew folder {path:?} is writable by everyone."
            )));
        }
        if fingerprint.mode & 0o020 != 0 && !groups.contains(&fingerprint.gid) {
            return Err(scope_invalid(format!(
                "Homebrew folder {path:?} is writable by a group you are not in."
            )));
        }
        if !writable_root(
            fingerprint.uid,
            fingerprint.gid,
            fingerprint.mode,
            uid,
            &groups,
            path == b.prefix,
        ) {
            return Err(scope_invalid(format!(
                "Homebrew folder {path:?} must belong to you and be writable by you."
            )));
        }
        if let Some(parent) = e.parents.iter().find(|p| {
            // The canonical disposable HOME boundary is already application-owned;
            // outside it, root/user-owned non-writable ancestors are required.
            (!b.prefix.starts_with(&s.home) || p.path.starts_with(&s.home))
                && !safe_parent(&p.fingerprint, uid, &groups)
        }) {
            return Err(scope_invalid(format!(
                "Homebrew parent folder {:?} is writable by users outside your account or belongs to another account.",
                parent.path
            )));
        }
        Ok(e)
    }).collect::<Result<Vec<_>>>()?;
    let mut lookup = BTreeSet::new();
    lookup.extend(s.tool_search_dirs.iter().cloned());
    lookup.extend(s.search_dirs.iter().cloned());
    for c in &b.catalogs {
        lookup.extend(c.search_dirs.iter().cloned());
    }
    let tool_directories = lookup
        .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<_>>>()?;
    let mut bindings = npm::Sources {
        programs,
        directories,
        tool_directories,
    };
    // Schema 3 deduplicates repeated ancestor chains to keep native-layout
    // bindings inside the existing durable record bound. Every identity is kept.
    let mut parents = BTreeMap::<PathBuf, observation::Parent>::new();
    for e in bindings
        .programs
        .iter_mut()
        .chain(&mut bindings.directories)
        .chain(&mut bindings.tool_directories)
    {
        for p in std::mem::take(&mut e.parents) {
            if let Some(old) = parents.get(&p.path) {
                if !adoption::parent_identity(&old.fingerprint, &p.fingerprint) {
                    return Err(invalid("Homebrew parents changed while binding"));
                }
            } else {
                parents.insert(p.path.clone(), p);
            }
        }
    }
    Ok(Sources {
        bindings,
        parents: parents.into_values().collect(),
    })
}
pub(super) fn arguments(package: &str) -> Vec<String> {
    [
        "install".into(),
        "--formula".into(),
        "--no-ask".into(),
        "--skip-post-install".into(),
        format!("homebrew/core/{package}"),
    ]
    .into()
}
pub(super) fn probe_arguments(package: &str) -> Vec<Vec<String>> {
    vec![
        vec!["help".into(), "install".into()],
        vec!["--prefix".into()],
        vec!["--repository".into()],
        vec!["--cellar".into()],
        vec![
            "info".into(),
            "--json=v2".into(),
            "--formula".into(),
            format!("homebrew/core/{package}"),
        ],
    ]
}
pub(super) fn environment(command: &mut Command) {
    for key in CONTROLS {
        command.env(key, "1");
    }
}
fn command(executable: &Path, home: &Path, search_dirs: &[PathBuf], args: &[String]) -> Command {
    let mut c = Command::new(executable);
    c.env_clear()
        .env("HOME", home)
        .env("XDG_CONFIG_HOME", home.join(".config"))
        .env("XDG_CACHE_HOME", home.join(".cache"))
        .env("XDG_DATA_HOME", home.join(".local/share"))
        .env("XDG_STATE_HOME", home.join(".local/state"))
        .env(
            "PATH",
            std::env::join_paths(search_dirs).expect("validated scope"),
        )
        .env("LC_ALL", "C")
        .env("LANG", "C")
        .env("NO_COLOR", "1")
        .current_dir("/")
        .args(args);
    environment(&mut c);
    c
}
fn query(s: &super::Scope, args: &[String]) -> Result<Vec<u8>> {
    inspection::run_bounded(
        command(&s.executable, &s.home, &s.search_dirs, args),
        4 * 1024 * 1024,
        Duration::from_secs(10),
    )
    .map_err(|_| {
        invalid("Homebrew probe unavailable, failed or exceeded bounds; raw output withheld")
    })
}
pub(super) fn closure_prefix(
    executable: &Path,
    home: &Path,
    search_dirs: &[PathBuf],
    expected: &Path,
) -> Result<()> {
    let bytes = inspection::run_bounded(
        command(executable, home, search_dirs, &["--prefix".into()]),
        8192,
        Duration::from_secs(10),
    )
    .map_err(|_| invalid("Homebrew closure prefix probe unavailable; contents withheld"))?;
    if path(&bytes)? != expected {
        return Err(invalid(
            "Homebrew closure prefix differs from approved scope",
        ));
    }
    Ok(())
}
fn path(bytes: &[u8]) -> Result<PathBuf> {
    let text =
        std::str::from_utf8(bytes).map_err(|_| invalid("invalid Homebrew layout metadata"))?;
    let text = text.strip_suffix('\n').unwrap_or(text);
    if text.len() > 4096 || text.bytes().any(|b| b < 32 || b == 127) {
        return Err(invalid("invalid Homebrew layout metadata"));
    }
    let p = PathBuf::from(text);
    // Never inspect a provider-supplied path before matching the explicit scope:
    // an observation error could otherwise copy unapproved raw output into errors.
    // The caller brackets comparison with checks of the expected physical paths.
    if !inspection::absolute(&p) {
        return Err(invalid("invalid Homebrew layout path"));
    }
    Ok(p)
}
/// Execute only after lock/journal/attempt/source guards. Every separately owned
/// bounded query is bracketed by independently reconstructed source checks.
pub(super) fn probe(s: &super::Scope, mut check: impl FnMut() -> Result<()>) -> Result<Probe> {
    let b = s.brew.as_ref().expect("brew scope");
    let args = probe_arguments(&s.package);
    check()?;
    let help = query(s, &args[0])?;
    check()?;
    let help = std::str::from_utf8(&help)
        .map_err(|_| invalid("Homebrew install capabilities unavailable"))?;
    for flag in [
        "--formula",
        "--no-ask",
        "--skip-post-install",
        "HOMEBREW_NO_INSTALL_UPGRADE",
        "HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK",
        "HOMEBREW_NO_INSTALL_CLEANUP",
    ] {
        if !help
            .split(|c: char| !(c.is_ascii_alphanumeric() || c == '_' || c == '-'))
            .any(|word| word == flag)
        {
            return Err(invalid(
                "Homebrew does not advertise all required install controls; no weaker fallback",
            ));
        }
    }
    for (args, expected) in args[1..4].iter().zip([&b.prefix, &b.repository, &b.cellar]) {
        check()?;
        let observed = path(&query(s, args)?)?;
        check()?;
        if &observed != expected {
            return Err(invalid(
                "Homebrew native layout differs from selected physical scope",
            ));
        }
    }
    check()?;
    let (stable_version, installed_versions) = formula_info(s)?;
    check()?;
    Ok(Probe {
        required_controls_advertised: true,
        prefix: b.prefix.clone(),
        repository: b.repository.clone(),
        cellar: b.cellar.clone(),
        stable_version,
        installed_versions,
    })
}
#[cfg(test)]
mod tests {
    #[test]
    fn root_owned_prefix_never_grants_permission_to_native_write_roots() {
        use super::writable_root;
        let groups = [20];
        assert!(writable_root(0, 0, 0o755, 501, &groups, true));
        assert!(!writable_root(0, 0, 0o755, 501, &groups, false));
        assert!(writable_root(501, 20, 0o755, 501, &groups, false));
        for mode in [0o777, 0o500, 0o555] {
            assert!(!writable_root(501, 20, mode, 501, &groups, false));
        }
        assert!(!writable_root(502, 20, 0o755, 501, &groups, false));
    }
    #[test]
    fn group_write_is_accepted_only_for_the_callers_own_groups() {
        use super::{safe_parent, writable_root};
        let groups = [20, 80];
        assert!(writable_root(501, 20, 0o775, 501, &groups, false));
        assert!(writable_root(501, 80, 0o770, 501, &groups, true));
        assert!(!writable_root(501, 99, 0o770, 501, &groups, false));
        assert!(!writable_root(501, 20, 0o777, 501, &groups, false));
        assert!(writable_root(0, 20, 0o775, 501, &groups, true));
        assert!(!writable_root(0, 99, 0o775, 501, &groups, true));
        assert!(!writable_root(0, 20, 0o775, 501, &groups, false));
        assert!(writable_root(0, 20, 0o755, 501, &groups, true));
        let parent = |uid, gid, mode| crate::observation::Fingerprint {
            device: 0,
            inode: 0,
            links: 0,
            mode,
            uid,
            gid,
            size: 0,
            modified: (0, 0),
            changed: (0, 0),
        };
        assert!(safe_parent(&parent(501, 20, 0o775), 501, &groups));
        assert!(!safe_parent(&parent(501, 99, 0o775), 501, &groups));
        assert!(!safe_parent(&parent(501, 20, 0o777), 501, &groups));
        assert!(safe_parent(&parent(0, 0, 0o755), 501, &groups));
        assert!(!safe_parent(&parent(502, 20, 0o755), 501, &groups));
    }
}
#[derive(Deserialize)]
struct Info {
    formulae: Vec<Formula>,
}
#[derive(Deserialize)]
struct Formula {
    name: String,
    full_name: String,
    tap: String,
    disabled: bool,
    #[serde(default)]
    aliases: Vec<String>,
    #[serde(default)]
    oldnames: Vec<String>,
    installed: Vec<Installed>,
    versions: Versions,
}
#[derive(Deserialize)]
struct Versions {
    stable: Option<String>,
    head: Option<String>,
}
#[derive(Deserialize)]
struct Installed {
    version: String,
}
pub(super) fn formula_info(s: &super::Scope) -> Result<(String, Vec<String>)> {
    let bytes = query(s, &probe_arguments(&s.package)[4])?;
    let mut info: Info = serde_json::from_slice(&bytes)
        .map_err(|_| invalid("invalid Homebrew formula metadata; raw output withheld"))?;
    if info.formulae.len() != 1 {
        return Err(invalid("ambiguous Homebrew formula metadata"));
    }
    let f = info.formulae.remove(0);
    if f.name != s.package
        || (f.full_name != s.package && f.full_name != format!("homebrew/core/{}", s.package))
        || f.tap != "homebrew/core"
        || f.disabled
        || !f.aliases.is_empty()
        || !f.oldnames.is_empty()
        || f.versions
            .stable
            .as_ref()
            .is_none_or(|v| !super::super::valid_observed_version(v))
        || f.installed.len() > 256
    {
        return Err(invalid(
            "Homebrew target is not an available unambiguous canonical core formula",
        ));
    }
    // HEAD availability is not permission to install HEAD; only the fixed stable
    // install protocol is supported. Installed HEAD claims cannot prove presence.
    let mut versions = Vec::new();
    for i in f.installed {
        if !super::super::valid_observed_version(&i.version)
            || i.version.starts_with("HEAD")
            || f.versions.head.as_ref() == Some(&i.version)
        {
            return Err(invalid("unsupported Homebrew installed version metadata"));
        }
        versions.push(i.version);
    }
    versions.sort();
    versions.dedup();
    Ok((f.versions.stable.expect("validated stable"), versions))
}