-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.go
434 lines (379 loc) · 12.3 KB
/
rules.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright (C) 2023-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"
"regexp"
"strings"
"golang.org/x/mod/semver"
)
type rule struct {
extractFrom func(step *JobStep) string
fix func(violation *Violation) []fix
id string
title string
description string
}
type actionRule struct {
appliesTo func(uses *StepUses) bool
rule rule
}
type stepRule struct {
appliesTo func(step *JobStep) bool
rule rule
}
type fix struct {
// New is the replacement string to fix a violation.
New string
// Old is a regular expression to search and replace with in order to fix a violation.
Old regexp.Regexp
}
var actionRuleActionsGitHubScript = actionRule{
appliesTo: func(_ *StepUses) bool {
return true
},
rule: rule{
id: "ADES101",
title: "Expression in 'actions/github-script' script",
description: `
When an expression appears in a 'actions/github-script' script you can avoid potential attacks by
extracting the expression into an environment variable and using the environment variable instead.
For example, given the workflow snippet:
- name: Example step
uses: actions/github-script@v6
with:
script: console.log('Hello ${{ inputs.name }}')
it can be made safer by converting it into:
- name: Example step
uses: actions/github-script@v6
env:
NAME: ${{ inputs.name }} # <- Assign the expression to an environment variable
with:
script: console.log(` + "`" + `Hello ${process.env.NAME}` + "`" + `)
# ^ ^^^^^^^^^^^^^^^^^^^
# | | Replace the expression with the environment variable
# |
# | Note: the use of backticks is required in this example (for interpolation)`,
extractFrom: func(step *JobStep) string {
return step.With["script"]
},
},
}
var actionRuleAtlassianGajiraCreate = actionRule{
appliesTo: func(uses *StepUses) bool {
return isBeforeVersion(uses, "v2.0.1")
},
rule: rule{
id: "ADES202",
title: "Expression in 'atlassian/gajira-create' summary input",
description: `
When an expression is used in the summary input for 'atlassian/gajira-create' in v2.0.0 or earlier
it may be used to execute arbitrary JavaScript code, see GHSA-4xqx-pqpj-9fqw. To mitigate this,
upgrade the action to a non-vulnerable version.`,
extractFrom: func(step *JobStep) string {
return step.With["summary"]
},
},
}
var actionRuleEriccornelissenGitTagAnnotationAction = actionRule{
appliesTo: func(uses *StepUses) bool {
return isBeforeVersion(uses, "v1.0.1")
},
rule: rule{
id: "ADES200",
title: "Expression in 'ericcornelissen/git-tag-annotation-action' tag input",
description: `
When an expression is used in the tag input for 'ericcornelissen/git-tag-annotation-action' in
v1.0.0 or earlier it may be used to execute arbitrary shell commands, see GHSA-hgx2-4pp9-357g. To
mitigate this, upgrade the action to a non-vulnerable version.`,
extractFrom: func(step *JobStep) string {
return step.With["tag"]
},
},
}
var actionRuleKcebGitMessageAction = actionRule{
appliesTo: func(uses *StepUses) bool {
return isBeforeVersion(uses, "v1.2.0")
},
rule: rule{
id: "ADES201",
title: "Expression in 'kceb/git-message-action' sha input",
description: `
When an expression is used in the sha input for 'kceb/git-message-action' in v1.1.0 or earlier it
may be used to execute arbitrary shell commands (no vulnerability identifier available). To mitigate
this, upgrade the action to a non-vulnerable version.`,
extractFrom: func(step *JobStep) string {
return step.With["sha"]
},
},
}
var actionRuleRootsIssueCloserIssueCloseMessage = actionRule{
appliesTo: func(_ *StepUses) bool {
return true
},
rule: rule{
id: "ADES102",
title: "Expression in 'roots/issue-closer' issue close message",
description: `
When an expression appears in the issue close message of 'roots/issue-closer' it is interpreted as
an ES6-style template literal. You can avoid potential attacks by extracting the expression into an
environment variable and using the environment variable instead.
For example, given the workflow snippet:
- name: Example step
uses: roots/issue-closer@v1
with:
issue-close-message: Closing ${{ github.event.issue.title }}
it can be made safer by converting it into:
- name: Example step
uses: roots/issue-closer@v1
env:
NAME: ${{ github.event.issue.title }} # <- Assign the expression to an environment variable
with:
issue-close-message: Closing ${process.env.NAME}
# ^^^^^^^^^^^^^^^^^^^
# | Replace the expression with the environment variable`,
extractFrom: func(step *JobStep) string {
return step.With["issue-close-message"]
},
fix: func(violation *Violation) []fix {
var step JobStep
switch source := (violation.source).(type) {
case *Manifest:
step = source.Runs.Steps[violation.stepIndex]
case *Workflow:
step = source.Jobs[violation.jobKey].Steps[violation.stepIndex]
}
name := getVariableNameForExpression(violation.Problem)
if _, ok := step.Env[name]; ok {
return nil
}
fixes := fixAddEnvVar(step, name, violation.Problem)
fixes = append(fixes, fixReplaceIn(
step.With["issue-close-message"],
violation.Problem,
fmt.Sprintf("${process.env.%s}", name),
))
return fixes
},
},
}
var actionRuleRootsIssueCloserPrCloseMessage = actionRule{
appliesTo: func(_ *StepUses) bool {
return true
},
rule: rule{
id: "ADES103",
title: "Expression in 'roots/issue-closer' pull request close message",
description: `
When an expression appears in the pull request close message of 'roots/issue-closer' it is
interpreted as an ES6-style template literal. You can avoid potential attacks by extracting the
expression into an environment variable and using the environment variable instead.
For example, given the workflow snippet:
- name: Example step
uses: roots/issue-closer@v1
with:
pr-close-message: Closing ${{ github.event.issue.title }}
it can be made safer by converting it into:
- name: Example step
uses: roots/issue-closer@v1
env:
NAME: ${{ github.event.issue.title }} # <- Assign the expression to an environment variable
with:
pr-close-message: Closing ${process.env.NAME}
# ^^^^^^^^^^^^^^^^^^^
# | Replace the expression with the environment variable`,
extractFrom: func(step *JobStep) string {
return step.With["pr-close-message"]
},
},
}
var actionRuleSergeysovaJqAction = actionRule{
appliesTo: func(_ *StepUses) bool {
return true
},
rule: rule{
id: "ADES104",
title: "Expression in 'sergeysova/jq-action' command",
description: `
When an expression appears in the command input of 'sergeysova/jq-action' you can avoid any
potential attack by extracting the expression into an environment variable and using the environment
variable instead.
For example, given the workflow snippet:
- name: Example step
uses: sergeysova/jq-action@v2
with:
cmd: jq .version ${{ github.event.inputs.file }} -r
it can be made safer by converting it into:
- name: Example step
uses: sergeysova/jq-action@v2
env:
FILE: ${{ github.event.inputs.file }} # <- Assign the expression to an environment variable
with:
# | Note: use double quotes to avoid argument splitting
# v
cmd: jq .version "$FILE" -r
# ^^^^^
# | Replace the expression with the environment variable`,
extractFrom: func(step *JobStep) string {
return step.With["cmd"]
},
},
}
var actionRules = map[string][]actionRule{
"actions/github-script": {
actionRuleActionsGitHubScript,
},
"atlassian/gajira-create": {
actionRuleAtlassianGajiraCreate,
},
"ericcornelissen/git-tag-annotation-action": {
actionRuleEriccornelissenGitTagAnnotationAction,
},
"kceb/git-message-action": {
actionRuleKcebGitMessageAction,
},
"roots/issue-closer": {
actionRuleRootsIssueCloserIssueCloseMessage,
actionRuleRootsIssueCloserPrCloseMessage,
},
"sergeysova/jq-action": {
actionRuleSergeysovaJqAction,
},
}
var stepRuleRun = stepRule{
appliesTo: func(step *JobStep) bool {
return len(step.Run) > 0
},
rule: rule{
id: "ADES100",
title: "Expression in 'run:' directive",
description: `
When an expression appears in a 'run:' directive you can avoid potential attacks by extracting the
expression into an environment variable and using the environment variable instead.
For example, given the workflow snippet:
- name: Example step
run: |
echo 'Hello ${{ inputs.name }}'
it can be made safer by converting it into:
- name: Example step
env:
NAME: ${{ inputs.name }} # <- Assign the expression to an environment variable
run: |
echo "Hello $NAME"
# ^ ^^^^^
# | | Replace the expression with the environment variable
# |
# | Note: the use of double quotes is required in this example (for interpolation)`,
extractFrom: func(step *JobStep) string {
return step.Run
},
},
}
var stepRules = []stepRule{
stepRuleRun,
}
func isBeforeVersion(uses *StepUses, version string) bool {
ref := uses.Ref
if !semver.IsValid(ref) {
ref = uses.Annotation
if !semver.IsValid(ref) {
return false
}
}
switch {
case semver.Canonical(ref) == ref:
return semver.Compare(ref, version) < 0
case semver.MajorMinor(ref) == ref:
return semver.Compare(ref, semver.MajorMinor(version)) < 0
default:
return semver.Compare(ref, semver.Major(version)) < 0
}
}
// Explain returns an explanation for a rule.
func Explain(ruleId string) (string, error) {
r, err := findRule(ruleId)
if err != nil {
return "", err
}
explanation := fmt.Sprintf("%s - %s\n%s", r.id, r.title, r.description)
return explanation, nil
}
// Fix produces a set of fixes to address the violation if possible. If the return value is nil the
// violation cannot be fixed automatically.
func Fix(violation *Violation) ([]fix, error) {
ruleId := violation.RuleId
r, err := findRule(ruleId)
if err != nil {
return nil, err
}
if r.fix == nil {
return nil, nil
}
return r.fix(violation), nil
}
func findRule(ruleId string) (rule, error) {
for _, rs := range actionRules {
for _, r := range rs {
if r.rule.id == ruleId {
return r.rule, nil
}
}
}
for _, r := range stepRules {
if r.rule.id == ruleId {
return r.rule, nil
}
}
return rule{}, fmt.Errorf("unknown rule %q", ruleId)
}
func fixAddEnvVar(step JobStep, name, value string) []fix {
if step.Env == nil {
return []fix{
{
Old: *regexp.MustCompile(fmt.Sprintf(`\n(\s+)uses:\s*%s.*?\n`, step.Uses)),
New: fmt.Sprintf("${0}${1}env:\n${1} %s: %s\n", name, value),
},
{
Old: *regexp.MustCompile(fmt.Sprintf(`\n(\s+)-(\s+)uses:\s*%s.*?\n`, step.Uses)),
New: fmt.Sprintf("${0}${1} ${2}env:\n${1} ${2} %s: %s\n", name, value),
},
}
} else {
var sb strings.Builder
sb.WriteString(`env:\s*\n(?:`)
for k, v := range step.Env {
sb.WriteString(fmt.Sprintf(`(\s*)%s\s*:\s*%s\s*\n|`, k, v))
}
sb.WriteString(`)+`)
return []fix{
{
Old: *regexp.MustCompile(sb.String()),
New: fmt.Sprintf("${0}${1}%s: %s\n", name, value),
},
}
}
}
func fixReplaceIn(s, old, new string) fix {
return fix{
Old: *regexp.MustCompile(regexp.QuoteMeta(s)),
New: strings.ReplaceAll(s, old, new),
}
}
func getVariableNameForExpression(expression string) string {
name := expression[strings.LastIndex(expression, ".")+1:]
name = strings.TrimRight(name, "}")
name = strings.TrimSpace(name)
return strings.ToUpper(name)
}