Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
update LaunchDarkly detector to use the caller-identity API instead of the tokens API, and instantiating an SDK (#3018)
Patrick Kaeding4e0468771242
diff --git a/pkg/detectors/launchdarkly/launchdarkly.go b/pkg/detectors/launchdarkly/launchdarkly.goindex f6bbe9f36..e5f6f4ba5 100644--- a/pkg/detectors/launchdarkly/launchdarkly.go+++ b/pkg/detectors/launchdarkly/launchdarkly.go@@ -3,20 +3,15 @@ package launchdarklyimport ("context""encoding/json"- "errors""fmt""net/http""strings"- "time"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"-- ldclient "github.com/launchdarkly/go-server-sdk/v7"- "github.com/launchdarkly/go-server-sdk/v7/ldcomponents")type Scanner struct {@@ -27,28 +22,26 @@ type Scanner struct {var _ detectors.Detector = (*Scanner)(nil)var (- defaultClient = common.SaneHttpClient()- defaultSDKConfig = ldclient.Config{- Logging: ldcomponents.NoLogging(),- }- defaultSDKTimeout = 10 * time.Second- invalidSDKKeyError = "SDK key contains invalid characters"+ defaultClient = common.SaneHttpClient()// Launchdarkly keys are UUIDv4s with either api- or sdk- prefixes.// mob- keys are possible, but are not sensitive credentials.keyPat = regexp.MustCompile(`\b((?:api|sdk)-[a-z0-9]{8}-[a-z0-9]{4}-4[a-z0-9]{3}-[a-z0-9]{4}-[a-z0-9]{12})\b`))-type tokenResponse struct {- Items []token `json:"items"`- TotalCount int32 `json:"totalCount"`-}--type token struct {- Name string `json:"name"`- Role string `json:"role"`- Token string `json:"token"`- IsServiceToken bool `json:"serviceToken"`+type callerIdentity struct {+ AccountId string `json:"accountId,omitempty"`+ EnvironmentId string `json:"environmentId,omitempty"`+ ProjectId string `json:"projectId,omitempty"`+ EnvironmentName string `json:"environmentName,omitempty"`+ ProjectName string `json:"projectName,omitempty"`+ AuthKind string `json:"authKind,omitempty"`+ TokenKind string `json:"tokenKind,omitempty"`+ ClientID string `json:"clientId,omitempty"`+ TokenName string `json:"tokenName,omitempty"`+ TokenId string `json:"tokenId,omitempty"`+ MemberId string `json:"memberId,omitempty"`+ ServiceToken bool `json:"serviceToken"`}// We are not including "mob-" because client keys are not sensitive.@@ -76,58 +69,49 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result}if verify {- if strings.HasPrefix(resMatch, "api-") {- s1.ExtraData["type"] = "API"- req, err := http.NewRequestWithContext(ctx, "GET", "https://app.launchdarkly.com/api/v2/tokens", nil)- if err != nil {- continue- }- client := s.client- if client == nil {- client = defaultClient- }- req.Header.Add("Authorization", resMatch)- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- var tokenResponse tokenResponse- if err := json.NewDecoder(res.Body).Decode(&tokenResponse); err == nil && len(tokenResponse.Items) > 0 { // no error in parsing and have atleast one item- // set first token information only- token := tokenResponse.Items[0]- s1.ExtraData["token_name"] = token.Name- s1.ExtraData["token_role"] = token.Role- s1.ExtraData["token_value"] = token.Token- if token.IsServiceToken {+ req, err := http.NewRequestWithContext(ctx, "GET", "https://app.launchdarkly.com/api/v2/caller-identity", nil)+ if err != nil {+ continue+ }+ client := s.client+ if client == nil {+ client = defaultClient+ }+ req.Header.Add("Authorization", resMatch)+ res, err := client.Do(req)+ if err == nil {+ defer res.Body.Close()+ if res.StatusCode >= 200 && res.StatusCode < 300 {+ s1.Verified = true+ var callerIdentity callerIdentity+ if err := json.NewDecoder(res.Body).Decode(&callerIdentity); err == nil { // no error in parsing+ s1.ExtraData["type"] = callerIdentity.TokenKind+ s1.ExtraData["account_id"] = callerIdentity.AccountId+ s1.ExtraData["environment_id"] = callerIdentity.EnvironmentId+ s1.ExtraData["project_id"] = callerIdentity.ProjectId+ s1.ExtraData["environment_name"] = callerIdentity.EnvironmentName+ s1.ExtraData["project_name"] = callerIdentity.ProjectName+ s1.ExtraData["auth_kind"] = callerIdentity.AuthKind+ s1.ExtraData["token_kind"] = callerIdentity.TokenKind+ s1.ExtraData["client_id"] = callerIdentity.ClientID+ s1.ExtraData["token_name"] = callerIdentity.TokenName+ s1.ExtraData["member_id"] = callerIdentity.MemberId+ if callerIdentity.TokenKind == "auth" {+ if callerIdentity.ServiceToken {s1.ExtraData["token_type"] = "service"} else {s1.ExtraData["token_type"] = "personal"}- s1.ExtraData["total_token_count"] = fmt.Sprintf("%d", tokenResponse.TotalCount)}- } else if res.StatusCode == 401 {- // 401 is expected for an invalid token, so there is nothing to do here.- } else {- err = fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)- s1.SetVerificationError(err, resMatch)}+ } else if res.StatusCode == 401 {+ // 401 is expected for an invalid token, so there is nothing to do here.} else {+ err = fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)s1.SetVerificationError(err, resMatch)}} else {- // This is a server SDK key. Try to initialize using the SDK.- s1.ExtraData["type"] = "SDK"- _, err := ldclient.MakeCustomClient(resMatch, defaultSDKConfig, defaultSDKTimeout)- if err == nil {- s1.Verified = true- } else if errors.Is(err, ldclient.ErrInitializationFailed) || err.Error() == invalidSDKKeyError {- // If initialization fails, the key is not valid, so do nothing.- } else {- // If the error isn't nil or known, then this is likely a timeout error: ldclient.ErrInitializationTimeout- // But any other error here means we don't know if this key is valid.- s1.SetVerificationError(err, resMatch)- }+ s1.SetVerificationError(err, resMatch)}}diff --git a/pkg/detectors/launchdarkly/launchdarkly_test.go b/pkg/detectors/launchdarkly/launchdarkly_test.goindex 2f269462f..a8500fb59 100644--- a/pkg/detectors/launchdarkly/launchdarkly_test.go+++ b/pkg/detectors/launchdarkly/launchdarkly_test.go@@ -23,6 +23,7 @@ func TestLaunchDarkly_FromChunk(t *testing.T) {if err != nil {t.Fatalf("could not get test secrets from GCP: %s", err)}+ sdkSecret := testSecrets.MustGetField("LAUNCHDARKLY_SDK_TOKEN")secret := testSecrets.MustGetField("LAUNCHDARKLY_TOKEN")inactiveSecret := testSecrets.MustGetField("LAUNCHDARKLY_INACTIVE")@@ -99,30 +100,26 @@ func TestLaunchDarkly_FromChunk(t *testing.T) {wantErr: false,wantVerificationErr: true,},- // TODO: enable this test and add the sdk token- /*- {- name: "found, valid sdk token",- s: Scanner{},- args: args{- ctx: context.Background(),- data: []byte(fmt.Sprintf("You can find a launchdarkly sdk secret %s within", sdkSecret)),- verify: true,- },- want: []detectors.Result{- {- DetectorType: detectorspb.DetectorType_LaunchDarkly,- Verified: true,- },+ {+ name: "found, valid sdk token",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a launchdarkly sdk secret %s within", sdkSecret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_LaunchDarkly,+ Verified: true,},- wantErr: false,},- */+ 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)+ got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)if (err != nil) != tt.wantErr {t.Errorf("LaunchDarkly.FromData() error = %v, wantErr %v", err, tt.wantErr)return@@ -132,6 +129,14 @@ func TestLaunchDarkly_FromChunk(t *testing.T) {t.Fatalf("no raw secret present: \n %+v", got[i])}got[i].Raw = nil++ // Do we expect to be comparing the ExtraData?+ got[i].ExtraData = nil++ // If we expect a verification error, copy over the one we got so the later comparison can work+ if tt.wantVerificationErr && len(tt.want) > i {+ tt.want[i].SetVerificationError(got[i].VerificationError())+ }}if diff := pretty.Compare(got, tt.want); diff != "" {t.Errorf("LaunchDarkly.FromData() %s diff: (-got +want)\n%s", tt.name, diff)