| 1 | package runnerproto |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestGitHubSecretShapes(t *testing.T) { |
| 10 | for _, prefix := range []string{"ghs_", "ghu_", "gho_", "ghr_"} { |
| 11 | t.Run(prefix, func(t *testing.T) { |
| 12 | secret := prefix + strings.Repeat("Ab9z", 12) |
| 13 | if got := RedactText("before "+secret+" after", nil); got != "before *** after" { |
| 14 | t.Fatal("unregistered GitHub token shape was not redacted") |
| 15 | } |
| 16 | raw, _ := json.Marshal(map[string]any{"detail": []string{secret}}) |
| 17 | if strings.Contains(string(RedactJSON(raw, nil)), secret) { |
| 18 | t.Fatal("token escaped nested JSON redaction") |
| 19 | } |
| 20 | }) |
| 21 | } |
| 22 | for _, kind := range []string{"", "RSA ", "EC ", "OPENSSH ", "ENCRYPTED "} { |
| 23 | for _, truncated := range []bool{false, true} { |
| 24 | name := strings.TrimSpace(kind) |
| 25 | if truncated { |
| 26 | name += " truncated" |
| 27 | } |
| 28 | t.Run(name, func(t *testing.T) { |
| 29 | header := strings.Repeat("-", 5) + "BEGIN " + kind + "PRIVATE KEY" + strings.Repeat("-", 5) |
| 30 | body := strings.Repeat("aGVsbG8=", 8) |
| 31 | secret := header + "\n" + body + "\n" |
| 32 | suffix := "" |
| 33 | if !truncated { |
| 34 | secret += strings.Replace(header, "BEGIN", "END", 1) |
| 35 | suffix = " after" |
| 36 | } |
| 37 | if got := RedactText("before "+secret+suffix, nil); got != "before ***"+suffix { |
| 38 | t.Fatal("private key block or truncated body was not redacted") |
| 39 | } |
| 40 | raw, _ := json.Marshal(map[string]string{"detail": secret}) |
| 41 | if got := string(RedactJSON(raw, nil)); strings.Contains(got, body) || strings.Contains(got, "PRIVATE KEY") { |
| 42 | t.Fatal("key escaped JSON redaction") |
| 43 | } |
| 44 | }) |
| 45 | } |
| 46 | } |
| 47 | if got := RedactText("github.com/oficina/probe is a repository", nil); got != "github.com/oficina/probe is a repository" { |
| 48 | t.Fatal("ordinary repository text was removed") |
| 49 | } |
| 50 | } |
| 51 | |