Skip to content

Commit 9390f0c

Browse files
Merge pull request step-security#2597 from vamshi-stepsecurity/bug/hr-config
avoid duplicate addition for custom actions
2 parents 8fd04df + edd2d5a commit 9390f0c

6 files changed

Lines changed: 160 additions & 1 deletion

File tree

remediation/workflow/hardenrunner/addaction.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ func AddAction(inputYaml string, hardenRunnerConfig HardenRunnerConfig, pinActio
9393
return "", updated, fmt.Errorf("unable to parse yaml %v", err)
9494
}
9595

96+
// Extract the action path from the config to detect custom actions already present.
97+
configAction := getActionFromConfig(hardenRunnerConfig)
98+
configActionPath := strings.Split(configAction, "@")[0]
99+
96100
// Build a map of jobName → yaml.Node for runs-on label lookup
97101
jobNodeMap := map[string]*yaml.Node{}
98102
if hardenRunnerConfig.SkipHardenRunner && len(hardenRunnerConfig.RunnerLabels) > 0 {
@@ -128,7 +132,7 @@ func AddAction(inputYaml string, hardenRunnerConfig HardenRunnerConfig, pinActio
128132
}
129133
alreadyPresent := false
130134
for _, step := range job.Steps {
131-
if len(step.Uses) > 0 && strings.HasPrefix(step.Uses, HardenRunnerActionPath) {
135+
if len(step.Uses) > 0 && (strings.HasPrefix(step.Uses, HardenRunnerActionPath) || strings.HasPrefix(step.Uses, configActionPath)) {
132136
alreadyPresent = true
133137
break
134138
}

remediation/workflow/hardenrunner/addaction_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ func TestCustomActionConfig(t *testing.T) {
9191
wantUpdated: true,
9292
outputFile: "customActionSubtractive.yml",
9393
},
94+
{
95+
name: "three jobs: custom present, harden-runner present, empty gets action",
96+
inputFile: "customActionAlreadyPresent.yml",
97+
config: HardenRunnerConfig{Config: customConfig},
98+
wantUpdated: true,
99+
outputFile: "customActionAlreadyPresent.yml",
100+
},
101+
{
102+
name: "subtractive three jobs: custom unchanged, harden-runner replaced, empty gets action",
103+
inputFile: "customActionAlreadyPresentSubtractive.yml",
104+
config: HardenRunnerConfig{Config: customConfig, Subtractive: true},
105+
wantUpdated: true,
106+
outputFile: "customActionAlreadyPresentSubtractive.yml",
107+
},
94108
}
95109

96110
for _, tt := range tests {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: test-custom-action-three-jobs
2+
on:
3+
push:
4+
jobs:
5+
build:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Security Scanner
9+
uses: org/security-scanner@v3
10+
with:
11+
mode: strict
12+
scan-level: deep
13+
14+
- uses: actions/checkout@v3
15+
- run: echo "build"
16+
17+
lint:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Harden the runner (Audit all outbound calls)
21+
uses: step-security/harden-runner@v2
22+
with:
23+
egress-policy: audit
24+
25+
- uses: actions/checkout@v3
26+
- run: echo "lint"
27+
28+
deploy:
29+
runs-on: ubuntu-latest
30+
steps:
31+
- uses: actions/checkout@v3
32+
- run: echo "deploy"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: test-custom-action-three-jobs-subtractive
2+
on:
3+
push:
4+
jobs:
5+
build:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Security Scanner
9+
uses: org/security-scanner@v3
10+
with:
11+
mode: strict
12+
scan-level: deep
13+
14+
- uses: actions/checkout@v3
15+
- run: echo "build"
16+
17+
lint:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Harden the runner (Audit all outbound calls)
21+
uses: step-security/harden-runner@v2
22+
with:
23+
egress-policy: audit
24+
25+
- uses: actions/checkout@v3
26+
- run: echo "lint"
27+
28+
deploy:
29+
runs-on: ubuntu-latest
30+
steps:
31+
- uses: actions/checkout@v3
32+
- run: echo "deploy"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: test-custom-action-three-jobs
2+
on:
3+
push:
4+
jobs:
5+
build:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Security Scanner
9+
uses: org/security-scanner@v3
10+
with:
11+
mode: strict
12+
scan-level: deep
13+
14+
- uses: actions/checkout@v3
15+
- run: echo "build"
16+
17+
lint:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Harden the runner (Audit all outbound calls)
21+
uses: step-security/harden-runner@v2
22+
with:
23+
egress-policy: audit
24+
25+
- uses: actions/checkout@v3
26+
- run: echo "lint"
27+
28+
deploy:
29+
runs-on: ubuntu-latest
30+
steps:
31+
- name: Security Scanner
32+
uses: org/security-scanner@v3
33+
with:
34+
mode: strict
35+
scan-level: deep
36+
37+
- uses: actions/checkout@v3
38+
- run: echo "deploy"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: test-custom-action-three-jobs-subtractive
2+
on:
3+
push:
4+
jobs:
5+
build:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Security Scanner
9+
uses: org/security-scanner@v3
10+
with:
11+
mode: strict
12+
scan-level: deep
13+
14+
- uses: actions/checkout@v3
15+
- run: echo "build"
16+
17+
lint:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Security Scanner
21+
uses: org/security-scanner@v3
22+
with:
23+
mode: strict
24+
scan-level: deep
25+
26+
- uses: actions/checkout@v3
27+
- run: echo "lint"
28+
29+
deploy:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- name: Security Scanner
33+
uses: org/security-scanner@v3
34+
with:
35+
mode: strict
36+
scan-level: deep
37+
38+
- uses: actions/checkout@v3
39+
- run: echo "deploy"

0 commit comments

Comments
 (0)