| 1 | package diffscan |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "amadan.net/rastrillo/rastrillo/budget" |
| 6 | "context" |
| 7 | "encoding/base64" |
| 8 | "errors" |
| 9 | "github.com/zricethezav/gitleaks/v8/config" |
| 10 | "os" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | func TestMain(m *testing.M) { os.Exit(budget.Main(m)) } |
| 16 | |
| 17 | func TestScanClosed(t *testing.T) { |
| 18 | secret := "ghp_" + "0123456789abcdefghijklmnopqrstuvwxyz" |
| 19 | for i, s := range []string{"rk_live_" + "0123456789abcdefghijklmnop", "rk_live_" + "0123456789abcdefghijklmnop # gitleaks:allow", secret, "-token = '" + secret + "' # gitleaks:allow", base64.StdEncoding.EncodeToString([]byte(secret)), "context\n" + secret + "\nend", "-----BEGIN RSA PRIVATE KEY-----\nMIIBOgIBAAJBALlongprivatekey\n-----END RSA PRIVATE KEY-----"} { |
| 20 | if got := Check(context.Background(), s, nil); got != wire.DiffWithheld { |
| 21 | t.Fatalf("case %d secret not withheld: %q", i, got) |
| 22 | } |
| 23 | } |
| 24 | if Check(context.Background(), "let x = 1\n", nil) != "" { |
| 25 | t.Fatal("safe refused") |
| 26 | } |
| 27 | if Check(context.Background(), "harmless opaque value", []string{"opaque value"}) != wire.DiffWithheld { |
| 28 | t.Fatal("known secret") |
| 29 | } |
| 30 | ctx, cancel := context.WithCancel(context.Background()) |
| 31 | cancel() |
| 32 | if Check(ctx, "let x = 1\n", nil) != wire.DiffUnavailable { |
| 33 | t.Fatal("cancelled scan") |
| 34 | } |
| 35 | if Check(context.Background(), strings.Repeat("x", wire.MaxDiffBlobBytes+1), nil) != wire.DiffTooLarge { |
| 36 | t.Fatal("oversize scan") |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestEmbeddedPasswordURL(t *testing.T) { |
| 41 | for _, value := range []string{"https://reader:tiny@repo.example.test/path", "postgres://app:p%40ss@db.example.test/main", "custom+scheme://user:password@host", "redis://:secret@host"} { |
| 42 | if Check(context.Background(), value, nil) != wire.DiffWithheld { |
| 43 | t.Fatal("embedded password accepted") |
| 44 | } |
| 45 | } |
| 46 | for _, value := range []string{"https://reader@repo.example.test/path", "https://repo.example.test/path"} { |
| 47 | if Check(context.Background(), value, nil) != "" { |
| 48 | t.Fatal("URL without password refused") |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | func TestScannerFailureClosed(t *testing.T) { |
| 53 | original := rules |
| 54 | t.Cleanup(func() { rules = original }) |
| 55 | rules = func() (config.Config, error) { panic("scanner failed") } |
| 56 | if Check(context.Background(), "ordinary text", nil) != wire.DiffUnavailable { |
| 57 | t.Fatal("panic accepted") |
| 58 | } |
| 59 | rules = func() (config.Config, error) { return config.Config{}, errors.New("scanner failed") } |
| 60 | if Check(context.Background(), "ordinary text", nil) != wire.DiffUnavailable { |
| 61 | t.Fatal("error accepted") |
| 62 | } |
| 63 | rules = original |
| 64 | originalScan := scanText |
| 65 | t.Cleanup(func() { scanText = originalScan }) |
| 66 | scanText = func(context.Context, config.Config, string) (bool, error) { panic("detector failed") } |
| 67 | if Check(context.Background(), "ordinary text", nil) != wire.DiffUnavailable { |
| 68 | t.Fatal("detector panic accepted") |
| 69 | } |
| 70 | scanText = func(context.Context, config.Config, string) (bool, error) { |
| 71 | return false, errors.New("detector failed") |
| 72 | } |
| 73 | if Check(context.Background(), "ordinary text", nil) != wire.DiffUnavailable { |
| 74 | t.Fatal("detector error accepted") |
| 75 | } |
| 76 | } |
| 77 | |