From 03cfc5b9fb3d83fc487159835b5b0f2e2d65df51 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 21:46:52 +0100 Subject: [PATCH] feat: scheduled-audit audits every repository's dependencies weekly --- crates/sourceorigin-standard/README.md | 1 + crates/sourceorigin-standard/src/rules/mod.rs | 4 ++ .../src/rules/scheduled_audit.rs | 57 +++++++++++++++++++ .../src/rules/tests/mod.rs | 1 + .../src/rules/tests/scheduled_audit.rs | 34 +++++++++++ 5 files changed, 97 insertions(+) create mode 100644 crates/sourceorigin-standard/src/rules/scheduled_audit.rs create mode 100644 crates/sourceorigin-standard/src/rules/tests/scheduled_audit.rs diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index e6469d3..337e12d 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -57,6 +57,7 @@ A repository adopts the standard in four steps: - `workflow-permissions`: Every workflow's top-level `permissions` is exactly `contents: read`; a job that needs more sets its own. - `job-timeouts`: Every job of a workflow sets `timeout-minutes`, except one calling a reusable workflow. - `cache-saves`: Every `Swatinem/rust-cache` step's `save-if` is `false` or saves from `refs/heads/main` only, and no composite action, nor workflow run by `pull_request`, `pull_request_target` or `merge_group`, uses `actions/cache`, which saves on a miss: they use `actions/cache/restore`, saving with `actions/cache/save` from `main`. +- `scheduled-audit`: A workflow runs `just audit` (or `cargo deny`) on a `schedule` at least weekly: a cron whose day of the month and month are `*`. 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 75a9994..a0ab658 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -29,6 +29,7 @@ mod recipes; mod recipes_only; mod report; mod rustfmt_config; +mod scheduled_audit; mod settings; mod source; mod t3; @@ -240,6 +241,9 @@ rules! { /// `actions/cache`, which saves on a miss: they use /// `actions/cache/restore`, saving with `actions/cache/save` from `main`. CacheSaves = "cache-saves" => cache_saves, + /// A workflow runs `just audit` (or `cargo deny`) on a `schedule` at + /// least weekly: a cron whose day of the month and month are `*`. + ScheduledAudit = "scheduled-audit" => scheduled_audit, } /// Why the rules couldn't be checked. diff --git a/crates/sourceorigin-standard/src/rules/scheduled_audit.rs b/crates/sourceorigin-standard/src/rules/scheduled_audit.rs new file mode 100644 index 0000000..3de6f20 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/scheduled_audit.rs @@ -0,0 +1,57 @@ +//! `scheduled-audit`: advisories published after a change still surface. + +use super::workflows::Workflow; +use super::yaml::Node; +use super::{Finding, Repository}; + +/// The commands that audit the dependencies. +const AUDITS: [&str; 2] = ["just audit", "cargo deny"]; + +impl Workflow { + /// Whether a `schedule` trigger runs it at least weekly: a cron whose + /// day of the month and month are `*`. + fn weekly(&self) -> bool { + let schedules = self + .document + .get("on") + .and_then(|on| on.get("schedule")) + .map_or(&[][..], Node::items); + schedules + .iter() + .filter_map(|schedule| schedule.get("cron")?.as_str()) + .any(|cron| { + matches!( + cron.split_whitespace().collect::>()[..], + [_, _, "*", "*", _] + ) + }) + } + + /// Whether a step runs an audit. + fn audits(&self) -> bool { + self.steps() + .filter_map(|step| step.get("run")?.as_str()) + .any(|run| AUDITS.iter().any(|audit| run.contains(audit))) + } +} + +impl Repository { + /// A repository where no workflow audits the dependencies at least + /// weekly. + pub(super) fn scheduled_audit(&self) -> Vec { + if self + .workflows + .iter() + .any(|workflow| workflow.weekly() && workflow.audits()) + { + return Vec::new(); + } + vec![Finding { + path: ".github/workflows".into(), + line: 1, + message: "no workflow audits the dependencies on a schedule: add one running \ + `just audit` at least weekly, such as `cron: \"0 8 * * 1\"`" + .to_owned(), + }] + } +} diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index 481b0a8..d8ef909 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -22,6 +22,7 @@ mod recipes; mod recipes_only; mod report; mod rustfmt_config; +mod scheduled_audit; mod t3_setup; mod toolchain; mod unsafe_modules; diff --git a/crates/sourceorigin-standard/src/rules/tests/scheduled_audit.rs b/crates/sourceorigin-standard/src/rules/tests/scheduled_audit.rs new file mode 100644 index 0000000..ac4011f --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/scheduled_audit.rs @@ -0,0 +1,34 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +/// A workflow running `just audit` on `cron`. +fn audit(cron: &str) -> String { + format!( + "on:\n schedule:\n - cron: \"{cron}\"\njobs:\n audit:\n steps:\n \ + - run: just audit\n" + ) +} + +#[test] +fn a_workflow_audits_the_dependencies_weekly() { + let fixture = Fixture::new(); + fixture + .add_crate("core", INHERITS) + .write(".github/workflows/audit.yml", &audit("0 0 1 * *")) + .write( + ".github/workflows/ci.yml", + "on: push\njobs:\n checks:\n steps:\n - run: just audit\n", + ); + assert_eq!( + fixture.violations(Rule::ScheduledAudit), + [ + ".github/workflows:1: no workflow audits the dependencies on a schedule: add one \ + running `just audit` at least weekly, such as `cron: \"0 8 * * 1\"` (scheduled-audit)" + ] + ); + fixture.write(".github/workflows/audit.yml", &audit("0 8 * * 1")); + assert_eq!( + fixture.violations(Rule::ScheduledAudit), + Vec::::new() + ); +}