-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanalyze.go
More file actions
233 lines (189 loc) · 5.94 KB
/
analyze.go
File metadata and controls
233 lines (189 loc) · 5.94 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (C) 2023-2026 Eric Cornelissen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package ades
import (
"fmt"
"io"
"io/fs"
"path/filepath"
"slices"
"github.com/ericcornelissen/go-gha-models"
)
// Violation contain information on problematic GitHub Actions Expressions found in a workflow or
// manifest.
type Violation struct {
// Source is a reference to the Workflow or Manifest struct in which the violation occurs.
source any
// JobId is an identifier of a job in a GitHub Actions workflow, either the name or key.
//
// This will be the zero value if the violation is for a GitHub Actions manifest.
JobId string
// StepId is the identifier of a step in a GitHub Actions workflow or manifest, either the name
// or index.
StepId string
// Problem is the problematic GitHub Actions Expression as observed in the workflow or manifest.
Problem string
// RuleId is the identifier of the ades rule that produced the violation.
RuleId string
// JobKey is the key of the job in which the violation occurs. Different from JobId in that it
// always uniquely identifies the job.
//
// This will be the zero value if the violation is for a GitHub Actions manifest.
jobKey string
// StepIndex is the index of the step in which the violation occurs. Different from StepId in
// that it always uniquely identifies the step.
stepIndex int
}
// AnalyzeRepo analyzes a GitHub repository for problematic GitHub Actions Expressions in manifests
// and workflows.
func AnalyzeRepo(fsys fs.FS, matcher ExprMatcher) (map[string][]Violation, error) {
report := make(map[string][]Violation)
return report, fs.WalkDir(fsys, ".", func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() {
if path == ".git" {
return fs.SkipDir
}
return nil
}
var (
dir = filepath.Dir(path)
base = filepath.Base(path)
ext = filepath.Ext(path)
isManifest = base == "action.yml" || base == "action.yaml"
isWorkflow = dir == ".github/workflows" && (ext == ".yml" || ext == ".yaml")
)
if !isManifest && !isWorkflow {
return nil
}
file, err := fsys.Open(path)
if err != nil {
return fmt.Errorf("could not open %q: %v", path, err)
}
defer func() { _ = file.Close() }()
content, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("could not read %q: %v", path, err)
}
if isWorkflow {
workflow, err := gha.ParseWorkflow(content)
if err != nil {
return fmt.Errorf("could not process workflow %q: %v", path, err)
}
report[path] = AnalyzeWorkflow(&workflow, matcher)
} else {
manifest, err := gha.ParseManifest(content)
if err != nil {
return fmt.Errorf("could not process manifest %q: %v", path, err)
}
report[path] = AnalyzeManifest(&manifest, matcher)
}
return nil
})
}
// AnalyzeManifest analyzes a GitHub Actions manifest for problematic GitHub Actions Expressions.
func AnalyzeManifest(manifest *gha.Manifest, matcher ExprMatcher) []Violation {
violations := make([]Violation, 0)
if manifest == nil {
return violations
}
if manifest.Runs.Using != "composite" {
return violations
}
for _, violation := range analyzeSteps(manifest.Runs.Steps, matcher) {
violation.source = manifest
violations = append(violations, violation)
}
return violations
}
// AnalyzeWorkflow analyzes a GitHub Actions workflow for problematic GitHub Actions Expressions.
func AnalyzeWorkflow(workflow *gha.Workflow, matcher ExprMatcher) []Violation {
violations := make([]Violation, 0)
if workflow == nil {
return violations
}
for id, job := range workflow.Jobs {
for _, violation := range analyzeJob(id, &job, matcher) {
violation.source = workflow
violations = append(violations, violation)
}
}
return violations
}
func analyzeJob(id string, job *gha.Job, matcher ExprMatcher) []Violation {
name := job.Name
if name == "" {
name = id
}
violations := analyzeSteps(job.Steps, matcher)
out := make([]Violation, 0, len(violations))
for _, violation := range violations {
if matrixSafe(violation.Problem, job.Strategy.Matrix, matcher) {
continue
}
violation.jobKey = id
violation.JobId = name
out = append(out, violation)
}
return out
}
func analyzeSteps(steps []gha.Step, matcher ExprMatcher) []Violation {
violations := make([][]Violation, len(steps))
for i, step := range steps {
violations[i] = analyzeStep(i, &step, matcher)
}
return slices.Concat(violations...)
}
func analyzeStep(id int, step *gha.Step, matcher ExprMatcher) []Violation {
name := step.Name
if step.Name == "" {
name = fmt.Sprintf("#%d", id)
}
applicable := make([]rule, 0)
for _, r := range rules {
if r.appliesTo(step) {
applicable = append(applicable, r)
}
}
violations := make([][]Violation, len(applicable))
for i, rule := range applicable {
vs := analyzeString(rule.extractFrom(step), matcher)
for i := range vs {
vs[i].RuleId = rule.id
vs[i].StepId = name
vs[i].stepIndex = id
}
violations[i] = vs
}
return slices.Concat(violations...)
}
func analyzeString(s string, matcher ExprMatcher) []Violation {
b := []byte(s)
if matcher.FindAll(stripSafe(b)) == nil {
return nil
}
violations := make([]Violation, 0)
for _, problem := range matcher.FindAll(b) {
if len(matcher.FindAll(stripSafe(problem))) == 0 {
continue
}
violations = append(violations, Violation{
Problem: string(problem),
})
}
return violations
}