mirror of
https://github.com/trufflesecurity/trufflehog.git
synced 2024-11-10 07:04:24 +00:00
Adding Larksuite Detectors + Tests (#3008)
* implemented larksuite detectores for tokens and api keys. test implemented for larksuite token based detectors. * implemented test for larksuiteapikey detector * load credentials from GCP secret manager for larksuite api keys
This commit is contained in:
parent
3c20b000e1
commit
dddeca5224
7 changed files with 678 additions and 7 deletions
136
pkg/detectors/larksuite/larksuite.go
Normal file
136
pkg/detectors/larksuite/larksuite.go
Normal file
|
@ -0,0 +1,136 @@
|
|||
package larksuite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
regexp "github.com/wasilibs/go-re2"
|
||||
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
|
||||
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
||||
)
|
||||
|
||||
type Scanner struct {
|
||||
detectors.DefaultMultiPartCredentialProvider
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// Check that the LarkSuite scanner implements the SecretScanner interface at compile time.
|
||||
var _ detectors.Detector = Scanner{}
|
||||
|
||||
type tokenType string
|
||||
|
||||
const (
|
||||
TenantAccessToken tokenType = "Tenant Access Token"
|
||||
UserAccessToken tokenType = "User Access Token"
|
||||
AppAccessToken tokenType = "App Access Token"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultClient = common.SaneHttpClient()
|
||||
tokenPats = map[tokenType]*regexp.Regexp{
|
||||
TenantAccessToken: regexp.MustCompile(detectors.PrefixRegex([]string{"lark", "larksuite", "tenant"}) + `\b(t-[a-z0-9A-Z_.]{14,50})\b`),
|
||||
UserAccessToken: regexp.MustCompile(detectors.PrefixRegex([]string{"lark", "larksuite", "user"}) + `\b(u-[a-z0-9A-Z_.]{14,50})\b`),
|
||||
AppAccessToken: regexp.MustCompile(detectors.PrefixRegex([]string{"lark", "larksuite", "app"}) + `\b(a-[a-z0-9A-Z_.]{14,50})\b`),
|
||||
}
|
||||
|
||||
verificationUrls = map[tokenType]string{
|
||||
TenantAccessToken: "https://open.larksuite.com/open-apis/tenant/v2/tenant/query",
|
||||
UserAccessToken: "https://open.larksuite.com/open-apis/authen/v1/user_info",
|
||||
AppAccessToken: "https://open.larksuite.com/open-apis/calendar/v4/calendars",
|
||||
}
|
||||
)
|
||||
|
||||
// Keywords are used for efficiently pre-filtering chunks.
|
||||
// Use identifiers in the secret preferably, or the provider name.
|
||||
func (s Scanner) Keywords() []string {
|
||||
return []string{"lark", "larksuite", "t-", "a-", "u-"}
|
||||
}
|
||||
|
||||
// FromData will find and optionally verify Larksuite secrets in a given set of bytes.
|
||||
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
|
||||
dataStr := string(data)
|
||||
|
||||
for key, tokenPat := range tokenPats {
|
||||
uniqueMatches := make(map[string]struct{})
|
||||
for _, match := range tokenPat.FindAllStringSubmatch(dataStr, -1) {
|
||||
uniqueMatches[match[1]] = struct{}{}
|
||||
}
|
||||
|
||||
for token := range uniqueMatches {
|
||||
s1 := detectors.Result{
|
||||
DetectorType: detectorspb.DetectorType_LarkSuite,
|
||||
Raw: []byte(token),
|
||||
}
|
||||
s1.ExtraData = map[string]string{
|
||||
"token_type": string(key),
|
||||
}
|
||||
if verify {
|
||||
client := s.client
|
||||
if s.client == nil {
|
||||
client = defaultClient
|
||||
}
|
||||
|
||||
var (
|
||||
isVerified bool
|
||||
err error
|
||||
)
|
||||
|
||||
isVerified, err = verifyAccessToken(ctx, client, verificationUrls[key], token)
|
||||
s1.Verified = isVerified
|
||||
s1.SetVerificationError(err, token)
|
||||
}
|
||||
|
||||
results = append(results, s1)
|
||||
}
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (s Scanner) Type() detectorspb.DetectorType {
|
||||
return detectorspb.DetectorType_LarkSuite
|
||||
}
|
||||
|
||||
func verifyAccessToken(ctx context.Context, client *http.Client, url string, token string) (bool, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer func() {
|
||||
_, _ = io.Copy(io.Discard, res.Body)
|
||||
_ = res.Body.Close()
|
||||
}()
|
||||
switch res.StatusCode {
|
||||
case http.StatusOK, http.StatusBadRequest:
|
||||
var bodyResponse verificationResponse
|
||||
if err := json.NewDecoder(res.Body).Decode(&bodyResponse); err != nil {
|
||||
err = fmt.Errorf("failed to decode response: %w", err)
|
||||
return false, err
|
||||
} else {
|
||||
if bodyResponse.Code == 0 || bodyResponse.Code == 99991672 {
|
||||
return true, nil
|
||||
} else {
|
||||
return false, fmt.Errorf("unexpected verification response code %d, message %s", bodyResponse.Code, bodyResponse.Message)
|
||||
}
|
||||
}
|
||||
default:
|
||||
// 500 larksuite was unable to generate a result
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
type verificationResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
}
|
209
pkg/detectors/larksuite/larksuite_test.go
Normal file
209
pkg/detectors/larksuite/larksuite_test.go
Normal file
|
@ -0,0 +1,209 @@
|
|||
//go:build detectors
|
||||
// +build detectors
|
||||
|
||||
package larksuite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/kylelemons/godebug/pretty"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
|
||||
)
|
||||
|
||||
func TestLarksuite_Pattern(t *testing.T) {
|
||||
d := Scanner{}
|
||||
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "tenant token pattern",
|
||||
input: "larksuite_token = 't-KkBmh6TUBIcyFAp20XXa'",
|
||||
want: []string{"t-KkBmh6TUBIcyFAp20XXa"},
|
||||
},
|
||||
{
|
||||
name: "user token pattern",
|
||||
input: "larksuite_token = 'u-fM_lEWSNhfFqE.dZU6YZ28SRlnWR4hk59Pow05gg00DFA'",
|
||||
want: []string{"u-fM_lEWSNhfFqE.dZU6YZ28SRlnWR4hk59Pow05gg00DFA"},
|
||||
},
|
||||
{
|
||||
name: "app token pattern",
|
||||
input: "larksuite_token = 'a-KkBmh6TUBIcyFAp20XXa'",
|
||||
want: []string{"a-KkBmh6TUBIcyFAp20XXa"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
|
||||
if len(matchedDetectors) == 0 {
|
||||
t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
|
||||
return
|
||||
}
|
||||
|
||||
results, err := d.FromData(context.Background(), false, []byte(test.input))
|
||||
if err != nil {
|
||||
t.Errorf("error = %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(results) != len(test.want) {
|
||||
if len(results) == 0 {
|
||||
t.Errorf("did not receive result")
|
||||
} else {
|
||||
t.Errorf("expected %d results, only received %d", len(test.want), len(results))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
actual := make(map[string]struct{}, len(results))
|
||||
for _, r := range results {
|
||||
if len(r.RawV2) > 0 {
|
||||
actual[string(r.RawV2)] = struct{}{}
|
||||
} else {
|
||||
actual[string(r.Raw)] = struct{}{}
|
||||
}
|
||||
}
|
||||
expected := make(map[string]struct{}, len(test.want))
|
||||
for _, v := range test.want {
|
||||
expected[v] = struct{}{}
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(expected, actual); diff != "" {
|
||||
t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLarksuite_FromChunk(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")
|
||||
if err != nil {
|
||||
t.Fatalf("could not get test secrets from GCP: %s", err)
|
||||
}
|
||||
tenantToken := testSecrets.MustGetField("LARKSUITE_TENANT")
|
||||
userToken := testSecrets.MustGetField("LARKSUITE_USER")
|
||||
appToken := testSecrets.MustGetField("LARKSUITE_APP")
|
||||
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
data []byte
|
||||
verify bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
s Scanner
|
||||
args args
|
||||
want []detectors.Result
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "found tenant token, verified",
|
||||
s: Scanner{},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
data: []byte(fmt.Sprintf("You can find a larksuite token %s within", tenantToken)),
|
||||
verify: true,
|
||||
},
|
||||
want: []detectors.Result{
|
||||
{
|
||||
DetectorType: detectorspb.DetectorType_LarkSuite,
|
||||
Verified: true,
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "found user token, verified",
|
||||
s: Scanner{},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
data: []byte(fmt.Sprintf("You can find a larksuite token %s within", userToken)),
|
||||
verify: true,
|
||||
},
|
||||
want: []detectors.Result{
|
||||
{
|
||||
DetectorType: detectorspb.DetectorType_LarkSuite,
|
||||
Verified: true,
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "found app token, unverified",
|
||||
s: Scanner{},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
data: []byte(fmt.Sprintf("You can find a larksuite app %s within", appToken)),
|
||||
verify: true,
|
||||
},
|
||||
want: func() []detectors.Result {
|
||||
r := detectors.Result{
|
||||
DetectorType: detectorspb.DetectorType_LarkSuite,
|
||||
Verified: false,
|
||||
}
|
||||
r.SetVerificationError(fmt.Errorf("unexpected verification response code 99991668, message Invalid access token for authorization. Please make a request with token attached."))
|
||||
return []detectors.Result{r}
|
||||
}(),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "not found",
|
||||
s: Scanner{},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
data: []byte("You cannot find the secret within"),
|
||||
verify: true,
|
||||
},
|
||||
want: nil,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := Scanner{}
|
||||
got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Larksuite.FromData() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
for i := range got {
|
||||
if len(got[i].Raw) == 0 {
|
||||
t.Fatalf("no raw secret present: \n %+v", got[i])
|
||||
}
|
||||
got[i].Raw = nil
|
||||
got[i].ExtraData = nil
|
||||
}
|
||||
if diff := pretty.Compare(got, tt.want); diff != "" {
|
||||
t.Errorf("Larksuite.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkFromData(benchmark *testing.B) {
|
||||
ctx := context.Background()
|
||||
s := Scanner{}
|
||||
for name, data := range detectors.MustGetBenchmarkData() {
|
||||
benchmark.Run(name, func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
_, err := s.FromData(ctx, false, data)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
123
pkg/detectors/larksuiteapikey/larksuiteapikey.go
Normal file
123
pkg/detectors/larksuiteapikey/larksuiteapikey.go
Normal file
|
@ -0,0 +1,123 @@
|
|||
package larksuiteapikey
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
regexp "github.com/wasilibs/go-re2"
|
||||
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
|
||||
)
|
||||
|
||||
type Scanner struct {
|
||||
detectors.DefaultMultiPartCredentialProvider
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// Ensure the Scanner satisfies the interface at compile time.
|
||||
var _ detectors.Detector = (*Scanner)(nil)
|
||||
|
||||
var (
|
||||
defaultClient = common.SaneHttpClient()
|
||||
|
||||
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
|
||||
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"lark", "larksuite"}) + `\b(cli_[a-z0-9A-Z]{16})\b`)
|
||||
secretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"lark", "larksuite"}) + `\b([a-z0-9A-Z]{32})\b`)
|
||||
)
|
||||
|
||||
// Keywords are used for efficiently pre-filtering chunks.
|
||||
// Use identifiers in the secret preferably, or the provider name.
|
||||
func (s Scanner) Keywords() []string {
|
||||
return []string{"lark", "larksuite", "cli_"}
|
||||
}
|
||||
|
||||
// FromData will find and optionally verify larksuite secrets in a given set of bytes.
|
||||
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
|
||||
dataStr := string(data)
|
||||
|
||||
// find for app id + secrets
|
||||
idMatches := make(map[string]struct{})
|
||||
for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) {
|
||||
idMatches[match[1]] = struct{}{}
|
||||
}
|
||||
secretMatches := make(map[string]struct{})
|
||||
for _, match := range secretPat.FindAllStringSubmatch(dataStr, -1) {
|
||||
secretMatches[match[1]] = struct{}{}
|
||||
}
|
||||
|
||||
for appId := range idMatches {
|
||||
for appSecret := range secretMatches {
|
||||
resMatch := strings.TrimSpace(appId)
|
||||
resSecretMatch := strings.TrimSpace(appSecret)
|
||||
|
||||
s1 := detectors.Result{
|
||||
DetectorType: detectorspb.DetectorType_LarkSuiteApiKey,
|
||||
Raw: []byte(resMatch),
|
||||
RawV2: []byte(resMatch + resSecretMatch),
|
||||
}
|
||||
|
||||
if verify {
|
||||
client := s.client
|
||||
if client == nil {
|
||||
client = defaultClient
|
||||
}
|
||||
isVerified, verificationErr := verifyCredentials(ctx, client, resMatch, resSecretMatch)
|
||||
s1.Verified = isVerified
|
||||
s1.SetVerificationError(verificationErr, resMatch)
|
||||
}
|
||||
|
||||
results = append(results, s1)
|
||||
}
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (s Scanner) Type() detectorspb.DetectorType {
|
||||
return detectorspb.DetectorType_LarkSuiteApiKey
|
||||
}
|
||||
|
||||
func verifyCredentials(ctx context.Context, client *http.Client, appId, appSecret string) (bool, error) {
|
||||
payload := strings.NewReader(fmt.Sprintf(`{"app_id": "%s", "app_secret": "%s"}`, appId, appSecret))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://open.larksuite.com/open-apis/auth/v3/tenant_access_token/internal", payload)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer func() {
|
||||
_, _ = io.Copy(io.Discard, res.Body)
|
||||
_ = res.Body.Close()
|
||||
}()
|
||||
switch res.StatusCode {
|
||||
case http.StatusOK:
|
||||
var bodyResponse verificationResponse
|
||||
if err := json.NewDecoder(res.Body).Decode(&bodyResponse); err != nil {
|
||||
err = fmt.Errorf("failed to decode response: %w", err)
|
||||
return false, err
|
||||
} else {
|
||||
if bodyResponse.Code == 0 {
|
||||
return true, nil
|
||||
} else {
|
||||
return false, fmt.Errorf("Verification failed code %d, message %s", bodyResponse.Code, bodyResponse.Message)
|
||||
}
|
||||
}
|
||||
default:
|
||||
// 500 larksuite was unable to generate a result
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
type verificationResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
}
|
189
pkg/detectors/larksuiteapikey/larksuiteapikey_test.go
Normal file
189
pkg/detectors/larksuiteapikey/larksuiteapikey_test.go
Normal file
|
@ -0,0 +1,189 @@
|
|||
//go:build detectors
|
||||
// +build detectors
|
||||
|
||||
package larksuiteapikey
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/kylelemons/godebug/pretty"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
|
||||
)
|
||||
|
||||
func TestLarksuiteApiKey_Pattern(t *testing.T) {
|
||||
d := Scanner{}
|
||||
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "Secrets Pattern",
|
||||
input: `{
|
||||
"lark_app_id": "cli_1234567890123456",
|
||||
"lark_app_secret": "2nuq0H1dZUqRpHMzKMvbbwlgNWaJiokl",
|
||||
}`,
|
||||
want: []string{"cli_12345678901234562nuq0H1dZUqRpHMzKMvbbwlgNWaJiokl"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
|
||||
if len(matchedDetectors) == 0 {
|
||||
t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
|
||||
return
|
||||
}
|
||||
|
||||
results, err := d.FromData(context.Background(), false, []byte(test.input))
|
||||
if err != nil {
|
||||
t.Errorf("error = %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(results) != len(test.want) {
|
||||
if len(results) == 0 {
|
||||
t.Errorf("did not receive result")
|
||||
} else {
|
||||
t.Errorf("expected %d results, only received %d", len(test.want), len(results))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
actual := make(map[string]struct{}, len(results))
|
||||
for _, r := range results {
|
||||
if len(r.RawV2) > 0 {
|
||||
actual[string(r.RawV2)] = struct{}{}
|
||||
} else {
|
||||
actual[string(r.Raw)] = struct{}{}
|
||||
}
|
||||
}
|
||||
expected := make(map[string]struct{}, len(test.want))
|
||||
for _, v := range test.want {
|
||||
expected[v] = struct{}{}
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(expected, actual); diff != "" {
|
||||
t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLarksuiteApiKey_FromChunk(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")
|
||||
if err != nil {
|
||||
t.Fatalf("could not get test secrets from GCP: %s", err)
|
||||
}
|
||||
id := testSecrets.MustGetField("LARKSUITE_APP_ID")
|
||||
secret := testSecrets.MustGetField("LARKSUITE_APP_SECRET")
|
||||
inactiveSecret := testSecrets.MustGetField("LARKSUITE_APP_SECRET_INACTIVE")
|
||||
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
data []byte
|
||||
verify bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
s Scanner
|
||||
args args
|
||||
want []detectors.Result
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "found verified",
|
||||
s: Scanner{},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
data: []byte(fmt.Sprintf("You can find a larksuite appid %s and secret %s within", id, secret)),
|
||||
verify: true,
|
||||
},
|
||||
want: []detectors.Result{
|
||||
{
|
||||
DetectorType: detectorspb.DetectorType_LarkSuiteApiKey,
|
||||
Verified: true,
|
||||
Raw: []byte(id),
|
||||
RawV2: []byte(id + secret),
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "found unverified",
|
||||
s: Scanner{},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
data: []byte(fmt.Sprintf("You can find a larksuite appid %s and larksuite secret %s within", id, inactiveSecret)),
|
||||
verify: true,
|
||||
},
|
||||
want: func() []detectors.Result {
|
||||
r := detectors.Result{
|
||||
DetectorType: detectorspb.DetectorType_LarkSuiteApiKey,
|
||||
Verified: false,
|
||||
Raw: []byte(id),
|
||||
RawV2: []byte(id + inactiveSecret),
|
||||
}
|
||||
r.SetVerificationError(fmt.Errorf("Verification failed code 10014, message app secret invalid"))
|
||||
return []detectors.Result{r}
|
||||
}(),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "not found",
|
||||
s: Scanner{},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
data: []byte("You cannot find the secret within"),
|
||||
verify: true,
|
||||
},
|
||||
want: nil,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := Scanner{}
|
||||
got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Larksuite.FromData() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
for i := range got {
|
||||
if len(got[i].Raw) == 0 {
|
||||
t.Fatalf("no raw secret present: \n %+v", got[i])
|
||||
}
|
||||
got[i].ExtraData = nil
|
||||
}
|
||||
if diff := pretty.Compare(got, tt.want); diff != "" {
|
||||
t.Errorf("Larksuite.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkFromData(benchmark *testing.B) {
|
||||
ctx := context.Background()
|
||||
s := Scanner{}
|
||||
for name, data := range detectors.MustGetBenchmarkData() {
|
||||
benchmark.Run(name, func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
_, err := s.FromData(ctx, false, data)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -377,6 +377,8 @@ import (
|
|||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/kucoin"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/kylas"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/languagelayer"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/larksuite"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/larksuiteapikey"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/launchdarkly"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/ldap"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/leadfeeder"
|
||||
|
@ -1610,6 +1612,8 @@ func DefaultDetectors() []detectors.Detector {
|
|||
groq.Scanner{},
|
||||
twitterconsumerkey.Scanner{},
|
||||
eraser.Scanner{},
|
||||
larksuite.Scanner{},
|
||||
larksuiteapikey.Scanner{},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1090,6 +1090,8 @@ const (
|
|||
DetectorType_Groq DetectorType = 988
|
||||
DetectorType_TwitterConsumerkey DetectorType = 989
|
||||
DetectorType_Eraser DetectorType = 990
|
||||
DetectorType_LarkSuite DetectorType = 991
|
||||
DetectorType_LarkSuiteApiKey DetectorType = 992
|
||||
)
|
||||
|
||||
// Enum value maps for DetectorType.
|
||||
|
@ -2082,6 +2084,8 @@ var (
|
|||
988: "Groq",
|
||||
989: "TwitterConsumerkey",
|
||||
990: "Eraser",
|
||||
991: "LarkSuite",
|
||||
992: "LarkSuiteApiKey",
|
||||
}
|
||||
DetectorType_value = map[string]int32{
|
||||
"Alibaba": 0,
|
||||
|
@ -3071,6 +3075,8 @@ var (
|
|||
"Groq": 988,
|
||||
"TwitterConsumerkey": 989,
|
||||
"Eraser": 990,
|
||||
"LarkSuite": 991,
|
||||
"LarkSuiteApiKey": 992,
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -3524,7 +3530,7 @@ var file_detectors_proto_rawDesc = []byte{
|
|||
0x4c, 0x41, 0x49, 0x4e, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x42, 0x41, 0x53, 0x45, 0x36, 0x34,
|
||||
0x10, 0x02, 0x12, 0x09, 0x0a, 0x05, 0x55, 0x54, 0x46, 0x31, 0x36, 0x10, 0x03, 0x12, 0x13, 0x0a,
|
||||
0x0f, 0x45, 0x53, 0x43, 0x41, 0x50, 0x45, 0x44, 0x5f, 0x55, 0x4e, 0x49, 0x43, 0x4f, 0x44, 0x45,
|
||||
0x10, 0x04, 0x2a, 0xba, 0x7e, 0x0a, 0x0c, 0x44, 0x65, 0x74, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x54,
|
||||
0x10, 0x04, 0x2a, 0xe0, 0x7e, 0x0a, 0x0c, 0x44, 0x65, 0x74, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x54,
|
||||
0x79, 0x70, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x41, 0x6c, 0x69, 0x62, 0x61, 0x62, 0x61, 0x10, 0x00,
|
||||
0x12, 0x08, 0x0a, 0x04, 0x41, 0x4d, 0x51, 0x50, 0x10, 0x01, 0x12, 0x07, 0x0a, 0x03, 0x41, 0x57,
|
||||
0x53, 0x10, 0x02, 0x12, 0x09, 0x0a, 0x05, 0x41, 0x7a, 0x75, 0x72, 0x65, 0x10, 0x03, 0x12, 0x0a,
|
||||
|
@ -4535,12 +4541,14 @@ var file_detectors_proto_rawDesc = []byte{
|
|||
0x12, 0x0c, 0x0a, 0x07, 0x49, 0x6e, 0x74, 0x72, 0x61, 0x34, 0x32, 0x10, 0xdb, 0x07, 0x12, 0x09,
|
||||
0x0a, 0x04, 0x47, 0x72, 0x6f, 0x71, 0x10, 0xdc, 0x07, 0x12, 0x17, 0x0a, 0x12, 0x54, 0x77, 0x69,
|
||||
0x74, 0x74, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x73, 0x75, 0x6d, 0x65, 0x72, 0x6b, 0x65, 0x79, 0x10,
|
||||
0xdd, 0x07, 0x12, 0x0b, 0x0a, 0x06, 0x45, 0x72, 0x61, 0x73, 0x65, 0x72, 0x10, 0xde, 0x07, 0x42,
|
||||
0x3d, 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x72,
|
||||
0x75, 0x66, 0x66, 0x6c, 0x65, 0x73, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2f, 0x74, 0x72,
|
||||
0x75, 0x66, 0x66, 0x6c, 0x65, 0x68, 0x6f, 0x67, 0x2f, 0x76, 0x33, 0x2f, 0x70, 0x6b, 0x67, 0x2f,
|
||||
0x70, 0x62, 0x2f, 0x64, 0x65, 0x74, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x73, 0x70, 0x62, 0x62, 0x06,
|
||||
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0xdd, 0x07, 0x12, 0x0b, 0x0a, 0x06, 0x45, 0x72, 0x61, 0x73, 0x65, 0x72, 0x10, 0xde, 0x07, 0x12,
|
||||
0x0e, 0x0a, 0x09, 0x4c, 0x61, 0x72, 0x6b, 0x53, 0x75, 0x69, 0x74, 0x65, 0x10, 0xdf, 0x07, 0x12,
|
||||
0x14, 0x0a, 0x0f, 0x4c, 0x61, 0x72, 0x6b, 0x53, 0x75, 0x69, 0x74, 0x65, 0x41, 0x70, 0x69, 0x4b,
|
||||
0x65, 0x79, 0x10, 0xe0, 0x07, 0x42, 0x3d, 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e,
|
||||
0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x72, 0x75, 0x66, 0x66, 0x6c, 0x65, 0x73, 0x65, 0x63, 0x75, 0x72,
|
||||
0x69, 0x74, 0x79, 0x2f, 0x74, 0x72, 0x75, 0x66, 0x66, 0x6c, 0x65, 0x68, 0x6f, 0x67, 0x2f, 0x76,
|
||||
0x33, 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x62, 0x2f, 0x64, 0x65, 0x74, 0x65, 0x63, 0x74, 0x6f,
|
||||
0x72, 0x73, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
|
@ -1000,6 +1000,8 @@ enum DetectorType {
|
|||
Groq = 988;
|
||||
TwitterConsumerkey = 989;
|
||||
Eraser = 990;
|
||||
LarkSuite = 991;
|
||||
LarkSuiteApiKey = 992;
|
||||
}
|
||||
|
||||
message Result {
|
||||
|
|
Loading…
Reference in a new issue