|
| 1 | +package guard |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/goccy/go-yaml" |
| 10 | +) |
| 11 | + |
| 12 | +// Every checkout in a workflow turns the job's token off once the tree is on |
| 13 | +// disk. |
| 14 | +// |
| 15 | +// actions/checkout writes the token it cloned with into .git/config and leaves |
| 16 | +// it there, so that a later step can push. No step in these workflows pushes: |
| 17 | +// the release, the attestation and the pages talk to GitHub through gh with a |
| 18 | +// token in the environment, or through actions that carry their own. What the |
| 19 | +// jobs do after a checkout is run go test over the code of the pull request |
| 20 | +// under test - and that code can read .git/config. The token of a pull request |
| 21 | +// from a fork is read only, so the class is real and the weight is small, and |
| 22 | +// it is the same class on every one of the twenty four checkouts, which is why |
| 23 | +// the answer is every one of them and a guard, not a fix at the job somebody |
| 24 | +// happened to review (O230). |
| 25 | +// |
| 26 | +// Asked through the YAML parser rather than by searching the text, because |
| 27 | +// `with:` can sit under a comment block and the key can sit anywhere inside |
| 28 | +// it - a regular expression tying the key to the line after `uses:` would read |
| 29 | +// pages.yml wrong today. |
| 30 | +func TestEveryCheckoutTurnsItsTokenOff(t *testing.T) { |
| 31 | + // Asking the predicate about shapes the tree does not currently contain. |
| 32 | + // Every checkout DOES turn its token off today, so a change that weakened |
| 33 | + // the rule would find nothing to let through and stay green. These cases |
| 34 | + // keep a hold on the rule itself. |
| 35 | + for _, c := range []struct { |
| 36 | + with map[string]any |
| 37 | + off bool |
| 38 | + why string |
| 39 | + }{ |
| 40 | + {map[string]any{"persist-credentials": false}, true, "the key is there and false"}, |
| 41 | + {map[string]any{"persist-credentials": "false"}, true, "the action reads its inputs as strings, so a quoted false is the same answer"}, |
| 42 | + {nil, false, "no with block at all means the default, which keeps the token"}, |
| 43 | + {map[string]any{"fetch-depth": 0}, false, "a with block that says nothing about the token keeps it"}, |
| 44 | + {map[string]any{"persist-credentials": true}, false, "the key is there and true"}, |
| 45 | + {map[string]any{"persist-credentials": "no"}, false, "a word that is not false is not false"}, |
| 46 | + } { |
| 47 | + if tokenTurnedOff(c.with) != c.off { |
| 48 | + t.Errorf("tokenTurnedOff(%v) should be %v, because %s", c.with, c.off, c.why) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + dir := filepath.Join(repoRoot(t), ".github", "workflows") |
| 53 | + entries, err := os.ReadDir(dir) |
| 54 | + if err != nil { |
| 55 | + t.Skipf("the workflows are not here: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + seen := 0 |
| 59 | + for _, e := range entries { |
| 60 | + if e.IsDir() || !strings.HasSuffix(e.Name(), ".yml") { |
| 61 | + continue |
| 62 | + } |
| 63 | + body, err := os.ReadFile(filepath.Join(dir, e.Name())) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("reading %s: %v", e.Name(), err) |
| 66 | + } |
| 67 | + var workflow struct { |
| 68 | + Jobs map[string]struct { |
| 69 | + Steps []struct { |
| 70 | + Uses string `yaml:"uses"` |
| 71 | + With map[string]any `yaml:"with"` |
| 72 | + } `yaml:"steps"` |
| 73 | + } `yaml:"jobs"` |
| 74 | + } |
| 75 | + if err := yaml.Unmarshal(body, &workflow); err != nil { |
| 76 | + t.Fatalf("reading %s: %v", e.Name(), err) |
| 77 | + } |
| 78 | + for jobName, job := range workflow.Jobs { |
| 79 | + for _, step := range job.Steps { |
| 80 | + if !strings.HasPrefix(step.Uses, "actions/checkout@") { |
| 81 | + continue |
| 82 | + } |
| 83 | + seen++ |
| 84 | + if tokenTurnedOff(step.With) { |
| 85 | + continue |
| 86 | + } |
| 87 | + t.Errorf("%s, job %q checks out with the token left in .git/config, and the steps "+ |
| 88 | + "after it run the pull request's own code. Nothing in these workflows pushes, "+ |
| 89 | + "so turn it off:\n"+ |
| 90 | + " with:\n"+ |
| 91 | + " persist-credentials: false", |
| 92 | + e.Name(), jobName) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Measured 2026-09-22: twenty four checkouts across eight workflows. A |
| 98 | + // walk that found far fewer would report a clean tree while reading |
| 99 | + // nothing - a renamed key, a changed suffix, a parser that stopped seeing |
| 100 | + // steps - which is the way this guard is most likely to break. |
| 101 | + if seen < 20 { |
| 102 | + t.Errorf("only %d checkouts were found under %s, and there are twenty four. Either the "+ |
| 103 | + "workflows moved or the way this reads them stopped working, and this guard "+ |
| 104 | + "checked nothing", seen, dir) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// tokenTurnedOff reports whether a checkout's with block says |
| 109 | +// persist-credentials: false. |
| 110 | +// |
| 111 | +// A bare false parses as a boolean and a quoted one as a string, and the |
| 112 | +// action reads either as false, so both are accepted. Anything else - the key |
| 113 | +// missing, true, or a word - leaves the token where the action puts it. |
| 114 | +func tokenTurnedOff(with map[string]any) bool { |
| 115 | + switch v := with["persist-credentials"].(type) { |
| 116 | + case bool: |
| 117 | + return !v |
| 118 | + case string: |
| 119 | + return v == "false" |
| 120 | + } |
| 121 | + return false |
| 122 | +} |
0 commit comments