Merge branch 'main' into knewbury01/adjust-actions-queries-untrusted-… · github/codeql@5503140 · GitHub
Skip to content

Commit 5503140

Browse files
authored
Merge branch 'main' into knewbury01/adjust-actions-queries-untrusted-checkout-second-iteration
2 parents a094a8e + c25398e commit 5503140

111 files changed

Lines changed: 17761 additions & 5490 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎actions/ql/examples/snippets/uses_pinned_sha.ql‎

Lines changed: 1 addition & 1 deletion
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The GitHub Actions analysis now recognizes more Bash regex checks that restrict a value to alphanumeric characters, include regexes like `^[0-9a-zA-Z]{40}([0-9a-zA-Z]{24})?$` which check for a sha1 or sha256 hash. This may reduce false positive results where command output is validated with grouped or optional alphanumeric patterns before being used.

‎actions/ql/lib/codeql/actions/Bash.qll‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,22 @@ module Bash {
785785

786786
/**
787787
* Holds if the given regex is used to match an alphanumeric string
788-
* eg: `^[0-9a-zA-Z]{40}$`, `^[0-9]+$` or `^[a-zA-Z0-9_]+$`
788+
* eg: `^[0-9a-zA-Z]{40}([0-9a-zA-Z]{24})?$`, `^[0-9]+$` or `^[a-zA-Z0-9_]+$`
789789
*/
790-
string alphaNumericRegex() { result = "^\\^\\[([09azAZ_-]+)\\](\\+|\\{\\d+\\})\\$$" }
790+
string alphaNumericRegex() {
791+
exists(string r1, string r2, string r3, string r4 |
792+
// An alphanumeric character class
793+
r1 = "\\[([09azAZ_-]+)\\]" and
794+
// The same as above, followed by a quantifier like `+` or `{20}`
795+
r2 = r1 + "(\\+|\\{\\d+\\})" and
796+
// The same as above, possibly with parentheses around it
797+
r3 = "\\(?" + r2 + "\\)?" and
798+
// The same as above, possibly with a `?` after it
799+
r4 = r3 + "\\??"
800+
|
801+
// The same as above, repeated one or more times, and with `^` at the
802+
// beginning and `$` at the end
803+
result = "^\\^(" + r4 + ")+\\$$"
804+
)
805+
}
791806
}

‎actions/ql/src/Security/CWE-829/UnpinnedActionsTag.ql‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @name Unpinned tag for a non-immutable Action in workflow
2+
* @name Unpinned tag for a non-immutable Action in workflow or composite action
33
* @description Using a tag for a non-immutable Action that is not pinned to a commit can lead to executing an untrusted Action through a supply chain attack.
44
* @kind problem
55
* @security-severity 5.0
@@ -15,7 +15,9 @@ import actions
1515
import codeql.actions.security.UseOfUnversionedImmutableAction
1616

1717
bindingset[version]
18-
private predicate isPinnedCommit(string version) { version.regexpMatch("^[A-Fa-f0-9]{40}$") }
18+
private predicate isPinnedCommit(string version) {
19+
version.regexpMatch("^[A-Fa-f0-9]{40}([A-Fa-f0-9]{24})?$")
20+
}
1921

2022
bindingset[nwo]
2123
private predicate isTrustedOwner(string nwo) {
@@ -31,15 +33,26 @@ private predicate isPinnedContainer(string version) {
3133
bindingset[nwo]
3234
private predicate isContainerImage(string nwo) { nwo.regexpMatch("^docker://.+") }
3335

34-
from UsesStep uses, string nwo, string version, Workflow workflow, string name
36+
private predicate getStepContainerName(UsesStep uses, string name) {
37+
exists(Workflow workflow |
38+
uses.getEnclosingWorkflow() = workflow and
39+
(
40+
workflow.getName() = name
41+
or
42+
not exists(workflow.getName()) and workflow.getLocation().getFile().getBaseName() = name
43+
)
44+
)
45+
or
46+
exists(CompositeAction action |
47+
uses.getEnclosingCompositeAction() = action and
48+
name = action.getLocation().getFile().getBaseName()
49+
)
50+
}
51+
52+
from UsesStep uses, string nwo, string version, string name
3553
where
3654
uses.getCallee() = nwo and
37-
uses.getEnclosingWorkflow() = workflow and
38-
(
39-
workflow.getName() = name
40-
or
41-
not exists(workflow.getName()) and workflow.getLocation().getFile().getBaseName() = name
42-
) and
55+
getStepContainerName(uses, name) and
4356
uses.getVersion() = version and
4457
not isTrustedOwner(nwo) and
4558
not (if isContainerImage(nwo) then isPinnedContainer(version) else isPinnedCommit(version)) and
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `actions/unpinned-tag` query now analyzes composite action metadata (`action.yml`/`action.yaml` files) in addition to workflow files, providing more comprehensive detection of unpinned action references across the entire Actions ecosystem.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: majorAnalysis
3+
---
4+
* Adjusted `actions/untrusted-checkout/critical` to align more with other untrusted resource queries, where the alert location is the location where the artifact is obtained from (the checkout point). This aligns with the other 2 related queries. This will cause the same alerts to re-open for closed alerts of this query.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `actions/unpinned-tag` query now recognizes 64-character SHA-256 commit hashes as properly pinned references, in addition to 40-character SHA-1 hashes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: Composite unpinned tag test
2+
runs:
3+
using: "composite"
4+
steps:
5+
- uses: foo/bar@v2
6+
- uses: foo/bar@25b062c917b0c75f8b47d8469aff6c94ffd89abb

‎actions/ql/test/query-tests/Security/CWE-829/.github/workflows/unpinned_tags.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ jobs:
1111
- uses: foo/bar@25b062c917b0c75f8b47d8469aff6c94ffd89abb
1212
- uses: docker://foo/bar@latest
1313
- uses: docker://foo/bar@sha256:887a259a5a534f3c4f36cb02dca341673c6089431057242cdc931e9f133147e9
14+
# SHA-256 pinned (64 hex chars) - should NOT be flagged
15+
- uses: foo/bar@25b062c917b0c75f8b47d8469aff6c94ffd89abb25b062c917b0c75f8b47d84d
16+
# SHA-1 pinned (40 hex chars) regression - should NOT be flagged
17+
- uses: foo/bar@a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2
18+
# Invalid 50-char hex string - should be flagged
19+
- uses: foo/bar@a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2a1b2c3d4e5

‎actions/ql/test/query-tests/Security/CWE-829/UnpinnedActionsTag.expected‎

Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)