phoebus_os/installer-rs/crates/phoebus-installer/tests/sys_flaws.rs
2026-09-28 23:42:34 -07:00

197 lines
6.3 KiB
Rust

//! Regression battery for the six `sys` flaws found during the audit.
//!
//! Validation-level cases call `phoebus-core` directly (fast, deterministic);
//! the binary-private phrase parsers and privilege gates run the `sys` binary.
use phoebus_core::action::{parse_action, parse_natural, valid_package_spec, SystemAction};
use std::process::Command;
fn sys() -> Command {
Command::new(env!("CARGO_BIN_EXE_sys"))
}
fn effective_uid_is_root() -> bool {
Command::new("id")
.arg("-u")
.output()
.ok()
.map(|o| String::from_utf8_lossy(&o.stdout).trim() == "0")
.unwrap_or(false)
}
// ── Flaw 1: versioned atoms, slots, wildcards, repositories ────────────────
#[test]
fn versioned_slotted_and_repo_atoms_validate() {
for atom in [
"vim",
"app-editors/vim",
"=app-editors/vim-9.0.2167",
"~net-misc/curl-8.5.0",
">=dev-lang/rust-1.75.0",
"dev-lang/python:3.11",
"dev-libs/openssl:0/1.1",
"sys-devel/llvm:17",
"=app-editors/vim-9.0*",
"sys-apps/portage::gentoo",
"app-editors/vim[python]",
"dev-libs/openssl:0[ssl]",
] {
assert!(valid_package_spec(atom), "atom {atom:?} should be valid");
}
}
#[test]
fn dangerous_specs_are_still_rejected() {
for spec in [
"-C",
"--color=n",
"cat/pkg;rm -rf /",
"cat/pkg&echo pwned",
"cat/pkg|cat /etc/shadow",
"cat/pkg$(id)",
"cat/pkg`id`",
"../etc/passwd",
"a/b/c",
"app/--evil",
"cat/pkg::",
"cat/pkg:",
"cat/pkg[flag",
"",
" ",
" vim",
"vim ",
"app-editors/vim ",
] {
assert!(!valid_package_spec(spec), "spec {spec:?} must be rejected");
}
}
#[test]
fn versioned_atoms_pass_the_json_action_validation_gate() {
for atom in [
"=app-editors/vim-9.0.2167",
"dev-lang/python:3.11",
"sys-apps/portage::gentoo",
"=app-editors/vim-9.0*",
] {
let json = format!(r#"{{"action":"inspect_package","atom":"{atom}"}}"#);
assert!(
parse_action(&json).is_ok(),
"JSON validation wrongly rejected {atom:?}"
);
}
}
// ── Flaw 2: quoted multi-word application names ─────────────────────────────
#[test]
fn quoted_multi_word_install_parses_in_legacy_mode() {
let root = tempfile::tempdir().expect("temp root");
let empty = tempfile::tempdir().expect("empty PATH");
let out = sys()
.env("PATH", empty.path())
.arg("--root")
.arg(root.path())
.args(["install", "\"visual studio code\"", "--legacy", "--flatpak"])
.output()
.expect("run sys");
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
!stderr.contains("could not parse"),
"quoted multi-word install was not parsed: {stderr}"
);
}
#[test]
fn quoted_multi_word_remove_parses_in_legacy_mode() {
let root = tempfile::tempdir().expect("temp root");
let empty = tempfile::tempdir().expect("empty PATH");
let out = sys()
.env("PATH", empty.path())
.arg("--root")
.arg(root.path())
.args(["remove", "\"Google Chrome\"", "--legacy"])
.output()
.expect("run sys");
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
!stderr.contains("could not parse"),
"quoted multi-word remove was not parsed: {stderr}"
);
}
// ── Flaw 5: path normalization in `who owns` ────────────────────────────────
#[test]
fn who_owns_normalizes_a_missing_leading_slash() {
match parse_natural("who owns usr/bin/vim") {
Some(SystemAction::PackageOwners { path }) => assert_eq!(path, "/usr/bin/vim"),
other => panic!("expected a normalized PackageOwners, got {other:?}"),
}
match parse_natural("who owns /usr/bin/vim") {
Some(SystemAction::PackageOwners { path }) => assert_eq!(path, "/usr/bin/vim"),
other => panic!("expected PackageOwners, got {other:?}"),
}
// A relative path is still refused by the JSON contract: normalization is a
// natural-language convenience, not a weaker validation rule.
assert!(parse_action(r#"{"action":"package_owners","path":"relative"}"#).is_err());
}
#[test]
fn who_owns_relative_path_runs_without_a_parse_failure() {
let empty = tempfile::tempdir().expect("empty PATH");
let out = sys()
.env("PATH", empty.path())
.args(["who", "owns", "usr/bin/bash", "--legacy"])
.output()
.expect("run sys");
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
!stderr.contains("invalid file path"),
"relative path was not normalized: {stderr}"
);
}
// ── Flaw 6: service lifecycle privilege gates ──────────────────────────────
#[test]
fn service_mutations_require_root() {
if effective_uid_is_root() {
return;
}
for verb in ["enable", "disable", "start", "stop", "restart"] {
let out = sys()
.args([verb, "sshd", "--legacy"])
.output()
.expect("run sys");
assert!(!out.status.success(), "{verb} should require root");
let combined = format!(
"{}{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
assert!(
combined.contains("root"),
"{verb} refusal must mention root: {combined}"
);
}
}
#[test]
fn service_inspect_is_not_a_privileged_mutation() {
// `inspect` must parse and must not be refused as a mutating action. With an
// empty PATH the read-only probe cannot run, but the refusal (if any) is
// never the root gate and never a parse failure.
let empty = tempfile::tempdir().expect("empty PATH");
let out = sys()
.env("PATH", empty.path())
.args(["inspect", "sshd", "--legacy"])
.output()
.expect("run sys");
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
!stderr.contains("mutating actions require root") && !stderr.contains("could not parse"),
"inspect was wrongly refused: {stderr}"
);
}