955 lines
34 KiB
Rust
955 lines
34 KiB
Rust
//! Adversarial stress battery for `sys`.
|
||
//!
|
||
//! This file systematically tries to break every input surface of `sys` with
|
||
//! hostile, malformed and boundary inputs. It is deliberately conservative in
|
||
//! its expectations: a test asserts a *security invariant* (never crash, never
|
||
//! execute unvalidated input, reject a malformed operand) rather than an
|
||
//! incidental output string.
|
||
//!
|
||
//! Two layers are exercised:
|
||
//!
|
||
//! * the validators and parsers in `phoebus-core::action` directly (fast and
|
||
//! deterministic), and
|
||
//! * the `sys` binary itself as a subprocess, isolated with an empty `PATH` and
|
||
//! no AI provider so nothing on the host is touched or contacted.
|
||
//!
|
||
//! When a case fails it reveals a real gap in validation or routing. Fix the
|
||
//! underlying code, not the assertion.
|
||
|
||
use phoebus_core::action::{
|
||
parse_action, parse_natural, valid_appimage_name, valid_application_name, valid_ebuild_atom,
|
||
valid_ebuild_version, valid_flatpak_app_id, valid_http_url, valid_nix_attribute,
|
||
valid_package_spec, valid_search_query, valid_sha256, valid_snap_channel, valid_snap_name,
|
||
};
|
||
use std::process::{Command, Output};
|
||
|
||
fn sys() -> Command {
|
||
let mut cmd = Command::new(env!("CARGO_BIN_EXE_sys"));
|
||
// Isolate from the host: an empty PATH prevents any real tool from being
|
||
// found, and an unconfigured provider prevents any network call. The
|
||
// `effective_uid` probe fails closed to a non-root sentinel, so privilege
|
||
// gates are exercised deterministically.
|
||
cmd.env("PATH", "/nonexistent")
|
||
.env("PHOEBUS_AI_PROVIDER", "")
|
||
.env("PHOEBUS_AI_API_KEY_FILE", "")
|
||
.env("PHOEBUS_AI_BACKEND", "direct");
|
||
cmd
|
||
}
|
||
|
||
fn action(json: &str) -> Output {
|
||
sys()
|
||
.args(["--legacy", "--action", json])
|
||
.output()
|
||
.expect("run sys")
|
||
}
|
||
|
||
fn run_legacy(args: &[&str]) -> Output {
|
||
let root = tempfile::tempdir().expect("temp root");
|
||
sys()
|
||
.arg("--root")
|
||
.arg(root.path())
|
||
.args(["--legacy"])
|
||
.args(args)
|
||
.output()
|
||
.expect("run sys")
|
||
}
|
||
|
||
/// On Unix a signal death (segfault, abort, stack overflow) leaves `code()`
|
||
/// unset. A panic in `sys` must never happen on any input.
|
||
fn assert_no_crash(out: &Output, context: &str) {
|
||
if out.status.code().is_none() {
|
||
panic!(
|
||
"sys CRASHED (signal death) on: {context}\nstdout: {}\nstderr: {}",
|
||
String::from_utf8_lossy(&out.stdout),
|
||
String::from_utf8_lossy(&out.stderr),
|
||
);
|
||
}
|
||
}
|
||
|
||
/// The input must be refused: no crash, non-zero exit.
|
||
fn assert_rejected(out: &Output, context: &str) {
|
||
assert_no_crash(out, context);
|
||
assert!(
|
||
!out.status.success(),
|
||
"sys accepted a hostile input that must be refused: {context}\nstdout: {}\nstderr: {}",
|
||
String::from_utf8_lossy(&out.stdout),
|
||
String::from_utf8_lossy(&out.stderr),
|
||
);
|
||
}
|
||
|
||
fn accepted(json: &str) -> bool {
|
||
parse_action(json).is_ok()
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
// Category 1 — shell injection through every field type
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
||
/// Payloads that must never survive into any command. Appended to a benign
|
||
/// operand so a version-operator prefix cannot accidentally make it valid.
|
||
const INJECTIONS: &[&str] = &[
|
||
";id", "&&id", "||id", "|id", "$(id)", "`id`", "\nid", ">out", "<in", "\0id", "&id", "\\;id",
|
||
];
|
||
|
||
#[test]
|
||
fn package_atoms_reject_shell_injection() {
|
||
for payload in INJECTIONS {
|
||
let atom = format!("app-misc/hello{payload}");
|
||
assert!(
|
||
!valid_package_spec(&atom),
|
||
"package atom must reject injection: {atom:?}"
|
||
);
|
||
let json = format!(r#"{{"action":"install_package","atom":"{atom}"}}"#);
|
||
assert!(!accepted(&json), "JSON atom must reject injection: {json}");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn flatpak_ids_reject_shell_injection() {
|
||
for payload in INJECTIONS {
|
||
let id = format!("org.evil.Test{payload}");
|
||
assert!(
|
||
!valid_flatpak_app_id(&id),
|
||
"Flatpak id must reject injection: {id:?}"
|
||
);
|
||
let json = format!(r#"{{"action":"flatpak_install","app_id":"{id}"}}"#);
|
||
assert!(!accepted(&json), "JSON Flatpak id must reject: {json}");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn nix_attributes_reject_shell_injection() {
|
||
for payload in INJECTIONS {
|
||
let attr = format!("hello{payload}");
|
||
assert!(
|
||
!valid_nix_attribute(&attr),
|
||
"Nix attribute must reject injection: {attr:?}"
|
||
);
|
||
let json = format!(r#"{{"action":"nix_prepare","packages":["{attr}"]}}"#);
|
||
assert!(!accepted(&json), "JSON Nix attribute must reject: {json}");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn snap_names_reject_shell_injection() {
|
||
for payload in INJECTIONS {
|
||
let name = format!("hello{payload}");
|
||
assert!(
|
||
!valid_snap_name(&name),
|
||
"Snap name must reject injection: {name:?}"
|
||
);
|
||
let json = format!(r#"{{"action":"snap_install","name":"{name}"}}"#);
|
||
assert!(!accepted(&json), "JSON Snap name must reject: {json}");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn service_names_reject_shell_injection() {
|
||
for payload in INJECTIONS {
|
||
let name = format!("sshd{payload}");
|
||
let json = format!(r#"{{"action":"enable_service","name":"{name}"}}"#);
|
||
assert!(!accepted(&json), "service name must reject: {json}");
|
||
let json = format!(r#"{{"action":"restart_service","name":"{name}"}}"#);
|
||
assert!(!accepted(&json), "restart service name must reject: {json}");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn search_queries_reject_shell_injection() {
|
||
for payload in INJECTIONS {
|
||
let query = format!("firefox{payload}");
|
||
assert!(
|
||
!valid_search_query(&query),
|
||
"search query must reject injection: {query:?}"
|
||
);
|
||
let json = format!(r#"{{"action":"search_packages","query":"{query}"}}"#);
|
||
assert!(!accepted(&json), "JSON search query must reject: {json}");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn file_paths_reject_shell_injection() {
|
||
for payload in [";id", "$(id)", "`id`", "|id", "&id", "\\;id", "\0id"] {
|
||
let path = format!("/usr/bin/vim{payload}");
|
||
let json = format!(r#"{{"action":"package_owners","path":"{path}"}}"#);
|
||
assert!(
|
||
!accepted(&json),
|
||
"file path must reject shell metacharacters: {json}"
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn ebuild_atoms_reject_shell_injection() {
|
||
for payload in INJECTIONS {
|
||
let atom = format!("app-misc/hello{payload}");
|
||
assert!(
|
||
!valid_ebuild_atom(&atom),
|
||
"ebuild atom must reject injection: {atom:?}"
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn appimage_url_rejects_whitespace_and_control() {
|
||
for url in [
|
||
"https://evil.test/a b",
|
||
"https://evil.test/a\nb",
|
||
"https://evil.test/a\tb",
|
||
"https://evil.test/a\0b",
|
||
"ftp://evil.test/a",
|
||
"javascript:alert(1)",
|
||
"file:///etc/passwd",
|
||
"",
|
||
] {
|
||
assert!(!valid_http_url(url), "URL must be refused: {url:?}");
|
||
}
|
||
assert!(valid_http_url("https://example.test/app.AppImage"));
|
||
}
|
||
|
||
/// End-to-end: the full binary must refuse injected operands (the validator
|
||
/// gate is the last line of defence if a command ever gains a shell).
|
||
#[test]
|
||
fn binary_refuses_injection_actions() {
|
||
let cases = [
|
||
r#"{"action":"install_package","atom":"app-misc/hello;id"}"#,
|
||
r#"{"action":"flatpak_install","app_id":"org.evil.Test$(id)"}"#,
|
||
r#"{"action":"nix_prepare","packages":["hello`id`"]}"#,
|
||
r#"{"action":"snap_install","name":"hello|id"}"#,
|
||
r#"{"action":"enable_service","name":"sshd;id"}"#,
|
||
r#"{"action":"search_packages","query":"firefox&&id"}"#,
|
||
r#"{"action":"package_owners","path":"/usr/bin/vim;id"}"#,
|
||
];
|
||
for json in cases {
|
||
assert_rejected(&action(json), &format!("injection via binary: {json}"));
|
||
}
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
// Category 2 — empty, null and missing inputs
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
||
#[test]
|
||
fn empty_operands_are_refused() {
|
||
let cases = [
|
||
r#"{"action":"install_package","atom":""}"#,
|
||
r#"{"action":"remove_package","atom":""}"#,
|
||
r#"{"action":"flatpak_install","app_id":""}"#,
|
||
r#"{"action":"flatpak_remove","app_id":""}"#,
|
||
r#"{"action":"nix_prepare","packages":[]}"#,
|
||
r#"{"action":"snap_install","name":""}"#,
|
||
r#"{"action":"snap_remove","name":""}"#,
|
||
r#"{"action":"enable_service","name":""}"#,
|
||
r#"{"action":"search_packages","query":""}"#,
|
||
r#"{"action":"package_owners","path":""}"#,
|
||
r#"{"action":"package_info","package":""}"#,
|
||
r#"{"action":"snap_info","name":""}"#,
|
||
r#"{"action":"appimage_acquire","name":"","url":"https://e.test/a","sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}"#,
|
||
r#"{"action":"undo","snapshot":""}"#,
|
||
];
|
||
for json in cases {
|
||
assert!(!accepted(json), "empty operand must be refused: {json}");
|
||
assert_rejected(&action(json), &format!("empty operand via binary: {json}"));
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn null_bytes_are_refused_in_every_text_field() {
|
||
let cases = [
|
||
r#"{"action":"install_package","atom":"app-misc/hello\u0000evil"}"#,
|
||
r#"{"action":"enable_service","name":"sshd\u0000evil"}"#,
|
||
r#"{"action":"package_owners","path":"/etc/passwd\u0000"}"#,
|
||
r#"{"action":"flatpak_install","app_id":"org.evil\u0000.Test"}"#,
|
||
r#"{"action":"search_packages","query":"firefox\u0000evil"}"#,
|
||
];
|
||
for json in cases {
|
||
assert!(!accepted(json), "NUL byte must be refused: {json}");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn no_positional_arguments_does_not_crash() {
|
||
let out = run_legacy(&[]);
|
||
assert_no_crash(&out, "empty invocation");
|
||
assert!(!out.status.success(), "an empty instruction must fail");
|
||
}
|
||
|
||
#[test]
|
||
fn empty_positional_string_does_not_crash() {
|
||
let out = run_legacy(&[""]);
|
||
assert_no_crash(&out, "empty string operand");
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
// Category 3 — extreme lengths
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
||
#[test]
|
||
fn overlong_operands_are_refused() {
|
||
assert!(!valid_package_spec(&format!(
|
||
"app-misc/{}",
|
||
"a".repeat(10_000)
|
||
)));
|
||
assert!(!valid_search_query(&"x".repeat(10_000)));
|
||
assert!(!valid_http_url(&format!(
|
||
"https://e.test/{}",
|
||
"a".repeat(10_000)
|
||
)));
|
||
assert!(!valid_appimage_name(&"a".repeat(65)));
|
||
assert!(!valid_flatpak_app_id(&format!("org.{}", "a".repeat(260))));
|
||
assert!(!valid_nix_attribute(&"a".repeat(129)));
|
||
assert!(!valid_ebuild_version(&"1".repeat(65)));
|
||
|
||
let long_path = format!("/{}", "a/".repeat(5_000));
|
||
let json = format!(r#"{{"action":"package_owners","path":"{long_path}"}}"#);
|
||
assert!(!accepted(&json), "over-long path must be refused");
|
||
}
|
||
|
||
#[test]
|
||
fn too_many_nix_packages_are_refused() {
|
||
let packages: Vec<String> = (0..40).map(|i| format!("\"pkg{i}\"")).collect();
|
||
let json = format!(
|
||
r#"{{"action":"nix_prepare","packages":[{}]}}"#,
|
||
packages.join(",")
|
||
);
|
||
assert!(
|
||
!accepted(&json),
|
||
"more than 32 Nix packages must be refused"
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn extremely_long_natural_phrase_does_not_crash() {
|
||
let phrase = format!("install {}", "a".repeat(10_000));
|
||
let out = run_legacy(&[&phrase]);
|
||
assert_no_crash(&out, "10K natural phrase");
|
||
}
|
||
|
||
#[test]
|
||
fn many_positional_arguments_do_not_crash() {
|
||
let words: Vec<String> = (0..1_000).map(|_| "word".to_string()).collect();
|
||
let refs: Vec<&str> = words.iter().map(String::as_str).collect();
|
||
let out = run_legacy(&refs);
|
||
assert_no_crash(&out, "1000 positional arguments");
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
// Category 4 — Unicode, homoglyphs and invisible characters
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
||
#[test]
|
||
fn unicode_package_atoms_are_refused() {
|
||
for atom in [
|
||
"app-misc/héllo",
|
||
"app-misc/hello™",
|
||
"app-misc/hello🔥",
|
||
"категория/пакет",
|
||
"app-misc/hello\u{200B}evil", // zero-width space
|
||
"app-misc/hello\u{FEFF}evil", // BOM
|
||
"app-misc/hello\u{202E}evil", // RTL override
|
||
"app-misc/hello\u{0301}", // combining accent
|
||
] {
|
||
assert!(!valid_package_spec(atom), "atom must be refused: {atom:?}");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn homoglyph_package_atom_is_refused() {
|
||
// Cyrillic 'а' (U+0430) is visually identical to Latin 'a'.
|
||
assert!(!valid_package_spec("\u{0430}pp-misc/hello"));
|
||
}
|
||
|
||
#[test]
|
||
fn unicode_natural_language_does_not_crash() {
|
||
for phrase in [
|
||
"install 🔥🔥🔥",
|
||
"search категория",
|
||
"who owns /tmp/héllo",
|
||
"enable sshd™",
|
||
"install app-misc/héllo",
|
||
] {
|
||
let out = run_legacy(&[phrase]);
|
||
assert_no_crash(&out, &format!("unicode natural phrase: {phrase:?}"));
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn unicode_in_each_typed_operand_is_refused() {
|
||
let cases = [
|
||
r#"{"action":"search_packages","query":"fïrefox"}"#,
|
||
r#"{"action":"flatpak_install","app_id":"org.mözilla.Firefox"}"#,
|
||
r#"{"action":"snap_install","name":"hellö"}"#,
|
||
r#"{"action":"enable_service","name":"sshð"}"#,
|
||
r#"{"action":"nix_prepare","packages":["héllo"]}"#,
|
||
];
|
||
for json in cases {
|
||
assert!(!accepted(json), "unicode operand must be refused: {json}");
|
||
}
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
// Category 5 — path traversal
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
||
#[test]
|
||
fn path_traversal_package_atoms_are_refused() {
|
||
for atom in [
|
||
"../etc/passwd",
|
||
"../../root/.ssh/id_rsa",
|
||
"app-misc/../../../etc/shadow",
|
||
"./relative/path",
|
||
"cat/pkg/../../../etc/shadow",
|
||
"cat/pkg/..",
|
||
"cat/../pkg",
|
||
] {
|
||
assert!(!valid_package_spec(atom), "atom must be refused: {atom:?}");
|
||
}
|
||
assert!(valid_package_spec("app-editors/vim"));
|
||
}
|
||
|
||
#[test]
|
||
fn path_traversal_in_ebuild_atoms_is_refused() {
|
||
for atom in ["../../etc/passwd", "a/b/c", "/bin/sh", "a//b", "../pkg"] {
|
||
assert!(
|
||
!valid_ebuild_atom(atom),
|
||
"ebuild atom must be refused: {atom:?}"
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn path_traversal_in_application_names_is_refused() {
|
||
// An application name is not a path: `..` components and a leading slash
|
||
// could otherwise escape a managed directory when a record is resolved.
|
||
for name in [
|
||
"../../etc/passwd",
|
||
"/etc/passwd",
|
||
"a/../../etc",
|
||
"foo/..",
|
||
"foo/./bar",
|
||
"a//b",
|
||
] {
|
||
assert!(
|
||
!valid_application_name(name),
|
||
"application name must be refused: {name:?}"
|
||
);
|
||
}
|
||
assert!(valid_application_name("firefox"));
|
||
assert!(valid_application_name("org.mozilla.firefox"));
|
||
assert!(valid_application_name("app-editors/vim"));
|
||
}
|
||
|
||
#[test]
|
||
fn path_traversal_in_file_paths_is_refused() {
|
||
for path in ["/etc/../../../root/.ssh/id_rsa", "/tmp/../../etc/shadow"] {
|
||
let json = format!(r#"{{"action":"package_owners","path":"{path}"}}"#);
|
||
assert!(!accepted(&json), "traversal path must be refused: {path:?}");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn path_traversal_in_appimage_names_is_refused() {
|
||
for name in ["../evil", "../../root", ".hidden", "a/../../etc", ".."] {
|
||
assert!(
|
||
!valid_appimage_name(name),
|
||
"AppImage name must be refused: {name:?}"
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn path_traversal_in_custom_ebuild_json_is_refused() {
|
||
let json = r#"{"action":"custom_ebuild_prepare","atom":"../../etc/passwd","version":"1","rationale":"test","ebuild":"EAPI=8"}"#;
|
||
assert!(!accepted(json), "traversal ebuild atom must be refused");
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
// Category 6 — option / flag injection
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
||
#[test]
|
||
fn leading_dash_operands_are_refused() {
|
||
let cases = [
|
||
(r#"{"action":"install_package","atom":"-C"}"#, "atom"),
|
||
(
|
||
r#"{"action":"install_package","atom":"--config"}"#,
|
||
"atom-long",
|
||
),
|
||
(r#"{"action":"remove_package","atom":"-rf"}"#, "remove"),
|
||
(r#"{"action":"flatpak_install","app_id":"-y"}"#, "flatpak"),
|
||
(r#"{"action":"nix_prepare","packages":["--impure"]}"#, "nix"),
|
||
(r#"{"action":"snap_install","name":"--dangerous"}"#, "snap"),
|
||
(r#"{"action":"enable_service","name":"--user"}"#, "service"),
|
||
(r#"{"action":"search_packages","query":"--help"}"#, "search"),
|
||
(r#"{"action":"package_info","package":"--exec"}"#, "info"),
|
||
(
|
||
r#"{"action":"package_dependencies","package":"-R"}"#,
|
||
"deps",
|
||
),
|
||
(r#"{"action":"appimage_info","name":"-x"}"#, "appimage"),
|
||
];
|
||
for (json, ctx) in cases {
|
||
assert!(
|
||
!accepted(json),
|
||
"option injection ({ctx}) must be refused: {json}"
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn search_query_allows_only_safe_punctuation() {
|
||
for query in [
|
||
"firefox",
|
||
"google-chrome",
|
||
"visual studio code",
|
||
"org.mozilla.firefox",
|
||
"python@3.11",
|
||
"app-arch/p7zip",
|
||
] {
|
||
assert!(valid_search_query(query), "safe query rejected: {query:?}");
|
||
}
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
// Category 7 — malformed JSON actions
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
||
#[test]
|
||
fn malformed_json_is_refused_without_crashing() {
|
||
let cases = [
|
||
"",
|
||
"not json at all",
|
||
"{",
|
||
"{}",
|
||
"[]",
|
||
"null",
|
||
"42",
|
||
"true",
|
||
r#"{"action":null}"#,
|
||
r#"{"action":42}"#,
|
||
r#"{"action":""}"#,
|
||
r#"{"action":"install_package"}"#,
|
||
r#"{"action":"install_package","atom":42}"#,
|
||
r#"{"action":"install_package","atom":"vim","extra":"field"}"#,
|
||
r#"{"action":"history","x":1}"#,
|
||
r#"{"action":"run_shell","command":"rm -rf /"}"#,
|
||
];
|
||
for json in cases {
|
||
assert!(
|
||
!accepted(json),
|
||
"malformed action must be refused: {json:?}"
|
||
);
|
||
assert_rejected(
|
||
&action(json),
|
||
&format!("malformed JSON via binary: {json:?}"),
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn deeply_nested_json_does_not_crash() {
|
||
let mut json = String::from(r#"{"action":"install_package","atom":"#);
|
||
for _ in 0..400 {
|
||
json.push_str(r#"{"n":"#);
|
||
}
|
||
json.push_str(r#""leaf""#);
|
||
for _ in 0..400 {
|
||
json.push('}');
|
||
}
|
||
json.push('}');
|
||
assert!(!accepted(&json), "deeply nested JSON must be refused");
|
||
assert_no_crash(&action(&json), "deeply nested JSON");
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
// Category 8 — CLI flag combinations and routing
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
||
#[test]
|
||
fn conflicting_backend_flags_are_refused() {
|
||
let flags = [
|
||
"--portage",
|
||
"--flatpak",
|
||
"--nix",
|
||
"--snap",
|
||
"--appimage",
|
||
"--ebuild",
|
||
];
|
||
for (i, a) in flags.iter().enumerate() {
|
||
for b in &flags[i + 1..] {
|
||
let out = sys()
|
||
.args(["install", "firefox", a, b])
|
||
.output()
|
||
.expect("run sys");
|
||
assert_rejected(&out, &format!("conflicting backends {a} {b}"));
|
||
}
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn all_backend_flags_at_once_are_refused() {
|
||
let out = sys()
|
||
.args([
|
||
"install",
|
||
"firefox",
|
||
"--portage",
|
||
"--flatpak",
|
||
"--nix",
|
||
"--snap",
|
||
"--appimage",
|
||
"--ebuild",
|
||
])
|
||
.output()
|
||
.expect("run sys");
|
||
assert_rejected(&out, "all backends at once");
|
||
}
|
||
|
||
#[test]
|
||
fn backend_flag_without_a_verb_is_refused() {
|
||
for flag in [
|
||
"--portage",
|
||
"--flatpak",
|
||
"--nix",
|
||
"--snap",
|
||
"--appimage",
|
||
"--ebuild",
|
||
] {
|
||
let out = run_legacy(&["history", flag]);
|
||
assert_rejected(&out, &format!("backend flag without a verb: {flag}"));
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn typed_action_requires_legacy_or_broker() {
|
||
let out = sys()
|
||
.args(["--action", r#"{"action":"history"}"#])
|
||
.output()
|
||
.expect("run sys");
|
||
assert_rejected(&out, "--action without --legacy/--broker");
|
||
}
|
||
|
||
#[test]
|
||
fn repeated_legacy_flag_does_not_crash() {
|
||
let out = run_legacy(&["--legacy", "history"]);
|
||
// `--legacy` is forwarded here as a second occurrence via args; clap may
|
||
// accept or refuse it, but it must not crash either way.
|
||
assert_no_crash(&out, "double --legacy");
|
||
}
|
||
|
||
#[test]
|
||
fn version_with_extra_arguments_does_not_crash() {
|
||
let out = sys()
|
||
.args(["--version", "extra", "args"])
|
||
.output()
|
||
.expect("run sys");
|
||
assert_no_crash(&out, "--version with extra args");
|
||
}
|
||
|
||
#[test]
|
||
fn execute_plan_without_broker_is_refused() {
|
||
let out = sys()
|
||
.args(["--execute-plan", "plan-inst-deadbeef"])
|
||
.output()
|
||
.expect("run sys");
|
||
assert_rejected(&out, "--execute-plan without --broker");
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
// Category 9 — natural-language edge cases
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
||
#[test]
|
||
fn natural_language_variants_do_not_crash() {
|
||
let phrases = [
|
||
"INSTALL FIREFOX",
|
||
"Install Firefox",
|
||
"iNsTaLl FiReFoX",
|
||
"HISTORY",
|
||
" install firefox ",
|
||
"install\tfirefox",
|
||
"search \t firefox",
|
||
" history ",
|
||
"who owns /usr/bin/bash",
|
||
"please install firefox",
|
||
"Please please install firefox",
|
||
"can you install firefox",
|
||
"I want to install firefox",
|
||
"could you please install firefox for me",
|
||
];
|
||
for phrase in phrases {
|
||
let out = run_legacy(&[phrase]);
|
||
assert_no_crash(&out, &format!("natural phrase: {phrase:?}"));
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn ambiguous_verbs_do_not_crash_and_do_not_invent_actions() {
|
||
for phrase in [
|
||
"install", "remove", "search", "enable", "who owns", "info", "undo",
|
||
] {
|
||
let out = run_legacy(&[phrase]);
|
||
assert_no_crash(&out, &format!("ambiguous verb: {phrase:?}"));
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn typo_phrases_do_not_crash() {
|
||
for phrase in [
|
||
"instal firefox",
|
||
"instll firefox",
|
||
"remov firefox",
|
||
"serach firefox",
|
||
"histroy",
|
||
"updte",
|
||
"enble sshd",
|
||
] {
|
||
let out = run_legacy(&[phrase]);
|
||
assert_no_crash(&out, &format!("typo phrase: {phrase:?}"));
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn natural_parser_is_case_insensitive_and_trims() {
|
||
use phoebus_core::action::SystemAction;
|
||
assert_eq!(
|
||
parse_natural("HISTORY"),
|
||
Some(SystemAction::History),
|
||
"case must be folded"
|
||
);
|
||
assert_eq!(
|
||
parse_natural(" install vim "),
|
||
Some(SystemAction::InstallPackage {
|
||
atom: "vim".into(),
|
||
binary: None,
|
||
})
|
||
);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
// Category 10 — boundary values
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
||
#[test]
|
||
fn snap_name_boundaries() {
|
||
assert!(valid_snap_name("a"));
|
||
assert!(valid_snap_name(&"a".repeat(40)));
|
||
assert!(!valid_snap_name(&"a".repeat(41)));
|
||
assert!(valid_snap_name("a-b"));
|
||
assert!(!valid_snap_name("a--b"));
|
||
assert!(!valid_snap_name("-leading"));
|
||
assert!(!valid_snap_name("trailing-"));
|
||
assert!(!valid_snap_name("Uppercase"));
|
||
assert!(!valid_snap_name(""));
|
||
}
|
||
|
||
#[test]
|
||
fn sha256_boundaries() {
|
||
assert!(valid_sha256(&"a".repeat(64)));
|
||
assert!(!valid_sha256(&"a".repeat(63)));
|
||
assert!(!valid_sha256(&"a".repeat(65)));
|
||
assert!(!valid_sha256(&"0".repeat(64)));
|
||
// Uppercase hex is still valid hex; the digest is compared as given.
|
||
assert!(valid_sha256(&"A".repeat(64)));
|
||
assert!(!valid_sha256(&"g".repeat(64)));
|
||
assert!(!valid_sha256(""));
|
||
}
|
||
|
||
#[test]
|
||
fn service_name_boundaries() {
|
||
let cases = [
|
||
"sshd",
|
||
"sshd.service",
|
||
"user@1000.service",
|
||
"dbus-org.freedesktop.NetworkManager.service",
|
||
"a",
|
||
];
|
||
for unit in cases {
|
||
assert!(
|
||
accepted(&format!(
|
||
r#"{{"action":"inspect_service","name":"{unit}"}}"#
|
||
)),
|
||
"valid service name rejected: {unit:?}"
|
||
);
|
||
}
|
||
// The shared token validator caps a service name at 128 characters.
|
||
assert!(
|
||
!accepted(&format!(
|
||
r#"{{"action":"inspect_service","name":"{}"}}"#,
|
||
"a".repeat(257)
|
||
)),
|
||
"over-long service name must be refused"
|
||
);
|
||
// A path-like unit is refused.
|
||
assert!(!accepted(
|
||
r#"{"action":"enable_service","name":"../etc/shadow"}"#
|
||
));
|
||
}
|
||
|
||
#[test]
|
||
fn flatpak_id_boundaries() {
|
||
assert!(valid_flatpak_app_id("org.mozilla.Firefox"));
|
||
assert!(valid_flatpak_app_id("com.google.Chrome"));
|
||
assert!(!valid_flatpak_app_id("nodots"));
|
||
assert!(!valid_flatpak_app_id("org."));
|
||
assert!(!valid_flatpak_app_id(".org.test"));
|
||
assert!(!valid_flatpak_app_id("org..test"));
|
||
assert!(!valid_flatpak_app_id(&format!("org.{}", "a".repeat(260))));
|
||
assert!(!valid_flatpak_app_id(""));
|
||
}
|
||
|
||
#[test]
|
||
fn nix_attribute_boundaries() {
|
||
assert!(valid_nix_attribute("nixpkgs.hello"));
|
||
assert!(valid_nix_attribute("python311"));
|
||
assert!(valid_nix_attribute("hello"));
|
||
assert!(valid_nix_attribute("python311Packages.requests"));
|
||
assert!(!valid_nix_attribute(""));
|
||
assert!(!valid_nix_attribute("github:owner/repo"));
|
||
assert!(!valid_nix_attribute("nixpkgs#x/y"));
|
||
assert!(!valid_nix_attribute(&"a".repeat(129)));
|
||
assert!(!valid_nix_attribute("--impure"));
|
||
}
|
||
|
||
#[test]
|
||
fn snap_channel_boundaries() {
|
||
assert!(valid_snap_channel("stable"));
|
||
assert!(valid_snap_channel("latest/stable"));
|
||
assert!(valid_snap_channel("latest/stable/hotfix-1"));
|
||
assert!(!valid_snap_channel("latest/stable/edge/extra"));
|
||
assert!(!valid_snap_channel("-edge"));
|
||
assert!(!valid_snap_channel("Stable"));
|
||
assert!(!valid_snap_channel(""));
|
||
}
|
||
|
||
#[test]
|
||
fn ebuild_atom_and_version_boundaries() {
|
||
assert!(valid_ebuild_atom("app-misc/hello"));
|
||
assert!(!valid_ebuild_atom("hello"));
|
||
assert!(!valid_ebuild_atom("a/b/c"));
|
||
assert!(!valid_ebuild_atom("-bad/pkg"));
|
||
assert!(!valid_ebuild_atom("app-misc/"));
|
||
assert!(valid_ebuild_version("1.2.3"));
|
||
assert!(valid_ebuild_version("1.2.3-r1"));
|
||
assert!(!valid_ebuild_version(""));
|
||
assert!(!valid_ebuild_version("1/2"));
|
||
assert!(!valid_ebuild_version("-1"));
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
// Category 11 — privilege and state edge cases
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
||
/// The isolated `sys` always sees a non-root effective uid, so every mutating
|
||
/// action must be refused before any effect is resolved.
|
||
#[test]
|
||
fn mutating_actions_require_root() {
|
||
let actions = [
|
||
r#"{"action":"install_package","atom":"app-misc/hello"}"#,
|
||
r#"{"action":"remove_package","atom":"app-misc/hello"}"#,
|
||
r#"{"action":"flatpak_install","app_id":"org.test.Test"}"#,
|
||
r#"{"action":"flatpak_remove","app_id":"org.test.Test"}"#,
|
||
r#"{"action":"nix_prepare","packages":["hello"]}"#,
|
||
r#"{"action":"snap_install","name":"hello"}"#,
|
||
r#"{"action":"snap_remove","name":"hello"}"#,
|
||
r#"{"action":"enable_service","name":"sshd"}"#,
|
||
r#"{"action":"disable_service","name":"sshd"}"#,
|
||
r#"{"action":"start_service","name":"sshd"}"#,
|
||
r#"{"action":"stop_service","name":"sshd"}"#,
|
||
r#"{"action":"restart_service","name":"sshd"}"#,
|
||
r#"{"action":"apply_updates"}"#,
|
||
r#"{"action":"depclean"}"#,
|
||
r#"{"action":"clean_cache"}"#,
|
||
r#"{"action":"undo"}"#,
|
||
r#"{"action":"convert_to_source","app":"firefox"}"#,
|
||
r#"{"action":"build_tuned_kernel","make_default":false}"#,
|
||
];
|
||
for json in actions {
|
||
let out = action(json);
|
||
assert_rejected(&out, &format!("non-root mutation must be refused: {json}"));
|
||
let stderr = String::from_utf8_lossy(&out.stderr);
|
||
assert!(
|
||
stderr.contains("require root"),
|
||
"refusal must name root for {json}: {stderr}"
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn read_only_actions_are_not_refused_as_mutations() {
|
||
let actions = [
|
||
r#"{"action":"history"}"#,
|
||
r#"{"action":"rollback_status"}"#,
|
||
r#"{"action":"appimage_list"}"#,
|
||
r#"{"action":"custom_ebuild_list"}"#,
|
||
r#"{"action":"flatpak_list"}"#,
|
||
r#"{"action":"snap_list"}"#,
|
||
r#"{"action":"list_packages"}"#,
|
||
r#"{"action":"check_updates"}"#,
|
||
];
|
||
for json in actions {
|
||
let out = action(json);
|
||
assert_no_crash(&out, &format!("read-only action: {json}"));
|
||
let stderr = String::from_utf8_lossy(&out.stderr);
|
||
assert!(
|
||
!stderr.contains("mutating actions require root"),
|
||
"read-only action wrongly gated as a mutation: {json}: {stderr}"
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn unusual_root_paths_do_not_crash() {
|
||
// A non-existent root.
|
||
let out = sys()
|
||
.args([
|
||
"--root",
|
||
"/nonexistent/path/that/does/not/exist",
|
||
"--legacy",
|
||
"history",
|
||
])
|
||
.output()
|
||
.expect("run sys");
|
||
assert_no_crash(&out, "non-existent root");
|
||
|
||
// A file used as a root.
|
||
let out = sys()
|
||
.args(["--root", "/etc/passwd", "--legacy", "history"])
|
||
.output()
|
||
.expect("run sys");
|
||
assert_no_crash(&out, "file as root");
|
||
|
||
// A symlink used as a root.
|
||
let dir = tempfile::tempdir().expect("temp dir");
|
||
let link = dir.path().join("link");
|
||
std::os::unix::fs::symlink("/tmp", &link).expect("symlink");
|
||
let out = sys()
|
||
.arg("--root")
|
||
.arg(link.to_str().expect("utf-8 path"))
|
||
.args(["--legacy", "list"])
|
||
.output()
|
||
.expect("run sys");
|
||
assert_no_crash(&out, "symlink as root");
|
||
}
|
||
|
||
#[test]
|
||
fn adding_root_to_a_mutating_action_does_not_weaken_the_gate() {
|
||
// Even with an explicit `--root`, the privileged gate is tied to the
|
||
// effective uid, not to a caller-supplied path.
|
||
let root = tempfile::tempdir().expect("temp root");
|
||
let out = sys()
|
||
.arg("--root")
|
||
.arg(root.path())
|
||
.args([
|
||
"--legacy",
|
||
"--action",
|
||
r#"{"action":"install_package","atom":"app-misc/hello"}"#,
|
||
])
|
||
.output()
|
||
.expect("run sys");
|
||
assert_rejected(&out, "mutating action with a caller root");
|
||
assert!(
|
||
String::from_utf8_lossy(&out.stderr).contains("require root"),
|
||
"stderr: {}",
|
||
String::from_utf8_lossy(&out.stderr)
|
||
);
|
||
}
|