-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[CSM-1857] Stabilize Azure detector hash_v2 with deterministic iteration #4846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dipto-truffle
wants to merge
2
commits into
main
Choose a base branch
from
test/csm-1857-hashv2-instability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+221
−17
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
pkg/detectors/azure_entra/serviceprincipal/v2/spv2_determinism_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| package v2 | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestProcessData_RawV2DependsOnIDCount shows that the same Azure client | ||
| // secret produces different RawV2 values depending on how many candidate | ||
| // client/tenant IDs appear in the chunk. | ||
| // | ||
| // With verify=false, ProcessData populates RawV2 only when there is exactly | ||
| // one client ID and exactly one tenant ID. If either set is ambiguous (>1), | ||
| // the IDs are cleared and RawV2 is nil. | ||
| // | ||
| // This is the root cause of CSM-1857's secondary issue: the same logical | ||
| // secret can get different hash_v2 values across scans if the surrounding | ||
| // chunk context changes, producing duplicate secret rows in the database. | ||
| func TestProcessData_RawV2DependsOnIDCount(t *testing.T) { | ||
| const ( | ||
| clientSecret = "abc4Q~fake-secret-that-is-long-enough1234567" | ||
| clientID1 = "a1b2c3d4-e5f6-7890-abcd-ef1234567890" | ||
| clientID2 = "f9e8d7c6-b5a4-3210-fedc-ba9876543210" | ||
| tenantID1 = "11111111-2222-3333-4444-555566667777" | ||
| tenantID2 = "aaaaaaaa-bbbb-cccc-dddd-eeeeffff0000" | ||
| ) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| clientIDs map[string]struct{} | ||
| tenantIDs map[string]struct{} | ||
| wantRawV2 bool | ||
| }{ | ||
| { | ||
| name: "single client and tenant", | ||
| clientIDs: map[string]struct{}{clientID1: {}}, | ||
| tenantIDs: map[string]struct{}{tenantID1: {}}, | ||
| wantRawV2: true, | ||
| }, | ||
| { | ||
| name: "multiple clients clears IDs", | ||
| clientIDs: map[string]struct{}{clientID1: {}, clientID2: {}}, | ||
| tenantIDs: map[string]struct{}{tenantID1: {}}, | ||
| wantRawV2: false, | ||
| }, | ||
| { | ||
| name: "multiple tenants clears IDs", | ||
| clientIDs: map[string]struct{}{clientID1: {}}, | ||
| tenantIDs: map[string]struct{}{tenantID1: {}, tenantID2: {}}, | ||
| wantRawV2: false, | ||
| }, | ||
| { | ||
| name: "multiple clients and tenants clears IDs", | ||
| clientIDs: map[string]struct{}{clientID1: {}, clientID2: {}}, | ||
| tenantIDs: map[string]struct{}{tenantID1: {}, tenantID2: {}}, | ||
| wantRawV2: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| secrets := map[string]struct{}{clientSecret: {}} | ||
| results := ProcessData(context.Background(), secrets, tc.clientIDs, tc.tenantIDs, false, nil) | ||
|
|
||
| require.Len(t, results, 1) | ||
| assert.Equal(t, []byte(clientSecret), results[0].Raw, "Raw should always be the client secret") | ||
|
|
||
| if tc.wantRawV2 { | ||
| assert.NotNil(t, results[0].RawV2, "RawV2 should be populated with unambiguous IDs") | ||
| } else { | ||
| assert.Nil(t, results[0].RawV2, "RawV2 should be nil when IDs are ambiguous") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestProcessData_DeterministicRawV2 verifies that ProcessData produces | ||
| // identical RawV2 on repeated calls with the same inputs. Before the sorted | ||
| // iteration fix, Go map randomization could cause different (clientId, tenantId) | ||
| // pairings across runs. | ||
| func TestProcessData_DeterministicRawV2(t *testing.T) { | ||
| const ( | ||
| clientSecret = "abc4Q~fake-secret-that-is-long-enough1234567" | ||
| clientID = "a1b2c3d4-e5f6-7890-abcd-ef1234567890" | ||
| tenantID = "11111111-2222-3333-4444-555566667777" | ||
| ) | ||
|
|
||
| var firstRawV2 []byte | ||
| for i := 0; i < 50; i++ { | ||
| results := ProcessData( | ||
| context.Background(), | ||
| map[string]struct{}{clientSecret: {}}, | ||
| map[string]struct{}{clientID: {}}, | ||
| map[string]struct{}{tenantID: {}}, | ||
| false, nil, | ||
| ) | ||
| require.Len(t, results, 1) | ||
| if i == 0 { | ||
| firstRawV2 = results[0].RawV2 | ||
| require.NotNil(t, firstRawV2) | ||
| } else { | ||
| assert.Equal(t, firstRawV2, results[0].RawV2, | ||
| "RawV2 must be identical across repeated calls (iteration %d)", i) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TestProcessData_DoesNotMutateCallerMaps verifies that ProcessData does not | ||
| // modify the maps passed by the caller. | ||
| func TestProcessData_DoesNotMutateCallerMaps(t *testing.T) { | ||
| const clientSecret = "abc4Q~fake-secret-that-is-long-enough1234567" | ||
|
|
||
| secrets := map[string]struct{}{clientSecret: {}} | ||
| clientIDs := map[string]struct{}{ | ||
| "a1b2c3d4-e5f6-7890-abcd-ef1234567890": {}, | ||
| "f9e8d7c6-b5a4-3210-fedc-ba9876543210": {}, | ||
| } | ||
| tenantIDs := map[string]struct{}{ | ||
| "11111111-2222-3333-4444-555566667777": {}, | ||
| "aaaaaaaa-bbbb-cccc-dddd-eeeeffff0000": {}, | ||
| } | ||
|
|
||
| origSecretLen := len(secrets) | ||
| origClientLen := len(clientIDs) | ||
| origTenantLen := len(tenantIDs) | ||
|
|
||
| _ = ProcessData(context.Background(), secrets, clientIDs, tenantIDs, false, nil) | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| assert.Len(t, secrets, origSecretLen, "caller's secrets map must not be mutated") | ||
| assert.Len(t, clientIDs, origClientLen, "caller's clientIDs map must not be mutated") | ||
| assert.Len(t, tenantIDs, origTenantLen, "caller's tenantIDs map must not be mutated") | ||
| } | ||
|
|
||
| // TestProcessData_SameSecretDifferentRawV2 demonstrates the chain: | ||
| // the same client secret scanned with different chunk contexts produces | ||
| // different RawV2 bytes depending on whether IDs are ambiguous. | ||
| func TestProcessData_SameSecretDifferentRawV2(t *testing.T) { | ||
| const ( | ||
| clientSecret = "abc4Q~fake-secret-that-is-long-enough1234567" | ||
| clientID = "a1b2c3d4-e5f6-7890-abcd-ef1234567890" | ||
| tenantID = "11111111-2222-3333-4444-555566667777" | ||
| ) | ||
|
|
||
| // Scan 1: chunk has exactly one client ID and one tenant ID. | ||
| results1 := ProcessData( | ||
| context.Background(), | ||
| map[string]struct{}{clientSecret: {}}, | ||
| map[string]struct{}{clientID: {}}, | ||
| map[string]struct{}{tenantID: {}}, | ||
| false, nil, | ||
| ) | ||
| require.Len(t, results1, 1) | ||
| rawV2Populated := results1[0].RawV2 | ||
| require.NotNil(t, rawV2Populated, "scan 1: RawV2 should be populated") | ||
|
|
||
| // Scan 2: same secret, but chunk now contains an extra client-like UUID. | ||
| results2 := ProcessData( | ||
| context.Background(), | ||
| map[string]struct{}{clientSecret: {}}, | ||
| map[string]struct{}{clientID: {}, "f9e8d7c6-b5a4-3210-fedc-ba9876543210": {}}, | ||
| map[string]struct{}{tenantID: {}}, | ||
| false, nil, | ||
| ) | ||
| require.Len(t, results2, 1) | ||
| rawV2Nil := results2[0].RawV2 | ||
| assert.Nil(t, rawV2Nil, "scan 2: RawV2 should be nil due to ambiguous client IDs") | ||
|
|
||
| assert.NotEqual(t, rawV2Populated, rawV2Nil, | ||
| "same logical secret produces different RawV2 depending on chunk context") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might be mistaken, but removing the comments feels a bit aggressive; the PR strips out almost all comments in ProcessData. What do you think?