package runnerproto import ( "encoding/json" "strings" "testing" ) func TestGitHubSecretShapes(t *testing.T) { for _, prefix := range []string{"ghs_", "ghu_", "gho_", "ghr_"} { t.Run(prefix, func(t *testing.T) { secret := prefix + strings.Repeat("Ab9z", 12) if got := RedactText("before "+secret+" after", nil); got != "before *** after" { t.Fatal("unregistered GitHub token shape was not redacted") } raw, _ := json.Marshal(map[string]any{"detail": []string{secret}}) if strings.Contains(string(RedactJSON(raw, nil)), secret) { t.Fatal("token escaped nested JSON redaction") } }) } for _, kind := range []string{"", "RSA ", "EC ", "OPENSSH ", "ENCRYPTED "} { for _, truncated := range []bool{false, true} { name := strings.TrimSpace(kind) if truncated { name += " truncated" } t.Run(name, func(t *testing.T) { header := strings.Repeat("-", 5) + "BEGIN " + kind + "PRIVATE KEY" + strings.Repeat("-", 5) body := strings.Repeat("aGVsbG8=", 8) secret := header + "\n" + body + "\n" suffix := "" if !truncated { secret += strings.Replace(header, "BEGIN", "END", 1) suffix = " after" } if got := RedactText("before "+secret+suffix, nil); got != "before ***"+suffix { t.Fatal("private key block or truncated body was not redacted") } raw, _ := json.Marshal(map[string]string{"detail": secret}) if got := string(RedactJSON(raw, nil)); strings.Contains(got, body) || strings.Contains(got, "PRIVATE KEY") { t.Fatal("key escaped JSON redaction") } }) } } if got := RedactText("github.com/oficina/probe is a repository", nil); got != "github.com/oficina/probe is a repository" { t.Fatal("ordinary repository text was removed") } }