-
Notifications
You must be signed in to change notification settings - Fork 450
/
remediations.go
118 lines (100 loc) · 3.1 KB
/
remediations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2022 Security Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package remediation
import (
"errors"
"fmt"
"strings"
"sync"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/clients"
)
var (
branch string
repo string
once *sync.Once
setupErr error
)
var errInvalidArg = errors.New("invalid argument")
var (
workflowText = "update your workflow using https://app.stepsecurity.io/secureworkflow/%s/%s/%s?enable=%s"
//nolint
workflowMarkdown = "update your workflow using [https://app.stepsecurity.io](https://app.stepsecurity.io/secureworkflow/%s/%s/%s?enable=%s)"
dockerfilePinText = "pin your Docker image by updating %[1]s to %[1]s@%s"
)
//nolint:gochecknoinits
func init() {
once = new(sync.Once)
}
// Setup sets up remediation code.
func Setup(c *checker.CheckRequest) error {
once.Do(func() {
// Get the branch for remediation.
b, err := c.RepoClient.GetDefaultBranch()
if err != nil {
if !errors.Is(err, clients.ErrUnsupportedFeature) {
setupErr = err
}
return
}
if b != nil && b.Name != nil {
branch = *b.Name
uri := c.RepoClient.URI()
parts := strings.Split(uri, "/")
if len(parts) != 3 {
setupErr = fmt.Errorf("%w: enpty: %s", errInvalidArg, uri)
return
}
repo = fmt.Sprintf("%s/%s", parts[1], parts[2])
}
})
return setupErr
}
// CreateWorkflowPermissionRemediation create remediation for workflow permissions.
func CreateWorkflowPermissionRemediation(filepath string) *checker.Remediation {
return createWorkflowRemediation(filepath, "permissions")
}
// CreateWorkflowPinningRemediation create remediaiton for pinninn GH Actions.
func CreateWorkflowPinningRemediation(filepath string) *checker.Remediation {
return createWorkflowRemediation(filepath, "pin")
}
func createWorkflowRemediation(path, t string) *checker.Remediation {
p := strings.TrimPrefix(path, ".github/workflows/")
if branch == "" || repo == "" {
return nil
}
text := fmt.Sprintf(workflowText, repo, p, branch, t)
markdown := fmt.Sprintf(workflowMarkdown, repo, p, branch, t)
return &checker.Remediation{
HelpText: text,
HelpMarkdown: markdown,
}
}
// CreateDockerfilePinningRemediation create remediaiton for pinning Dockerfile images.
func CreateDockerfilePinningRemediation(name *string) *checker.Remediation {
if name == nil {
return nil
}
hash, err := crane.Digest(*name)
if err != nil {
return nil
}
text := fmt.Sprintf(dockerfilePinText, *name, hash)
markdown := text
return &checker.Remediation{
HelpText: text,
HelpMarkdown: markdown,
}
}