| 1 | //go:build testbuild |
| 2 | |
| 3 | package thin |
| 4 | |
| 5 | import ( |
| 6 | "encoding/hex" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // Whole-branch fix 3, #4: a known secret, hex-encoded, written where |
| 14 | // every bound cuts it (the headline's 120 bytes, a tail line's 160, the |
| 15 | // commit message's 1,500 bytes of summary, the summary's 2,000) is |
| 16 | // redacted whole before anything is clipped: no fragment long enough to |
| 17 | // matter reaches the headline, the summary, the tail, the step log or |
| 18 | // the commit message. |
| 19 | func TestWorkerSecretAcrossEveryBound(t *testing.T) { |
| 20 | x := newEngRig(t) |
| 21 | h := hex.EncodeToString([]byte(engBearer)) |
| 22 | pad := func(n int) string { return strings.Repeat("w", n) } |
| 23 | // The summary is "Wrote Notes.txt. " and the said text: one copy of |
| 24 | // the secret straddles the commit message's 1,500 bytes, another the |
| 25 | // summary's 2,000; the headline's copy straddles its 120 bytes and, |
| 26 | // in the reply's first line, the tail line's 160. No whole copy |
| 27 | // reaches the commit, so the object scan alone would not see it. |
| 28 | prefix := len("Wrote Notes.txt. ") |
| 29 | first := 1500 - 36 - prefix |
| 30 | second := 2000 - 36 - (prefix + first + len(h)) |
| 31 | x.note("create:Notes.txt|hello\nheadsay:" + pad(90) + h + "\nsay:" + pad(first) + h + pad(second) + h) |
| 32 | rep := x.run(t) |
| 33 | if rep.CandidateSHA == "" { |
| 34 | t.Fatalf("no commit: %+v", rep) |
| 35 | } |
| 36 | msg, err := x.store(t, "log", "-1", "--format=%B", rep.CandidateSHA) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | logged, _ := os.ReadFile(filepath.Join(x.w.Engine.root(), stepKey(x.r), "log.jsonl")) |
| 41 | for name, text := range map[string]string{"headline": rep.Headline, "summary": rep.Summary, "tail": rep.LogTail, "commit message": msg, "step log": string(logged)} { |
| 42 | for i := 0; i+16 <= len(h); i++ { |
| 43 | if strings.Contains(strings.ToLower(text), h[i:i+16]) { |
| 44 | t.Fatalf("%s carries part of the secret (%q at %d)", name, h[i:i+16], i) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | // Redacted where it was written (the headline's trailing *** is |
| 49 | // trimmed with its markdown). |
| 50 | if !strings.Contains(msg, "***") || !strings.Contains(rep.Summary, "***") || rep.Headline != pad(90) { |
| 51 | t.Fatalf("not redacted where written: headline %q, message %q", rep.Headline, msg[:min(len(msg), 200)]) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Whole-branch fix 3, #4: what only the generic patterns take for a |
| 56 | // secret (a GitHub token) is also redacted from the whole reply before |
| 57 | // the commit message's 1,500 bytes cut it. |
| 58 | func TestWorkerGenericTokenAcrossTheCommitBound(t *testing.T) { |
| 59 | x := newEngRig(t) |
| 60 | token := "ghp_" + "R4nd0mT0k3nW1thH1ghEntr0pyXyZ987654321" |
| 61 | pad := 1500 - 20 - len("Created Notes.txt. ") |
| 62 | x.note("create:Notes.txt|hello\nsay:" + strings.Repeat("w", pad) + " " + token) |
| 63 | rep := x.run(t) |
| 64 | if rep.CandidateSHA == "" { |
| 65 | t.Fatalf("no commit: %+v", rep) |
| 66 | } |
| 67 | msg, err := x.store(t, "log", "-1", "--format=%B", rep.CandidateSHA) |
| 68 | if err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | if strings.Contains(msg, token[:12]) { |
| 72 | t.Fatalf("the commit message carries part of the token: %q", msg[len(msg)-120:]) |
| 73 | } |
| 74 | } |
| 75 | |