|
| 1 | +package secrets |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// awsKey is a fake AWS access key that matches the gitleaks pattern. |
| 9 | +// It does not end in "EXAMPLE" (which is allowlisted) and has the correct format. |
| 10 | +const awsKey = "AKIAIOSFODNN7REALKEY" |
| 11 | + |
| 12 | +// githubPAT is a fake GitHub PAT that matches the gitleaks github-pat rule. |
| 13 | +// It is exactly 36 alphanumeric chars after "ghp_" and does not contain |
| 14 | +// the "abcdefghijklmnopqrstuvwxyz" stopword. |
| 15 | +const githubPAT = "ghp_0123456789ABCDEFGHIJ0123456789ABCDEF" |
| 16 | + |
| 17 | +// stripeKey matches the gitleaks stripe-access-token rule. |
| 18 | +const stripeKey = "sk_live_abcdefghijklmnopqrstuvwx" |
| 19 | + |
| 20 | +func TestDetect_AWSKey(t *testing.T) { |
| 21 | + d, err := NewDetector() |
| 22 | + if err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + findings := d.Detect("access key is " + awsKey) |
| 26 | + if len(findings) == 0 { |
| 27 | + t.Fatal("expected to detect AWS access key") |
| 28 | + } |
| 29 | + found := false |
| 30 | + for _, f := range findings { |
| 31 | + if strings.Contains(f.Match, awsKey) { |
| 32 | + found = true |
| 33 | + } |
| 34 | + } |
| 35 | + if !found { |
| 36 | + t.Errorf("expected finding to contain the AWS key, got %+v", findings) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestDetect_GitHubPAT(t *testing.T) { |
| 41 | + d, err := NewDetector() |
| 42 | + if err != nil { |
| 43 | + t.Fatal(err) |
| 44 | + } |
| 45 | + findings := d.Detect(githubPAT) |
| 46 | + if len(findings) == 0 { |
| 47 | + t.Fatal("expected to detect GitHub PAT") |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestDetect_StripeKey(t *testing.T) { |
| 52 | + d, err := NewDetector() |
| 53 | + if err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } |
| 56 | + findings := d.Detect("stripe key " + stripeKey) |
| 57 | + if len(findings) == 0 { |
| 58 | + t.Fatal("expected to detect Stripe key") |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestDetect_NoMatch(t *testing.T) { |
| 63 | + d, err := NewDetector() |
| 64 | + if err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + findings := d.Detect("this is just normal text with no secrets") |
| 68 | + if len(findings) != 0 { |
| 69 | + t.Errorf("expected no findings, got %d: %+v", len(findings), findings) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestDetect_Multiple(t *testing.T) { |
| 74 | + d, err := NewDetector() |
| 75 | + if err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + text := "aws key " + awsKey + " and github " + githubPAT |
| 79 | + findings := d.Detect(text) |
| 80 | + if len(findings) < 2 { |
| 81 | + t.Errorf("expected at least 2 findings, got %d: %+v", len(findings), findings) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestRedact_ReplacesWithRuleID(t *testing.T) { |
| 86 | + d, err := NewDetector() |
| 87 | + if err != nil { |
| 88 | + t.Fatal(err) |
| 89 | + } |
| 90 | + text := "my key is " + awsKey + " ok" |
| 91 | + redacted, findings := d.Redact(text) |
| 92 | + if len(findings) == 0 { |
| 93 | + t.Fatal("expected findings") |
| 94 | + } |
| 95 | + if strings.Contains(redacted, awsKey) { |
| 96 | + t.Errorf("expected key to be redacted, got: %s", redacted) |
| 97 | + } |
| 98 | + if !strings.Contains(redacted, "[REDACTED:") { |
| 99 | + t.Errorf("expected [REDACTED:...] placeholder, got: %s", redacted) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestRedact_ReturnsFindings(t *testing.T) { |
| 104 | + d, err := NewDetector() |
| 105 | + if err != nil { |
| 106 | + t.Fatal(err) |
| 107 | + } |
| 108 | + _, findings := d.Redact("key is " + awsKey) |
| 109 | + if len(findings) == 0 { |
| 110 | + t.Fatal("expected findings") |
| 111 | + } |
| 112 | + f := findings[0] |
| 113 | + if f.RuleID == "" { |
| 114 | + t.Error("expected non-empty RuleID") |
| 115 | + } |
| 116 | + if !strings.Contains(f.Match, awsKey) { |
| 117 | + t.Errorf("expected Match to contain the key, got: %s", f.Match) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestRedact_NoMatch(t *testing.T) { |
| 122 | + d, err := NewDetector() |
| 123 | + if err != nil { |
| 124 | + t.Fatal(err) |
| 125 | + } |
| 126 | + text := "nothing secret here" |
| 127 | + redacted, findings := d.Redact(text) |
| 128 | + if redacted != text { |
| 129 | + t.Errorf("expected unchanged text, got: %s", redacted) |
| 130 | + } |
| 131 | + if len(findings) != 0 { |
| 132 | + t.Errorf("expected no findings, got %d", len(findings)) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func TestNilDetector(t *testing.T) { |
| 137 | + var d *Detector |
| 138 | + findings := d.Detect(awsKey) |
| 139 | + if findings != nil { |
| 140 | + t.Errorf("expected nil from nil detector, got %+v", findings) |
| 141 | + } |
| 142 | + redacted, findings := d.Redact(awsKey) |
| 143 | + if redacted != awsKey { |
| 144 | + t.Errorf("expected original text from nil detector, got: %s", redacted) |
| 145 | + } |
| 146 | + if findings != nil { |
| 147 | + t.Errorf("expected nil findings from nil detector, got %+v", findings) |
| 148 | + } |
| 149 | +} |