//go:build testbuild package thin import ( "encoding/hex" "os" "path/filepath" "strings" "testing" ) // Whole-branch fix 3, #4: a known secret, hex-encoded, written where // every bound cuts it (the headline's 120 bytes, a tail line's 160, the // commit message's 1,500 bytes of summary, the summary's 2,000) is // redacted whole before anything is clipped: no fragment long enough to // matter reaches the headline, the summary, the tail, the step log or // the commit message. func TestWorkerSecretAcrossEveryBound(t *testing.T) { x := newEngRig(t) h := hex.EncodeToString([]byte(engBearer)) pad := func(n int) string { return strings.Repeat("w", n) } // The summary is "Wrote Notes.txt. " and the said text: one copy of // the secret straddles the commit message's 1,500 bytes, another the // summary's 2,000; the headline's copy straddles its 120 bytes and, // in the reply's first line, the tail line's 160. No whole copy // reaches the commit, so the object scan alone would not see it. prefix := len("Wrote Notes.txt. ") first := 1500 - 36 - prefix second := 2000 - 36 - (prefix + first + len(h)) x.note("create:Notes.txt|hello\nheadsay:" + pad(90) + h + "\nsay:" + pad(first) + h + pad(second) + h) rep := x.run(t) if rep.CandidateSHA == "" { t.Fatalf("no commit: %+v", rep) } msg, err := x.store(t, "log", "-1", "--format=%B", rep.CandidateSHA) if err != nil { t.Fatal(err) } logged, _ := os.ReadFile(filepath.Join(x.w.Engine.root(), stepKey(x.r), "log.jsonl")) for name, text := range map[string]string{"headline": rep.Headline, "summary": rep.Summary, "tail": rep.LogTail, "commit message": msg, "step log": string(logged)} { for i := 0; i+16 <= len(h); i++ { if strings.Contains(strings.ToLower(text), h[i:i+16]) { t.Fatalf("%s carries part of the secret (%q at %d)", name, h[i:i+16], i) } } } // Redacted where it was written (the headline's trailing *** is // trimmed with its markdown). if !strings.Contains(msg, "***") || !strings.Contains(rep.Summary, "***") || rep.Headline != pad(90) { t.Fatalf("not redacted where written: headline %q, message %q", rep.Headline, msg[:min(len(msg), 200)]) } } // Whole-branch fix 3, #4: what only the generic patterns take for a // secret (a GitHub token) is also redacted from the whole reply before // the commit message's 1,500 bytes cut it. func TestWorkerGenericTokenAcrossTheCommitBound(t *testing.T) { x := newEngRig(t) token := "ghp_" + "R4nd0mT0k3nW1thH1ghEntr0pyXyZ987654321" pad := 1500 - 20 - len("Created Notes.txt. ") x.note("create:Notes.txt|hello\nsay:" + strings.Repeat("w", pad) + " " + token) rep := x.run(t) if rep.CandidateSHA == "" { t.Fatalf("no commit: %+v", rep) } msg, err := x.store(t, "log", "-1", "--format=%B", rep.CandidateSHA) if err != nil { t.Fatal(err) } if strings.Contains(msg, token[:12]) { t.Fatalf("the commit message carries part of the token: %q", msg[len(msg)-120:]) } }