Skip to content

Commit d3df974

Browse files
Merge pull request #2531 from vamshi-stepsecurity/bug-replaceable-actions
Bug replaceable actions
2 parents 8fd4b72 + 76debb9 commit d3df974

File tree

10 files changed

+76
-7
lines changed

10 files changed

+76
-7
lines changed

remediation/workflow/maintainedactions/getlatestrelease.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func getMajorVersion(version string) string {
3232

3333
func GetLatestRelease(ownerRepo string) (string, error) {
3434
splitOnSlash := strings.Split(ownerRepo, "/")
35-
if len(splitOnSlash) != 2 {
35+
if len(splitOnSlash) < 2 {
3636
return "", fmt.Errorf("invalid owner/repo format: %s", ownerRepo)
3737
}
3838
owner := splitOnSlash[0]

remediation/workflow/maintainedactions/maintainedActions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func ReplaceActions(inputYaml string, customerMaintainedActions map[string]strin
8181
if newAction, ok := actionMap[actionName]; ok {
8282
latestVersion, err := GetLatestRelease(newAction)
8383
if err != nil {
84-
return "", updated, fmt.Errorf("unable to get latest release: %v", err)
84+
return inputYaml, updated, fmt.Errorf("unable to get latest release: %v", err)
8585
}
8686
replacements = append(replacements, replacement{
8787
jobName: jobName,

remediation/workflow/maintainedactions/maintainedActions.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,5 +493,14 @@
493493
},
494494
"score": 10,
495495
"image": "https://avatars.githubusercontent.com/u/88700172?v=4"
496+
},
497+
{
498+
"name": "step-security/actions-cache/restore",
499+
"description": "GitHub Action to restore cache",
500+
"forkedFrom": {
501+
"name": "tespkg/actions-cache/restore"
502+
},
503+
"score": 10,
504+
"image": "https://avatars.githubusercontent.com/u/88700172?v=4"
496505
}
497506
]

remediation/workflow/maintainedactions/maintainedactions_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ func TestReplaceActions(t *testing.T) {
4141
"created_at": "2023-01-01T00:00:00Z"
4242
}`))
4343

44+
httpmock.RegisterResponder("GET", "https://api.github.com/repos/step-security/actions-cache/releases/latest",
45+
httpmock.NewStringResponder(200, `{
46+
"tag_name": "v1.0.0",
47+
"name": "v1.0.0",
48+
"body": "Release notes",
49+
"created_at": "2023-01-01T00:00:00Z"
50+
}`))
51+
4452
tests := []struct {
4553
name string
4654
inputFile string

remediation/workflow/secureworkflow.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func SecureWorkflow(queryStringParams map[string]string, inputYaml string, svc d
133133
if replaceMaintainedActions {
134134
secureWorkflowReponse.FinalOutput, replacedMaintainedActions, err = maintainedactions.ReplaceActions(secureWorkflowReponse.FinalOutput, maintainedActionsMap)
135135
if err != nil {
136+
log.Printf("Error replacing maintained actions: %v", err)
136137
secureWorkflowReponse.HasErrors = true
137138
}
138139
}

remediation/workflow/secureworkflow_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,28 @@ func TestSecureWorkflow(t *testing.T) {
186186
}
187187
]`))
188188

189+
httpmock.RegisterResponder("GET", "https://api.github.com/repos/step-security/actions-cache/commits/v1",
190+
httpmock.NewStringResponder(200, `d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0a1b2c3`))
191+
192+
httpmock.RegisterResponder("GET", "https://api.github.com/repos/step-security/actions-cache/git/matching-refs/tags/v1.",
193+
httpmock.NewStringResponder(200, `[
194+
{
195+
"ref": "refs/tags/v1.0.0",
196+
"object": {
197+
"sha": "d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0a1b2c3",
198+
"type": "commit"
199+
}
200+
}
201+
]`))
202+
203+
httpmock.RegisterResponder("GET", "https://api.github.com/repos/step-security/actions-cache/releases/latest",
204+
httpmock.NewStringResponder(200, `{
205+
"tag_name": "v1.0.0",
206+
"name": "v1.0.0",
207+
"body": "Release notes",
208+
"created_at": "2023-01-01T00:00:00Z"
209+
}`))
210+
189211
tests := []struct {
190212
fileName string
191213
wantPinnedActions bool
@@ -244,7 +266,6 @@ func TestSecureWorkflow(t *testing.T) {
244266
t.Errorf("unable to load the file %s", err)
245267
}
246268
output, err = SecureWorkflow(queryParams, string(input), &mockDynamoDBClient{}, []string{}, false, actionMap)
247-
248269
} else {
249270
output, err = SecureWorkflow(queryParams, string(input), &mockDynamoDBClient{})
250271
}
@@ -275,6 +296,10 @@ func TestSecureWorkflow(t *testing.T) {
275296
t.Errorf("test failed %s did not match expected PinnedActions value. Expected:%v Actual:%v", test.fileName, test.wantPinnedActions, output.PinnedActions)
276297
}
277298

299+
if output.AddedMaintainedActions != test.wantAddedMaintainedActions {
300+
t.Errorf("test failed %s did not match expected AddedMaintainedActions value. Expected:%v Actual:%v", test.fileName, test.wantAddedMaintainedActions, output.AddedMaintainedActions)
301+
}
302+
278303
}
279304
}
280305

testfiles/maintainedActions/input/oneJob.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ jobs:
1414
do_not_skip: '["release"]'
1515
- uses: chetan/git-restore-mtime-action@v1
1616
with:
17-
pattern: '**/*'
17+
pattern: '**/*'
18+
- uses: tespkg/actions-cache/restore@v1
19+
with:
20+
path: ~/.npm
21+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
22+
restore-keys: |
23+
${{ runner.os }}-node-

testfiles/maintainedActions/output/oneJob.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ jobs:
1414
do_not_skip: '["release"]'
1515
- uses: step-security/git-restore-mtime-action@v2
1616
with:
17-
pattern: '**/*'
17+
pattern: '**/*'
18+
- uses: step-security/actions-cache/restore@v1
19+
with:
20+
path: ~/.npm
21+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
22+
restore-keys: |
23+
${{ runner.os }}-node-

testfiles/secureworkflow/input/replaceactions.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ jobs:
2323
- uses: github/super-linter@v3
2424
env:
2525
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26-
DISABLE_ERRORS: true
26+
DISABLE_ERRORS: true
27+
- uses: tespkg/actions-cache/restore@v1
28+
with:
29+
path: ~/.npm
30+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
31+
restore-keys: |
32+
${{ runner.os }}-node-
33+

testfiles/secureworkflow/output/replaceactions.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,11 @@ jobs:
3333
- uses: github/super-linter@34b2f8032d759425f6b42ea2e52231b33ae05401 # v3.17.1
3434
env:
3535
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36-
DISABLE_ERRORS: true
36+
DISABLE_ERRORS: true
37+
- uses: step-security/actions-cache/restore@d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0a1b2c3 # v1.0.0
38+
with:
39+
path: ~/.npm
40+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
41+
restore-keys: |
42+
${{ runner.os }}-node-
43+

0 commit comments

Comments
 (0)