Skip to content

Commit 6d1c59f

Browse files
oss-87: added new sanity detector (#3836)
1 parent 81d5859 commit 6d1c59f

File tree

6 files changed

+331
-6
lines changed

6 files changed

+331
-6
lines changed

pkg/detectors/sanity/sanity.go

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package sanity
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
9+
regexp "github.com/wasilibs/go-re2"
10+
11+
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
12+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
13+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
14+
)
15+
16+
type Scanner struct {
17+
client *http.Client
18+
}
19+
20+
// Ensure the Scanner satisfies the interface at compile time.
21+
var _ detectors.Detector = (*Scanner)(nil)
22+
23+
var (
24+
authTokenPat = regexp.MustCompile(detectors.PrefixRegex([]string{"sanity"}) + `\b(sk[A-Za-z0-9]{79})\b`)
25+
)
26+
27+
// Keywords are used for efficiently pre-filtering chunks.
28+
// Use identifiers in the secret preferably, or the provider name.
29+
func (s Scanner) Keywords() []string {
30+
return []string{"sanity"}
31+
}
32+
33+
func (s Scanner) Description() string {
34+
return "Sanity is the modern CMS that transforms content into a competitive advantage. Customize, collaborate, and scale your digital experiences seamlessly."
35+
}
36+
37+
// FromData will find and optionally verify Meraki API Key secrets in a given set of bytes.
38+
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
39+
dataStr := string(data)
40+
41+
// uniqueMatches will hold unique match values and ensure we only process unique matches found in the data string
42+
var uniqueMatches = make(map[string]struct{})
43+
44+
for _, match := range authTokenPat.FindAllStringSubmatch(dataStr, -1) {
45+
uniqueMatches[match[1]] = struct{}{}
46+
}
47+
48+
for match := range uniqueMatches {
49+
s1 := detectors.Result{
50+
DetectorType: detectorspb.DetectorType_Sanity,
51+
Raw: []byte(match),
52+
}
53+
54+
if verify {
55+
if s.client == nil {
56+
s.client = common.SaneHttpClient()
57+
}
58+
59+
isVerified, verificationErr := verifySanityAuthToken(ctx, s.client, match)
60+
s1.Verified = isVerified
61+
s1.SetVerificationError(verificationErr)
62+
}
63+
64+
results = append(results, s1)
65+
}
66+
67+
return results, nil
68+
}
69+
70+
func (s Scanner) Type() detectorspb.DetectorType {
71+
return detectorspb.DetectorType_Sanity
72+
}
73+
74+
/*
75+
verifySanityAuthToken verifies if the passed matched auth token for sanity is active or not.
76+
auth docs: https://www.sanity.io/docs/http-auth
77+
api docs: https://www.sanity.io/docs/reference/http/access#tag/permissions/GET/vX/access/permissions/me
78+
*/
79+
func verifySanityAuthToken(ctx context.Context, client *http.Client, authToken string) (bool, error) {
80+
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.sanity.io/vX/access/permissions/me", http.NoBody)
81+
if err != nil {
82+
return false, err
83+
}
84+
85+
// set the required auth header
86+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", authToken))
87+
88+
resp, err := client.Do(req)
89+
if err != nil {
90+
return false, err
91+
}
92+
defer func() {
93+
_, _ = io.Copy(io.Discard, resp.Body)
94+
_ = resp.Body.Close()
95+
}()
96+
97+
switch resp.StatusCode {
98+
case http.StatusOK:
99+
return true, nil
100+
case http.StatusUnauthorized, http.StatusForbidden:
101+
return false, nil
102+
default:
103+
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//go:build detectors
2+
// +build detectors
3+
4+
package sanity
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"testing"
10+
"time"
11+
12+
"github.com/google/go-cmp/cmp"
13+
"github.com/google/go-cmp/cmp/cmpopts"
14+
15+
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
16+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
17+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
18+
)
19+
20+
func TestSanity_FromChunk(t *testing.T) {
21+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
22+
defer cancel()
23+
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")
24+
if err != nil {
25+
t.Fatalf("could not get test secrets from GCP: %s", err)
26+
}
27+
secret := testSecrets.MustGetField("SANITY_AUTHTOKEN")
28+
inactiveSecret := testSecrets.MustGetField("SANITY_AUTHTOKEN_INACTIVE")
29+
30+
type args struct {
31+
ctx context.Context
32+
data []byte
33+
verify bool
34+
}
35+
tests := []struct {
36+
name string
37+
s Scanner
38+
args args
39+
want []detectors.Result
40+
wantErr bool
41+
wantVerificationErr bool
42+
}{
43+
{
44+
name: "found, verified",
45+
s: Scanner{},
46+
args: args{
47+
ctx: ctx,
48+
data: []byte(fmt.Sprintf("You can find a sanity apikey %s within", secret)),
49+
verify: true,
50+
},
51+
want: []detectors.Result{
52+
{
53+
DetectorType: detectorspb.DetectorType_Sanity,
54+
Verified: true,
55+
},
56+
},
57+
wantErr: false,
58+
wantVerificationErr: false,
59+
},
60+
{
61+
name: "found, unverified",
62+
s: Scanner{},
63+
args: args{
64+
ctx: ctx,
65+
data: []byte(fmt.Sprintf("You can find a sanity apikey secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation
66+
verify: true,
67+
},
68+
want: []detectors.Result{
69+
{
70+
DetectorType: detectorspb.DetectorType_Sanity,
71+
Verified: false,
72+
},
73+
},
74+
wantErr: false,
75+
wantVerificationErr: false,
76+
},
77+
{
78+
name: "not found",
79+
s: Scanner{},
80+
args: args{
81+
ctx: context.Background(),
82+
data: []byte("You cannot find the secret within"),
83+
verify: true,
84+
},
85+
want: nil,
86+
wantErr: false,
87+
wantVerificationErr: false,
88+
},
89+
}
90+
for _, tt := range tests {
91+
t.Run(tt.name, func(t *testing.T) {
92+
got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
93+
if (err != nil) != tt.wantErr {
94+
t.Errorf("Sanity.FromData() error = %v, wantErr %v", err, tt.wantErr)
95+
return
96+
}
97+
for i := range got {
98+
if len(got[i].Raw) == 0 {
99+
t.Fatalf("no raw secret present: \n %+v", got[i])
100+
}
101+
if (got[i].VerificationError() != nil) != tt.wantVerificationErr {
102+
t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError())
103+
}
104+
}
105+
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError")
106+
if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {
107+
t.Errorf("Sanity.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
108+
}
109+
})
110+
}
111+
}
112+
113+
func BenchmarkFromData(benchmark *testing.B) {
114+
ctx := context.Background()
115+
s := Scanner{}
116+
for name, data := range detectors.MustGetBenchmarkData() {
117+
benchmark.Run(name, func(b *testing.B) {
118+
b.ResetTimer()
119+
for n := 0; n < b.N; n++ {
120+
_, err := s.FromData(ctx, false, data)
121+
if err != nil {
122+
b.Fatal(err)
123+
}
124+
}
125+
})
126+
}
127+
}

pkg/detectors/sanity/sanity_test.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package sanity
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/google/go-cmp/cmp"
9+
10+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
11+
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
12+
)
13+
14+
var (
15+
validPattern = `Information used in API calls for sanity
16+
Variable name | Initial Value
17+
sanity auth token |skCrESWvpXysjmfakeaMGdMecRnw2mTVURqlVABArKApL1j4SLUhFAKEEizjp7ymM8pebv0ScqyqelbLD
18+
networkId |L_646829496481117067
19+
serial |`
20+
21+
invalidPattern = "skCr_SWvpXysjmfakeaMGdMecRnw$mTVURqlVABArKApL1j4SLUhFAKEEizjp7ymM8pebv0ScqyqelbLD"
22+
)
23+
24+
func TestSanity_Pattern(t *testing.T) {
25+
d := Scanner{}
26+
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
27+
28+
tests := []struct {
29+
name string
30+
input string
31+
want []string
32+
}{
33+
{
34+
name: "valid pattern",
35+
input: validPattern,
36+
want: []string{"skCrESWvpXysjmfakeaMGdMecRnw2mTVURqlVABArKApL1j4SLUhFAKEEizjp7ymM8pebv0ScqyqelbLD"},
37+
},
38+
{
39+
name: "invalid pattern",
40+
input: fmt.Sprintf("sanity = '%s'", invalidPattern),
41+
want: nil,
42+
},
43+
}
44+
45+
for _, test := range tests {
46+
t.Run(test.name, func(t *testing.T) {
47+
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
48+
if len(matchedDetectors) == 0 {
49+
t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
50+
return
51+
}
52+
53+
results, err := d.FromData(context.Background(), false, []byte(test.input))
54+
if err != nil {
55+
t.Errorf("error = %v", err)
56+
return
57+
}
58+
59+
if len(results) != len(test.want) {
60+
if len(results) == 0 {
61+
t.Errorf("did not receive result")
62+
} else {
63+
t.Errorf("expected %d results, only received %d", len(test.want), len(results))
64+
}
65+
return
66+
}
67+
68+
actual := make(map[string]struct{}, len(results))
69+
for _, r := range results {
70+
if len(r.RawV2) > 0 {
71+
actual[string(r.RawV2)] = struct{}{}
72+
} else {
73+
actual[string(r.Raw)] = struct{}{}
74+
}
75+
}
76+
expected := make(map[string]struct{}, len(test.want))
77+
for _, v := range test.want {
78+
expected[v] = struct{}{}
79+
}
80+
81+
if diff := cmp.Diff(expected, actual); diff != "" {
82+
t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
83+
}
84+
})
85+
}
86+
}

pkg/engine/defaults/defaults.go

+2
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ import (
601601
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/salesflare"
602602
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/salesforce"
603603
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/salesmate"
604+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/sanity"
604605
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/satismeterprojectkey"
605606
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/satismeterwritekey"
606607
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/saucelabs"
@@ -1444,6 +1445,7 @@ func buildDetectorList() []detectors.Detector {
14441445
&salesflare.Scanner{},
14451446
&salesforce.Scanner{},
14461447
&salesmate.Scanner{},
1448+
&sanity.Scanner{},
14471449
&satismeterprojectkey.Scanner{},
14481450
&satismeterwritekey.Scanner{},
14491451
&saucelabs.Scanner{},

pkg/pb/detectorspb/detectors.pb.go

+10-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/detectors.proto

+1
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ enum DetectorType {
10211021
Flexport = 1009;
10221022
TwitchAccessToken = 1010;
10231023
TwilioApiKey = 1011;
1024+
Sanity = 1012;
10241025
}
10251026

10261027
message Result {

0 commit comments

Comments
 (0)