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 @@ -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.
<!-- 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 @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
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 @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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)]
);
}
49 changes: 49 additions & 0 deletions crates/sourceorigin-standard/src/rules/workflow_permissions.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
5 changes: 5 additions & 0 deletions crates/sourceorigin-standard/src/rules/workflows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
Loading