From 3c5c26d25b46fa2ce7b7f0bd49031b69aeebab67 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 21:43:58 +0100 Subject: [PATCH] feat: workflow-permissions keeps every workflow's token to contents: read --- crates/sourceorigin-standard/README.md | 1 + crates/sourceorigin-standard/src/rules/mod.rs | 4 ++ .../src/rules/tests/mod.rs | 1 + .../src/rules/tests/workflow_permissions.rs | 40 +++++++++++++++ .../src/rules/workflow_permissions.rs | 49 +++++++++++++++++++ .../src/rules/workflows.rs | 5 ++ 6 files changed, 100 insertions(+) create mode 100644 crates/sourceorigin-standard/src/rules/tests/workflow_permissions.rs create mode 100644 crates/sourceorigin-standard/src/rules/workflow_permissions.rs diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 96157b7..29d7e1c 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -54,6 +54,7 @@ A repository adopts the standard in four steps: - `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. +- `workflow-permissions`: Every workflow's top-level `permissions` is exactly `contents: read`; a job that needs more sets its own. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index 67fe650..d0b07a2 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -37,6 +37,7 @@ mod text_file; mod toml_file; mod toolchain; mod unsafe_modules; +mod workflow_permissions; mod workflows; mod workspace_dependencies; mod workspace_lints; @@ -225,6 +226,9 @@ rules! { /// 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, + /// Every workflow's top-level `permissions` is exactly + /// `contents: read`; a job that needs more sets its own. + WorkflowPermissions = "workflow-permissions" => workflow_permissions, } /// Why the rules couldn't be checked. diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index f074c86..20ff0fb 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -23,6 +23,7 @@ mod rustfmt_config; mod t3_setup; mod toolchain; mod unsafe_modules; +mod workflow_permissions; mod workspace_dependencies; mod workspace_lints; mod workspace_manifest; diff --git a/crates/sourceorigin-standard/src/rules/tests/workflow_permissions.rs b/crates/sourceorigin-standard/src/rules/tests/workflow_permissions.rs new file mode 100644 index 0000000..953e6ad --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/workflow_permissions.rs @@ -0,0 +1,40 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +#[test] +fn workflows_read_the_repository_and_jobs_ask_for_more() { + let fixture = Fixture::new(); + fixture + .add_crate("core", INHERITS) + .write( + ".github/workflows/ci.yml", + "on: push\npermissions:\n contents: read\njobs:\n release:\n permissions:\n \ + contents: write\n", + ) + .write( + ".github/workflows/release.yml", + "on: push\npermissions: write-all\njobs:\n release: {}\n", + ) + .write( + ".github/workflows/audit.yml", + "on: push\n\njobs:\n audit: {}\n", + ); + let wrong = |path: &str, line: usize| { + format!( + "{path}:{line}: the workflow's `permissions` aren't `contents: read`: set them to it \ + at the top, and grant more to the jobs that need it (workflow-permissions)" + ) + }; + assert_eq!( + fixture.violations(Rule::WorkflowPermissions), + [ + wrong(".github/workflows/audit.yml", 1), + wrong(".github/workflows/release.yml", 2), + ] + ); + fixture.fix_rule(Rule::WorkflowPermissions); + assert_eq!( + fixture.violations(Rule::WorkflowPermissions), + [wrong(".github/workflows/release.yml", 2)] + ); +} diff --git a/crates/sourceorigin-standard/src/rules/workflow_permissions.rs b/crates/sourceorigin-standard/src/rules/workflow_permissions.rs new file mode 100644 index 0000000..5ced1f7 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/workflow_permissions.rs @@ -0,0 +1,49 @@ +//! `workflow-permissions`: a workflow's token reads the repository, and a +//! job that needs more asks for it. + +use super::fix::{Checked, Fix}; +use super::{Finding, Repository}; + +impl Repository { + /// Each workflow whose top-level `permissions` isn't exactly + /// `contents: read`. A missing one is fixed by adding it before `jobs:`. + pub(super) fn workflow_permissions(&self) -> Checked { + let mut checked = Checked::default(); + for workflow in self + .workflows + .iter() + .filter(|workflow| !workflow.is_action()) + { + let permissions = workflow.document.get("permissions"); + let read_only = permissions.is_some_and(|permissions| { + matches!( + permissions.entries(), + [(key, value)] if key.as_str() == Some("contents") && value.as_str() == Some("read") + ) + }); + if read_only { + continue; + } + checked.findings.push(Finding { + path: workflow.path.clone(), + line: permissions.map_or(1, |permissions| permissions.line), + message: "the workflow's `permissions` aren't `contents: read`: set them to it \ + at the top, and grant more to the jobs that need it" + .to_owned(), + }); + let jobs = workflow + .text + .lines() + .position(|line| line.trim_end() == "jobs:"); + if let (None, Some(index)) = (permissions, jobs) { + checked.fixes.push(Fix::line( + workflow.path.clone(), + "set `permissions: contents: read`".to_owned(), + index + 1, + "permissions:\n contents: read\n\njobs:".to_owned(), + )); + } + } + checked + } +} diff --git a/crates/sourceorigin-standard/src/rules/workflows.rs b/crates/sourceorigin-standard/src/rules/workflows.rs index 473ef1b..a41ae3a 100644 --- a/crates/sourceorigin-standard/src/rules/workflows.rs +++ b/crates/sourceorigin-standard/src/rules/workflows.rs @@ -42,6 +42,11 @@ impl Workflow { .collect() } + /// Whether it is a composite action rather than a workflow. + pub(super) fn is_action(&self) -> bool { + self.path.starts_with(".github/actions") + } + /// The events that trigger it, from `on` as a name, a list or a /// mapping. pub(super) fn triggers(&self) -> Vec<&str> {