-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.go
162 lines (135 loc) · 4.42 KB
/
analyze.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
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
// Copyright (C) 2024 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"
)
// 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
}
// AnalyzeManifest analyzes a GitHub Actions manifest for problematic GitHub Actions Expressions.
func AnalyzeManifest(manifest *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 *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 *WorkflowJob, matcher ExprMatcher) []Violation {
name := job.Name
if name == "" {
name = id
}
violations := make([]Violation, 0)
for _, violation := range analyzeSteps(job.Steps, matcher) {
violation.jobKey = id
violation.JobId = name
violations = append(violations, violation)
}
return violations
}
func analyzeSteps(steps []JobStep, matcher ExprMatcher) []Violation {
violations := make([]Violation, 0)
for i, step := range steps {
violations = append(violations, analyzeStep(i, &step, matcher)...)
}
return violations
}
func analyzeStep(id int, step *JobStep, matcher ExprMatcher) []Violation {
name := step.Name
if step.Name == "" {
name = fmt.Sprintf("#%d", id)
}
rules := make([]rule, 0)
if uses, err := ParseUses(step); err == nil {
if rs, ok := actionRules[uses.Name]; ok {
for _, r := range rs {
if r.appliesTo(&uses) {
rules = append(rules, r.rule)
}
}
}
} else {
for _, r := range stepRules {
if r.appliesTo(step) {
rules = append(rules, r.rule)
}
}
}
violations := make([]Violation, 0)
for _, rule := range rules {
for _, violation := range analyzeString(rule.extractFrom(step), matcher) {
violation.RuleId = rule.id
violation.StepId = name
violation.stepIndex = id
violations = append(violations, violation)
}
}
return violations
}
func analyzeString(s string, matcher ExprMatcher) []Violation {
violations := make([]Violation, 0)
if matches := matcher.FindAll([]byte(s)); matches != nil {
for _, problem := range matches {
violations = append(violations, Violation{
Problem: string(problem),
})
}
}
return violations
}