package runnerproto_test import ( "strings" "testing" "unicode/utf8" "amadan.net/oficina/equipo/internal/runnerproto" ) // An error's words for a log line or a step report: every credential // shape gone, one line, no control characters, never past the bound. func TestRedactDetail(t *testing.T) { hex64 := strings.Repeat("ab12", 16) for _, c := range []struct{ in, gone, kept string }{ {"Authorization: Bearer s3cr3t-T0ken and more", "s3cr3t-T0ken", "Authorization: *** and more"}, {"authorization=token s3cr3t-T0ken", "s3cr3t-T0ken", "authorization=***"}, {"proxy-authorization: Basic eDpzM2NyM3Q=", "eDpzM2NyM3Q", "proxy-authorization: ***"}, {"bearer s3cr3t-T0ken was refused", "s3cr3t-T0ken", "was refused"}, {`fatal: unable to access 'https://x:s3cr3t-pw@amadan.net/ns/repo/': 403`, "s3cr3t-pw", "https://***@amadan.net/ns/repo/"}, {`{"error":"no","access_token":"s3cr3t-T0ken"}`, "s3cr3t-T0ken", `"access_token":"***"`}, {"cookie: session=s3cr3t-T0ken", "s3cr3t-T0ken", "cookie: ***"}, {"token " + hex64 + " rejected", hex64, "rejected"}, {"key sk-ant-s3cr3tT0kenValue here", "s3cr3tT0kenValue", "here"}, {"jwt eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4In0.c2lnbmF0dXJl", "eyJzdWIiOiJ4In0", "jwt ***"}, // A commit SHA, a task branch and a path are not credentials. {"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"}, } { got := runnerproto.RedactDetail(c.in, 500) if c.gone != "" && strings.Contains(got, c.gone) || !strings.Contains(got, c.kept) { t.Fatalf("RedactDetail(%q) = %q", c.in, got) } } // Control characters (a forged log line, a terminal escape, a bidi // override) become spaces; backticks cannot close a code block. got := runnerproto.RedactDetail("first\nlevel=ERROR msg=forged\r\x1b[31mred\u202e ```done```", 500) if strings.ContainsAny(got, "\n\r\x1b\u202e`") || got != "first level=ERROR msg=forged [31mred '''done'''" { t.Fatalf("control characters: %q", got) } // Bounded, at a rune, with the cut marked; redaction happens before // the cut, so a credential across the bound is never half shown. long := strings.Repeat("é", 235) + " https://x:s3cr3t-pw@amadan.net/" + strings.Repeat("x ", 100) got = runnerproto.RedactDetail(long, 500) if len(got) > 500 || !utf8.ValidString(got) || !strings.HasSuffix(got, "…") || strings.Contains(got, "s3cr3t") { t.Fatalf("bounded: %d %q", len(got), got) } if got := runnerproto.RedactDetail("x:"+strings.Repeat("y", 300)+"@", 10); len(got) > 10 { t.Fatalf("tiny bound: %q", got) } if got := runnerproto.RedactDetail(string([]byte{0xff, 'o', 'k'}), 500); !utf8.ValidString(got) { t.Fatalf("invalid UTF-8: %q", got) } }