Skip to content

Commit d429422

Browse files
added sentry org token detector (#3985)
1 parent 14645c9 commit d429422

File tree

6 files changed

+329
-7
lines changed

6 files changed

+329
-7
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package sentryorgtoken
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+
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
25+
orgAuthTokenPat = regexp.MustCompile(`\b(sntrys_eyJ[a-zA-Z0-9=_+/]{197})\b`)
26+
)
27+
28+
// Keywords are used for efficiently pre-filtering chunks.
29+
// Use identifiers in the secret preferably, or the provider name.
30+
func (s Scanner) Keywords() []string {
31+
return []string{"sntrys_eyJ"}
32+
}
33+
34+
func (s Scanner) Type() detectorspb.DetectorType {
35+
return detectorspb.DetectorType_SentryOrgToken
36+
}
37+
38+
func (s Scanner) Description() string {
39+
return "Sentry is an error tracking service that helps developers monitor and fix crashes in real time. Sentry Organization Auth Tokens can be used in many places to interact with Sentry programmatically. For example, they can be used for sentry-cli, bundler plugins, or similar use cases."
40+
}
41+
42+
// FromData will find and optionally verify SentryToken secrets in a given set of bytes.
43+
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
44+
dataStr := string(data)
45+
46+
// find all unique org auth tokens
47+
var uniqueOrgTokens = make(map[string]struct{})
48+
49+
for _, orgToken := range orgAuthTokenPat.FindAllStringSubmatch(dataStr, -1) {
50+
uniqueOrgTokens[orgToken[1]] = struct{}{}
51+
}
52+
53+
for orgToken := range uniqueOrgTokens {
54+
s1 := detectors.Result{
55+
DetectorType: detectorspb.DetectorType_SentryOrgToken,
56+
Raw: []byte(orgToken),
57+
}
58+
59+
if verify {
60+
if s.client == nil {
61+
s.client = common.SaneHttpClient()
62+
}
63+
64+
isVerified, verificationErr := verifySentryOrgToken(ctx, s.client, orgToken)
65+
s1.Verified = isVerified
66+
s1.SetVerificationError(verificationErr, orgToken)
67+
}
68+
69+
results = append(results, s1)
70+
}
71+
72+
return results, nil
73+
}
74+
75+
// docs: https://docs.sentry.io/account/auth-tokens/#organization-auth-tokens
76+
func verifySentryOrgToken(ctx context.Context, client *http.Client, token string) (bool, error) {
77+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://sentry.io/api/0/auth/validate", nil)
78+
if err != nil {
79+
return false, err
80+
}
81+
82+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
83+
84+
resp, err := client.Do(req)
85+
if err != nil {
86+
return false, err
87+
}
88+
defer func() {
89+
_, _ = io.Copy(io.Discard, resp.Body)
90+
_ = resp.Body.Close()
91+
}()
92+
93+
switch resp.StatusCode {
94+
case http.StatusOK:
95+
return true, nil
96+
case http.StatusForbidden, http.StatusUnauthorized:
97+
return false, nil
98+
default:
99+
return false, fmt.Errorf("unexpected HTTP response status %d", resp.StatusCode)
100+
}
101+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//go:build detectors
2+
// +build detectors
3+
4+
package sentryorgtoken
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 TestSentryOrgToken_FromChunk(t *testing.T) {
21+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
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("SENTRY_ORG_TOKEN")
28+
inactiveSecret := testSecrets.MustGetField("SENTRY_ORG_TOKEN_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: context.Background(),
48+
data: []byte(fmt.Sprintf("You can find a sentry secret %s within", secret)),
49+
verify: true,
50+
},
51+
want: []detectors.Result{
52+
{
53+
DetectorType: detectorspb.DetectorType_SentryOrgToken,
54+
Verified: true,
55+
},
56+
},
57+
wantErr: false,
58+
},
59+
{
60+
name: "found, unverified",
61+
s: Scanner{},
62+
args: args{
63+
ctx: context.Background(),
64+
data: []byte(fmt.Sprintf("You can find a sentry secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation
65+
verify: true,
66+
},
67+
want: []detectors.Result{
68+
{
69+
DetectorType: detectorspb.DetectorType_SentryOrgToken,
70+
Verified: false,
71+
},
72+
},
73+
wantErr: false,
74+
},
75+
{
76+
name: "not found",
77+
s: Scanner{},
78+
args: args{
79+
ctx: context.Background(),
80+
data: []byte("You cannot find the secret within"),
81+
verify: true,
82+
},
83+
want: nil,
84+
wantErr: false,
85+
},
86+
}
87+
88+
for _, tt := range tests {
89+
t.Run(tt.name, func(t *testing.T) {
90+
got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
91+
if (err != nil) != tt.wantErr {
92+
t.Errorf("SentryOrgToken.FromData() error = %v, wantErr %v", err, tt.wantErr)
93+
return
94+
}
95+
for i := range got {
96+
if len(got[i].Raw) == 0 {
97+
t.Fatal("no raw secret present")
98+
}
99+
if (got[i].VerificationError() != nil) != tt.wantVerificationErr {
100+
t.Fatalf("wantVerificationError = %v, verification error = %v,", tt.wantVerificationErr, got[i].VerificationError())
101+
}
102+
}
103+
opts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError")
104+
if diff := cmp.Diff(got, tt.want, opts); diff != "" {
105+
t.Errorf("SentryOrgToken.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
106+
}
107+
})
108+
}
109+
}
110+
111+
func BenchmarkFromData(benchmark *testing.B) {
112+
ctx := context.Background()
113+
s := Scanner{}
114+
for name, data := range detectors.MustGetBenchmarkData() {
115+
benchmark.Run(name, func(b *testing.B) {
116+
b.ResetTimer()
117+
for n := 0; n < b.N; n++ {
118+
_, err := s.FromData(ctx, false, data)
119+
if err != nil {
120+
b.Fatal(err)
121+
}
122+
}
123+
})
124+
}
125+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package sentryorgtoken
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 = `
16+
sentry_token := sntrys_eyJFAKEiOjE3NDIzNjM1NTIuNTAzMzA5LCJ1cmwiOiJodHRwczovL3NlbnRyeS5pbyIsInJlZ2lvbl91cmwiOiJodHRwczovL3VzLnNlbnRyeS5pbfakem9yZyI6InRydWZmbGUtc2VjdXJpdHktamQifQ==_+zqSnKjs87cicc3FAK08vmZs5cWx9C5EARKHFtW5lqI
17+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", sentry_token))
18+
`
19+
invalidPattern = "sntrys_eyJFAKE-OjE3NDIzNjM1NTIuNTAzMzA5LCJ1cmwiOiJodHRwczovL3NlbnRyeS5pbyIsInJlZ2lvbl91cmwiOiJodHRwczovL3VzLnNlbnRyeS5pbfakem9yZyI6InRydWZmbGUtc2VjdXJpdHktamQifQ==_+zqSnKjs87cicc3FAK08vmZs5cWx9C5EARKHFtW5lqI"
20+
token = "sntrys_eyJFAKEiOjE3NDIzNjM1NTIuNTAzMzA5LCJ1cmwiOiJodHRwczovL3NlbnRyeS5pbyIsInJlZ2lvbl91cmwiOiJodHRwczovL3VzLnNlbnRyeS5pbfakem9yZyI6InRydWZmbGUtc2VjdXJpdHktamQifQ==_+zqSnKjs87cicc3FAK08vmZs5cWx9C5EARKHFtW5lqI"
21+
)
22+
23+
func TestSentryToken_Pattern(t *testing.T) {
24+
d := Scanner{}
25+
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
26+
tests := []struct {
27+
name string
28+
input string
29+
want []string
30+
}{
31+
{
32+
name: "valid pattern - with keyword sentry org token",
33+
input: validPattern,
34+
want: []string{token},
35+
},
36+
{
37+
name: "valid pattern - ignore duplicate",
38+
input: fmt.Sprintf("token = '%s' | '%s'", validPattern, validPattern),
39+
want: []string{token},
40+
},
41+
{
42+
name: "invalid pattern",
43+
input: invalidPattern,
44+
want: []string{},
45+
},
46+
}
47+
48+
for _, test := range tests {
49+
t.Run(test.name, func(t *testing.T) {
50+
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
51+
if len(matchedDetectors) == 0 {
52+
t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
53+
return
54+
}
55+
56+
results, err := d.FromData(context.Background(), false, []byte(test.input))
57+
if err != nil {
58+
t.Errorf("error = %v", err)
59+
return
60+
}
61+
62+
if len(results) != len(test.want) {
63+
if len(results) == 0 {
64+
t.Errorf("did not receive result")
65+
} else {
66+
t.Errorf("expected %d results, only received %d", len(test.want), len(results))
67+
}
68+
return
69+
}
70+
71+
actual := make(map[string]struct{}, len(results))
72+
for _, r := range results {
73+
if len(r.RawV2) > 0 {
74+
actual[string(r.RawV2)] = struct{}{}
75+
} else {
76+
actual[string(r.Raw)] = struct{}{}
77+
}
78+
}
79+
expected := make(map[string]struct{}, len(test.want))
80+
for _, v := range test.want {
81+
expected[v] = struct{}{}
82+
}
83+
84+
if diff := cmp.Diff(expected, actual); diff != "" {
85+
t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
86+
}
87+
})
88+
}
89+
}

pkg/engine/defaults/defaults.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ import (
629629
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/sendbirdorganizationapi"
630630
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/sendgrid"
631631
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/sendinbluev2"
632+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/sentryorgtoken"
632633
sentrytokenv1 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/sentrytoken/v1"
633634
sentrytokenv2 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/sentrytoken/v2"
634635
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/serphouse"
@@ -1480,6 +1481,7 @@ func buildDetectorList() []detectors.Detector {
14801481
&sendinbluev2.Scanner{},
14811482
&sentrytokenv1.Scanner{},
14821483
&sentrytokenv2.Scanner{},
1484+
&sentryorgtoken.Scanner{},
14831485
&serphouse.Scanner{},
14841486
&serpstack.Scanner{},
14851487
&sheety.Scanner{},

pkg/pb/detectorspb/detectors.pb.go

Lines changed: 11 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/detectors.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,7 @@ enum DetectorType {
10261026
AirtableOAuth = 1014;
10271027
AirtablePersonalAccessToken = 1015;
10281028
StoryblokPersonalAccessToken = 1016;
1029+
SentryOrgToken = 1017;
10291030
}
10301031

10311032
message Result {

0 commit comments

Comments
 (0)