Skip to content

Commit 22d0600

Browse files
author
Alvaro Muñoz
committed
Support more PR head checkouts
1 parent d12b248 commit 22d0600

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

ql/lib/codeql/actions/Ast.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ abstract class SimpleReferenceExpression extends AstNode instanceof SimpleRefere
232232
AstNode getTarget() { result = super.getTarget() }
233233
}
234234

235-
class StepsExpression extends SimpleReferenceExpression instanceof StepsExpressionImpl { }
235+
class StepsExpression extends SimpleReferenceExpression instanceof StepsExpressionImpl {
236+
string getStepId() { result = super.getStepId() }
237+
}
236238

237239
class NeedsExpression extends SimpleReferenceExpression instanceof NeedsExpressionImpl { }
238240

ql/lib/codeql/actions/ast/internal/Ast.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,8 @@ class StepsExpressionImpl extends SimpleReferenceExpressionImpl {
858858
this.getEnclosingJob() = result.getEnclosingJob() and
859859
result.(StepImpl).getId() = stepId
860860
}
861+
862+
string getStepId() { result = stepId }
861863
}
862864

863865
/**

ql/src/Security/CWE-829/UntrustedCheckout.ql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ predicate containsHeadRef(string s) {
6666
"\\bgithub\\.event\\.check_run\\.pull_requests\\[\\d+\\]\\.head\\.sha\\b",
6767
"\\bgithub\\.event\\.check_run\\.pull_requests\\[\\d+\\]\\.id\\b",
6868
"\\bgithub\\.event\\.check_run\\.pull_requests\\[\\d+\\]\\.number\\b",
69+
"\\bhead\\.sha\\b", "\\bhead\\.ref\\b"
6970
], _, _)
7071
)
7172
}
@@ -80,6 +81,14 @@ class ActionsCheckout extends PRHeadCheckoutStep instanceof UsesStep {
8081
(
8182
containsHeadRef(this.getArgumentExpr("ref").getExpression())
8283
or
84+
exists(StepsExpression e |
85+
this.getArgumentExpr("ref") = e and
86+
(
87+
e.getStepId().matches(["%sha%", "%head%", "branch"]) or
88+
e.getFieldName().matches(["%sha%", "%head%", "branch"])
89+
)
90+
)
91+
or
8392
exists(UsesStep head |
8493
head.getCallee() = ["eficode/resolve-pr-refs", "xt0rted/pull-request-comment-branch"] and
8594
DataFlow::hasLocalFlowExpr(head, this.getArgumentExpr("ref"))

ql/test/query-tests/Security/CWE-829/UntrustedCheckout.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@
1010
| .github/workflows/issue_comment_direct.yml:28:9:32:2 | Uses Step | Potential unsafe checkout of untrusted pull request on privileged workflow. |
1111
| .github/workflows/issue_comment_direct.yml:35:9:40:2 | Uses Step | Potential unsafe checkout of untrusted pull request on privileged workflow. |
1212
| .github/workflows/issue_comment_direct.yml:43:9:46:126 | Uses Step | Potential unsafe checkout of untrusted pull request on privileged workflow. |
13+
| .github/workflows/issue_comment_heuristic.yml:28:9:33:2 | Uses Step | Potential unsafe checkout of untrusted pull request on privileged workflow. |
14+
| .github/workflows/issue_comment_heuristic.yml:48:7:50:46 | Uses Step | Potential unsafe checkout of untrusted pull request on privileged workflow. |
15+
| .github/workflows/issue_comment_octokit.yml:26:9:30:6 | Uses Step | Potential unsafe checkout of untrusted pull request on privileged workflow. |
16+
| .github/workflows/issue_comment_octokit.yml:30:9:35:2 | Uses Step | Potential unsafe checkout of untrusted pull request on privileged workflow. |
17+
| .github/workflows/issue_comment_octokit.yml:57:9:62:2 | Uses Step | Potential unsafe checkout of untrusted pull request on privileged workflow. |
18+
| .github/workflows/issue_comment_octokit.yml:79:9:83:2 | Uses Step | Potential unsafe checkout of untrusted pull request on privileged workflow. |
19+
| .github/workflows/issue_comment_octokit.yml:95:9:100:2 | Uses Step | Potential unsafe checkout of untrusted pull request on privileged workflow. |
20+
| .github/workflows/issue_comment_octokit.yml:109:9:114:66 | Uses Step | Potential unsafe checkout of untrusted pull request on privileged workflow. |
1321
| .github/workflows/untrusted_checkout.yml:9:7:13:4 | Uses Step | Potential unsafe checkout of untrusted pull request on privileged workflow. |

0 commit comments

Comments
 (0)