Skip to content

Commit 0129077

Browse files
authored
Feature: Airtable Personal Access Token Detector (#3933)
* added airtable personal access token detector * added prefix in airtable pat regex
1 parent e07eb54 commit 0129077

File tree

6 files changed

+362
-7
lines changed

6 files changed

+362
-7
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package airtablepersonalaccesstoken
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+
defaultClient = common.SaneHttpClient()
25+
tokenPat = regexp.MustCompile(detectors.PrefixRegex([]string{"airtable"}) + `\b(pat[[:alnum:]]{14}\.[a-f0-9]{64})\b`)
26+
)
27+
28+
func (s Scanner) Keywords() []string {
29+
return []string{"airtable"}
30+
}
31+
32+
// FromData will find and optionally verify AirtableOAuth secrets in a given set of bytes.
33+
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
34+
dataStr := string(data)
35+
36+
uniqueMatches := make(map[string]struct{})
37+
for _, match := range tokenPat.FindAllStringSubmatch(dataStr, -1) {
38+
uniqueMatches[match[1]] = struct{}{}
39+
}
40+
41+
for match := range uniqueMatches {
42+
s1 := detectors.Result{
43+
DetectorType: detectorspb.DetectorType_AirtablePersonalAccessToken,
44+
Raw: []byte(match),
45+
}
46+
47+
if verify {
48+
client := s.client
49+
if client == nil {
50+
client = defaultClient
51+
}
52+
53+
isVerified, extraData, verificationErr := verifyMatch(ctx, client, match)
54+
s1.Verified = isVerified
55+
s1.ExtraData = extraData
56+
s1.SetVerificationError(verificationErr, match)
57+
}
58+
59+
results = append(results, s1)
60+
}
61+
62+
return
63+
}
64+
65+
func verifyMatch(ctx context.Context, client *http.Client, token string) (bool, map[string]string, error) {
66+
endpoint := "https://api.airtable.com/v0/meta/whoami"
67+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
68+
if err != nil {
69+
return false, nil, nil
70+
}
71+
72+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
73+
res, err := client.Do(req)
74+
if err != nil {
75+
return false, nil, err
76+
}
77+
defer func() {
78+
_, _ = io.Copy(io.Discard, res.Body)
79+
_ = res.Body.Close()
80+
}()
81+
82+
switch res.StatusCode {
83+
case http.StatusOK:
84+
return true, nil, nil
85+
case http.StatusUnauthorized:
86+
// The secret is determinately not verified (nothing to do)
87+
return false, nil, nil
88+
default:
89+
return false, nil, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
90+
}
91+
}
92+
93+
func (s Scanner) Type() detectorspb.DetectorType {
94+
return detectorspb.DetectorType_AirtablePersonalAccessToken
95+
}
96+
97+
func (s Scanner) Description() string {
98+
return "Airtable is a cloud collaboration service that offers database-like features. Airtable OAuth tokens can be used to access and modify data within Airtable bases."
99+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
//go:build detectors
2+
// +build detectors
3+
4+
package airtablepersonalaccesstoken
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 TestAirtablepersonalaccesstoken_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("AIRTABLEPERSONALACCESSTOKEN")
28+
inactiveSecret := testSecrets.MustGetField("AIRTABLEPERSONALACCESSTOKEN_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 an airtable secret %s within", secret)),
49+
verify: true,
50+
},
51+
want: []detectors.Result{
52+
{
53+
DetectorType: detectorspb.DetectorType_AirtablePersonalAccessToken,
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: context.Background(),
65+
data: []byte(fmt.Sprintf("You can find an airtable 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_AirtablePersonalAccessToken,
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+
name: "found, would be verified if not for timeout",
91+
s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)},
92+
args: args{
93+
ctx: context.Background(),
94+
data: []byte(fmt.Sprintf("You can find a airtablepersonalaccesstoken secret %s within", secret)),
95+
verify: true,
96+
},
97+
want: []detectors.Result{
98+
{
99+
DetectorType: detectorspb.DetectorType_AirtablePersonalAccessToken,
100+
Verified: false,
101+
},
102+
},
103+
wantErr: false,
104+
wantVerificationErr: true,
105+
},
106+
{
107+
name: "found, verified but unexpected api surface",
108+
s: Scanner{client: common.ConstantResponseHttpClient(404, "")},
109+
args: args{
110+
ctx: context.Background(),
111+
data: []byte(fmt.Sprintf("You can find a airtablepersonalaccesstoken secret %s within", secret)),
112+
verify: true,
113+
},
114+
want: []detectors.Result{
115+
{
116+
DetectorType: detectorspb.DetectorType_AirtablePersonalAccessToken,
117+
Verified: false,
118+
},
119+
},
120+
wantErr: false,
121+
wantVerificationErr: true,
122+
},
123+
}
124+
for _, tt := range tests {
125+
t.Run(tt.name, func(t *testing.T) {
126+
got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
127+
if (err != nil) != tt.wantErr {
128+
t.Errorf("Airtablepersonalaccesstoken.FromData() error = %v, wantErr %v", err, tt.wantErr)
129+
return
130+
}
131+
for i := range got {
132+
if len(got[i].Raw) == 0 {
133+
t.Fatalf("no raw secret present: \n %+v", got[i])
134+
}
135+
if (got[i].VerificationError() != nil) != tt.wantVerificationErr {
136+
t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError())
137+
}
138+
}
139+
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError")
140+
if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {
141+
t.Errorf("Airtablepersonalaccesstoken.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
142+
}
143+
})
144+
}
145+
}
146+
147+
func BenchmarkFromData(benchmark *testing.B) {
148+
ctx := context.Background()
149+
s := Scanner{}
150+
for name, data := range detectors.MustGetBenchmarkData() {
151+
benchmark.Run(name, func(b *testing.B) {
152+
b.ResetTimer()
153+
for n := 0; n < b.N; n++ {
154+
_, err := s.FromData(ctx, false, data)
155+
if err != nil {
156+
b.Fatal(err)
157+
}
158+
}
159+
})
160+
}
161+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package airtablepersonalaccesstoken
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+
validPattern1 = "patfqpIZBPU6EAt5x.458546d9c77b21f8a98141f2a4039d5626010f19efc16c20d57c4f41d44c8c85"
16+
validPattern2 = "pat0VXr5I2HcapZE8.da2606afb7d97e936719ec952a4a18b44045e385d4ddf4f38dcc246fb63f0165"
17+
invalidPattern = "tokfqpIZBPU6EAt5x.458546d9c77b21f8a98141f2a403-d5626010f19efc16c20d57c4f41d44c8c85"
18+
)
19+
20+
func TestAirtablepersonalaccesstoken_Pattern(t *testing.T) {
21+
d := Scanner{}
22+
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
23+
tests := []struct {
24+
name string
25+
input string
26+
want []string
27+
}{
28+
{
29+
name: "typical pattern",
30+
input: fmt.Sprintf("airtable token = '%s'", validPattern1),
31+
want: []string{validPattern1},
32+
},
33+
{
34+
name: "finds all matches",
35+
input: fmt.Sprintf(`airtable token 1 = '%s'
36+
airtable token 2 = '%s'`, validPattern1, validPattern2),
37+
want: []string{validPattern1, validPattern2},
38+
},
39+
{
40+
name: "invalid pattern",
41+
input: fmt.Sprintf("airtable token = '%s'", invalidPattern),
42+
want: []string{},
43+
},
44+
}
45+
46+
for _, test := range tests {
47+
t.Run(test.name, func(t *testing.T) {
48+
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
49+
if len(matchedDetectors) == 0 {
50+
t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
51+
return
52+
}
53+
54+
results, err := d.FromData(context.Background(), false, []byte(test.input))
55+
if err != nil {
56+
t.Errorf("error = %v", err)
57+
return
58+
}
59+
60+
if len(results) != len(test.want) {
61+
if len(results) == 0 {
62+
t.Errorf("did not receive result")
63+
} else {
64+
t.Errorf("expected %d results, only received %d", len(test.want), len(results))
65+
}
66+
return
67+
}
68+
69+
actual := make(map[string]struct{}, len(results))
70+
for _, r := range results {
71+
if len(r.RawV2) > 0 {
72+
actual[string(r.RawV2)] = struct{}{}
73+
} else {
74+
actual[string(r.Raw)] = struct{}{}
75+
}
76+
}
77+
expected := make(map[string]struct{}, len(test.want))
78+
for _, v := range test.want {
79+
expected[v] = struct{}{}
80+
}
81+
82+
if diff := cmp.Diff(expected, actual); diff != "" {
83+
t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
84+
}
85+
})
86+
}
87+
}

pkg/engine/defaults/defaults.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/airship"
1616
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/airtableapikey"
1717
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/airtableoauth"
18+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/airtablepersonalaccesstoken"
1819
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/airvisual"
1920
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/aiven"
2021
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/alchemy"
@@ -839,6 +840,7 @@ func buildDetectorList() []detectors.Detector {
839840
&airship.Scanner{},
840841
&airtableapikey.Scanner{},
841842
&airtableoauth.Scanner{},
843+
&airtablepersonalaccesstoken.Scanner{},
842844
&airvisual.Scanner{},
843845
&aiven.Scanner{},
844846
&alchemy.Scanner{},

0 commit comments

Comments
 (0)