Skip to content

Find, verify, and analyze leaked credentials

Prefer nearest client/tenant IDs for Azure SP secrets (#5449)

Kashif Khan71598daab7f1
diff --git a/pkg/detectors/azure_entra/common.go b/pkg/detectors/azure_entra/common.go
index 3ce5ceb12..9d8ca2ed6 100644
--- a/pkg/detectors/azure_entra/common.go
+++ b/pkg/detectors/azure_entra/common.go
@@ -4,6 +4,7 @@ import (
"fmt"
"io"
"net/http"
+ "sort"
"strings"
regexp "github.com/wasilibs/go-re2"
@@ -38,45 +39,93 @@ var (
// FindTenantIdMatches returns a list of potential tenant IDs in the provided |data|.
func FindTenantIdMatches(data string) map[string]struct{} {
- uniqueMatches := make(map[string]struct{})
-
- for _, match := range tenantIdPat.FindAllStringSubmatch(data, -1) {
- var m string
- if match[1] != "" {
- m = strings.ToLower(match[1])
- } else if match[2] != "" {
- m = strings.ToLower(match[2])
- } else if match[3] != "" {
- m = strings.ToLower(match[3])
- } else if match[4] != "" {
- m = strings.ToLower(match[4])
- }
- if _, ok := detectors.UuidFalsePositives[detectors.FalsePositive(m)]; ok {
- continue
- } else if detectors.StringShannonEntropy(m) < 3 {
- continue
+ return offsetsToSet(FindTenantIdOffsets(data))
+}
+
+// FindTenantIdOffsets returns potential tenant IDs in |data|, mapped to the byte offsets where each was found.
+func FindTenantIdOffsets(data string) map[string][]int {
+ matches := make(map[string][]int)
+
+ for _, idx := range tenantIdPat.FindAllStringSubmatchIndex(data, -1) {
+ // The pattern has four alternative capture groups; use the one that matched.
+ for group := 1; group <= 4; group++ {
+ start, end := idx[2*group], idx[2*group+1]
+ if start < 0 {
+ continue
+ }
+ m := strings.ToLower(data[start:end])
+ if isLikelyId(m) {
+ matches[m] = append(matches[m], start)
+ }
+ break
}
- uniqueMatches[m] = struct{}{}
}
- for _, match := range tenantOnMicrosoftPat.FindAllStringSubmatch(data, -1) {
- uniqueMatches[match[1]] = struct{}{}
+ for _, idx := range tenantOnMicrosoftPat.FindAllStringSubmatchIndex(data, -1) {
+ m := data[idx[2]:idx[3]]
+ matches[m] = append(matches[m], idx[2])
}
- return uniqueMatches
+ return matches
}
// FindClientIdMatches returns a list of potential client UUIDs in the provided |data|.
func FindClientIdMatches(data string) map[string]struct{} {
- uniqueMatches := make(map[string]struct{})
- for _, match := range clientIdPat.FindAllStringSubmatch(data, -1) {
- m := strings.ToLower(match[1])
- if _, ok := detectors.UuidFalsePositives[detectors.FalsePositive(m)]; ok {
- continue
- } else if detectors.StringShannonEntropy(m) < 3 {
- continue
+ return offsetsToSet(FindClientIdOffsets(data))
+}
+
+// FindClientIdOffsets returns potential client UUIDs in |data|, mapped to the byte offsets where each was found.
+func FindClientIdOffsets(data string) map[string][]int {
+ matches := make(map[string][]int)
+ for _, idx := range clientIdPat.FindAllStringSubmatchIndex(data, -1) {
+ m := strings.ToLower(data[idx[2]:idx[3]])
+ if isLikelyId(m) {
+ matches[m] = append(matches[m], idx[2])
}
- uniqueMatches[m] = struct{}{}
}
- return uniqueMatches
+ return matches
+}
+
+// NearestFirst returns the keys of |candidates| ordered by their distance to |pos|.
+// A candidate's distance is that of its closest occurrence. Ties are broken by value so the order is stable.
+func NearestFirst(candidates map[string][]int, pos int) []string {
+ distances := make(map[string]int, len(candidates))
+ ordered := make([]string, 0, len(candidates))
+ for value, offsets := range candidates {
+ closest := -1
+ for _, offset := range offsets {
+ d := offset - pos
+ if d < 0 {
+ d = -d
+ }
+ if closest < 0 || d < closest {
+ closest = d
+ }
+ }
+ distances[value] = closest
+ ordered = append(ordered, value)
+ }
+ sort.Slice(ordered, func(i, j int) bool {
+ if distances[ordered[i]] != distances[ordered[j]] {
+ return distances[ordered[i]] < distances[ordered[j]]
+ }
+ return ordered[i] < ordered[j]
+ })
+ return ordered
+}
+
+// isLikelyId filters out well-known placeholder UUIDs and low-entropy values.
+func isLikelyId(m string) bool {
+ if _, ok := detectors.UuidFalsePositives[detectors.FalsePositive(m)]; ok {
+ return false
+ }
+ return detectors.StringShannonEntropy(m) >= 3
+}
+
+func offsetsToSet(offsets map[string][]int) map[string]struct{} {
+ set := make(map[string]struct{}, len(offsets))
+ for value := range offsets {
+ set[value] = struct{}{}
+ }
+ return set
}
var (
@@ -85,35 +134,42 @@ var (
)
// TenantExists returns whether the tenant exists according to Microsoft's well-known OpenID endpoint.
-func TenantExists(ctx context.Context, client *http.Client, tenant string) bool {
+// A non-nil error means the answer is unknown (e.g., a network failure); it is not cached.
+func TenantExists(ctx context.Context, client *http.Client, tenant string) (bool, error) {
// Use cached value where possible.
if tenantExists, isCached := tenantCache.Get(tenant); isCached {
- return tenantExists
+ return tenantExists, nil
}
// https://www.codingexplorations.com/blog/understanding-singleflight-in-golang-a-solution-for-eliminating-redundant-work
- tenantExists, _, _ := tenantGroup.Do(tenant, func() (interface{}, error) {
- result := queryTenant(ctx, client, tenant)
- tenantCache.Set(tenant, result)
- return result, nil
+ tenantExists, err, _ := tenantGroup.Do(tenant, func() (interface{}, error) {
+ result, err := queryTenant(ctx, client, tenant)
+ // Only remember definite answers, so a transient failure doesn't stick.
+ if err == nil {
+ tenantCache.Set(tenant, result)
+ }
+ return result, err
})
+ if err != nil {
+ return false, err
+ }
- return tenantExists.(bool)
+ return tenantExists.(bool), nil
}
-func queryTenant(ctx context.Context, client *http.Client, tenant string) bool {
+func queryTenant(ctx context.Context, client *http.Client, tenant string) (bool, error) {
logger := ctx.Logger().WithName("azure").WithValues("tenant", tenant)
tenantUrl := fmt.Sprintf("https://login.microsoftonline.com/%s/.well-known/openid-configuration", tenant)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, tenantUrl, nil)
if err != nil {
- return false
+ return false, err
}
res, err := client.Do(req)
if err != nil {
logger.Error(err, "Failed to check if tenant exists")
- return false
+ return false, err
}
defer func() {
_, _ = io.Copy(io.Discard, res.Body)
@@ -122,13 +178,13 @@ func queryTenant(ctx context.Context, client *http.Client, tenant string) bool {
switch res.StatusCode {
case http.StatusOK:
- return true
+ return true, nil
case http.StatusBadRequest:
logger.V(4).Info("Tenant does not exist.")
- return false
+ return false, nil
default:
bodyBytes, _ := io.ReadAll(res.Body)
logger.Error(nil, "WARNING: Unexpected response when checking if tenant exists", "status_code", res.StatusCode, "body", string(bodyBytes))
- return false
+ return false, fmt.Errorf("unexpected status %d checking tenant", res.StatusCode)
}
}
diff --git a/pkg/detectors/azure_entra/common_test.go b/pkg/detectors/azure_entra/common_test.go
index 9d3f1e5a0..0e7e17a28 100644
--- a/pkg/detectors/azure_entra/common_test.go
+++ b/pkg/detectors/azure_entra/common_test.go
@@ -1,9 +1,14 @@
package azure_entra
import (
+ "errors"
+ "net/http"
+ "strings"
"testing"
"github.com/google/go-cmp/cmp"
+
+ "github.com/trufflesecurity/trufflehog/v3/pkg/context"
)
type testCase struct {
@@ -360,3 +365,61 @@ subscription_id = "47ab1364-000d-4a53-838d-1537b1e3b49f"
runPatTest(t, cases, FindClientIdMatches)
}
+
+func Test_NearestFirst(t *testing.T) {
+ candidates := map[string][]int{
+ "far": {500},
+ "near": {95, 900},
+ "tie-b": {110},
+ "tie-a": {90},
+ "adjacent": {101},
+ }
+ got := NearestFirst(candidates, 100)
+ want := []string{"adjacent", "near", "tie-a", "tie-b", "far"}
+ if diff := cmp.Diff(want, got); diff != "" {
+ t.Errorf("diff: (-want +got)\n%s", diff)
+ }
+}
+
+func Test_FindIdOffsets(t *testing.T) {
+ data := "pad\nclient_id: 1474bfe8-663c-486e-9daf-f1f580302218\ntenant_id: 608e4ac4-2ca8-40dd-a046-4064540a1cde\n"
+
+ clients := FindClientIdOffsets(data)
+ wantClient := map[string][]int{"1474bfe8-663c-486e-9daf-f1f580302218": {strings.Index(data, "1474bfe8")}}
+ if diff := cmp.Diff(wantClient, clients); diff != "" {
+ t.Errorf("clients diff: (-want +got)\n%s", diff)
+ }
+
+ tenants := FindTenantIdOffsets(data)
+ wantTenant := map[string][]int{"608e4ac4-2ca8-40dd-a046-4064540a1cde": {strings.Index(data, "608e4ac4")}}
+ if diff := cmp.Diff(wantTenant, tenants); diff != "" {
+ t.Errorf("tenants diff: (-want +got)\n%s", diff)
+ }
+}
+
+type roundTripFunc func(*http.Request) (*http.Response, error)
+
+func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
+
+// A failed lookup must not be remembered as "tenant doesn't exist".
+func Test_TenantExists_DoesNotCacheErrors(t *testing.T) {
+ const tenant = "5e6f7a8b-1c2d-4e3f-8a9b-0c1d2e3f4a5b"
+ fail := true
+ client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
+ if fail {
+ return nil, errors.New("network down")
+ }
+ return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody}, nil
+ })}
+
+ exists, err := TenantExists(context.Background(), client, tenant)
+ if err == nil || exists {
+ t.Fatalf("expected an error and no tenant, got exists=%v err=%v", exists, err)
+ }
+
+ fail = false
+ exists, err = TenantExists(context.Background(), client, tenant)
+ if err != nil || !exists {
+ t.Fatalf("expected the tenant to exist after the network recovered, got exists=%v err=%v", exists, err)
+ }
+}
diff --git a/pkg/detectors/azure_entra/refreshtoken/refreshtoken.go b/pkg/detectors/azure_entra/refreshtoken/refreshtoken.go
index b214cfe7f..1f4327945 100644
--- a/pkg/detectors/azure_entra/refreshtoken/refreshtoken.go
+++ b/pkg/detectors/azure_entra/refreshtoken/refreshtoken.go
@@ -112,7 +112,7 @@ func (s Scanner) processMatches(ctx context.Context, refreshTokens, clientIds, t
}
if _, ok := validTenants[tenantId]; !ok {
- if azure_entra.TenantExists(logCtx, client, tenantId) {
+ if exists, err := azure_entra.TenantExists(logCtx, client, tenantId); err == nil && exists {
validTenants[tenantId] = struct{}{}
} else {
delete(tenantIds, tenantId)
diff --git a/pkg/detectors/azure_entra/serviceprincipal/v1/spv1.go b/pkg/detectors/azure_entra/serviceprincipal/v1/spv1.go
index 585d7013a..4d3327fad 100644
--- a/pkg/detectors/azure_entra/serviceprincipal/v1/spv1.go
+++ b/pkg/detectors/azure_entra/serviceprincipal/v1/spv1.go
@@ -53,18 +53,18 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
if len(clientSecrets) == 0 {
return
}
- clientIds := azure_entra.FindClientIdMatches(dataStr)
+ clientIds := azure_entra.FindClientIdOffsets(dataStr)
if len(clientIds) == 0 {
return
}
- tenantIds := azure_entra.FindTenantIdMatches(dataStr)
+ tenantIds := azure_entra.FindTenantIdOffsets(dataStr)
client := s.client
if client == nil {
client = defaultClient
}
// The handling logic is identical for both versions.
- results = append(results, v2.ProcessData(ctx, clientSecrets, clientIds, tenantIds, verify, client)...)
+ results = append(results, v2.ProcessData(ctx, dataStr, clientSecrets, clientIds, tenantIds, verify, client)...)
return results, nil
}
diff --git a/pkg/detectors/azure_entra/serviceprincipal/v2/spv2.go b/pkg/detectors/azure_entra/serviceprincipal/v2/spv2.go
index a379a673c..801589680 100644
--- a/pkg/detectors/azure_entra/serviceprincipal/v2/spv2.go
+++ b/pkg/detectors/azure_entra/serviceprincipal/v2/spv2.go
@@ -58,34 +58,54 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
if len(clientSecrets) == 0 {
return results, nil
}
- clientIds := azure_entra.FindClientIdMatches(dataStr)
- tenantIds := azure_entra.FindTenantIdMatches(dataStr)
+ clientIds := azure_entra.FindClientIdOffsets(dataStr)
+ tenantIds := azure_entra.FindTenantIdOffsets(dataStr)
client := s.client
if client == nil {
client = defaultClient
}
- results = append(results, ProcessData(ctx, clientSecrets, clientIds, tenantIds, verify, client)...)
+ results = append(results, ProcessData(ctx, dataStr, clientSecrets, clientIds, tenantIds, verify, client)...)
return results, nil
}
-func ProcessData(ctx context.Context, clientSecrets, clientIds, tenantIds map[string]struct{}, verify bool, client *http.Client) (results []detectors.Result) {
+// ProcessData verifies each secret against the client and tenant IDs found in |data|.
+// |clientIds| and |tenantIds| map each candidate to the offsets it was found at, so the
+// candidates closest to a secret are tried (and reported) first.
+func ProcessData(ctx context.Context, data string, clientSecrets map[string]struct{}, clientIds, tenantIds map[string][]int, verify bool, client *http.Client) (results []detectors.Result) {
logCtx := logContext.AddLogger(ctx)
invalidClientsForTenant := make(map[string]map[string]struct{})
+ // Tenants whose lookup failed. Not retried again in this call, so one flaky tenant
+ // costs one request instead of one per secret and client.
+ unknownTenants := make(map[string]struct{})
for clientSecret := range clientSecrets {
var (
- r *detectors.Result
- clientId string
- tenantId string
+ r *detectors.Result
+ // The first client/tenant pair that recognised the app but rejected the secret.
+ knownClientId, knownTenantId string
)
+ // Candidates nearest to the secret are the most likely to belong to it.
+ secretPos := strings.Index(data, clientSecret)
+ clientOrder := azure_entra.NearestFirst(clientIds, secretPos)
+ tenantOrder := azure_entra.NearestFirst(tenantIds, secretPos)
+
ClientLoop:
- for cId := range clientIds {
- clientId = cId
- for tId := range tenantIds {
- tenantId = tId
+ for _, clientId := range clientOrder {
+ // Skip candidates removed earlier (e.g., a tenant that was also read as a client).
+ if _, ok := clientIds[clientId]; !ok {
+ continue
+ }
+ for _, tenantId := range tenantOrder {
+ if _, ok := tenantIds[tenantId]; !ok {
+ continue
+ }
+
+ if _, unknown := unknownTenants[tenantId]; unknown {
+ continue
+ }
// Skip known invalid tenants.
invalidClients := invalidClientsForTenant[tenantId]
@@ -98,7 +118,14 @@ func ProcessData(ctx context.Context, clientSecrets, clientIds, tenantIds map[st
}
if verify {
- if !azure_entra.TenantExists(logCtx, client, tenantId) {
+ exists, err := azure_entra.TenantExists(logCtx, client, tenantId)
+ if err != nil {
+ // Unknown, not "missing". Skip it for now but keep it as a candidate.
+ // If no pair is recognised, the fallback still picks the nearest tenant.
+ unknownTenants[tenantId] = struct{}{}
+ continue
+ }
+ if !exists {
// Tenant doesn't exist
delete(tenantIds, tenantId)
continue
@@ -113,6 +140,11 @@ func ProcessData(ctx context.Context, clientSecrets, clientIds, tenantIds map[st
case errors.Is(verificationErr, serviceprincipal.ErrConditionalAccessPolicy):
// Do nothing.
case errors.Is(verificationErr, serviceprincipal.ErrSecretInvalid):
+ // The app exists in this tenant, only the secret is wrong (e.g.,
+ // rotated). Remember the pair so the fallback matches the live result.
+ if knownClientId == "" {
+ knownClientId, knownTenantId = clientId, tenantId
+ }
continue ClientLoop
case errors.Is(verificationErr, serviceprincipal.ErrSecretExpired):
r = createResult(tenantId, clientId, clientSecret, false, nil, nil)
@@ -138,12 +170,12 @@ func ProcessData(ctx context.Context, clientSecrets, clientIds, tenantIds map[st
}
if r == nil {
- // Only include the clientId and tenantId if we're confident which one it is.
- if len(clientIds) != 1 {
- clientId = ""
- }
- if len(tenantIds) != 1 {
- tenantId = ""
+ // Nothing verified. Use the pair Azure recognised, if any. Otherwise report
+ // the nearest tenant, then the nearest client that isn't ruled out for it.
+ // This keeps the result the same whether the secret is live or rotated.
+ clientId, tenantId := knownClientId, knownTenantId
+ if clientId == "" {
+ clientId, tenantId = nearestPair(clientIds, tenantIds, invalidClientsForTenant, secretPos)
}
r = createResult(tenantId, clientId, clientSecret, false, nil, nil)
}
@@ -153,6 +185,21 @@ func ProcessData(ctx context.Context, clientSecrets, clientIds, tenantIds map[st
return results
}
+// nearestPair picks the tenant nearest to |pos|, then the nearest client that isn't
+// ruled out for that tenant and isn't the tenant itself.
+func nearestPair(clientIds, tenantIds map[string][]int, invalidClientsForTenant map[string]map[string]struct{}, pos int) (clientId, tenantId string) {
+ if remaining := azure_entra.NearestFirst(tenantIds, pos); len(remaining) > 0 {
+ tenantId = remaining[0]
+ }
+ for _, candidate := range azure_entra.NearestFirst(clientIds, pos) {
+ if _, ruledOut := invalidClientsForTenant[tenantId][candidate]; ruledOut || candidate == tenantId {
+ continue
+ }
+ return candidate, tenantId
+ }
+ return "", tenantId
+}
+
func createResult(tenantId string, clientId string, clientSecret string, verified bool, extraData map[string]string, err error) *detectors.Result {
r := &detectors.Result{
DetectorType: detector_typepb.DetectorType_Azure,
diff --git a/pkg/detectors/azure_entra/serviceprincipal/v2/spv2_processdata_test.go b/pkg/detectors/azure_entra/serviceprincipal/v2/spv2_processdata_test.go
index 7923696dd..95843fe6f 100644
--- a/pkg/detectors/azure_entra/serviceprincipal/v2/spv2_processdata_test.go
+++ b/pkg/detectors/azure_entra/serviceprincipal/v2/spv2_processdata_test.go
@@ -3,13 +3,16 @@ package v2
import (
"context"
"encoding/json"
+ "errors"
"fmt"
"net/http"
"net/http/httptest"
+ "strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb"
)
@@ -102,10 +105,10 @@ func TestProcessData_VerificationErrors(t *testing.T) {
client := newMockAzureClient(tenantID, tc.tokenStatus, tc.tokenResponse)
secrets := map[string]struct{}{clientSecret: {}}
- clientIDs := map[string]struct{}{clientID: {}}
- tenantIDs := map[string]struct{}{tenantID: {}}
+ clientIDs := map[string][]int{clientID: {0}}
+ tenantIDs := map[string][]int{tenantID: {0}}
- results := ProcessData(context.Background(), secrets, clientIDs, tenantIDs, true, client)
+ results := ProcessData(context.Background(), clientSecret, secrets, clientIDs, tenantIDs, true, client)
require.Len(t, results, tc.wantResultCount)
if tc.wantResultCount > 0 {
@@ -132,10 +135,10 @@ func TestProcessData_ExpiredSecretShouldEmitResult(t *testing.T) {
})
secrets := map[string]struct{}{clientSecret: {}}
- clientIDs := map[string]struct{}{clientID: {}}
- tenantIDs := map[string]struct{}{tenantID: {}}
+ clientIDs := map[string][]int{clientID: {0}}
+ tenantIDs := map[string][]int{tenantID: {0}}
- results := ProcessData(context.Background(), secrets, clientIDs, tenantIDs, true, client)
+ results := ProcessData(context.Background(), clientSecret, secrets, clientIDs, tenantIDs, true, client)
require.Len(t, results, 1, "expired secret must still produce a result")
r := results[0]
@@ -145,3 +148,216 @@ func TestProcessData_ExpiredSecretShouldEmitResult(t *testing.T) {
assert.NotNil(t, r.RawV2, "RawV2 should be set when clientId and tenantId are known")
assert.Nil(t, r.VerificationError(), "expired secret is definitively invalid, not indeterminate")
}
+
+// Two secrets, each with its own client and tenant. Rotated (verify off) and live
+// secrets must report the same nearest pair, so the result keeps one identity.
+func TestProcessData_NearestClientAndTenantWins(t *testing.T) {
+ const (
+ secretA = "abc4Q~fake-secret-that-is-long-enough1234567"
+ secretB = "xyz4Q~another-fake-secret-long-enough7654321"
+ clientA = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
+ tenantA = "f9e8d7c6-1111-2222-3333-ccc000000099"
+ clientB = "b2c3d4e5-f6a7-8901-bcde-f12345678901"
+ tenantB = "e8d7c6b5-4444-5555-6666-ddd000000088"
+ )
+ data := "client_id=" + clientA + "\ntenant_id=" + tenantA + "\nsecret=" + secretA + "\n" +
+ strings.Repeat("filler\n", 50) +
+ "client_id=" + clientB + "\ntenant_id=" + tenantB + "\nsecret=" + secretB + "\n"
+
+ clientIDs := map[string][]int{
+ clientA: {strings.Index(data, clientA)},
+ clientB: {strings.Index(data, clientB)},
+ }
+ tenantIDs := map[string][]int{
+ tenantA: {strings.Index(data, tenantA)},
+ tenantB: {strings.Index(data, tenantB)},
+ }
+ secrets := map[string]struct{}{secretA: {}, secretB: {}}
+
+ results := ProcessData(context.Background(), data, secrets, clientIDs, tenantIDs, false, nil)
+
+ require.Len(t, results, 2)
+ for _, r := range results {
+ var want string
+ switch string(r.Raw) {
+ case secretA:
+ want = fmt.Sprintf(`{"clientSecret":"%s","clientId":"%s","tenantId":"%s"}`, secretA, clientA, tenantA)
+ case secretB:
+ want = fmt.Sprintf(`{"clientSecret":"%s","clientId":"%s","tenantId":"%s"}`, secretB, clientB, tenantB)
+ }
+ assert.Equal(t, want, string(r.RawV2))
+ }
+}
+
+// roundTripFunc lets a test script the HTTP responses directly.
+type roundTripFunc func(*http.Request) (*http.Response, error)
+
+func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
+
+func tokenErrorResponse(status int, description string) *http.Response {
+ rec := httptest.NewRecorder()
+ rec.WriteHeader(status)
+ _ = json.NewEncoder(rec).Encode(map[string]string{"error": "invalid_client", "error_description": description})
+ return rec.Result()
+}
+
+// An unrelated client sits closer to the secret than the real one. Once the unrelated
+// client is ruled out, the live and the rotated secret must report the same client.
+func TestProcessData_RotatedSecretKeepsClientThatVerified(t *testing.T) {
+ const (
+ secret = "abc4Q~fake-secret-that-is-long-enough1234567"
+ otherApp = "1474bfe8-663c-486e-9daf-f1f580302218"
+ realApp = "8595f61a-109a-497d-8c8f-566b733e95fe"
+ tenant = "7a1c2b3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d"
+ )
+ data := "client_id: " + otherApp + "\nsecret: " + secret + "\n" + strings.Repeat("filler\n", 20) + "client_id: " + realApp + "\n"
+
+ run := func(realAppStatus int, realAppDescription string) detectors.Result {
+ client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
+ if r.Method == http.MethodGet {
+ return tokenErrorResponse(http.StatusOK, ""), nil
+ }
+ _ = r.ParseForm()
+ switch {
+ case r.PostForm.Get("client_id") == otherApp:
+ return tokenErrorResponse(http.StatusBadRequest, "AADSTS700016: app not found"), nil
+ case realAppStatus == http.StatusOK:
+ rec := httptest.NewRecorder()
+ _ = json.NewEncoder(rec).Encode(map[string]string{"access_token": "x"})
+ return rec.Result(), nil
+ default:
+ return tokenErrorResponse(realAppStatus, realAppDescription), nil
+ }
+ })}
+ clientIDs := map[string][]int{otherApp: {strings.Index(data, otherApp)}, realApp: {strings.Index(data, realApp)}}
+ tenantIDs := map[string][]int{tenant: {0}}
+ results := ProcessData(context.Background(), data, map[string]struct{}{secret: {}}, clientIDs, tenantIDs, true, client)
+ require.Len(t, results, 1)
+ return results[0]
+ }
+
+ live := run(http.StatusOK, "")
+ rotated := run(http.StatusUnauthorized, "AADSTS7000215: Invalid client secret provided.")
+
+ assert.True(t, live.Verified)
+ assert.False(t, rotated.Verified)
+ assert.Contains(t, string(live.RawV2), realApp)
+ assert.Equal(t, string(live.RawV2), string(rotated.RawV2))
+}
+
+// Without verification nothing removes a tenant ID that also looked like a client ID,
+// so the fallback must not report it as both.
+func TestProcessData_ClientIsNeverTheTenant(t *testing.T) {
+ const (
+ secret = "abc4Q~fake-secret-that-is-long-enough1234567"
+ tenant = "608e4ac4-2ca8-40dd-a046-4064540a1cde"
+ app = "8595f61a-109a-497d-8c8f-566b733e95fe"
+ )
+ data := secret + " " + tenant + " " + app
+ clientIDs := map[string][]int{tenant: {len(secret) + 1}, app: {1000}}
+ tenantIDs := map[string][]int{tenant: {len(secret) + 1}}
+
+ results := ProcessData(context.Background(), data, map[string]struct{}{secret: {}}, clientIDs, tenantIDs, false, nil)
+
+ require.Len(t, results, 1)
+ assert.JSONEq(t, fmt.Sprintf(`{"clientSecret":%q,"clientId":%q,"tenantId":%q}`, secret, app, tenant), string(results[0].RawV2))
+}
+
+// A failed tenant lookup is "unknown", not "missing". The nearest tenant must stay
+// the reported one even though it couldn't be checked.
+func TestProcessData_TransientTenantErrorKeepsNearestTenant(t *testing.T) {
+ const (
+ secret = "abc4Q~fake-secret-that-is-long-enough1234567"
+ app = "8595f61a-109a-497d-8c8f-566b733e95fe"
+ nearTenant = "3b1f0c2e-9d4a-4c6b-a7e8-5f2d1c0b9a87"
+ farTenant = "c4d5e6f7-1a2b-4c3d-9e8f-0a1b2c3d4e5f"
+ )
+ data := "tenant_id: " + nearTenant + "\nsecret: " + secret + "\n" + strings.Repeat("filler\n", 20) + "tenant_id: " + farTenant + "\n"
+ client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
+ if r.Method == http.MethodGet {
+ if strings.Contains(r.URL.Path, nearTenant) {
+ return nil, errors.New("network down")
+ }
+ return tokenErrorResponse(http.StatusOK, ""), nil
+ }
+ // The app isn't found in the tenant that could be checked, so nothing is learned.
+ return tokenErrorResponse(http.StatusBadRequest, "AADSTS700016: app not found"), nil
+ })}
+ clientIDs := map[string][]int{app: {0}}
+ tenantIDs := map[string][]int{nearTenant: {strings.Index(data, nearTenant)}, farTenant: {strings.Index(data, farTenant)}}
+
+ results := ProcessData(context.Background(), data, map[string]struct{}{secret: {}}, clientIDs, tenantIDs, true, client)
+
+ require.Len(t, results, 1)
+ assert.Contains(t, string(results[0].RawV2), nearTenant)
+}
+
+// An unrelated tenant sits closer to the secret than the real one. A rotated secret is
+// still recognised by the real tenant (secret invalid, not app missing), so the result
+// must match the live one.
+func TestProcessData_RotatedSecretKeepsTenantThatRecognisedTheApp(t *testing.T) {
+ const (
+ secret = "abc4Q~fake-secret-that-is-long-enough1234567"
+ app = "8595f61a-109a-497d-8c8f-566b733e95fe"
+ otherTenant = "d1e2f3a4-5b6c-4d7e-8f90-a1b2c3d4e5f6"
+ realTenant = "e7f8a9b0-2c3d-4e5f-9a1b-6c7d8e9f0a1b"
+ )
+ data := "tenant_id: " + otherTenant + "\nsecret: " + secret + "\n" + strings.Repeat("filler\n", 20) + "tenant_id: " + realTenant + "\n"
+
+ run := func(live bool) detectors.Result {
+ client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
+ if r.Method == http.MethodGet {
+ return tokenErrorResponse(http.StatusOK, ""), nil
+ }
+ switch {
+ case strings.Contains(r.URL.Path, otherTenant):
+ return tokenErrorResponse(http.StatusBadRequest, "AADSTS700016: app not found"), nil
+ case live:
+ rec := httptest.NewRecorder()
+ _ = json.NewEncoder(rec).Encode(map[string]string{"access_token": "x"})
+ return rec.Result(), nil
+ default:
+ return tokenErrorResponse(http.StatusUnauthorized, "AADSTS7000215: Invalid client secret provided."), nil
+ }
+ })}
+ clientIDs := map[string][]int{app: {0}}
+ tenantIDs := map[string][]int{otherTenant: {strings.Index(data, otherTenant)}, realTenant: {strings.Index(data, realTenant)}}
+ results := ProcessData(context.Background(), data, map[string]struct{}{secret: {}}, clientIDs, tenantIDs, true, client)
+ require.Len(t, results, 1)
+ return results[0]
+ }
+
+ live := run(true)
+ rotated := run(false)
+
+ assert.True(t, live.Verified)
+ assert.False(t, rotated.Verified)
+ assert.Contains(t, string(live.RawV2), realTenant)
+ assert.Equal(t, string(live.RawV2), string(rotated.RawV2))
+}
+
+// A tenant lookup that keeps failing must be tried once, not once per secret and client.
+func TestProcessData_FailedTenantLookupIsNotRepeated(t *testing.T) {
+ const (
+ tenant = "9a8b7c6d-5e4f-4a3b-8c2d-1e0f9a8b7c6d"
+ clientA = "8595f61a-109a-497d-8c8f-566b733e95fe"
+ clientB = "d2c1b0a9-8f7e-4d6c-b5a4-3f2e1d0c9b8a"
+ )
+ secrets := map[string]struct{}{
+ "abc4Q~fake-secret-that-is-long-enough1234567": {},
+ "xyz9Q~another-fake-secret-long-enough7654321": {},
+ }
+ data := "tenant_id: " + tenant + "\n"
+ lookups := 0
+ client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
+ lookups++
+ return tokenErrorResponse(http.StatusTooManyRequests, ""), nil
+ })}
+ clientIDs := map[string][]int{clientA: {0}, clientB: {1}}
+ tenantIDs := map[string][]int{tenant: {len("tenant_id: ")}}
+
+ results := ProcessData(context.Background(), data, secrets, clientIDs, tenantIDs, true, client)
+
+ assert.Len(t, results, 2)
+ assert.Equal(t, 1, lookups)
+}