Stripe Payment Intent Detector (#4138)
* stripe payment intent detector added * updated the stripe payment intent detector to verify the payment intents * tests updated for stripe payment intent * simplified code in the stripe payment intent detector * fetch secrets from gcp * updated stripe api return values for 400 and above cases * fixed integration test * updated the stripe payment intent detector keywords * updated the stripe payment intent detector to store the client secrets against all keys * simplified code * improved the client secret verification logic
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
package stripepaymentintent
|
||||
|
||||
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 {
|
||||
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.
|
||||
clientSecretPat = regexp.MustCompile(`\b(pi_[a-zA-Z0-9]{24}_secret_[a-zA-Z0-9]{25})\b`)
|
||||
secretKeyPat = regexp.MustCompile(`\b([rs]k_live_[a-zA-Z0-9]{20,247})\b`)
|
||||
publishableKeyPat = regexp.MustCompile(`\b(pk_live_[a-zA-Z0-9]{20,247})\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{"_secret_"}
|
||||
}
|
||||
|
||||
func (s Scanner) getClient() *http.Client {
|
||||
if s.Client != nil {
|
||||
return s.Client
|
||||
}
|
||||
return defaultClient
|
||||
}
|
||||
|
||||
// FromData will find and optionally verify Stripe Payment Intent secrets in a given set of bytes.
|
||||
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) ([]detectors.Result, error) {
|
||||
dataStr := string(data)
|
||||
|
||||
// Stripe client secrets can't be verified on their own, they must be paired with a secret or publishable key.
|
||||
// Secret keys are preferred for verification, but in some real-world cases only publishable keys are present.
|
||||
// While typically used client-side, publishable keys can still confirm certain PaymentIntents.
|
||||
// To avoid missing valid detections, we verify using both key types.
|
||||
// If no keys are found, we skip detection since client secrets alone are not actionable.
|
||||
clientSecrets := extractMatches(clientSecretPat, dataStr)
|
||||
secretKeys := extractMatches(secretKeyPat, dataStr)
|
||||
publishableKeys := extractMatches(publishableKeyPat, dataStr)
|
||||
|
||||
results := make([]detectors.Result, 0, len(clientSecrets)*(len(secretKeys)+len(publishableKeys)))
|
||||
|
||||
// Process each client secret against all keys
|
||||
for clientSecret := range clientSecrets {
|
||||
for key := range secretKeys {
|
||||
result := detectors.Result{
|
||||
DetectorType: detectorspb.DetectorType_StripePaymentIntent,
|
||||
Raw: []byte(clientSecret),
|
||||
RawV2: []byte(clientSecret + key),
|
||||
ExtraData: map[string]string{
|
||||
"key_type": "secret",
|
||||
},
|
||||
}
|
||||
|
||||
if verify {
|
||||
verified, err := verifyPaymentIntentWithSecretKey(ctx, s.getClient(), clientSecret, key)
|
||||
result.Verified = verified
|
||||
result.SetVerificationError(err)
|
||||
}
|
||||
|
||||
results = append(results, result)
|
||||
}
|
||||
|
||||
for key := range publishableKeys {
|
||||
result := detectors.Result{
|
||||
DetectorType: detectorspb.DetectorType_StripePaymentIntent,
|
||||
Raw: []byte(clientSecret),
|
||||
RawV2: []byte(clientSecret + key),
|
||||
ExtraData: map[string]string{
|
||||
"key_type": "publishable",
|
||||
},
|
||||
}
|
||||
|
||||
if verify {
|
||||
verified, err := verifyPaymentIntentWithPublishableKey(ctx, s.getClient(), clientSecret, key)
|
||||
result.Verified = verified
|
||||
result.SetVerificationError(err)
|
||||
}
|
||||
|
||||
results = append(results, result)
|
||||
}
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// Helper function to extract matches into a map for uniqueness
|
||||
func extractMatches(pattern *regexp.Regexp, data string) map[string]struct{} {
|
||||
matches := pattern.FindAllStringSubmatch(data, -1)
|
||||
result := make(map[string]struct{}, len(matches))
|
||||
|
||||
for _, match := range matches {
|
||||
if len(match) >= 2 {
|
||||
result[match[1]] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (s Scanner) Type() detectorspb.DetectorType {
|
||||
return detectorspb.DetectorType_StripePaymentIntent
|
||||
}
|
||||
|
||||
func (s Scanner) Description() string {
|
||||
return "Stripepaymentintent objects represent a customer's intent to pay and track the lifecycle of a payment. These objects are used to initiate and manage payment flows, including confirmation, authentication, and capture of funds."
|
||||
}
|
||||
|
||||
// VerifyPaymentIntentWithSecretKey verifies a Stripe PaymentIntent using the secret key.
|
||||
// It checks if the PaymentIntent ID is valid and if the secret key has access to it.
|
||||
// It returns a VerificationResult indicating the validity of the PaymentIntent and any error messages.
|
||||
func verifyPaymentIntentWithSecretKey(ctx context.Context, client *http.Client, clientSecret, secretKey string) (bool, error) {
|
||||
url := fmt.Sprintf("https://api.stripe.com/v1/payment_intents/%s", extractIntentID(clientSecret))
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("error creating request: %v", err)
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", secretKey))
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("request failed: %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
return isClientSecretValid(resp.Body, clientSecret)
|
||||
case http.StatusUnauthorized, http.StatusNotFound:
|
||||
return false, nil
|
||||
default:
|
||||
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
// verifyPaymentIntentWithPublishableKey verifies a Stripe PaymentIntent using the publishable key.
|
||||
// It checks if the PaymentIntent ID is valid and if the publishable key has access to it.
|
||||
// It returns a VerificationResult indicating the validity of the PaymentIntent and any error messages.
|
||||
// Note: It should only be used for client-side verification or in scenarios where the secret key is unavailable.
|
||||
func verifyPaymentIntentWithPublishableKey(ctx context.Context, client *http.Client, clientSecret, publishableKey string) (bool, error) {
|
||||
paymentIntentId := extractIntentID(clientSecret)
|
||||
if paymentIntentId == "" {
|
||||
return false, fmt.Errorf("payment intent ID is required")
|
||||
}
|
||||
|
||||
// Construct the request URL and add publishable key as a query parameter (this is how Stripe.js works)
|
||||
url := fmt.Sprintf("https://api.stripe.com/v1/payment_intents/%s", paymentIntentId)
|
||||
url = url + fmt.Sprintf("?key=%s", publishableKey)
|
||||
url = url + fmt.Sprintf("&client_secret=%s", clientSecret)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("error creating request: %v", err)
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("request failed: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
return isClientSecretValid(resp.Body, clientSecret)
|
||||
case http.StatusUnauthorized, http.StatusNotFound:
|
||||
return false, nil
|
||||
default:
|
||||
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func extractIntentID(clientSecret string) string {
|
||||
parts := strings.SplitN(clientSecret, "_secret_", 2)
|
||||
if len(parts) != 2 {
|
||||
return ""
|
||||
}
|
||||
return parts[0]
|
||||
}
|
||||
|
||||
func isClientSecretValid(body io.Reader, expectedSecret string) (bool, error) {
|
||||
var respBody struct {
|
||||
ClientSecret string `json:"client_secret"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(body).Decode(&respBody); err != nil {
|
||||
return false, fmt.Errorf("failed to decode response body: %v", err)
|
||||
}
|
||||
|
||||
return respBody.ClientSecret == expectedSecret, nil
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
//go:build detectors
|
||||
// +build detectors
|
||||
|
||||
package stripepaymentintent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
|
||||
)
|
||||
|
||||
func TestStripepaymentintent_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("STRIPE_SECRET")
|
||||
paymentIntent := testSecrets.MustGetField("STRIPE_PAYMENT_INTENT")
|
||||
secretInactive := testSecrets.MustGetField("STRIPE_INACTIVE")
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
data []byte
|
||||
verify bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
s Scanner
|
||||
args args
|
||||
wantVerified bool // Instead of expecting exact results, check if any result is verified
|
||||
wantResultCount int // Expected number of results
|
||||
wantErr bool
|
||||
wantVerificationErr bool
|
||||
}{
|
||||
{
|
||||
name: "found, verified",
|
||||
s: Scanner{},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
data: []byte(fmt.Sprintf("You can find a stripepaymentintent secret %s and payment intent: %s within", secret, paymentIntent)),
|
||||
verify: true,
|
||||
},
|
||||
wantVerified: true, // At least one result should be verified
|
||||
wantResultCount: 1, // 1 client secret × 1 key = 1 result
|
||||
wantErr: false,
|
||||
wantVerificationErr: false,
|
||||
},
|
||||
{
|
||||
name: "found, unverified",
|
||||
s: Scanner{},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
data: []byte(fmt.Sprintf("You can find a stripepaymentintent secret %s and payment intent %s within but not valid", secretInactive, paymentIntent)),
|
||||
verify: true,
|
||||
},
|
||||
wantVerified: false, // No results should be verified
|
||||
wantResultCount: 1, // 1 client secret × 1 key = 1 result
|
||||
wantErr: false,
|
||||
wantVerificationErr: false,
|
||||
},
|
||||
{
|
||||
name: "not found",
|
||||
s: Scanner{},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
data: []byte("You cannot find the secret within"),
|
||||
verify: true,
|
||||
},
|
||||
wantVerified: false,
|
||||
wantResultCount: 0, // No results expected
|
||||
wantErr: false,
|
||||
wantVerificationErr: false,
|
||||
},
|
||||
}
|
||||
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.Errorf("Stripepaymentintent.FromData() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
// Check result count
|
||||
if len(got) != tt.wantResultCount {
|
||||
t.Errorf("Stripepaymentintent.FromData() got %d results, want %d", len(got), tt.wantResultCount)
|
||||
return
|
||||
}
|
||||
|
||||
// Check each result
|
||||
hasVerified := false
|
||||
for i := range got {
|
||||
// Check that all results have the correct detector type
|
||||
if got[i].DetectorType != detectorspb.DetectorType_StripePaymentIntent {
|
||||
t.Errorf("Stripepaymentintent.FromData() result %d has wrong DetectorType", i)
|
||||
}
|
||||
|
||||
// Check that raw secret is present
|
||||
if len(got[i].Raw) == 0 {
|
||||
t.Fatalf("no raw secret present in result %d: \n %+v", i, got[i])
|
||||
}
|
||||
|
||||
// Check that RawV2 is present (should contain client secret + key)
|
||||
if len(got[i].RawV2) == 0 {
|
||||
t.Fatalf("no rawV2 present in result %d: \n %+v", i, got[i])
|
||||
}
|
||||
|
||||
// Check verification error expectation
|
||||
if (got[i].VerificationError() != nil) != tt.wantVerificationErr {
|
||||
t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError())
|
||||
}
|
||||
|
||||
// Track if any result is verified
|
||||
if got[i].Verified {
|
||||
hasVerified = true
|
||||
}
|
||||
}
|
||||
|
||||
// Check verification expectation
|
||||
if hasVerified != tt.wantVerified {
|
||||
t.Errorf("Stripepaymentintent.FromData() hasVerified = %v, want %v", hasVerified, tt.wantVerified)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
package stripepaymentintent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
|
||||
)
|
||||
|
||||
func TestStripepaymentintent_Pattern(t *testing.T) {
|
||||
d := Scanner{}
|
||||
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expectedPairs []string
|
||||
}{
|
||||
{
|
||||
name: "client secret with secret key",
|
||||
input: "stripepaymentintent_token = '" + "pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"' and stripe_key = '" + "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" + "'",
|
||||
expectedPairs: []string{"pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"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"},
|
||||
},
|
||||
{
|
||||
name: "client secret with publishable key",
|
||||
input: "stripepaymentintent_token = '" + "pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"' and stripe_key = '" + "pk_live_51MtwBwLkdIwHu7ix0AHtQwX6NWg8Fq7QKq5YGFy7WGBqRFHdTdE8LfKcKzQk7nJv3x5YJvnJYGGz1n3GyFdHJ5abc123456" + "'",
|
||||
expectedPairs: []string{"pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"pk_live_51MtwBwLkdIwHu7ix0AHtQwX6NWg8Fq7QKq5YGFy7WGBqRFHdTdE8LfKcKzQk7nJv3x5YJvnJYGGz1n3GyFdHJ5abc123456"},
|
||||
},
|
||||
{
|
||||
name: "multiple client secrets with single key",
|
||||
input: `stripepaymentintent_token1 = '` + "pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" + `'
|
||||
stripepaymentintent_token2 = '` + "pi_4NuxCxMleJwHu7ix28a3tqPb_secret_ZsLKVLsjdcCkdH9IWig123456" + `'
|
||||
stripe_key = '` + "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" + `'`,
|
||||
expectedPairs: []string{
|
||||
"pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"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",
|
||||
"pi_4NuxCxMleJwHu7ix28a3tqPb_secret_ZsLKVLsjdcCkdH9IWig123456" +
|
||||
"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",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "single client secret with multiple keys",
|
||||
input: `stripepaymentintent_token = '` + "pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" + `'
|
||||
stripe_secret_key = '` + "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" + `'
|
||||
stripe_publishable_key = '` + "pk_live_51MtwBwLkdIwHu7ix0AHtQwX6NWg8Fq7QKq5YGFy7WGBqRFHdTdE8LfKcKzQk7nJv3x5YJvnJYGGz1n3GyFdHJ5abc123456" + `'`,
|
||||
expectedPairs: []string{
|
||||
"pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"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",
|
||||
"pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"pk_live_51MtwBwLkdIwHu7ix0AHtQwX6NWg8Fq7QKq5YGFy7WGBqRFHdTdE8LfKcKzQk7nJv3x5YJvnJYGGz1n3GyFdHJ5abc123456",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multiple client secrets with multiple keys",
|
||||
input: `stripepaymentintent_token1 = '` + "pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" + `'
|
||||
stripepaymentintent_token2 = '` + "pi_4NuxCxMleJwHu7ix28a3tqPb_secret_ZsLKVLsjdcCkdH9IWig123456" + `'
|
||||
stripe_secret_key = '` + "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" + `'
|
||||
stripe_publishable_key = '` + "pk_live_51MtwBwLkdIwHu7ix0AHtQwX6NWg8Fq7QKq5YGFy7WGBqRFHdTdE8LfKcKzQk7nJv3x5YJvnJYGGz1n3GyFdHJ5abc123456" + `'`,
|
||||
expectedPairs: []string{
|
||||
"pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"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",
|
||||
"pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"pk_live_51MtwBwLkdIwHu7ix0AHtQwX6NWg8Fq7QKq5YGFy7WGBqRFHdTdE8LfKcKzQk7nJv3x5YJvnJYGGz1n3GyFdHJ5abc123456",
|
||||
"pi_4NuxCxMleJwHu7ix28a3tqPb_secret_ZsLKVLsjdcCkdH9IWig123456" +
|
||||
"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",
|
||||
"pi_4NuxCxMleJwHu7ix28a3tqPb_secret_ZsLKVLsjdcCkdH9IWig123456" +
|
||||
"pk_live_51MtwBwLkdIwHu7ix0AHtQwX6NWg8Fq7QKq5YGFy7WGBqRFHdTdE8LfKcKzQk7nJv3x5YJvnJYGGz1n3GyFdHJ5abc123456",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "only client secret without keys",
|
||||
input: "stripepaymentintent_token = '" + "pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" + "'",
|
||||
expectedPairs: []string{},
|
||||
},
|
||||
{
|
||||
name: "only keys without client secret",
|
||||
input: "stripe_key = '" + "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" + "'",
|
||||
expectedPairs: []string{},
|
||||
},
|
||||
{
|
||||
name: "invalid client secret with valid key",
|
||||
input: "stripepaymentintent_token = '" + "test_secret_test_1234567890abcdefg" + "' and stripe_key = '" +
|
||||
"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" + "'",
|
||||
expectedPairs: []string{},
|
||||
},
|
||||
{
|
||||
name: "mixed valid and invalid client secrets with key",
|
||||
input: `some_token = '` + "pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" + `'
|
||||
other_token = '` + "test_secret_test_1234567890abcdefg" + `'
|
||||
stripe_key = '` + "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" + `'`,
|
||||
expectedPairs: []string{"pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"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"},
|
||||
},
|
||||
{
|
||||
name: "complex scenario with multiple valid combinations",
|
||||
input: `
|
||||
# Multiple client secrets
|
||||
pi_token_1 = '` + "pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" + `'
|
||||
pi_token_2 = '` + "pi_4NuxCxMleJwHu7ix28a3tqPb_secret_ZsLKVLsjdcCkdH9IWig123456" + `'
|
||||
|
||||
# Multiple secret keys
|
||||
secret_key_1 = '` + "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" + `'
|
||||
secret_key_2 = '` + "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" + `'
|
||||
|
||||
# Multiple publishable keys
|
||||
pub_key_1 = '` + "pk_live_51MtwBwLkdIwHu7ix0AHtQwX6NWg8Fq7QKq5YGFy7WGBqRFHdTdE8LfKcKzQk7nJv3x5YJvnJYGGz1n3GyFdHJ5abc123456" + `'
|
||||
pub_key_2 = '` + "pk_live_51NuxCxMleJwHu7ix0BItRxY7OXh9Gr8RLr6ZHGz8XHCrSGIeUeF9MgLdLaRl8oKw4y6ZKwoKZHHa2o4HzGeIK6abc123456" + `'`,
|
||||
expectedPairs: []string{
|
||||
"pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"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",
|
||||
"pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"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",
|
||||
"pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"pk_live_51MtwBwLkdIwHu7ix0AHtQwX6NWg8Fq7QKq5YGFy7WGBqRFHdTdE8LfKcKzQk7nJv3x5YJvnJYGGz1n3GyFdHJ5abc123456",
|
||||
"pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" +
|
||||
"pk_live_51NuxCxMleJwHu7ix0BItRxY7OXh9Gr8RLr6ZHGz8XHCrSGIeUeF9MgLdLaRl8oKw4y6ZKwoKZHHa2o4HzGeIK6abc123456",
|
||||
"pi_4NuxCxMleJwHu7ix28a3tqPb_secret_ZsLKVLsjdcCkdH9IWig123456" +
|
||||
"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",
|
||||
"pi_4NuxCxMleJwHu7ix28a3tqPb_secret_ZsLKVLsjdcCkdH9IWig123456" +
|
||||
"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",
|
||||
"pi_4NuxCxMleJwHu7ix28a3tqPb_secret_ZsLKVLsjdcCkdH9IWig123456" +
|
||||
"pk_live_51MtwBwLkdIwHu7ix0AHtQwX6NWg8Fq7QKq5YGFy7WGBqRFHdTdE8LfKcKzQk7nJv3x5YJvnJYGGz1n3GyFdHJ5abc123456",
|
||||
"pi_4NuxCxMleJwHu7ix28a3tqPb_secret_ZsLKVLsjdcCkdH9IWig123456" +
|
||||
"pk_live_51NuxCxMleJwHu7ix0BItRxY7OXh9Gr8RLr6ZHGz8XHCrSGIeUeF9MgLdLaRl8oKw4y6ZKwoKZHHa2o4HzGeIK6abc123456",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "test keys should not match (only live keys are detected)",
|
||||
input: `stripepaymentintent_token = '` + "pi_3MtwBwLkdIwHu7ix28a3tqPa_secret_YrKJUKribcBjcG8HVhf123456" + `'
|
||||
test_secret_key = 'sk_test_51MtwBwLkdIwHu7ix0AHtQwX6NWg8Fq7QKq5YGFy7WGBqRFHdTdE8LfKcKzQk7nJv3x5YJvnJYGGz1n3GyFdHJ5nJGydHjE'
|
||||
test_pub_key = 'pk_test_51MtwBwLkdIwHu7ix0AHtQwX6NWg8Fq7QKq5YGFy7WGBqRFHdTdE8LfKcKzQk7nJv3x5YJvnJYGGz1n3GyFdHJ5nJGydHjE'`,
|
||||
expectedPairs: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
|
||||
if len(test.expectedPairs) > 0 {
|
||||
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.expectedPairs) {
|
||||
if len(results) == 0 {
|
||||
t.Errorf("did not receive result")
|
||||
} else {
|
||||
t.Errorf("expected %d results, only received %d", len(test.expectedPairs), 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.expectedPairs))
|
||||
for _, v := range test.expectedPairs {
|
||||
expected[v] = struct{}{}
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(expected, actual); diff != "" {
|
||||
t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -705,6 +705,7 @@ import (
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/strava"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/streak"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/stripe"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/stripepaymentintent"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/stripo"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/stytch"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/sugester"
|
||||
@@ -1706,6 +1707,7 @@ func buildDetectorList() []detectors.Detector {
|
||||
&zohocrm.Scanner{},
|
||||
&zonkafeedback.Scanner{},
|
||||
&zulipchat.Scanner{},
|
||||
&stripepaymentintent.Scanner{},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1131,6 +1131,7 @@ const (
|
||||
DetectorType_AzureDirectManagementKey DetectorType = 1024
|
||||
DetectorType_AzureAppConfigConnectionString DetectorType = 1025
|
||||
DetectorType_DeepSeek DetectorType = 1026
|
||||
DetectorType_StripePaymentIntent DetectorType = 1027
|
||||
)
|
||||
|
||||
// Enum value maps for DetectorType.
|
||||
@@ -2159,6 +2160,7 @@ var (
|
||||
1024: "AzureDirectManagementKey",
|
||||
1025: "AzureAppConfigConnectionString",
|
||||
1026: "DeepSeek",
|
||||
1027: "StripePaymentIntent",
|
||||
}
|
||||
DetectorType_value = map[string]int32{
|
||||
"Alibaba": 0,
|
||||
@@ -3184,6 +3186,7 @@ var (
|
||||
"AzureDirectManagementKey": 1024,
|
||||
"AzureAppConfigConnectionString": 1025,
|
||||
"DeepSeek": 1026,
|
||||
"StripePaymentIntent": 1027,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -3637,7 +3640,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, 0xaf, 0x84, 0x01, 0x0a, 0x0c, 0x44, 0x65, 0x74, 0x65, 0x63, 0x74, 0x6f, 0x72,
|
||||
0x10, 0x04, 0x2a, 0xc9, 0x84, 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, 0x03, 0x12,
|
||||
@@ -4696,11 +4699,13 @@ var file_detectors_proto_rawDesc = []byte{
|
||||
0x08, 0x12, 0x23, 0x0a, 0x1e, 0x41, 0x7a, 0x75, 0x72, 0x65, 0x41, 0x70, 0x70, 0x43, 0x6f, 0x6e,
|
||||
0x66, 0x69, 0x67, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72,
|
||||
0x69, 0x6e, 0x67, 0x10, 0x81, 0x08, 0x12, 0x0d, 0x0a, 0x08, 0x44, 0x65, 0x65, 0x70, 0x53, 0x65,
|
||||
0x65, 0x6b, 0x10, 0x82, 0x08, 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,
|
||||
0x65, 0x6b, 0x10, 0x82, 0x08, 0x12, 0x18, 0x0a, 0x13, 0x53, 0x74, 0x72, 0x69, 0x70, 0x65, 0x50,
|
||||
0x61, 0x79, 0x6d, 0x65, 0x6e, 0x74, 0x49, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x10, 0x83, 0x08, 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 (
|
||||
|
||||
@@ -1036,6 +1036,7 @@ enum DetectorType {
|
||||
AzureDirectManagementKey = 1024;
|
||||
AzureAppConfigConnectionString = 1025;
|
||||
DeepSeek = 1026;
|
||||
StripePaymentIntent = 1027;
|
||||
}
|
||||
|
||||
message Result {
|
||||
|
||||
Reference in New Issue
Block a user