Skip to main content

amont_runtime/
attest.rs

1//! The signed successor to [`crate::gate_stamp`]: an attestation CI can trust.
2//!
3//! `gate_stamp` answers a LOCAL question — "did the push gate already run on
4//! this commit?" — and its notes deliberately never leave the machine, because
5//! an unsigned note is only as honest as whoever can write the ref. This
6//! module answers the REMOTE version of the same question: "may CI skip a
7//! test job because the equivalent gate already passed here?" — and for that
8//! the note has to travel, so it has to be signed.
9//!
10//! Shape of the note, attached to each pushed tip in `refs/notes/amont-attest`:
11//!
12//! ```text
13//! amont-attest-v1
14//! tree <tree the gates ran against>
15//! gates <names of the pre-push checks that PASSED>
16//! amont <version that produced it>
17//!
18//! -----BEGIN SSH SIGNATURE-----
19//! …signature over the four lines above…
20//! -----END SSH SIGNATURE-----
21//! ```
22//!
23//! The signature covers the **tree**, not the commit: tests read content, not
24//! messages, so a reword or a tree-preserving rebase keeps its attestation —
25//! the same reasoning as `gate_stamp`'s tree binding. CI's skip condition is
26//! tree equality with its own checkout plus a valid signature over exactly
27//! that tree, verified with stock `ssh-keygen -Y verify` against an
28//! `allowed_signers` file committed in the consuming repository. amont itself
29//! still never runs in CI (`docs/ci.md`) — CI verifies a document.
30//!
31//! Signing uses `ssh-keygen -Y sign` as a subprocess, like every other tool
32//! this crate talks to. Hand-rolling ed25519 in a zero-dependency crate would
33//! be the one thing worse than a dependency.
34//!
35//! Every failure mode points the same direction as `gate_stamp`'s: no key, a
36//! signer that errors, a note git refused, a notes push the remote rejected —
37//! all mean "no attestation", and no attestation means CI RUNS the tests.
38//! Nothing here can let an untested tree skip CI; it can only cost a
39//! redundant run.
40//!
41//! One sharp edge is the notes push itself: `git push` from inside pre-push
42//! runs pre-push again. The child carries [`PUSH_GUARD`] in its environment
43//! and the dispatcher yields immediately when it sees it — checking a ref
44//! list that is only ever `refs/notes/amont-attest` would be work spent
45//! proving nothing.
46
47use std::path::PathBuf;
48use std::process::{Command, Stdio};
49
50use crate::pushrefs::PushRef;
51
52/// First token of every note body. Versioned like `gate_stamp::FORMAT`: a
53/// future amont that changes the payload bumps this, and CI's verifier reads
54/// an unknown version as "no attestation".
55///
56/// v1 → v2 added the `platform` line. The bump is the point: a v1 verifier
57/// has no idea the tests it is about to skip ran on a different operating
58/// system, and reading v2 as unknown makes it run them. Fail-safe in the
59/// only direction this module ever fails.
60pub const FORMAT: &str = "amont-attest-v2";
61
62/// The notes ref, spelled the way `git notes --ref` wants it.
63pub const NOTES_REF: &str = "amont-attest";
64
65/// The same ref, fully qualified — the push refspec and `update-ref -d` both
66/// need it.
67pub const NOTES_FULL_REF: &str = "refs/notes/amont-attest";
68
69/// The `ssh-keygen -Y` namespace, on both the signing and verifying side.
70/// Namespaces exist so a signature minted for one purpose cannot be replayed
71/// for another; an `allowed_signers` entry pinned to this namespace accepts
72/// nothing else.
73pub const NAMESPACE: &str = "amont-attest";
74
75/// Environment marker carried by the notes push so the recursive pre-push
76/// invocation stands down. See the module doc.
77pub const PUSH_GUARD: &str = "AMONT_ATTEST_PUSH";
78
79/// The opt-in switch. Off by default: an attestation is a statement to
80/// another system, and amont does not speak for a repository that never
81/// asked it to.
82const TOGGLE: &str = "amont.attest";
83
84/// Where the signing key lives when the repository does not say.
85const KEY_CONFIG: &str = "amont.attestKey";
86const KEY_DEFAULT: &str = ".ssh/amont-attest";
87
88/// Is the recursive-push marker set on THIS invocation?
89pub fn push_guard_active() -> bool {
90    std::env::var_os(PUSH_GUARD).is_some()
91}
92
93/// Has this repository opted in?
94pub fn enabled() -> bool {
95    crate::config::boolean_or(TOGGLE, false)
96}
97
98/// The signing key path: `amont.attestKey`, else `~/.ssh/amont-attest`.
99///
100/// Read like `amont.knownIdentity` is — a raw string through git, unset
101/// collapsing to the default — because a path has no shape git could
102/// validate for us anyway.
103fn key_path() -> Option<PathBuf> {
104    if let Some(k) = crate::git::stdout(&["config", "--get", KEY_CONFIG]) {
105        if !k.is_empty() {
106            return Some(PathBuf::from(k));
107        }
108    }
109    let home = std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE"))?;
110    Some(PathBuf::from(home).join(KEY_DEFAULT))
111}
112
113/// Where a suite ran, as `<arch>-<os>` — `aarch64-macos`, `x86_64-linux`,
114/// `x86_64-windows`.
115///
116/// Coarser than a target triple on purpose: the libc flavour is not
117/// something `std` can answer, and the question a CI matrix actually asks is
118/// "did this run on MY leg". Coarse and honest beats precise and guessed.
119pub fn platform() -> String {
120    format!("{}-{}", std::env::consts::ARCH, std::env::consts::OS)
121}
122
123/// The exact bytes the signature covers. One datum per line, trailing
124/// newline included — CI reconstructs this from the note text, so the shape
125/// is a contract, not a convenience.
126///
127/// `platform` is signed alongside the gates because a pass is a pass **on
128/// something**: `cargo test` green on an arm64 Mac says nothing about the
129/// Windows leg of a matrix, and a note that omitted where it ran invited
130/// exactly that skip.
131pub fn payload(tree: &str, gates: &[String]) -> String {
132    format!(
133        "{FORMAT}\ntree {tree}\ngates {}\nplatform {}\namont {}\n",
134        gates.join(" "),
135        platform(),
136        env!("CARGO_PKG_VERSION")
137    )
138}
139
140/// `ssh-keygen -Y sign` over `payload`, armored signature back. `None` for
141/// every failure — a missing binary, a missing key, a signer that said no —
142/// because an attestation we cannot mint is simply one CI never sees.
143fn sign(payload: &str, key: &std::path::Path) -> Option<String> {
144    use std::io::Write;
145    let mut child = Command::new("ssh-keygen")
146        .args(["-Y", "sign", "-n", NAMESPACE, "-f"])
147        .arg(key)
148        .stdin(Stdio::piped())
149        .stdout(Stdio::piped())
150        .stderr(Stdio::null())
151        .spawn()
152        .ok()?;
153    child.stdin.take()?.write_all(payload.as_bytes()).ok()?;
154    let out = child.wait_with_output().ok()?;
155    if !out.status.success() {
156        return None;
157    }
158    let sig = String::from_utf8_lossy(&out.stdout).trim().to_string();
159    sig.starts_with("-----BEGIN SSH SIGNATURE-----")
160        .then_some(sig)
161}
162
163/// `ssh-keygen -Y verify`: is `sig` a valid signature over `payload` by a
164/// `principal` key listed in `allowed_signers` for our namespace?
165///
166/// The runtime never gates on this — CI verifies with its own stock tooling —
167/// but owning the verifying half keeps the roundtrip honest in tests and
168/// gives a future `amont attest verify` its engine.
169pub fn verify(
170    payload: &str,
171    sig: &str,
172    allowed_signers: &std::path::Path,
173    principal: &str,
174) -> bool {
175    use std::io::Write;
176    // -Y verify takes the signature as a FILE; the payload rides stdin.
177    let sig_file = std::env::temp_dir().join(format!(
178        "amont-attest-verify-{}-{:p}.sig",
179        std::process::id(),
180        &sig
181    ));
182    if std::fs::write(&sig_file, format!("{sig}\n")).is_err() {
183        return false;
184    }
185    let ok = (|| {
186        let mut child = Command::new("ssh-keygen")
187            .args(["-Y", "verify", "-n", NAMESPACE, "-I", principal, "-f"])
188            .arg(allowed_signers)
189            .arg("-s")
190            .arg(&sig_file)
191            .stdin(Stdio::piped())
192            .stdout(Stdio::null())
193            .stderr(Stdio::null())
194            .spawn()
195            .ok()?;
196        child.stdin.take()?.write_all(payload.as_bytes()).ok()?;
197        child.wait().ok().map(|s| s.success())
198    })()
199    .unwrap_or(false);
200    let _ = std::fs::remove_file(&sig_file);
201    ok
202}
203
204/// pre-push, after every block gate has passed: attest each pushed tip and
205/// send the notes ref to the remote being pushed.
206///
207/// `gates` is what the dispatcher saw actually PASS — `Warned` and
208/// `Unavailable` never appear in it, because "could not run" is not
209/// "passed". Empty means nothing testlike ran, and an attestation listing
210/// no gates would be a signed way of saying nothing.
211///
212/// Best-effort throughout, and quiet about it: pre-push has already printed
213/// its verdicts, and a push that works minus its CI shortcut is not a
214/// problem anyone needs to solve at push time.
215pub fn attest_push(remote: &str, refs: &[PushRef], gates: &[String]) {
216    if gates.is_empty() || remote.is_empty() || !enabled() {
217        return;
218    }
219    let Some(key) = key_path() else { return };
220    if !key.exists() {
221        crate::config::complain(
222            TOGGLE,
223            &format!("signing key {} does not exist", key.display()),
224            "no attestation (CI will run the tests)",
225        );
226        return;
227    }
228    let mut noted = false;
229    for r in refs {
230        if is_zero(&r.local_oid) {
231            continue; // deleting a ref pushes no code
232        }
233        let spec = format!("{}^{{tree}}", r.local_oid);
234        let Some(tree) = crate::git::stdout(&["rev-parse", &spec]) else {
235            continue;
236        };
237        let body = match sign(&payload(&tree, gates), &key) {
238            Some(sig) => format!("{}\n{sig}", payload(&tree, gates)),
239            None => continue,
240        };
241        if crate::git::succeeds(&[
242            "notes",
243            "--ref",
244            NOTES_REF,
245            "add",
246            "-f",
247            "-m",
248            &body,
249            &r.local_oid,
250        ]) {
251            noted = true;
252        }
253    }
254    if noted && push_notes(remote) {
255        crate::say!(
256            "{} attested {} for CI ({})",
257            crate::ui::valid_sign(),
258            crate::ui::highlight(&gates.join(" ")),
259            NOTES_REF,
260        );
261    }
262}
263
264/// `amont attest covered` — the verifying side, as CI's one-liner.
265///
266/// Answers "which gates does a VALID attestation cover for the tree checked
267/// out here?", doing everything the workflow snippet used to spell out in
268/// sh: freshen the notes ref (best-effort), look for a note on `HEAD` and —
269/// for a PR's merge commit — on `HEAD^2`, insist on the format version,
270/// insist the attested tree is byte-for-byte `HEAD^{tree}`, and verify the
271/// signature against `allowed_signers`. Thirty lines of workflow copied into
272/// every repository is exactly the drift this binary exists to end.
273///
274/// `None` for every failure, and the CLI prints nothing and exits 0 on
275/// `None` — fail-open is the caller's contract, not its option. A CI step
276/// reading empty output runs its tests, which is always the safe answer.
277///
278/// This is amont running in CI, which `docs/ci.md` forbids for CHECKS — the
279/// line held is narrower than the slogan: CI still never runs a check
280/// through amont; this verifies a document about checks that already ran.
281/// `require_platform` is the leg asking. `Some("x86_64-linux")` covers only
282/// a suite that ran there; `None` is the caller stating that this suite's
283/// result does not depend on where it ran (a pure-JS unit run, say) and is
284/// spelled `--platform any` in a committed workflow, where it is reviewed
285/// like any other line of the repository.
286pub fn covered(
287    signers: &std::path::Path,
288    principal: &str,
289    require_platform: Option<&str>,
290) -> Option<String> {
291    let refspec = format!("+{NOTES_FULL_REF}:{NOTES_FULL_REF}");
292    let _ = crate::git::succeeds(&["fetch", "origin", &refspec]);
293    let head_tree = crate::git::stdout(&["rev-parse", "HEAD^{tree}"])?;
294    // HEAD first: a push event's checkout IS the attested commit. HEAD^2
295    // second: a PR checkout is a merge commit git made a moment ago, whose
296    // second parent is the pushed tip that carries the note — and the tree
297    // comparison below still measures against what is ACTUALLY checked out,
298    // so a merge whose tree drifted from the tested tip never skips.
299    for candidate in ["HEAD", "HEAD^2"] {
300        let Some(commit) = crate::git::stdout(&["rev-parse", "--verify", candidate]) else {
301            continue;
302        };
303        let Some(body) = crate::git::stdout(&["notes", "--ref", NOTES_REF, "show", &commit]) else {
304            continue;
305        };
306        let Some((payload, sig)) = split_note(&body) else {
307            continue;
308        };
309        // By prefix, not by position: the payload has grown a line once
310        // already, and a positional reader silently mis-assigns every field
311        // after an insertion rather than failing.
312        let mut lines = payload.lines();
313        if lines.next() != Some(FORMAT) {
314            continue;
315        }
316        let field = |name: &str| {
317            payload
318                .lines()
319                .find_map(|l| l.strip_prefix(name).and_then(|r| r.strip_prefix(' ')))
320                .map(str::trim)
321        };
322        let (Some(tree), Some(gates), Some(ran_on)) =
323            (field("tree"), field("gates"), field("platform"))
324        else {
325            continue;
326        };
327        if tree != head_tree || gates.is_empty() {
328            continue; // wrong content, or a signed way of saying nothing
329        }
330        // The leg asking is not the leg that ran: a macOS `cargo test` is no
331        // evidence about Windows. `None` means the caller has stated this
332        // suite is platform-independent.
333        if require_platform.is_some_and(|want| want != ran_on) {
334            continue;
335        }
336        if verify(&payload, &sig, signers, principal) {
337            return Some(gates.to_string());
338        }
339    }
340    None
341}
342
343/// Where a repository keeps its `allowed_signers` when the caller does not
344/// say — the Forgejo location first, the GitHub one second. `None` when
345/// neither exists, which the CLI reads as "nothing is covered".
346/// Resolved from the REPOSITORY ROOT, not the working directory. A workflow
347/// that sets `working-directory` (a monorepo running a matrix inside
348/// `packages/<x>`, say) puts the step in a subdirectory, where a relative
349/// `.forgejo/allowed_signers` does not exist — and the CLI would then find no
350/// signers, print nothing, and fail open FOREVER. Silently: the suite still
351/// runs, CI still passes, and nothing anywhere says the gate is dead. That is
352/// the worst shape a fail-open can take, so the path is anchored.
353pub fn default_signers() -> Option<PathBuf> {
354    let root = crate::git::stdout(&["rev-parse", "--show-toplevel"]).map(PathBuf::from);
355    [".forgejo/allowed_signers", ".github/allowed_signers"]
356        .into_iter()
357        .map(|rel| match &root {
358            Some(root) => root.join(rel),
359            None => PathBuf::from(rel),
360        })
361        .find(|p| p.exists())
362}
363
364/// The first principal an `allowed_signers` file names — the identity to
365/// verify against when the caller does not pass `--principal`. One key, one
366/// principal is the overwhelmingly common shape of this file; a multi-signer
367/// team passes the flag.
368pub fn first_principal(signers: &std::path::Path) -> Option<String> {
369    let body = std::fs::read_to_string(signers).ok()?;
370    body.lines()
371        .map(str::trim)
372        .find(|l| !l.is_empty() && !l.starts_with('#'))
373        .and_then(|l| l.split_whitespace().next())
374        .map(str::to_string)
375}
376
377/// A note body back into the exact bytes that were signed, plus the
378/// signature block. The blank-line split ate the payload's trailing newline;
379/// it is part of the signed bytes, so it goes back.
380fn split_note(body: &str) -> Option<(String, String)> {
381    let (payload, sig) = body.split_once("\n\n")?;
382    if !sig.starts_with("-----BEGIN SSH SIGNATURE-----") {
383        return None;
384    }
385    Some((format!("{payload}\n"), sig.to_string()))
386}
387
388/// Push the notes ref, marked so the recursive pre-push yields.
389///
390/// Not `git::succeeds` — that helper cannot set an environment variable, and
391/// the guard is the entire point of this wrapper existing.
392fn push_notes(remote: &str) -> bool {
393    let refspec = format!("{NOTES_FULL_REF}:{NOTES_FULL_REF}");
394    Command::new("git")
395        .args(["push", remote, &refspec])
396        .env(PUSH_GUARD, "1")
397        .stdin(Stdio::null())
398        .stdout(Stdio::null())
399        .stderr(Stdio::null())
400        .status()
401        .map(|s| s.success())
402        .unwrap_or(false)
403}
404
405/// A ref oid that is all zeros — git's spelling of "no object" in the
406/// pre-push ref list, for any hash width.
407fn is_zero(oid: &str) -> bool {
408    !oid.is_empty() && oid.bytes().all(|b| b == b'0')
409}
410
411/// uninstall: forget the local ref. The copies already pushed to remotes are
412/// statements we made and stand by; only OUR bookkeeping is removed — the
413/// same line `gate_stamp::forget` draws.
414pub fn forget() -> bool {
415    crate::git::succeeds(&["update-ref", "-d", NOTES_FULL_REF])
416}
417
418/// The same, for a repository this process is not standing in.
419pub fn forget_in(repo: &std::path::Path) -> bool {
420    crate::git::succeeds_in(repo, &["update-ref", "-d", NOTES_FULL_REF])
421}
422
423#[cfg(test)]
424mod tests {
425    use super::*;
426    use std::path::Path;
427
428    /// Real repositories and real keys: every function here is a conversation
429    /// with git or ssh-keygen, and a mocked conversation tests the one we
430    /// imagined. Same doctrine as `gate_stamp`'s tests.
431    fn dir(name: &str) -> PathBuf {
432        let d = std::env::temp_dir().join(format!("attest-{name}-{}", std::process::id()));
433        let _ = std::fs::remove_dir_all(&d);
434        std::fs::create_dir_all(&d).unwrap();
435        d
436    }
437
438    fn git(dir: &Path, args: &[&str]) -> String {
439        let out = std::process::Command::new("git")
440            .arg("-C")
441            .arg(dir)
442            .args(args)
443            .output()
444            .expect("git");
445        String::from_utf8_lossy(&out.stdout).trim().to_string()
446    }
447
448    fn repo(name: &str) -> PathBuf {
449        let d = dir(name);
450        git(&d, &["init", "-q", "--template=", "."]);
451        git(&d, &["config", "user.email", "t@t.test"]);
452        git(&d, &["config", "user.name", "t"]);
453        d
454    }
455
456    /// A throwaway ed25519 key plus the `allowed_signers` line CI would
457    /// commit for it, namespace-pinned exactly as the docs instruct.
458    fn keypair(d: &Path) -> (PathBuf, PathBuf) {
459        let key = d.join("attest_key");
460        let ok = std::process::Command::new("ssh-keygen")
461            .args(["-q", "-t", "ed25519", "-N", "", "-C", "test", "-f"])
462            .arg(&key)
463            .status()
464            .expect("ssh-keygen must exist for these tests")
465            .success();
466        assert!(ok, "keygen failed");
467        let pubkey = std::fs::read_to_string(key.with_extension("pub")).unwrap();
468        let signers = d.join("allowed_signers");
469        std::fs::write(
470            &signers,
471            format!("t@t.test namespaces=\"{NAMESPACE}\" {pubkey}"),
472        )
473        .unwrap();
474        (key, signers)
475    }
476
477    /// The module talks to the repo at the process cwd; serialised against
478    /// every other cwd-moving test via the crate-wide lock.
479    fn in_repo<T>(dir: &Path, f: impl FnOnce() -> T) -> T {
480        let _guard = crate::TEST_CWD.lock().unwrap_or_else(|p| p.into_inner());
481        let prev = std::env::current_dir().unwrap();
482        std::env::set_current_dir(dir).unwrap();
483        let r = f();
484        std::env::set_current_dir(prev).unwrap();
485        r
486    }
487
488    #[test]
489    fn the_payload_is_the_documented_contract() {
490        let p = payload(
491            "abc123",
492            &["pre-push-pytest".into(), "pre-push-cargo-test".into()],
493        );
494        let lines: Vec<&str> = p.lines().collect();
495        assert_eq!(lines[0], FORMAT);
496        assert_eq!(lines[1], "tree abc123");
497        assert_eq!(lines[2], "gates pre-push-pytest pre-push-cargo-test");
498        assert_eq!(lines[3], format!("platform {}", platform()));
499        assert_eq!(lines[4], format!("amont {}", env!("CARGO_PKG_VERSION")));
500        assert!(
501            p.ends_with('\n'),
502            "CI reconstructs these bytes; the trailing newline is part of them"
503        );
504    }
505
506    #[test]
507    fn sign_verify_roundtrip_and_tamper_rejection() {
508        let d = dir("roundtrip");
509        let (key, signers) = keypair(&d);
510        let p = payload("deadbeef", &["pre-push-pytest".into()]);
511        let sig = sign(&p, &key).expect("signing with a real key succeeds");
512        assert!(verify(&p, &sig, &signers, "t@t.test"));
513        // One byte of the tree changed: the signature must not carry over —
514        // this is the entire difference between this module and gate_stamp.
515        let tampered = payload("deadbeee", &["pre-push-pytest".into()]);
516        assert!(!verify(&tampered, &sig, &signers, "t@t.test"));
517        // The right payload under the wrong principal is also no.
518        assert!(!verify(&p, &sig, &signers, "someone@else.test"));
519        let _ = std::fs::remove_dir_all(&d);
520    }
521
522    #[test]
523    fn a_missing_key_signs_nothing() {
524        assert!(sign("anything", Path::new("/nonexistent/key")).is_none());
525    }
526
527    /// The full journey: a repo with the toggle on pushes, and the BARE
528    /// remote ends up holding a note whose payload verifies and matches the
529    /// pushed tree. This is everything CI relies on, minus CI.
530    #[test]
531    fn an_enabled_push_leaves_a_verifiable_note_on_the_remote() {
532        let d = dir("e2e");
533        let (key, signers) = keypair(&d);
534        let remote = d.join("remote.git");
535        std::fs::create_dir_all(&remote).unwrap();
536        git(&remote, &["init", "-q", "--bare", "--template=", "."]);
537        let work = repo("e2e-work");
538        git(
539            &work,
540            &["remote", "add", "origin", remote.to_str().unwrap()],
541        );
542        git(&work, &["config", "amont.attest", "true"]);
543        git(&work, &["config", "amont.attestKey", key.to_str().unwrap()]);
544        std::fs::write(work.join("a.ts"), "x").unwrap();
545        git(&work, &["add", "a.ts"]);
546        git(&work, &["commit", "-qm", "chore: a"]);
547        let head = git(&work, &["rev-parse", "HEAD"]);
548        let tree = git(&work, &["rev-parse", "HEAD^{tree}"]);
549        let push_ref = PushRef {
550            local_ref: "refs/heads/main".into(),
551            local_oid: head.clone(),
552            remote_ref: "refs/heads/main".into(),
553            remote_oid: "0".repeat(40),
554        };
555        in_repo(&work, || {
556            attest_push("origin", &[push_ref], &["pre-push-run-tests-js".into()]);
557        });
558        // The note exists on the REMOTE — the whole point is that it travels.
559        let body = git(&remote, &["notes", "--ref", NOTES_REF, "show", &head]);
560        assert!(!body.is_empty(), "no note reached the remote");
561        let (p, sig) = body
562            .split_once("\n\n")
563            .expect("payload, blank line, signature");
564        let p = format!("{p}\n"); // the blank-line split ate payload's trailing newline
565        assert!(p.starts_with(FORMAT));
566        assert!(
567            p.contains(&format!("tree {tree}")),
568            "attests the pushed tree"
569        );
570        assert!(
571            verify(&p, sig, &signers, "t@t.test"),
572            "the remote copy verifies"
573        );
574        let _ = std::fs::remove_dir_all(&d);
575        let _ = std::fs::remove_dir_all(&work);
576    }
577
578    /// Off by default: a repo that never opted in makes no statement, even
579    /// with everything else in place.
580    #[test]
581    fn no_opt_in_means_no_note() {
582        let d = dir("optout");
583        let (key, _) = keypair(&d);
584        let remote = d.join("remote.git");
585        std::fs::create_dir_all(&remote).unwrap();
586        git(&remote, &["init", "-q", "--bare", "--template=", "."]);
587        let work = repo("optout-work");
588        git(
589            &work,
590            &["remote", "add", "origin", remote.to_str().unwrap()],
591        );
592        git(&work, &["config", "amont.attestKey", key.to_str().unwrap()]);
593        std::fs::write(work.join("a.ts"), "x").unwrap();
594        git(&work, &["add", "a.ts"]);
595        git(&work, &["commit", "-qm", "chore: a"]);
596        let head = git(&work, &["rev-parse", "HEAD"]);
597        let push_ref = PushRef {
598            local_ref: "refs/heads/main".into(),
599            local_oid: head.clone(),
600            remote_ref: "refs/heads/main".into(),
601            remote_oid: "0".repeat(40),
602        };
603        in_repo(&work, || {
604            attest_push("origin", &[push_ref], &["pre-push-run-tests-js".into()]);
605        });
606        assert!(
607            git(&remote, &["notes", "--ref", NOTES_REF, "list"]).is_empty(),
608            "an un-opted-in repo attested something"
609        );
610        let _ = std::fs::remove_dir_all(&d);
611        let _ = std::fs::remove_dir_all(&work);
612    }
613
614    /// A deletion pushes no code; an empty gate list says nothing. Neither
615    /// may produce a note even in an enabled repo.
616    #[test]
617    fn deletions_and_empty_gates_attest_nothing() {
618        let d = dir("nothing");
619        let (key, _) = keypair(&d);
620        let remote = d.join("remote.git");
621        std::fs::create_dir_all(&remote).unwrap();
622        git(&remote, &["init", "-q", "--bare", "--template=", "."]);
623        let work = repo("nothing-work");
624        git(
625            &work,
626            &["remote", "add", "origin", remote.to_str().unwrap()],
627        );
628        git(&work, &["config", "amont.attest", "true"]);
629        git(&work, &["config", "amont.attestKey", key.to_str().unwrap()]);
630        std::fs::write(work.join("a.ts"), "x").unwrap();
631        git(&work, &["add", "a.ts"]);
632        git(&work, &["commit", "-qm", "chore: a"]);
633        let head = git(&work, &["rev-parse", "HEAD"]);
634        let deletion = PushRef {
635            local_ref: "(delete)".into(),
636            local_oid: "0".repeat(40),
637            remote_ref: "refs/heads/gone".into(),
638            remote_oid: head.clone(),
639        };
640        let real = PushRef {
641            local_ref: "refs/heads/main".into(),
642            local_oid: head,
643            remote_ref: "refs/heads/main".into(),
644            remote_oid: "0".repeat(40),
645        };
646        in_repo(&work, || {
647            attest_push("origin", &[deletion], &["pre-push-pytest".into()]);
648            attest_push("origin", &[real], &[]);
649        });
650        assert!(git(&remote, &["notes", "--ref", NOTES_REF, "list"]).is_empty());
651        let _ = std::fs::remove_dir_all(&d);
652        let _ = std::fs::remove_dir_all(&work);
653    }
654
655    /// The half CI actually calls, from CI's own vantage point: a fresh
656    /// clone. `covered` fetches the notes ref itself, verifies, and answers
657    /// with the gates — then stops answering the moment the tree drifts or
658    /// the note is replaced by something unsigned.
659    #[test]
660    fn covered_answers_in_a_fresh_clone_and_rejects_drift_and_forgery() {
661        let d = dir("covered");
662        let (key, signers) = keypair(&d);
663        let remote = d.join("remote.git");
664        std::fs::create_dir_all(&remote).unwrap();
665        git(&remote, &["init", "-q", "--bare", "--template=", "."]);
666        // The fixture pushes to `main`; a bare init on a machine whose
667        // init.defaultBranch is the historical default leaves HEAD on
668        // `master`, and a clone of that repository checks out NOTHING —
669        // `covered` then answers None with a perfectly good note sitting in
670        // the ref. Caught only in CI: dev machines set main globally.
671        git(&remote, &["symbolic-ref", "HEAD", "refs/heads/main"]);
672        let work = repo("covered-work");
673        git(
674            &work,
675            &["remote", "add", "origin", remote.to_str().unwrap()],
676        );
677        git(&work, &["config", "amont.attest", "true"]);
678        git(&work, &["config", "amont.attestKey", key.to_str().unwrap()]);
679        std::fs::write(work.join("a.ts"), "x").unwrap();
680        git(&work, &["add", "a.ts"]);
681        git(&work, &["commit", "-qm", "chore: a"]);
682        git(&work, &["push", "-q", "origin", "HEAD:main"]);
683        let head = git(&work, &["rev-parse", "HEAD"]);
684        let push_ref = PushRef {
685            local_ref: "refs/heads/main".into(),
686            local_oid: head.clone(),
687            remote_ref: "refs/heads/main".into(),
688            remote_oid: "0".repeat(40),
689        };
690        in_repo(&work, || {
691            attest_push("origin", &[push_ref], &["pre-push-pytest".into()]);
692        });
693        let clone = d.join("ci-checkout");
694        git(
695            &d,
696            &[
697                "clone",
698                "-q",
699                "--template=",
700                remote.to_str().unwrap(),
701                clone.to_str().unwrap(),
702            ],
703        );
704        in_repo(&clone, || {
705            assert_eq!(
706                covered(&signers, "t@t.test", Some(&platform())).as_deref(),
707                Some("pre-push-pytest"),
708                "a fresh clone verifies the attestation and reads the gates"
709            );
710            assert_eq!(
711                covered(&signers, "t@t.test", None).as_deref(),
712                Some("pre-push-pytest"),
713                "`any` covers a platform-independent suite"
714            );
715            // The matrix case this exists for: another leg asking about a
716            // suite that never ran there.
717            assert_eq!(
718                covered(&signers, "t@t.test", Some("s390x-aix")),
719                None,
720                "a pass on one platform is not evidence about another"
721            );
722            assert_eq!(
723                covered(&signers, "someone@else.test", None),
724                None,
725                "an unlisted principal covers nothing"
726            );
727        });
728        // Tree drift: a new commit in the checkout is not the attested tree.
729        std::fs::write(clone.join("b.ts"), "y").unwrap();
730        git(&clone, &["config", "user.email", "t@t.test"]);
731        git(&clone, &["config", "user.name", "t"]);
732        git(&clone, &["add", "b.ts"]);
733        git(&clone, &["commit", "-qm", "chore: b"]);
734        in_repo(&clone, || {
735            assert_eq!(covered(&signers, "t@t.test", None), None, "drifted tree");
736        });
737        // Forgery: replace the remote's note with an unsigned one. covered's
738        // own fetch pulls it in, and it must read as "no attestation".
739        git(
740            &work,
741            &[
742                "notes", "--ref", NOTES_REF, "add", "-f", "-m", "garbage", &head,
743            ],
744        );
745        git(
746            &work,
747            &[
748                "push",
749                "-q",
750                "origin",
751                &format!("+{NOTES_FULL_REF}:{NOTES_FULL_REF}"),
752            ],
753        );
754        git(&clone, &["reset", "-q", "--hard", &head]);
755        in_repo(&clone, || {
756            assert_eq!(
757                covered(&signers, "t@t.test", None),
758                None,
759                "a foreign note is not a stamp"
760            );
761        });
762        let _ = std::fs::remove_dir_all(&d);
763        let _ = std::fs::remove_dir_all(&work);
764    }
765
766    /// The silent-death case: a workflow step running with a
767    /// `working-directory` inside the repo must still find the committed
768    /// signers file. Before this, `default_signers` looked relative to the
769    /// cwd, found nothing, and every such repo fail-opened forever with no
770    /// symptom — CI stayed green and the gate simply never fired.
771    #[test]
772    fn default_signers_is_found_from_a_subdirectory() {
773        let work = repo("signers-subdir");
774        std::fs::create_dir_all(work.join(".forgejo")).unwrap();
775        std::fs::write(work.join(".forgejo/allowed_signers"), "t@t.test x\n").unwrap();
776        let sub = work.join("packages").join("thing");
777        std::fs::create_dir_all(&sub).unwrap();
778        in_repo(&sub, || {
779            let found = default_signers().expect("resolved from the repo root, not the cwd");
780            assert!(found.ends_with(".forgejo/allowed_signers"));
781            assert!(found.exists(), "the path it returns must be usable as-is");
782            assert_eq!(
783                first_principal(&found).as_deref(),
784                Some("t@t.test"),
785                "and readable from there"
786            );
787        });
788        let _ = std::fs::remove_dir_all(&work);
789    }
790
791    #[test]
792    fn zero_oids_of_any_width_are_zero() {
793        assert!(is_zero(&"0".repeat(40)));
794        assert!(is_zero(&"0".repeat(64)));
795        assert!(!is_zero("0a0000"));
796        assert!(!is_zero(""));
797    }
798
799    #[test]
800    fn forget_removes_the_local_ref() {
801        let work = repo("forget");
802        std::fs::write(work.join("a.ts"), "x").unwrap();
803        git(&work, &["add", "a.ts"]);
804        git(&work, &["commit", "-qm", "chore: a"]);
805        git(
806            &work,
807            &["notes", "--ref", NOTES_REF, "add", "-m", "x", "HEAD"],
808        );
809        in_repo(&work, forget);
810        assert!(git(&work, &["notes", "--ref", NOTES_REF, "list"]).is_empty());
811        let _ = std::fs::remove_dir_all(&work);
812    }
813}