Skip to content

Commit 6e8fc89

Browse files
committed
Add default branch name check
1 parent 3120d4b commit 6e8fc89

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

ql/lib/codeql/actions/dataflow/ExternalFlow.qll

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ private import codeql.actions.DataFlow
33
private import actions
44

55
predicate workflowDataModel(
6-
string path, string visibility, string job, string secrets_source, string permissions,
6+
string path, string trigger, string job, string secrets_source, string permissions,
77
string runner
88
) {
9-
Extensions::workflowDataModel(path, visibility, job, secrets_source, permissions, runner)
9+
Extensions::workflowDataModel(path, trigger, job, secrets_source, permissions, runner)
10+
}
11+
12+
predicate repositoryDataModel(
13+
string visibility, string default_branch_name
14+
) {
15+
Extensions::repositoryDataModel(visibility, default_branch_name)
1016
}
1117

1218
/**

ql/lib/codeql/actions/dataflow/internal/ExternalFlowExtensions.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ extensible predicate sinkModel(
2424
);
2525

2626
extensible predicate workflowDataModel(
27-
string path, string visibility, string job, string secrets_source, string permissions,
27+
string path, string trigger, string job, string secrets_source, string permissions,
2828
string runner
2929
);
30+
31+
extensible predicate repositoryDataModel(
32+
string visibility, string default_branch_name
33+
);

ql/lib/codeql/actions/security/CachePoisoningQuery.qll

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ string defaultBranchTriggerEvent() {
1010
]
1111
}
1212

13-
string defaultBranchNames() { result = ["main", "master", "default"] }
14-
1513
predicate runsOnDefaultBranch(Job j) {
1614
exists(Event e |
1715
j.getATriggerEvent() = e and
16+
exists(string default_branch_name |
17+
repositoryDataModel(_, default_branch_name)
18+
) and
1819
(
1920
e.getName() = defaultBranchTriggerEvent() and
2021
not e.getName() = "pull_request_target"
2122
or
2223
e.getName() = "push" and
23-
e.getAPropertyValue("branches") = defaultBranchNames()
24+
e.getAPropertyValue("branches") = default_branch_name
2425
or
2526
e.getName() = "pull_request_target" and
2627
(
@@ -30,18 +31,18 @@ predicate runsOnDefaultBranch(Job j) {
3031
// only branches-ignore filter
3132
e.hasProperty("branches-ignore") and
3233
not e.hasProperty("branches") and
33-
not e.getAPropertyValue("branches-ignore") = defaultBranchNames()
34+
not e.getAPropertyValue("branches-ignore") = default_branch_name
3435
or
3536
// only branches filter
3637
e.hasProperty("branches") and
3738
not e.hasProperty("branches-ignore") and
38-
e.getAPropertyValue("branches") = defaultBranchNames()
39+
e.getAPropertyValue("branches") = default_branch_name
3940
or
4041
// branches and branches-ignore filters
4142
e.hasProperty("branches") and
4243
e.hasProperty("branches-ignore") and
43-
e.getAPropertyValue("branches") = defaultBranchNames() and
44-
not e.getAPropertyValue("branches-ignore") = defaultBranchNames()
44+
e.getAPropertyValue("branches") = default_branch_name and
45+
not e.getAPropertyValue("branches-ignore") = default_branch_name
4546
)
4647
)
4748
)

ql/lib/ext/workflow-models/workflow-models.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
extensions:
2+
- addsTo:
3+
pack: githubsecuritylab/actions-all
4+
extensible: repositoryDataModel
5+
data: [
6+
- ["public", "main"]
7+
]
28
- addsTo:
39
pack: githubsecuritylab/actions-all
410
extensible: workflowDataModel

ql/test/library-tests/workflowenum.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import actions
22
import codeql.actions.dataflow.internal.ExternalFlowExtensions as Extensions
33

44
from
5-
string path, string visibility, string job, string secrets_source, string permissions,
5+
string path, string trigger, string job, string secrets_source, string permissions,
66
string runner
7-
where Extensions::workflowDataModel(path, visibility, job, secrets_source, permissions, runner)
8-
select visibility, path, job, secrets_source, permissions, runner
7+
where Extensions::workflowDataModel(path, trigger, job, secrets_source, permissions, runner)
8+
select trigger, path, job, secrets_source, permissions, runner

0 commit comments

Comments
 (0)