Skip to content

Find, verify, and analyze leaked credentials

Updated Lokalise Detector (#4279)

Kashif Khan00f9ae6c3f62
diff --git a/pkg/detectors/lokalisetoken/lokalisetoken.go b/pkg/detectors/lokalisetoken/lokalisetoken.go
index c6c604b84..5285747e1 100644
--- a/pkg/detectors/lokalisetoken/lokalisetoken.go
+++ b/pkg/detectors/lokalisetoken/lokalisetoken.go
@@ -2,10 +2,14 @@ package lokalisetoken
import (
"context"
- regexp "github.com/wasilibs/go-re2"
+ "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"
@@ -29,10 +33,17 @@ func (s Scanner) Keywords() []string {
return []string{"lokalise"}
}
+func (s Scanner) Type() detectorspb.DetectorType {
+ return detectorspb.DetectorType_LokaliseToken
+}
+
+func (s Scanner) Description() string {
+ return "Lokalise is a translation management system that helps teams to manage and automate their localization process. Lokalise tokens can be used to access its API and modify project data."
+}
+
// FromData will find and optionally verify LokaliseToken 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)
- // log.Println(dataStr)
matches := keyPat.FindAllStringSubmatch(dataStr, -1)
@@ -42,20 +53,15 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
s1 := detectors.Result{
DetectorType: detectorspb.DetectorType_LokaliseToken,
Raw: []byte(resMatch),
+ ExtraData: make(map[string]string),
}
if verify {
- req, err := http.NewRequestWithContext(ctx, "GET", "https://api.lokalise.com/api2/system/languages", nil)
- if err != nil {
- continue
- }
- req.Header.Add("x-api-token", resMatch)
- res, err := client.Do(req)
- if err == nil {
- defer res.Body.Close()
- if res.StatusCode >= 200 && res.StatusCode < 300 {
- s1.Verified = true
- }
+ projectCreators, isVerified, verificationErr := verifyLokaliseKey(ctx, client, resMatch)
+ s1.Verified = isVerified
+ s1.SetVerificationError(verificationErr, resMatch)
+ if len(projectCreators) > 0 {
+ s1.ExtraData["project creators"] = strings.Join(projectCreators, ", ")
}
}
@@ -65,10 +71,47 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
return results, nil
}
-func (s Scanner) Type() detectorspb.DetectorType {
- return detectorspb.DetectorType_LokaliseToken
+type projectsResponse struct {
+ Projects []struct {
+ CreatedByEmail string `json:"created_by_email"`
+ } `json:"projects"`
}
-func (s Scanner) Description() string {
- return "Lokalise is a translation management system that helps teams to manage and automate their localization process. Lokalise tokens can be used to access its API and modify project data."
+func verifyLokaliseKey(ctx context.Context, client *http.Client, token string) ([]string, bool, error) {
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.lokalise.com/api2/projects", http.NoBody)
+ if err != nil {
+ return nil, false, err
+ }
+
+ req.Header.Add("X-Api-Token", token)
+ resp, err := client.Do(req)
+ if err != nil {
+ return nil, false, err
+ }
+
+ defer func() {
+ _, _ = io.Copy(io.Discard, resp.Body)
+ _ = resp.Body.Close()
+ }()
+
+ switch resp.StatusCode {
+ case http.StatusOK:
+ var projectCreators projectsResponse
+ var creatorsList = make([]string, 0)
+
+ if err := json.NewDecoder(resp.Body).Decode(&projectCreators); err != nil {
+ // if failed to decode in case of 200 OK - return true without list
+ return nil, true, nil
+ }
+
+ for _, project := range projectCreators.Projects {
+ creatorsList = append(creatorsList, project.CreatedByEmail)
+ }
+
+ return creatorsList, true, nil
+ case http.StatusUnauthorized, http.StatusForbidden:
+ return nil, false, nil
+ default:
+ return nil, false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
+ }
}