Skip to content
Merged
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
41 changes: 13 additions & 28 deletions crates/zizmor/src/audit/artipacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use github_actions_models::common::{
expr::{ExplicitExpr, LoE},
};
use itertools::Itertools as _;
use subfeature::Subfeature;

use super::{Audit, AuditLoadError, audit_meta};
use crate::{
Expand Down Expand Up @@ -111,34 +110,16 @@ impl Artipacked {
continue;
};

let with = match with {
LoE::Literal(with) => with,
// Emit blanket pedantic finding if the `with:` block cannot be analyzed
LoE::Expr(_) => {
findings.push(
Self::finding()
.severity(Severity::Informational)
.confidence(Confidence::High)
.persona(Persona::Pedantic)
.add_location(
step.location()
.with_keys(["uses".into()])
.subfeature(Subfeature::new(0, uses.raw()))
.annotated("this checkout"),
)
.add_location(
step.location()
.primary()
.with_keys(["with".into()])
.annotated("may not set persist-credentials: false"),
)
.build(&step)?,
);
continue;
}
};

if uses.matches("actions/checkout") {
let with = match with {
LoE::Literal(with) => with,
LoE::Expr(_) => {
// We don't emit a finding if the entire `with` block
// is an expression, since the `obfuscation` audit covers that.
continue;
}
};

let is_v6_or_higher = self
.is_checkout_v6_or_higher(uses)
.await
Expand All @@ -160,6 +141,10 @@ impl Artipacked {
_ => vulnerable_checkouts.push((step, Persona::default(), is_v6_or_higher)),
}
} else if uses.matches("actions/upload-artifact") {
let LoE::Literal(with) = with else {
continue;
};

let Some(EnvValue::String(path)) = with.get("path") else {
continue;
};
Expand Down
179 changes: 120 additions & 59 deletions crates/zizmor/src/audit/unpinned_tools.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
use github_actions_models::common::{EnvValue, Uses, expr::LoE};
use std::sync::LazyLock;

use github_actions_models::common::Uses;
use subfeature::Subfeature;

use crate::audit::{Audit, AuditError, audit_meta};
use crate::config::Config;
use crate::finding::{Confidence, Finding, Severity};
use crate::models::StepBodyCommon;
use crate::models::uses::RepositoryUsesExt as _;
use crate::models::{StepCommon, action::CompositeStep, workflow::Step};
use crate::models::{
StepBodyCommon, StepCommon,
action::CompositeStep,
coordinate::{ActionCoordinate, ControlExpr, ControlFieldType, ControlOrigin, Toggle, Usage},
workflow::Step,
};
use crate::state::AuditState;
use crate::utils::ExtractedExpr;

use super::AuditLoadError;

static KNOWN_UNPINNED_TOOLS_ACTIONS: &[&str] =
&["aquasecurity/setup-trivy", "1password/load-secrets-action"];
#[allow(clippy::unwrap_used)]
static KNOWN_UNPINNED_TOOLS_ACTIONS: LazyLock<Vec<ActionCoordinate>> = LazyLock::new(|| {
["aquasecurity/setup-trivy", "1password/load-secrets-action"]
.into_iter()
.map(|uses_pattern| ActionCoordinate::Configurable {
uses_pattern: uses_pattern.parse().unwrap(),
control: ControlExpr::field(
Toggle::OptIn,
"version",
ControlFieldType::Exact(&["latest"]),
true,
),
})
.chain([ActionCoordinate::Configurable {
uses_pattern: "extractions/setup-just".parse().unwrap(),
control: ControlExpr::field(
Toggle::OptIn,
"just-version",
ControlFieldType::Exact(&["*"]),
true,
),
}])
.collect()
});

pub(crate) struct UnpinnedTools;

Expand All @@ -32,67 +58,102 @@ impl UnpinnedTools {

let Some(StepBodyCommon::Uses {
uses: Uses::Repository(uses),
with: LoE::Literal(with),
..
}) = step.body()
else {
return Ok(findings);
};

if !KNOWN_UNPINNED_TOOLS_ACTIONS
let Some(usage) = KNOWN_UNPINNED_TOOLS_ACTIONS
.iter()
.any(|action| uses.matches(action))
{
.find_map(|coordinate| coordinate.usage(step))
else {
return Ok(findings);
}
};

let finding = match with.get("version") {
None => Some(
Self::finding()
.confidence(Confidence::High)
.severity(Severity::Medium)
.add_location(
step.location()
.primary()
.with_keys(["uses".into()])
.subfeature(Subfeature::new(0, uses.raw()))
.annotated("action implicitly uses an unpinned latest version"),
),
),
Some(EnvValue::String(v)) if v == "latest" => Some(
Self::finding()
.confidence(Confidence::High)
.severity(Severity::Medium)
.add_location(
step.location()
.with_keys(["uses".into()])
.subfeature(Subfeature::new(0, uses.raw()))
.annotated("this action"),
let finding = match usage {
// The action is definitely installing an unpinned tool, because...
Usage::Enabled(origins) => {
if let Some(field) = origins.iter().find_map(|origin| match origin {
ControlOrigin::Default { field } => Some(*field),
_ => None,
}) {
// (a) the user didn't set any input, and the default is unpinned.
Some(
Self::finding()
.confidence(Confidence::High)
.severity(Severity::Medium)
.add_location(
step.location()
.primary()
.with_keys(["uses".into()])
.subfeature(Subfeature::new(0, uses.raw()))
.annotated(format!(
"omitting `{field}` implicitly selects an unpinned tool version"
)),
),
)
.add_location(
step.location()
.primary()
.with_keys(["with".into(), "version".into()])
.annotated("specifies `version: latest` which is unpinned"),
),
),
Some(EnvValue::String(v)) if ExtractedExpr::from_fenced(v).is_some() => Some(
Self::finding()
.confidence(Confidence::Low)
.severity(Severity::Medium)
.add_location(
step.location()
.with_keys(["uses".into()])
.subfeature(Subfeature::new(0, uses.raw()))
.annotated("this action"),
)
.add_location(
step.location()
.primary()
.with_keys(["with".into(), "version".into()])
.annotated("specifies `version` dynamically, which may be unpinned"),
),
),
Some(_) => None,
} else {
// (b) The user explicitly set the field to enable unpinning,
// e.g. `version: latest`.
origins
.iter()
.find_map(|origin| match origin {
ControlOrigin::Input { field } => Some(*field),
_ => None,
})
.map(|field| {
Self::finding()
.confidence(Confidence::High)
.severity(Severity::Medium)
.add_location(
step.location()
.with_keys(["uses".into()])
.subfeature(Subfeature::new(0, uses.raw()))
.annotated("this action"),
)
.add_location(
step.location()
.primary()
.with_keys(["with".into(), field.into()])
.annotated("selects an unpinned tool version".to_string()),
)
})
}
}
// The action *might* be installing an unpinned tool, but we don't
// know for sure because the input that controls pinning is
// opaque (e.g. `version: ${{ expr }}`).
// TODO: we could probably pierce some expressions here, e.g. `matrix`
// and `input` evaluations. The coordinate API could also potentially
// do that for us.
Usage::Conditional(origins) => origins
.iter()
.find_map(|origin| match origin {
ControlOrigin::Input { field } => Some(*field),
_ => None,
})
.map(|field| {
Self::finding()
.confidence(Confidence::Low)
.severity(Severity::Medium)
.add_location(
step.location()
.with_keys(["uses".into()])
.subfeature(Subfeature::new(0, uses.raw()))
.annotated("this action"),
)
.add_location(
step.location()
.primary()
.with_keys(["with".into(), field.into()])
.annotated("tool version may be unpinned".to_string()),
)
}),
// TODO: We don't have any of these at the moment, but in the future
// we might detect actions that always install an unpinned version
// no matter what.
Usage::Always => None,
};

if let Some(finding) = finding {
Expand Down
26 changes: 0 additions & 26 deletions crates/zizmor/tests/integration/audit/artipacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,32 +145,6 @@ fn test_issue_1709() -> anyhow::Result<()> {
Ok(())
}

/// Fix #1769: dynamic `with:` clause emits a blanket pedantic finding that
/// the clause cannot be analyzed.
///
/// See <https://github.com/zizmorcore/zizmor/issues/1769>
#[test]
fn test_issue_1769() -> anyhow::Result<()> {
insta::assert_snapshot!(
zizmor().input(input_under_test("artipacked/issue-1769-repro.yml")).args(["--persona=pedantic"]).run()?,
@"
info[artipacked]: credential persistence through GitHub Actions artifacts
--> @@INPUT@@:19:9
|
18 | - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # zizmor: ignore[obfuscation]
| --------------------------------------------------------- this checkout
19 | with: ${{ fromJson(steps.setup.outputs.options) }}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ may not set persist-credentials: false
|
= note: audit confidence → High

2 findings (1 ignored): 1 informational, 0 low, 0 medium, 0 high
"
);

Ok(())
}

/// Ensures that the artipacked audit works correctly on composite actions.
#[test]
fn test_composite_action() -> anyhow::Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions crates/zizmor/tests/integration/audit/obfuscation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,14 @@ fn test_issue_1769() -> Result<()> {
info[obfuscation]: obfuscated usage of GitHub Actions features
--> @@INPUT@@:19:9
|
18 | - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # zizmor: ignore[artipacked]
18 | - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
| --------------------------------------------------------------- this action
19 | with: ${{ fromJson(steps.setup.outputs.options) }}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of an expression for `with:` prevents analysis
|
= note: audit confidence → High

2 findings (1 suppressed): 1 informational, 0 low, 0 medium, 0 high
1 finding: 1 informational, 0 low, 0 medium, 0 high
"
);

Expand Down
Loading