Repository navigation
Expand file tree
/
Copy pathdocs_github_actions_examples_test.go
More file actions
356 lines (320 loc) · 9.76 KB
/
Copy pathdocs_github_actions_examples_test.go
File metadata and controls
356 lines (320 loc) · 9.76 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
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
package main
import (
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
"gopkg.in/yaml.v3"
)
// refNameInjectionPayload is a valid Git ref name (no space, `~`, `^`, `:`, `?`,
// `*`, `[`, or `\`) that appends a redirect if it ever reaches shell source.
const refNameInjectionPayload = "v1.0;>injected"
var actionsExpressionPattern = regexp.MustCompile(`\$\{\{\s*([^{}]+?)\s*\}\}`)
type docActionStep struct {
File string
Name string
Env map[string]string
Run string
}
func TestGitHubActionsDocExamplesKeepRefNameOutOfShellSource(t *testing.T) {
sawUntrustedEnv := false
for _, file := range githubActionsDocFiles(t) {
steps := gitHubActionsDocSteps(t, file)
for _, step := range steps {
if match := actionsExpressionPattern.FindString(step.Run); match != "" {
t.Errorf("%s: step %q interpolates %s inside run source; pass it through env instead", file, step.Name, match)
}
for name, value := range step.Env {
if hasUntrustedActionsExpression(value) {
sawUntrustedEnv = true
if referencesShellVariable(step.Run, name) && !referencesQuotedShellVariable(step.Run, name) {
t.Errorf("%s: step %q sets env %s but does not reference it as a quoted shell variable", file, step.Name, name)
}
}
}
}
}
if !sawUntrustedEnv {
t.Fatal("expected at least one documented step to route an untrusted GitHub value through env")
}
}
func TestGitHubActionsDocExamplesTreatRefNameAsInertArgument(t *testing.T) {
bashPath, err := exec.LookPath("bash")
if err != nil {
t.Skipf("bash is not installed; shell-safety of the documented examples cannot be executed here: %v", err)
}
for _, file := range githubActionsDocFiles(t) {
for _, step := range gitHubActionsDocSteps(t, file) {
if !strings.Contains(step.Run, "asc ") || !stepReferencesUntrustedEnv(step) {
continue
}
script, env := renderDocStepScript(t, file, step)
t.Run(file+"/"+step.Name, func(t *testing.T) {
workDir := t.TempDir()
argsFile := filepath.Join(workDir, "asc-args.txt")
stubDir := writeASCStub(t, argsFile)
cmd := exec.Command(bashPath, "-c", script)
cmd.Dir = workDir
cmd.Env = append(os.Environ(), env...)
cmd.Env = append(cmd.Env, "PATH="+stubDir+string(os.PathListSeparator)+os.Getenv("PATH"))
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("run documented step: %v\n%s", err, output)
}
if entries, err := os.ReadDir(workDir); err == nil {
for _, entry := range entries {
if entry.Name() == "injected" {
t.Fatalf("ref name executed shell syntax: %q was created", entry.Name())
}
}
}
args := recordedASCArgs(t, argsFile)
if len(args) == 0 {
t.Fatalf("expected the documented step to invoke asc")
}
sawFullRef := false
for _, arg := range args {
if !strings.Contains(arg, "v1.0") {
continue
}
if !strings.Contains(arg, refNameInjectionPayload) {
t.Fatalf("asc argument %q lost part of the ref; the ref must arrive as one inert argument containing %q", arg, refNameInjectionPayload)
}
sawFullRef = true
}
if !sawFullRef {
t.Fatalf("expected asc to receive the ref name; got args %q", args)
}
})
}
}
}
func githubActionsDocFiles(t *testing.T) []string {
t.Helper()
files := make([]string, 0)
err := filepath.WalkDir(".", func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() {
if path != "." && (entry.Name() == ".git" || entry.Name() == "node_modules") {
return filepath.SkipDir
}
return nil
}
ext := strings.ToLower(filepath.Ext(path))
if ext != ".md" && ext != ".mdx" {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
content := string(data)
if strings.Contains(content, "${{") && strings.Contains(content, "run:") {
files = append(files, filepath.Clean(path))
}
return nil
})
if err != nil {
t.Fatalf("discover GitHub Actions documentation: %v", err)
}
return files
}
func hasUntrustedActionsExpression(value string) bool {
for _, match := range actionsExpressionPattern.FindAllStringSubmatch(value, -1) {
expression := strings.TrimSpace(match[1])
if strings.HasPrefix(expression, "github.ref") ||
strings.HasPrefix(expression, "github.event.inputs.") ||
strings.HasPrefix(expression, "inputs.") ||
strings.HasPrefix(expression, "secrets.") {
return true
}
}
return false
}
func stepReferencesUntrustedEnv(step docActionStep) bool {
for name, value := range step.Env {
if hasRefOrInputActionsExpression(value) && referencesQuotedShellVariable(step.Run, name) {
return true
}
}
return false
}
func hasRefOrInputActionsExpression(value string) bool {
for _, match := range actionsExpressionPattern.FindAllStringSubmatch(value, -1) {
expression := strings.TrimSpace(match[1])
if strings.HasPrefix(expression, "github.ref") ||
strings.HasPrefix(expression, "github.event.inputs.") ||
strings.HasPrefix(expression, "inputs.") {
return true
}
}
return false
}
// referencesQuotedShellVariable reports whether script expands name at least
// once and never expands it outside double quotes, so word splitting and glob
// expansion cannot act on the value.
func referencesQuotedShellVariable(script string, name string) bool {
found := false
inSingle := false
inDouble := false
for i := 0; i < len(script); i++ {
switch c := script[i]; {
case c == '\\' && !inSingle:
i++
case c == '\'' && !inDouble:
inSingle = !inSingle
case c == '"' && !inSingle:
inDouble = !inDouble
case c == '$' && !inSingle:
rest := script[i+1:]
braced := strings.HasPrefix(rest, "{"+name+"}")
bare := strings.HasPrefix(rest, name) && !isShellNameByte(byteAt(rest, len(name)))
if !braced && !bare {
continue
}
if !inDouble {
return false
}
found = true
}
}
return found
}
func referencesShellVariable(script string, name string) bool {
return strings.Contains(script, "$"+name) || strings.Contains(script, "${"+name+"}")
}
func isShellNameByte(c byte) bool {
return c == '_' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}
func byteAt(value string, index int) byte {
if index >= len(value) {
return 0
}
return value[index]
}
func gitHubActionsDocSteps(t *testing.T, file string) []docActionStep {
t.Helper()
data, err := os.ReadFile(file)
if err != nil {
t.Fatalf("read %s: %v", file, err)
}
var steps []docActionStep
for _, language := range []string{"yaml", "yml"} {
for _, block := range fencedBlocks(string(data), language) {
if !strings.Contains(block, "run:") {
continue
}
var root yaml.Node
if err := yaml.Unmarshal([]byte(block), &root); err != nil {
t.Fatalf("%s: parse yaml example: %v\n%s", file, err, block)
}
collectDocActionSteps(file, &root, &steps)
}
}
return steps
}
func fencedBlocks(content string, language string) []string {
var blocks []string
var current []string
inBlock := false
for _, line := range strings.Split(content, "\n") {
if strings.HasPrefix(line, "```") {
if inBlock {
blocks = append(blocks, strings.Join(current, "\n"))
current = nil
inBlock = false
continue
}
inBlock = strings.HasPrefix(strings.TrimSpace(strings.TrimPrefix(line, "```")), language)
continue
}
if inBlock {
current = append(current, line)
}
}
return blocks
}
func collectDocActionSteps(file string, node *yaml.Node, steps *[]docActionStep) {
if node == nil {
return
}
if node.Kind == yaml.MappingNode {
step := docActionStep{File: file}
hasRun := false
for i := 0; i+1 < len(node.Content); i += 2 {
key := node.Content[i].Value
value := node.Content[i+1]
switch key {
case "name":
step.Name = value.Value
case "run":
step.Run = value.Value
hasRun = true
case "env":
if value.Kind != yaml.MappingNode {
continue
}
step.Env = make(map[string]string, len(value.Content)/2)
for j := 0; j+1 < len(value.Content); j += 2 {
step.Env[value.Content[j].Value] = value.Content[j+1].Value
}
}
}
if hasRun {
*steps = append(*steps, step)
}
}
for _, child := range node.Content {
collectDocActionSteps(file, child, steps)
}
}
// renderDocStepScript expands GitHub Actions expressions the way the runner
// does: expression results are substituted into the workflow text before the
// shell ever sees it.
func renderDocStepScript(t *testing.T, file string, step docActionStep) (string, []string) {
t.Helper()
env := make([]string, 0, len(step.Env))
for name, value := range step.Env {
env = append(env, name+"="+expandActionsExpressions(t, file, step, value))
}
return expandActionsExpressions(t, file, step, step.Run), env
}
func expandActionsExpressions(t *testing.T, file string, step docActionStep, value string) string {
t.Helper()
return actionsExpressionPattern.ReplaceAllStringFunc(value, func(match string) string {
expression := strings.TrimSpace(actionsExpressionPattern.FindStringSubmatch(match)[1])
if expression == "" {
t.Fatalf("%s: step %q contains an empty GitHub Actions expression", file, step.Name)
}
return refNameInjectionPayload
})
}
func writeASCStub(t *testing.T, argsFile string) string {
t.Helper()
stubDir := t.TempDir()
stub := "#!/bin/sh\nfor arg in \"$@\"; do printf '%s\\n' \"$arg\" >> " + argsFile + "; done\n"
stubPath := filepath.Join(stubDir, "asc")
if err := os.WriteFile(stubPath, []byte(stub), 0o700); err != nil {
t.Fatalf("write asc stub: %v", err)
}
return stubDir
}
func recordedASCArgs(t *testing.T, argsFile string) []string {
t.Helper()
data, err := os.ReadFile(argsFile)
if err != nil {
if os.IsNotExist(err) {
return nil
}
t.Fatalf("read recorded asc args: %v", err)
}
lines := strings.Split(strings.TrimRight(string(data), "\n"), "\n")
if len(lines) == 1 && lines[0] == "" {
return nil
}
return lines
}