Skip to content
Draft
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
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `*`.
<!-- rules:end -->

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand Down
4 changes: 4 additions & 0 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod recipes;
mod recipes_only;
mod report;
mod rustfmt_config;
mod scheduled_audit;
mod settings;
mod source;
mod t3;
Expand Down Expand Up @@ -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.
Expand Down
57 changes: 57 additions & 0 deletions crates/sourceorigin-standard/src/rules/scheduled_audit.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<_>>()[..],
[_, _, "*", "*", _]
)
})
}

/// 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<Finding> {
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(),
}]
}
}
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions crates/sourceorigin-standard/src/rules/tests/scheduled_audit.rs
Original file line number Diff line number Diff line change
@@ -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::<String>::new()
);
}