diff --git a/main.go b/main.go index 1f8e26d18..468a61448 100644 --- a/main.go +++ b/main.go @@ -554,6 +554,7 @@ func run(state overseer.State, logSync func() error) { feature.OpenRouterDetectorEnabled.Store(true) feature.NewRelicInsightsInsertKeyDetectorEnabled.Store(true) feature.DuffelTokenDetectorEnabled.Store(true) + feature.ShippoDetectorEnabled.Store(true) conf := &config.Config{} if *configFilename != "" { diff --git a/pkg/detectors/shippo/shippo.go b/pkg/detectors/shippo/shippo.go new file mode 100644 index 000000000..b0d7ff312 --- /dev/null +++ b/pkg/detectors/shippo/shippo.go @@ -0,0 +1,132 @@ +package shippo + +import ( + "context" + "fmt" + "io" + "net/http" + + 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/detector_typepb" +) + +type Scanner struct { + client *http.Client +} + +// Compile-time interface check +var _ detectors.Detector = (*Scanner)(nil) + +var ( + defaultClient = common.SaneHttpClient() + + // Shippo live tokens: + // Format: shippo_live_ or shippo_test_ + 40 hex characters + shippoLiveTokenPat = regexp.MustCompile( + `\b(shippo_(live|test)_[a-f0-9]{40})\b`, + ) +) + +// Keywords used for fast pre-filtering +func (s Scanner) Keywords() []string { + return []string{"shippo_live_", "shippo_test_"} +} + +func (s Scanner) getClient() *http.Client { + if s.client != nil { + return s.client + } + return defaultClient +} + +// FromData scans for Shippo live API tokens and optionally verifies them +func (s Scanner) FromData( + ctx context.Context, + verify bool, + data []byte, +) (results []detectors.Result, err error) { + + dataStr := string(data) + + uniqueTokens := make(map[string]struct{}) + for _, match := range shippoLiveTokenPat.FindAllStringSubmatch(dataStr, -1) { + uniqueTokens[match[1]] = struct{}{} + } + + for token := range uniqueTokens { + result := detectors.Result{ + DetectorType: detector_typepb.DetectorType_Shippo, + Raw: []byte(token), + Redacted: token[:12] + "...", + SecretParts: map[string]string{ + "key": token, + }, + } + + if verify { + verified, verificationErr := verifyShippoToken( + ctx, + s.getClient(), + token, + ) + result.SetVerificationError(verificationErr, token) + result.Verified = verified + } + + results = append(results, result) + } + + return +} + +func verifyShippoToken( + ctx context.Context, + client *http.Client, + token string, +) (bool, error) { + + req, err := http.NewRequestWithContext( + ctx, + http.MethodGet, + "https://api.goshippo.com/shippo-accounts?page=1&results=1", + http.NoBody, + ) + if err != nil { + return false, err + } + + req.Header.Set("Authorization", "ShippoToken "+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: + return true, nil + case http.StatusUnauthorized, http.StatusForbidden: + // Token invalid or revoked + return false, nil + default: + return false, fmt.Errorf( + "unexpected HTTP response status %d", + res.StatusCode, + ) + } +} + +func (s Scanner) Type() detector_typepb.DetectorType { + return detector_typepb.DetectorType_Shippo +} + +func (s Scanner) Description() string { + return "Shippo is a shipping API platform. Shippo live API tokens can be used to authenticate API requests and manage shipping operations." +} diff --git a/pkg/detectors/shippo/shippo_integration_test.go b/pkg/detectors/shippo/shippo_integration_test.go new file mode 100644 index 000000000..3d100e398 --- /dev/null +++ b/pkg/detectors/shippo/shippo_integration_test.go @@ -0,0 +1,182 @@ +//go:build detectors +// +build detectors + +package shippo + +import ( + "context" + "fmt" + "testing" + "time" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + + "github.com/trufflesecurity/trufflehog/v3/pkg/common" + "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" + "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb" +) + +func TestShippo_FromData(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) + defer cancel() + + // Secrets stored in GCP test project + testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors6") + if err != nil { + t.Fatalf("could not get test secrets from GCP: %s", err) + } + + activeToken := testSecrets.MustGetField("SHIPPO_TEST_TOKEN") + inactiveToken := "shippo_live_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + + type args struct { + ctx context.Context + data []byte + verify bool + } + + tests := []struct { + name string + s Scanner + args args + want []detectors.Result + wantErr bool + wantVerificationErr bool + }{ + { + name: "found, verified", + s: Scanner{}, + args: args{ + ctx: context.Background(), + data: fmt.Appendf([]byte{}, "Using Shippo API token %s for shipment", activeToken), + verify: true, + }, + want: []detectors.Result{ + { + DetectorType: detector_typepb.DetectorType_Shippo, + Verified: true, + Raw: []byte(activeToken), + Redacted: activeToken[:12] + "...", + }, + }, + }, + { + name: "found, real token, verification error due to timeout", + s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)}, + args: args{ + ctx: context.Background(), + data: fmt.Appendf([]byte{}, "Using Shippo API token %s for shipment", activeToken), + verify: true, + }, + want: []detectors.Result{ + { + DetectorType: detector_typepb.DetectorType_Shippo, + Verified: false, + Raw: []byte(activeToken), + Redacted: activeToken[:12] + "...", + }, + }, + wantVerificationErr: true, + }, + { + name: "found, real token, verification error due to unexpected api surface", + s: Scanner{client: common.ConstantResponseHttpClient(500, "{}")}, + args: args{ + ctx: context.Background(), + data: fmt.Appendf([]byte{}, "Using Shippo API token %s for shipment", activeToken), + verify: true, + }, + want: []detectors.Result{ + { + DetectorType: detector_typepb.DetectorType_Shippo, + Verified: false, + Raw: []byte(activeToken), + Redacted: activeToken[:12] + "...", + }, + }, + wantVerificationErr: true, + }, + { + name: "found, unverified (inactive token)", + s: Scanner{}, + args: args{ + ctx: context.Background(), + data: fmt.Appendf([]byte{}, "Using Shippo API token %s for shipment", inactiveToken), + verify: true, + }, + want: []detectors.Result{ + { + DetectorType: detector_typepb.DetectorType_Shippo, + Verified: false, + Raw: []byte(inactiveToken), + Redacted: inactiveToken[:12] + "...", + }, + }, + }, + { + name: "not found", + s: Scanner{}, + args: args{ + ctx: context.Background(), + data: []byte("no secrets here"), + verify: true, + }, + want: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) + if (err != nil) != tt.wantErr { + t.Fatalf("Shippo.FromData() error = %v, wantErr %v", err, tt.wantErr) + } + + for i := range got { + if len(got[i].Raw) == 0 { + t.Fatal("no raw secret present") + } + if (got[i].VerificationError() != nil) != tt.wantVerificationErr { + t.Fatalf( + "wantVerificationError = %v, verification error = %v", + tt.wantVerificationErr, + got[i].VerificationError(), + ) + } + } + + ignoreOpts := cmpopts.IgnoreFields( + detectors.Result{}, + "ExtraData", + "verificationError", + "primarySecret", + "SecretParts", + "Redacted", + "chunkOffset", + "chunkOffsetSet", + ) + + if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" { + t.Errorf("Shippo.FromData() %s diff: (-got +want)\n%s", tt.name, diff) + } + }) + } +} + +func BenchmarkShippo_FromData(b *testing.B) { + ctx := context.Background() + s := Scanner{} + + for name, data := range detectors.MustGetBenchmarkData() { + b.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) + } + } + }) + } +} diff --git a/pkg/detectors/shippo/shippo_test.go b/pkg/detectors/shippo/shippo_test.go new file mode 100644 index 000000000..47e48a859 --- /dev/null +++ b/pkg/detectors/shippo/shippo_test.go @@ -0,0 +1,240 @@ +package shippo + +import ( + "context" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/require" + + "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" + "github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick" +) + +func TestShippo_Pattern(t *testing.T) { + d := Scanner{} + ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d}) + + tests := []struct { + name string + input string + want []string + }{ + { + name: "valid pattern - basic", + input: ` + [INFO] Starting shipment service + [DEBUG] token=shippo_live_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + [INFO] Ready + `, + want: []string{ + "shippo_live_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + }, + }, + { + name: "valid pattern - with keyword nearby", + input: ` + [DEBUG] SHIPPO_API_KEY=shippo_live_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + `, + want: []string{ + "shippo_live_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + }, + }, + { + name: "valid pattern - multiple tokens", + input: ` + shippo_live_1111111111111111111111111111111111111111 + shippo_live_2222222222222222222222222222222222222222 + `, + want: []string{ + "shippo_live_1111111111111111111111111111111111111111", + "shippo_live_2222222222222222222222222222222222222222", + }, + }, + { + name: "invalid pattern - uppercase characters", + input: ` + shippo_live_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + `, + want: nil, + }, + { + name: "invalid pattern - too short", + input: ` + shippo_live_1234 + `, + want: nil, + }, + { + name: "invalid pattern - invalid token length", + input: ` + shippo_live_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + `, + want: nil, + }, + { + name: "invalid pattern - keyword only", + input: ` + [INFO] initializing shippo service shippo_live_ + `, + want: nil, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input)) + if len(matchedDetectors) == 0 { + t.Errorf( + "test %q failed: expected keywords %v to be found in the input", + test.name, + d.Keywords(), + ) + return + } + + results, err := d.FromData(context.Background(), false, []byte(test.input)) + require.NoError(t, err) + + if len(results) != len(test.want) { + t.Errorf( + "mismatch in result count: expected %d, got %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 TestShippo_TestKey_Pattern(t *testing.T) { + d := Scanner{} + ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d}) + + tests := []struct { + name string + input string + want []string + }{ + { + name: "valid test key - basic", + input: ` + shippo_test_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + `, + want: []string{ + "shippo_test_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + }, + }, + { + name: "valid test key - with keyword nearby", + input: ` + SHIPPO_API_KEY=shippo_test_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + `, + want: []string{ + "shippo_test_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + }, + }, + { + name: "valid test key - multiple tokens", + input: ` + shippo_test_1111111111111111111111111111111111111111 + shippo_test_2222222222222222222222222222222222222222 + `, + want: []string{ + "shippo_test_1111111111111111111111111111111111111111", + "shippo_test_2222222222222222222222222222222222222222", + }, + }, + { + name: "invalid test key - uppercase characters", + input: ` + shippo_test_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + `, + want: nil, + }, + { + name: "invalid test key - too short", + input: ` + shippo_test_1234 + `, + want: nil, + }, + { + name: "invalid test key - invalid token length", + input: ` + shippo_test_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + `, + want: nil, + }, + { + name: "invalid test key - keyword only", + input: ` + shippo_test_ + `, + want: nil, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input)) + if len(matchedDetectors) == 0 { + t.Errorf( + "test %q failed: expected keywords %v to be found in the input", + test.name, + d.Keywords(), + ) + return + } + + results, err := d.FromData(context.Background(), false, []byte(test.input)) + require.NoError(t, err) + + if len(results) != len(test.want) { + t.Errorf( + "mismatch in result count: expected %d, got %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) + } + }) + } +} diff --git a/pkg/engine/defaults/defaults.go b/pkg/engine/defaults/defaults.go index dd0e55ccd..b2c1451b3 100644 --- a/pkg/engine/defaults/defaults.go +++ b/pkg/engine/defaults/defaults.go @@ -686,6 +686,7 @@ import ( "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/sheety" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/sherpadesk" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/shipday" + "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/shippo" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/shodankey" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/shopify" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/shopifyoauth" @@ -1593,6 +1594,7 @@ func buildDetectorList() []detectors.Detector { &sheety.Scanner{}, &sherpadesk.Scanner{}, &shipday.Scanner{}, + &shippo.Scanner{}, &shodankey.Scanner{}, &shopify.Scanner{}, &shopifyoauth.Scanner{}, @@ -1828,6 +1830,8 @@ func buildDetectorList() []detectors.Detector { return !feature.NewRelicInsightsInsertKeyDetectorEnabled.Load() case *duffeltoken.Scanner: return !feature.DuffelTokenDetectorEnabled.Load() + case *shippo.Scanner: + return !feature.ShippoDetectorEnabled.Load() default: return false } diff --git a/pkg/engine/defaults/defaults_test.go b/pkg/engine/defaults/defaults_test.go index c09a04976..afea14e62 100644 --- a/pkg/engine/defaults/defaults_test.go +++ b/pkg/engine/defaults/defaults_test.go @@ -141,6 +141,7 @@ var excludedFromDefaultList = map[detector_typepb.DetectorType]struct{}{ detector_typepb.DetectorType_OpenRouter: {}, detector_typepb.DetectorType_NewRelicInsightsInsertKey: {}, detector_typepb.DetectorType_DuffelToken: {}, + detector_typepb.DetectorType_Shippo: {}, // Reserved / special types. detector_typepb.DetectorType_CustomRegex: {}, // added dynamically via engine config, not via buildDetectorList() diff --git a/pkg/feature/feature.go b/pkg/feature/feature.go index 47689bd42..2f83afeb0 100644 --- a/pkg/feature/feature.go +++ b/pkg/feature/feature.go @@ -33,6 +33,7 @@ var ( OpenRouterDetectorEnabled atomic.Bool NewRelicInsightsInsertKeyDetectorEnabled atomic.Bool DuffelTokenDetectorEnabled atomic.Bool + ShippoDetectorEnabled atomic.Bool ) type AtomicString struct { diff --git a/pkg/pb/detector_typepb/detector_type.pb.go b/pkg/pb/detector_typepb/detector_type.pb.go index 15d8e831e..e4fe7dac2 100644 --- a/pkg/pb/detector_typepb/detector_type.pb.go +++ b/pkg/pb/detector_typepb/detector_type.pb.go @@ -1113,6 +1113,7 @@ const ( DetectorType_OpenRouter DetectorType = 1057 DetectorType_NewRelicInsightsInsertKey DetectorType = 1058 DetectorType_DuffelToken DetectorType = 1059 + DetectorType_Shippo DetectorType = 1060 ) // Enum value maps for DetectorType. @@ -2174,6 +2175,7 @@ var ( 1057: "OpenRouter", 1058: "NewRelicInsightsInsertKey", 1059: "DuffelToken", + 1060: "Shippo", } DetectorType_value = map[string]int32{ "Alibaba": 0, @@ -3232,6 +3234,7 @@ var ( "OpenRouter": 1057, "NewRelicInsightsInsertKey": 1058, "DuffelToken": 1059, + "Shippo": 1060, } ) @@ -3267,7 +3270,7 @@ var File_detector_type_proto protoreflect.FileDescriptor var file_detector_type_proto_rawDesc = []byte{ 0x0a, 0x13, 0x64, 0x65, 0x74, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0d, 0x64, 0x65, 0x74, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x5f, - 0x74, 0x79, 0x70, 0x65, 0x2a, 0xf9, 0x89, 0x01, 0x0a, 0x0c, 0x44, 0x65, 0x74, 0x65, 0x63, 0x74, + 0x74, 0x79, 0x70, 0x65, 0x2a, 0x86, 0x8a, 0x01, 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, @@ -4371,11 +4374,12 @@ var file_detector_type_proto_rawDesc = []byte{ 0x1e, 0x0a, 0x19, 0x4e, 0x65, 0x77, 0x52, 0x65, 0x6c, 0x69, 0x63, 0x49, 0x6e, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x49, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x4b, 0x65, 0x79, 0x10, 0xa2, 0x08, 0x12, 0x10, 0x0a, 0x0b, 0x44, 0x75, 0x66, 0x66, 0x65, 0x6c, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x10, 0xa3, - 0x08, 0x42, 0x41, 0x5a, 0x3f, 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, 0x5f, 0x74, 0x79, - 0x70, 0x65, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x08, 0x12, 0x0b, 0x0a, 0x06, 0x53, 0x68, 0x69, 0x70, 0x70, 0x6f, 0x10, 0xa4, 0x08, 0x42, 0x41, + 0x5a, 0x3f, 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, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x70, + 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/proto/detector_type.proto b/proto/detector_type.proto index 75cf4225e..56ba654b9 100644 --- a/proto/detector_type.proto +++ b/proto/detector_type.proto @@ -1061,4 +1061,5 @@ enum DetectorType { OpenRouter = 1057; NewRelicInsightsInsertKey = 1058; DuffelToken = 1059; + Shippo = 1060; }