Skip to content

Commit f6b1daa

Browse files
author
Alvaro Muñoz
committed
Improve query
1 parent 2359e2d commit f6b1daa

File tree

6 files changed

+73
-6
lines changed

6 files changed

+73
-6
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ class SetupJavaUsesStep extends CacheWritingStep, UsesStep {
2121
}
2222

2323
class SetupGoUsesStep extends CacheWritingStep, UsesStep {
24-
SetupGoUsesStep() { this.getCallee() = "actions/setup-go" }
24+
SetupGoUsesStep() {
25+
this.getCallee() = "actions/setup-go" and
26+
(
27+
not exists(this.getArgument("cache"))
28+
or
29+
this.getArgument("cache") = "true"
30+
)
31+
}
2532
}
2633

2734
class SetupNodeUsesStep extends CacheWritingStep, UsesStep {
@@ -48,7 +55,7 @@ class SetupDotnetUsesStep extends CacheWritingStep, UsesStep {
4855
SetupDotnetUsesStep() {
4956
this.getCallee() = "actions/setup-dotnet" and
5057
(
51-
exists(this.getArgument("cache")) or
58+
this.getArgument("cache") = "true" or
5259
exists(this.getArgument("cache-dependency-path"))
5360
)
5461
}

ql/src/Security/CWE-349/CachePoisoning.ql

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
import actions
1515
import codeql.actions.security.UntrustedCheckoutQuery
1616
import codeql.actions.security.CachePoisoningQuery
17+
import codeql.actions.security.PoisonableSteps
1718

18-
from LocalJob j
19+
from LocalJob j, PRHeadCheckoutStep checkout
1920
where
2021
// The workflow runs in the context of the default branch
2122
// TODO: (require to collect trigger types)
@@ -29,7 +30,14 @@ where
2930
"repository_dispatch", "schedule", "watch", "workflow_run"
3031
]) and
3132
// The job checkouts untrusted code from a pull request
32-
j.getAStep() instanceof PRHeadCheckoutStep and
33-
// The job writes to the cache
34-
j.getAStep() instanceof CacheWritingStep
33+
j.getAStep() = checkout and
34+
(
35+
// The job writes to the cache
36+
// (No need to follow the checkout step as the cache writing is normally done after the job completes)
37+
j.getAStep() instanceof CacheWritingStep
38+
or
39+
// The job executes checked-out code
40+
// (The cache specific token can be leaked even for non-privileged workflows)
41+
checkout.getAFollowingStep() instanceof PoisonableStep
42+
)
3543
select j.getAStep().(CacheWritingStep), "Potential cache poisoning on privileged workflow."
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Cache Poisoning
2+
3+
on: pull_request_target
4+
5+
jobs:
6+
poison:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
with:
11+
ref: ${{ github.event.pull_request.head.sha }}
12+
- uses: actions/setup-go@v2
13+
with:
14+
go-version-file: 'go.mod'
15+
cache: false
16+
- run: do some go stuff
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Cache Poisoning
2+
3+
on: pull_request_target
4+
5+
jobs:
6+
poison:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
with:
11+
ref: ${{ github.event.pull_request.head.sha }}
12+
- uses: actions/setup-go@v2
13+
with:
14+
go-version-file: 'go.mod'
15+
cache: true
16+
- run: do some go stuff
17+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Cache Poisoning
2+
3+
on: pull_request_target
4+
5+
jobs:
6+
poison:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
with:
11+
ref: ${{ github.event.pull_request.head.sha }}
12+
- uses: actions/setup-go@v2
13+
with:
14+
go-version-file: 'go.mod'
15+
- run: do some go stuff
16+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
| .github/workflows/test1.yml:17:9:21:6 | Uses Step | Potential cache poisoning on privileged workflow. |
22
| .github/workflows/test2.yml:12:9:16:6 | Uses Step | Potential cache poisoning on privileged workflow. |
33
| .github/workflows/test3.yml:12:9:20:6 | Uses Step | Potential cache poisoning on privileged workflow. |
4+
| .github/workflows/test6.yml:12:9:16:6 | Uses Step | Potential cache poisoning on privileged workflow. |
5+
| .github/workflows/test7.yml:12:9:15:6 | Uses Step | Potential cache poisoning on privileged workflow. |

0 commit comments

Comments
 (0)