Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This repository is the published distribution of the Proofline CLI. `openapi.pub

On Linux and macOS, install the CLI with `curl -fsSL https://proofline.sh/cli | sh`, then run `proofline login`. Run `proofline setup` to open the browser organization selector. Pass `--organization NAME` to open that GitHub organization's installation page, or `--print` to only print its address. Cloud trust and connection changes still require a signed-in administrator browser.

The installer checks the archive against the release's `SHA256SUMS`, which comes from the same origin and so catches a damaged download but not a replaced release. To also check build provenance, run `curl -fsSL https://proofline.sh/cli | PROOFLINE_VERIFY_PROVENANCE=1 sh`. The installer then runs `gh attestation verify`, which needs the [GitHub CLI](https://cli.github.com/) signed in to github.com, and installs only an archive that GitHub signed for this repository's `release.yml` on a GitHub-hosted runner, at the requested version's `vX.Y.Z` tag or at `main`. Without `gh`, it downloads nothing. If the check fails, it installs nothing.

`proofline completions SHELL` prints a completion script for `zsh`, `bash`, `fish`, `powershell`, or `elvish` without signing in or making a network call. For example, add `source <(proofline completions zsh)` to `~/.zshrc`.

### Windows installation
Expand Down
36 changes: 36 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
# ~/.local/bin, or in PROOFLINE_INSTALL_DIR when that is set. PROOFLINE_VERSION
# selects another release.
#
# SHA256SUMS comes from the same origin as the archive, so it catches a damaged
# download but not a replaced release. PROOFLINE_VERIFY_PROVENANCE=1 also has
# `gh attestation verify` check the archive's build provenance: GitHub must have
# signed it for this repository's release workflow, on a GitHub-hosted runner,
# at this version's tag or at main. It needs the GitHub CLI signed in to
# github.com.
#
# Every statement is inside a function and the last line calls it, so a
# download cut short runs nothing.

Expand All @@ -17,6 +24,7 @@ set -eu
PROOFLINE_CLI=https://proofline.sh/cli
PROOFLINE_INSTALLER_VERSION=0.3.9
PROOFLINE_SETUP_MIN_VERSION=0.1.8
PROOFLINE_SOURCE_REPOSITORY=prooflinedev/cli

