Skip to content

Commit ff2bb7d

Browse files
Copilotowen-mc
authored andcommitted
Widen pinned SHA regex to support SHA-256 (64-char hex) and add tests
1 parent 5508b15 commit ff2bb7d

5 files changed

Lines changed: 12 additions & 3 deletions

File tree

actions/ql/examples/snippets/uses_pinned_sha.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
import actions
99

1010
from UsesStep uses
11-
where uses.getVersion().regexpMatch("^[A-Fa-f0-9]{40}$")
11+
where uses.getVersion().regexpMatch("^[A-Fa-f0-9]{40}([A-Fa-f0-9]{24})?$")
1212
select uses, "This 'uses' step has a pinned SHA version."

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ 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
*/
790790
string alphaNumericRegex() { result = "^\\^\\[([09azAZ_-]+)\\](\\+|\\{\\d+\\})\\$$" }
791791
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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) {

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
| .github/workflows/test18.yml:37:21:37:63 | sonarsource/sonarcloud-github-action@master | Unpinned 3rd party Action 'Sonar' step $@ uses 'sonarsource/sonarcloud-github-action' with ref 'master', not a pinned commit hash | .github/workflows/test18.yml:36:15:40:58 | Uses Step | Uses Step |
3434
| .github/workflows/unpinned_tags.yml:10:13:10:22 | foo/bar@v1 | Unpinned 3rd party Action 'unpinned_tags.yml' step $@ uses 'foo/bar' with ref 'v1', not a pinned commit hash | .github/workflows/unpinned_tags.yml:10:7:11:4 | Uses Step | Uses Step |
3535
| .github/workflows/unpinned_tags.yml:12:13:12:35 | docker://foo/bar@latest | Unpinned 3rd party Action 'unpinned_tags.yml' step $@ uses 'docker://foo/bar' with ref 'latest', not a pinned commit hash | .github/workflows/unpinned_tags.yml:12:7:13:4 | Uses Step | Uses Step |
36+
| .github/workflows/unpinned_tags.yml:19:13:19:70 | foo/bar@a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2a1b2c3d4e5 | Unpinned 3rd party Action 'unpinned_tags.yml' step $@ uses 'foo/bar' with ref 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2a1b2c3d4e5', not a pinned commit hash | .github/workflows/unpinned_tags.yml:19:7:19:70 | Uses Step | Uses Step |

0 commit comments

Comments
 (0)