From 4e3f901b752908d184d6d2273bc89ed51fe47e20 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 21:43:20 +0100 Subject: [PATCH] feat: actions-pinned pins every action to a commit, with its version beside it --- crates/sourceorigin-standard/README.md | 1 + .../src/rules/actions_pinned.rs | 47 +++++++++++++++++++ crates/sourceorigin-standard/src/rules/mod.rs | 5 ++ .../src/rules/tests/actions_pinned.rs | 30 ++++++++++++ .../src/rules/tests/mod.rs | 1 + 5 files changed, 84 insertions(+) create mode 100644 crates/sourceorigin-standard/src/rules/actions_pinned.rs create mode 100644 crates/sourceorigin-standard/src/rules/tests/actions_pinned.rs diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 7a7997c..96157b7 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -53,6 +53,7 @@ A repository adopts the standard in four steps: - `recipes-only`: Every command of `t3.json` runs `just`, and no line of a workflow's or composite action's `run:` starts with `cargo` unless `standard.toml`'s `cargo-in-ci` lists it; each listed line is one a `run:` holds. - `t3-setup`: A recipe `t3.json` runs on worktree creation that runs Cargo, itself or through the recipes it runs, sets `CARGO_BUILD_JOBS` to a default, half the cores, that a set one overrides, and runs the build with `nice -n 10`, both in its own body. - `ci-shape`: `.github/workflows/ci.yml` runs on `merge_group`, and has a `ci` job that needs every other job and runs `if: always()`: with `standard / pr-checks`, it is one of the repository's two required checks. +- `actions-pinned`: Every `uses:` of a workflow or composite action naming another repository's action or workflow pins it to a 40-character commit SHA, followed by a `# v…` comment giving its version. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and diff --git a/crates/sourceorigin-standard/src/rules/actions_pinned.rs b/crates/sourceorigin-standard/src/rules/actions_pinned.rs new file mode 100644 index 0000000..da05635 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/actions_pinned.rs @@ -0,0 +1,47 @@ +//! `actions-pinned`: a workflow runs the exact code of each action it +//! reviewed. + +use super::{Finding, Repository}; + +impl Repository { + /// Each `uses:` of a step or a job naming an action or workflow of + /// another repository by anything but a commit SHA with its version in + /// a comment. + pub(super) fn actions_pinned(&self) -> Vec { + let mut findings = Vec::new(); + for workflow in &self.workflows { + let jobs = workflow.jobs().iter().map(|(_, job)| job); + let uses = workflow + .steps() + .chain(jobs) + .filter_map(|node| node.get("uses")); + for used in uses { + let Some(action) = used.as_str() else { + continue; + }; + if action.starts_with("./") || action.starts_with("docker://") { + continue; + } + let pinned = action.rsplit_once('@').is_some_and(|(_, sha)| { + sha.len() == 40 && sha.bytes().all(|b| b.is_ascii_hexdigit()) + }); + let line = workflow.text.lines().nth(used.line - 1).unwrap_or_default(); + let versioned = line + .split_once(" #") + .is_some_and(|(_, comment)| comment.trim_start().starts_with('v')); + if !(pinned && versioned) { + findings.push(Finding { + path: workflow.path.clone(), + line: used.line, + message: format!( + "`{action}` isn't pinned: name the commit of its release, \ + `{}@<40-character SHA> # v`", + action.split('@').next().unwrap_or(action) + ), + }); + } + } + } + findings + } +} diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index e1e9174..67fe650 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -4,6 +4,7 @@ //! rules `standard.toml` deviates from. [`describe`] lists the rules with //! their docs. +mod actions_pinned; mod agent_files; mod agents_md; mod anyhow_edges; @@ -220,6 +221,10 @@ rules! { /// `standard / pr-checks`, it is one of the repository's two required /// checks. CiShape = "ci-shape" => ci_shape, + /// Every `uses:` of a workflow or composite action naming another + /// repository's action or workflow pins it to a 40-character commit + /// SHA, followed by a `# v…` comment giving its version. + ActionsPinned = "actions-pinned" => actions_pinned, } /// Why the rules couldn't be checked. diff --git a/crates/sourceorigin-standard/src/rules/tests/actions_pinned.rs b/crates/sourceorigin-standard/src/rules/tests/actions_pinned.rs new file mode 100644 index 0000000..5f6a369 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/actions_pinned.rs @@ -0,0 +1,30 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +#[test] +fn actions_are_pinned_to_a_commit_with_their_version() { + let fixture = Fixture::new(); + fixture.add_crate("core", INHERITS).write( + ".github/workflows/ci.yml", + "jobs:\n rust:\n steps:\n \ + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7\n \ + - uses: actions/cache@v4\n \ + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6\n \ + - uses: ./.github/actions/ui\n \ + release:\n uses: owner/repo/.github/workflows/release.yml@main\n", + ); + assert_eq!( + fixture.violations(Rule::ActionsPinned), + [ + ".github/workflows/ci.yml:5: `actions/cache@v4` isn't pinned: name the commit of its \ + release, `actions/cache@<40-character SHA> # v` (actions-pinned)", + ".github/workflows/ci.yml:6: `Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6` \ + isn't pinned: name the commit of its release, `Swatinem/rust-cache@<40-character \ + SHA> # v` (actions-pinned)", + ".github/workflows/ci.yml:9: `owner/repo/.github/workflows/release.yml@main` isn't \ + pinned: name the commit of its release, \ + `owner/repo/.github/workflows/release.yml@<40-character SHA> # v` \ + (actions-pinned)", + ] + ); +} diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index 17ffce5..f074c86 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -1,5 +1,6 @@ //! A fixture workspace the rules check, shared by the tests of each rule. +mod actions_pinned; mod agent_files; mod agents_md; mod anyhow_edges;