1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//! Audits reusable workflows and action usage for confusable refs.
//!
//! This is similar to "impostor" commit detection, but with only named
//! refs instead of fully pinned commits: a user may pin a ref such as
//! `@foo` thinking that `foo` will always refer to either a branch or a tag,
//! but the upstream repository may host *both* a branch and a tag named
//! `foo`, making it unclear to the end user which is selected.
use anyhow::anyhow;
use github_actions_models::common::{RepositoryUses, Uses};
use super::{Audit, AuditLoadError, Job, audit_meta};
use crate::audit::AuditError;
use crate::finding::Finding;
use crate::finding::location::Locatable as _;
use crate::models::{StepCommon, action::CompositeStep};
use crate::{
finding::{Confidence, Severity},
github,
models::uses::RepositoryUsesExt as _,
state::AuditState,
};
const REF_CONFUSION_ANNOTATION: &str =
"uses a ref that's provided by both the branch and tag namespaces";
pub(crate) struct RefConfusion {
client: github::Client,
}
audit_meta!(
RefConfusion,
"ref-confusion",
"git ref for action with ambiguous ref type"
);
impl RefConfusion {
async fn confusable(&self, uses: &RepositoryUses) -> Result<bool, AuditError> {
let Some(sym_ref) = uses.symbolic_ref() else {
return Ok(false);
};
// TODO: use a tokio JoinSet here?
let branches_match = self
.client
.has_branch(uses.owner(), uses.repo(), sym_ref)
.await
.map_err(Self::err)?;
let tags_match = self
.client
.has_tag(uses.owner(), uses.repo(), sym_ref)
.await
.map_err(Self::err)?;
// If both the branch and tag namespaces have a match, we have a
// confusable ref.
Ok(branches_match && tags_match)
}
}
#[async_trait::async_trait]
impl Audit for RefConfusion {
fn new(state: &AuditState) -> Result<Self, AuditLoadError>
where
Self: Sized,
{
if state.no_online_audits {
return Err(AuditLoadError::Skip(anyhow!(
"offline audits only requested"
)));
}
state
.gh_client
.clone()
.ok_or_else(|| AuditLoadError::Skip(anyhow!("can't run without a GitHub API token")))
.map(|client| RefConfusion { client })
}
async fn audit_workflow<'doc>(
&self,
workflow: &'doc crate::models::workflow::Workflow,
_config: &crate::config::Config,
) -> Result<Vec<crate::finding::Finding<'doc>>, AuditError> {
let mut findings = vec![];
for job in workflow.jobs() {
match job {
Job::NormalJob(normal) => {
for step in normal.steps() {
let Some(Uses::Repository(uses)) = step.uses() else {
continue;
};
if self.confusable(uses).await? {
findings.push(
Self::finding()
.severity(Severity::Medium)
.confidence(Confidence::High)
.add_location(
step.location()
.primary()
.with_keys(["uses".into()])
.annotated(REF_CONFUSION_ANNOTATION),
)
.build(workflow)
.map_err(Self::err)?,
);
}
}
}
Job::ReusableWorkflowCallJob(reusable) => {
let Uses::Repository(uses) = &reusable.uses else {
continue;
};
if self.confusable(uses).await? {
findings.push(
Self::finding()
.severity(Severity::Medium)
.confidence(Confidence::High)
.add_location(
reusable
.location()
.primary()
.annotated(REF_CONFUSION_ANNOTATION),
)
.build(workflow)
.map_err(Self::err)?,
)
}
}
}
}
Ok(findings)
}
async fn audit_composite_step<'a>(
&self,
step: &CompositeStep<'a>,
_config: &crate::config::Config,
) -> Result<Vec<Finding<'a>>, AuditError> {
let mut findings = vec![];
let Some(Uses::Repository(uses)) = step.uses() else {
return Ok(findings);
};
if self.confusable(uses).await? {
findings.push(
Self::finding()
.severity(Severity::Medium)
.confidence(Confidence::High)
.add_location(
step.location()
.primary()
.with_keys(["uses".into()])
.annotated(REF_CONFUSION_ANNOTATION),
)
.build(step)
.map_err(Self::err)?,
);
}
Ok(findings)
}
}