| 1 | package runnerproto_test |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | "unicode/utf8" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 9 | ) |
| 10 | |
| 11 | // An error's words for a log line or a step report: every credential |
| 12 | // shape gone, one line, no control characters, never past the bound. |
| 13 | func TestRedactDetail(t *testing.T) { |
| 14 | hex64 := strings.Repeat("ab12", 16) |
| 15 | for _, c := range []struct{ in, gone, kept string }{ |
| 16 | {"Authorization: Bearer s3cr3t-T0ken and more", "s3cr3t-T0ken", "Authorization: *** and more"}, |
| 17 | {"authorization=token s3cr3t-T0ken", "s3cr3t-T0ken", "authorization=***"}, |
| 18 | {"proxy-authorization: Basic eDpzM2NyM3Q=", "eDpzM2NyM3Q", "proxy-authorization: ***"}, |
| 19 | {"bearer s3cr3t-T0ken was refused", "s3cr3t-T0ken", "was refused"}, |
| 20 | {`fatal: unable to access 'https://x:s3cr3t-pw@amadan.net/ns/repo/': 403`, "s3cr3t-pw", "https://***@amadan.net/ns/repo/"}, |
| 21 | {`{"error":"no","access_token":"s3cr3t-T0ken"}`, "s3cr3t-T0ken", `"access_token":"***"`}, |
| 22 | {"cookie: session=s3cr3t-T0ken", "s3cr3t-T0ken", "cookie: ***"}, |
| 23 | {"token " + hex64 + " rejected", hex64, "rejected"}, |
| 24 | {"key sk-ant-s3cr3tT0kenValue here", "s3cr3tT0kenValue", "here"}, |
| 25 | {"jwt eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4In0.c2lnbmF0dXJl", "eyJzdWIiOiJ4In0", "jwt ***"}, |
| 26 | // A commit SHA, a task branch and a path are not credentials. |
| 27 | {"tip " + strings.Repeat("a", 40) + " on equipo/tk5fen4uajpn-the-first-merge-into-an-empty-one", "", strings.Repeat("a", 40) + " on equipo/tk5fen4uajpn-the-first-merge-into-an-empty-one"}, |
| 28 | } { |
| 29 | got := runnerproto.RedactDetail(c.in, 500) |
| 30 | if c.gone != "" && strings.Contains(got, c.gone) || !strings.Contains(got, c.kept) { |
| 31 | t.Fatalf("RedactDetail(%q) = %q", c.in, got) |
| 32 | } |
| 33 | } |
| 34 | // Control characters (a forged log line, a terminal escape, a bidi |
| 35 | // override) become spaces; backticks cannot close a code block. |
| 36 | got := runnerproto.RedactDetail("first\nlevel=ERROR msg=forged\r\x1b[31mred\u202e ```done```", 500) |
| 37 | if strings.ContainsAny(got, "\n\r\x1b\u202e`") || got != "first level=ERROR msg=forged [31mred '''done'''" { |
| 38 | t.Fatalf("control characters: %q", got) |
| 39 | } |
| 40 | // Bounded, at a rune, with the cut marked; redaction happens before |
| 41 | // the cut, so a credential across the bound is never half shown. |
| 42 | long := strings.Repeat("é", 235) + " https://x:s3cr3t-pw@amadan.net/" + strings.Repeat("x ", 100) |
| 43 | got = runnerproto.RedactDetail(long, 500) |
| 44 | if len(got) > 500 || !utf8.ValidString(got) || !strings.HasSuffix(got, "…") || strings.Contains(got, "s3cr3t") { |
| 45 | t.Fatalf("bounded: %d %q", len(got), got) |
| 46 | } |
| 47 | if got := runnerproto.RedactDetail("x:"+strings.Repeat("y", 300)+"@", 10); len(got) > 10 { |
| 48 | t.Fatalf("tiny bound: %q", got) |
| 49 | } |
| 50 | if got := runnerproto.RedactDetail(string([]byte{0xff, 'o', 'k'}), 500); !utf8.ValidString(got) { |
| 51 | t.Fatalf("invalid UTF-8: %q", got) |
| 52 | } |
| 53 | } |
| 54 | |