say() {
printf 'proofline install: %s\n' "$*" >&2
Expand Down Expand Up @@ -71,8 +79,27 @@ sha256() {
fi
}

# Checks the archive's build provenance with the GitHub CLI. The signing
# certificate names the workflow and the ref it ran at; a release is published
# from its vX.Y.Z tag or by a dispatch on main, so only those two refs pass.
verify_provenance() {
# The version is already checked to be digits and dots.
tag=$(printf '%s\n' "v$2" | sed 's/\./\\./g')
identity="^https://github\\.com/prooflinedev/cli/\\.github/workflows/release\\.yml@refs/(tags/$tag|heads/main)\$"
gh attestation verify "$1" \
--hostname github.com \
--repo "$PROOFLINE_SOURCE_REPOSITORY" \
--cert-identity-regex "$identity" \
--deny-self-hosted-runners >&2
}

main() {
setup=false
case "${PROOFLINE_VERIFY_PROVENANCE:-}" in
'' | 0) provenance=false ;;
1) provenance=true ;;
*) refuse "PROOFLINE_VERIFY_PROVENANCE must be 1 or 0; nothing was installed" ;;
esac
case "$#:$*" in
0:) ;;
1:--setup) setup=true ;;
Expand All @@ -89,6 +116,10 @@ main() {
if [ "$setup" = true ] && ! command -v git >/dev/null 2>&1; then
refuse "coding-agent setup needs git, which is not on PATH; nothing was installed; install Git and rerun this command, or omit --setup to install only the CLI"
fi
# Likewise, a provenance check that cannot run downloads nothing.
if [ "$provenance" = true ] && ! command -v gh >/dev/null 2>&1; then
refuse "PROOFLINE_VERIFY_PROVENANCE=1 needs the GitHub CLI (gh), which is not on PATH; nothing was installed"
fi

need curl
need tar
Expand Down Expand Up @@ -123,6 +154,11 @@ main() {
actual=$(sha256 "$work/$archive")
[ "$actual" = "$expected" ] ||
refuse "the download does not match SHA256SUMS ($actual, expected $expected); nothing was installed"
if [ "$provenance" = true ]; then
say "checking build provenance of $archive with gh attestation verify"
verify_provenance "$work/$archive" "$version" ||
refuse "$archive has no build provenance from $PROOFLINE_SOURCE_REPOSITORY's release workflow for $version; nothing was installed; run 'gh auth login' if gh is not signed in"
fi

tar -xzf "$work/$archive" -C "$work" proofline ||
refuse "$archive does not contain proofline"
Expand Down
265 changes: 265 additions & 0 deletions tests/install.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
//! `install.sh` checks a release before installing it. These tests run the
//! installer with stand-ins for `curl` and `gh` on a PATH that holds only the
//! tools it needs, so they use no network and no real GitHub CLI.
#![cfg(unix)]

use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};

const VERSION: &str = "9.9.9";
const INSTALLED: &str = "proofline 9.9.9";

/// Tools `install.sh` calls besides `curl` and `gh`.
const TOOLS: &[&str] = &[
"awk",
"cat",
"chmod",
"cp",
"cut",
"grep",
"gzip",
"mkdir",
"mktemp",
"mv",
"rm",
"sed",
"sha256sum",
"shasum",
"sysctl",
"tar",
"uname",
];

const FAKE_CURL: &str = r#"#!/bin/sh
printf '%s\n' "$*" >> "$FAKE_CURL_LOG"
out=
while [ $# -gt 1 ]; do
case $1 in --output) out=$2; shift ;; esac
shift
done
name=${1#https://proofline.sh/cli/download/9.9.9/}
[ "$name" != "$1" ] && [ -f "$FAKE_RELEASE/$name" ] || exit 22
cp "$FAKE_RELEASE/$name" "$out"
"#;

const FAKE_GH: &str = r#"#!/bin/sh
printf '%s\n' "$@" > "$FAKE_GH_LOG"
exit "$FAKE_GH_EXIT"
"#;

struct Fixture {
root: tempfile::TempDir,
}

impl Fixture {
/// A release whose archive matches its SHA256SUMS unless `corrupt` is set.
fn new(corrupt: bool) -> Self {
let root = tempfile::tempdir().unwrap();
let fixture = Self { root };
let stage = fixture.path("stage");
let release = fixture.path("release");
fs::create_dir_all(&stage).unwrap();
fs::create_dir_all(&release).unwrap();
executable(
&stage.join("proofline"),
&format!("#!/bin/sh\nprintf '%s\\n' '{INSTALLED}'\n"),
);
let archive = release.join(archive_name());
let tar = Command::new("tar")
.arg("-czf")
.arg(&archive)
.arg("-C")
.arg(&stage)
.arg("proofline")
.status()
.unwrap();
assert!(tar.success());
let mut digest = sha256(&archive);
if corrupt {
digest = "0".repeat(64);
}
fs::write(
release.join("SHA256SUMS"),
format!("{digest} {}\n", archive_name()),
)
.unwrap();

let tools = fixture.path("tools");
fs::create_dir_all(&tools).unwrap();
for tool in TOOLS {
if let Some(found) = which(tool) {
std::os::unix::fs::symlink(found, tools.join(tool)).unwrap();
}
}
executable(&tools.join("curl"), FAKE_CURL);
fixture
}

fn path(&self, name: &str) -> PathBuf {
self.root.path().join(name)
}

/// Puts the stand-in `gh` on PATH; it exits with `status`.
fn with_gh(self, status: i32) -> Self {
executable(&self.path("tools/gh"), FAKE_GH);
fs::write(self.path("gh-exit"), status.to_string()).unwrap();
self
}

fn install(&self, provenance: Option<&str>) -> Output {
let gh_exit = fs::read_to_string(self.path("gh-exit")).unwrap_or_default();
let mut command = Command::new("/bin/sh");
command
.arg(Path::new(env!("CARGO_MANIFEST_DIR")).join("install.sh"))
.env_clear()
.env("PATH", self.path("tools"))
.env("HOME", self.root.path())
.env("PROOFLINE_VERSION", VERSION)
.env("PROOFLINE_INSTALL_DIR", self.path("bin"))
.env("FAKE_RELEASE", self.path("release"))
.env("FAKE_CURL_LOG", self.path("curl.log"))
.env("FAKE_GH_LOG", self.path("gh.log"))
.env("FAKE_GH_EXIT", gh_exit);
if let Some(value) = provenance {
command.env("PROOFLINE_VERIFY_PROVENANCE", value);
}
command.output().unwrap()
}

fn installed(&self) -> bool {
self.path("bin/proofline").exists()
}

fn downloads(&self) -> usize {
fs::read_to_string(self.path("curl.log")).map_or(0, |log| log.lines().count())
}

fn gh_arguments(&self) -> Option<Vec<String>> {
fs::read_to_string(self.path("gh.log"))
.ok()
.map(|log| log.lines().map(str::to_owned).collect())
}
}

fn archive_name() -> String {
let arch = match std::env::consts::ARCH {
"aarch64" => "aarch64",
_ => "x86_64",
};
let os = if cfg!(target_os = "macos") {
"apple-darwin"
} else {
"unknown-linux-musl"
};
format!("proofline-{arch}-{os}.tar.gz")
}

fn executable(path: &Path, contents: &str) {
fs::write(path, contents).unwrap();
fs::set_permissions(path, fs::Permissions::from_mode(0o755)).unwrap();
}

fn which(tool: &str) -> Option<PathBuf> {
std::env::var_os("PATH").and_then(|path| {
std::env::split_paths(&path)
.map(|directory| directory.join(tool))
.find(|candidate| candidate.is_file())
})
}

fn sha256(path: &Path) -> String {
let output = Command::new("sh")
.arg("-c")
.arg(r#"sha256sum "$1" 2>/dev/null || shasum -a 256 "$1""#)
.arg("sh")
.arg(path)
.output()
.unwrap();
assert!(output.status.success(), "{output:?}");
let line = String::from_utf8(output.stdout).unwrap();
line.split_whitespace().next().unwrap().to_owned()
}

fn stderr(output: &Output) -> String {
String::from_utf8_lossy(&output.stderr).into_owned()
}

#[test]
fn installs_without_provenance_check_by_default() {
let fixture = Fixture::new(false).with_gh(1);
let output = fixture.install(None);
assert!(output.status.success(), "{}", stderr(&output));
assert!(stderr(&output).contains(&format!("installed {INSTALLED}")));
assert_eq!(fixture.gh_arguments(), None);

let output = Fixture::new(false).with_gh(1).install(Some("0"));
assert!(output.status.success(), "{}", stderr(&output));
}

#[test]
fn verified_provenance_names_the_repository_workflow_and_release_refs() {
let fixture = Fixture::new(false).with_gh(0);
let output = fixture.install(Some("1"));
assert!(output.status.success(), "{}", stderr(&output));
assert!(fixture.installed());

let arguments = fixture.gh_arguments().expect("gh was not called");
assert_eq!(arguments[..2], ["attestation", "verify"]);
assert!(arguments[2].ends_with(&format!("/{}", archive_name())));
let flag = |name: &str| {
let at = arguments.iter().position(|argument| argument == name);
at.map(|at| arguments[at + 1].as_str())
};
assert_eq!(flag("--hostname"), Some("github.com"));
assert_eq!(flag("--repo"), Some("prooflinedev/cli"));
assert_eq!(
flag("--cert-identity-regex"),
Some(
r"^https://github\.com/prooflinedev/cli/\.github/workflows/release\.yml@refs/(tags/v9\.9\.9|heads/main)$"
)
);
assert!(arguments.iter().any(|a| a == "--deny-self-hosted-runners"));
}

#[test]
fn failed_provenance_installs_nothing() {
let fixture = Fixture::new(false).with_gh(1);
let output = fixture.install(Some("1"));
assert!(!output.status.success());
let stderr = stderr(&output);
assert!(stderr.contains("has no build provenance"), "{stderr}");
assert!(stderr.contains("nothing was installed"), "{stderr}");
assert!(!fixture.installed());
}

#[test]
fn provenance_without_gh_downloads_nothing() {
let fixture = Fixture::new(false);
let output = fixture.install(Some("1"));
assert!(!output.status.success());
assert!(stderr(&output).contains("needs the GitHub CLI (gh)"));
assert_eq!(fixture.downloads(), 0);
assert!(!fixture.installed());
}

#[test]
fn unknown_provenance_setting_is_refused() {
let fixture = Fixture::new(false).with_gh(0);
let output = fixture.install(Some("yes"));
assert!(!output.status.success());
assert!(stderr(&output).contains("PROOFLINE_VERIFY_PROVENANCE must be 1 or 0"));
assert_eq!(fixture.downloads(), 0);
assert_eq!(fixture.gh_arguments(), None);
}

#[test]
fn checksum_mismatch_stops_before_provenance_check() {
let fixture = Fixture::new(true).with_gh(0);
let output = fixture.install(Some("1"));
assert!(!output.status.success());
assert!(stderr(&output).contains("does not match SHA256SUMS"));
assert_eq!(fixture.gh_arguments(), None);
assert!(!fixture.installed());
}