|
1 |
| -//go:build detectors |
2 |
| -// +build detectors |
3 |
| - |
4 | 1 | package bannerbear
|
5 | 2 |
|
6 | 3 | import (
|
7 | 4 | "context"
|
8 | 5 | "fmt"
|
9 | 6 | "testing"
|
10 |
| - "time" |
11 | 7 |
|
12 |
| - "github.com/kylelemons/godebug/pretty" |
13 |
| - "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" |
| 8 | + "github.com/google/go-cmp/cmp" |
14 | 9 |
|
15 |
| - "github.com/trufflesecurity/trufflehog/v3/pkg/common" |
16 |
| - "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" |
| 10 | + "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" |
| 11 | + "github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick" |
17 | 12 | )
|
18 | 13 |
|
19 |
| -func TestBannerbear_FromChunk(t *testing.T) { |
20 |
| - ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) |
21 |
| - defer cancel() |
22 |
| - testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors1") |
23 |
| - if err != nil { |
24 |
| - t.Fatalf("could not get test secrets from GCP: %s", err) |
25 |
| - } |
26 |
| - secret := testSecrets.MustGetField("BANNERBEAR") |
27 |
| - inactiveSecret := testSecrets.MustGetField("BANNERBEAR_INACTIVE") |
| 14 | +var ( |
| 15 | + validPattern = "yvxpthLIcYpZweFpPOVeCOtt" |
| 16 | + complexPattern = ` |
| 17 | + func main() { |
| 18 | + url := "https://api.example.com/v1/resource" |
| 19 | +
|
| 20 | + // Create a new request with the secret as a header |
| 21 | + req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte("{}"))) |
| 22 | + if err != nil { |
| 23 | + fmt.Println("Error creating request:", err) |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + bannerBearToken := "Bearer yvxpthLIcYpZweFpPOVeCOtt" |
| 28 | + req.Header.Set("Authorization", bannerBearToken) |
| 29 | +
|
| 30 | + // Perform the request |
| 31 | + client := &http.Client{} |
| 32 | + resp, _ := client.Do(req) |
| 33 | + defer resp.Body.Close() |
28 | 34 |
|
29 |
| - type args struct { |
30 |
| - ctx context.Context |
31 |
| - data []byte |
32 |
| - verify bool |
| 35 | + // Check response status |
| 36 | + if resp.StatusCode == http.StatusOK { |
| 37 | + fmt.Println("Request successful!") |
| 38 | + } else { |
| 39 | + fmt.Println("Request failed with status:", resp.Status) |
| 40 | + } |
33 | 41 | }
|
| 42 | + ` |
| 43 | + invalidPattern = "yvxpthLIcYpZweFpPOVeCOtot" |
| 44 | +) |
| 45 | + |
| 46 | +func TestBannerBear_Pattern(t *testing.T) { |
| 47 | + d := Scanner{} |
| 48 | + ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d}) |
| 49 | + |
34 | 50 | tests := []struct {
|
35 |
| - name string |
36 |
| - s Scanner |
37 |
| - args args |
38 |
| - want []detectors.Result |
39 |
| - wantErr bool |
| 51 | + name string |
| 52 | + input string |
| 53 | + want []string |
40 | 54 | }{
|
41 | 55 | {
|
42 |
| - name: "found, verified", |
43 |
| - s: Scanner{}, |
44 |
| - args: args{ |
45 |
| - ctx: context.Background(), |
46 |
| - data: []byte(fmt.Sprintf("You can find a bannerbear secret %s within", secret)), |
47 |
| - verify: true, |
48 |
| - }, |
49 |
| - want: []detectors.Result{ |
50 |
| - { |
51 |
| - DetectorType: detectorspb.DetectorType_Bannerbear, |
52 |
| - Verified: true, |
53 |
| - }, |
54 |
| - }, |
55 |
| - wantErr: false, |
| 56 | + name: "valid pattern", |
| 57 | + input: fmt.Sprintf("bannerbear credentials: %s", validPattern), |
| 58 | + want: []string{"yvxpthLIcYpZweFpPOVeCOtt"}, |
56 | 59 | },
|
57 | 60 | {
|
58 |
| - name: "found, unverified", |
59 |
| - s: Scanner{}, |
60 |
| - args: args{ |
61 |
| - ctx: context.Background(), |
62 |
| - data: []byte(fmt.Sprintf("You can find a bannerbear secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation |
63 |
| - verify: true, |
64 |
| - }, |
65 |
| - want: []detectors.Result{ |
66 |
| - { |
67 |
| - DetectorType: detectorspb.DetectorType_Bannerbear, |
68 |
| - Verified: false, |
69 |
| - }, |
70 |
| - }, |
71 |
| - wantErr: false, |
| 61 | + name: "valid pattern - complex", |
| 62 | + input: complexPattern, |
| 63 | + want: []string{"yvxpthLIcYpZweFpPOVeCOtt"}, |
72 | 64 | },
|
73 | 65 | {
|
74 |
| - name: "not found", |
75 |
| - s: Scanner{}, |
76 |
| - args: args{ |
77 |
| - ctx: context.Background(), |
78 |
| - data: []byte("You cannot find the secret within"), |
79 |
| - verify: true, |
80 |
| - }, |
81 |
| - want: nil, |
82 |
| - wantErr: false, |
| 66 | + name: "invalid pattern", |
| 67 | + input: fmt.Sprintf("bannerbear credentials: %s", invalidPattern), |
| 68 | + want: nil, |
83 | 69 | },
|
84 | 70 | }
|
85 |
| - for _, tt := range tests { |
86 |
| - t.Run(tt.name, func(t *testing.T) { |
87 |
| - s := Scanner{} |
88 |
| - got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) |
89 |
| - if (err != nil) != tt.wantErr { |
90 |
| - t.Errorf("Bannerbear.FromData() error = %v, wantErr %v", err, tt.wantErr) |
| 71 | + |
| 72 | + for _, test := range tests { |
| 73 | + t.Run(test.name, func(t *testing.T) { |
| 74 | + matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input)) |
| 75 | + if len(matchedDetectors) == 0 { |
| 76 | + t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input) |
91 | 77 | return
|
92 | 78 | }
|
93 |
| - for i := range got { |
94 |
| - if len(got[i].Raw) == 0 { |
95 |
| - t.Fatalf("no raw secret present: \n %+v", got[i]) |
96 |
| - } |
97 |
| - got[i].Raw = nil |
| 79 | + |
| 80 | + results, err := d.FromData(context.Background(), false, []byte(test.input)) |
| 81 | + if err != nil { |
| 82 | + t.Errorf("error = %v", err) |
| 83 | + return |
98 | 84 | }
|
99 |
| - if diff := pretty.Compare(got, tt.want); diff != "" { |
100 |
| - t.Errorf("Bannerbear.FromData() %s diff: (-got +want)\n%s", tt.name, diff) |
| 85 | + |
| 86 | + if len(results) != len(test.want) { |
| 87 | + if len(results) == 0 { |
| 88 | + t.Errorf("did not receive result") |
| 89 | + } else { |
| 90 | + t.Errorf("expected %d results, only received %d", len(test.want), len(results)) |
| 91 | + } |
| 92 | + return |
101 | 93 | }
|
102 |
| - }) |
103 |
| - } |
104 |
| -} |
105 | 94 |
|
106 |
| -func BenchmarkFromData(benchmark *testing.B) { |
107 |
| - ctx := context.Background() |
108 |
| - s := Scanner{} |
109 |
| - for name, data := range detectors.MustGetBenchmarkData() { |
110 |
| - benchmark.Run(name, func(b *testing.B) { |
111 |
| - b.ResetTimer() |
112 |
| - for n := 0; n < b.N; n++ { |
113 |
| - _, err := s.FromData(ctx, false, data) |
114 |
| - if err != nil { |
115 |
| - b.Fatal(err) |
| 95 | + actual := make(map[string]struct{}, len(results)) |
| 96 | + for _, r := range results { |
| 97 | + if len(r.RawV2) > 0 { |
| 98 | + actual[string(r.RawV2)] = struct{}{} |
| 99 | + } else { |
| 100 | + actual[string(r.Raw)] = struct{}{} |
116 | 101 | }
|
117 | 102 | }
|
| 103 | + expected := make(map[string]struct{}, len(test.want)) |
| 104 | + for _, v := range test.want { |
| 105 | + expected[v] = struct{}{} |
| 106 | + } |
| 107 | + |
| 108 | + if diff := cmp.Diff(expected, actual); diff != "" { |
| 109 | + t.Errorf("%s diff: (-want +got)\n%s", test.name, diff) |
| 110 | + } |
118 | 111 | })
|
119 | 112 | }
|
120 | 113 | }
|
0 commit comments