Skip to content

Commit

Permalink
feat: validation & verification fix for apiscience to apimetrics (#3475)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahil9001 authored Oct 22, 2024
1 parent 3f9ba20 commit d78239c
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 414 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package apiscience
package apimetrics

import (
"context"
"fmt"
regexp "github.com/wasilibs/go-re2"
"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"
Expand All @@ -21,16 +22,16 @@ var (
client = 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{"apiscience"}) + `\b([a-bA-Z0-9\S]{22})\b`)
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"apimetrics"}) + `\b([a-bA-Z0-9\S]{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{"apiscience"}
return []string{"apimetrics"}
}

// FromData will find and optionally verify ApiScience secrets in a given set of bytes.
// FromData will find and optionally verify ApiMetrics 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)

Expand All @@ -43,12 +44,12 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
resMatch := strings.TrimSpace(match[1])

s1 := detectors.Result{
DetectorType: detectorspb.DetectorType_ApiScience,
DetectorType: detectorspb.DetectorType_ApiMetrics,
Raw: []byte(resMatch),
}

if verify {
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.apiscience.com/v1/monitors", nil)
req, err := http.NewRequestWithContext(ctx, "GET", "https://client.apimetrics.io/api/2/calls/", nil)
if err != nil {
continue
}
Expand All @@ -69,9 +70,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
}

func (s Scanner) Type() detectorspb.DetectorType {
return detectorspb.DetectorType_ApiScience
return detectorspb.DetectorType_ApiMetrics
}

func (s Scanner) Description() string {
return "ApiScience is a tool for monitoring the performance of APIs. ApiScience keys can be used to access and manage API monitors."
return "ApiMetrics is a tool for monitoring the performance of APIs. ApiMetrics keys can be used to access and manage API monitors."
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build detectors
// +build detectors

package apiscience
package apimetrics

import (
"context"
Expand All @@ -16,15 +16,15 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)

func TestApiScience_FromChunk(t *testing.T) {
func TestApiMetrics_FromChunk(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors2")
if err != nil {
t.Fatalf("could not get test secrets from GCP: %s", err)
}
secret := testSecrets.MustGetField("APISCIENCE")
inactiveSecret := testSecrets.MustGetField("APISCIENCE_INACTIVE")
secret := testSecrets.MustGetField("APIMETRICS")
inactiveSecret := testSecrets.MustGetField("APIMETRICS_INACTIVE")

type args struct {
ctx context.Context
Expand All @@ -43,12 +43,12 @@ func TestApiScience_FromChunk(t *testing.T) {
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a apiscience secret %s within", secret)),
data: []byte(fmt.Sprintf("You can find a apimetrics secret %s within", secret)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_ApiScience,
DetectorType: detectorspb.DetectorType_ApiMetrics,
Verified: true,
},
},
Expand All @@ -59,12 +59,12 @@ func TestApiScience_FromChunk(t *testing.T) {
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a apiscience secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation
data: []byte(fmt.Sprintf("You can find a apimetrics secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_ApiScience,
DetectorType: detectorspb.DetectorType_ApiMetrics,
Verified: false,
},
},
Expand All @@ -87,7 +87,7 @@ func TestApiScience_FromChunk(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("ApiScience.FromData() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("ApiMetrics.FromData() error = %v, wantErr %v", err, tt.wantErr)
return
}
for i := range got {
Expand All @@ -97,7 +97,7 @@ func TestApiScience_FromChunk(t *testing.T) {
got[i].Raw = nil
}
if diff := pretty.Compare(got, tt.want); diff != "" {
t.Errorf("ApiScience.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
t.Errorf("ApiMetrics.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/apify"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/apilayer"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/apimatic"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/apiscience"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/apimetrics"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/apitemplate"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/appcues"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/appfollow"
Expand Down Expand Up @@ -1368,7 +1368,6 @@ func DefaultDetectors() []detectors.Detector {
livestorm.Scanner{},
// manifest.Scanner{},
formbucket.Scanner{},
apiscience.Scanner{},
dronahq.Scanner{},
webscraper.Scanner{},
versioneye.Scanner{},
Expand Down Expand Up @@ -1642,6 +1641,7 @@ func DefaultDetectors() []detectors.Detector {
meraki.Scanner{},
saladcloudapikey.Scanner{},
boxoauth.Scanner{},
apimetrics.Scanner{},
}

// Automatically initialize all detectors that implement
Expand Down
Loading

0 comments on commit d78239c

Please sign in to comment.