Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
ahrav committed Jan 31, 2024
1 parent 1361e85 commit 5f4f6fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
13 changes: 9 additions & 4 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,16 +595,21 @@ func likelyDuplicate(ctx context.Context, val []byte, dupes map[string]struct{})
if _, ok := dupes[string(val)]; ok {
return true
}

// The string conversion is purposefully placed after the dupes check to avoid the allocation.
// []byte -> string conversion within a map lookup does not allocate. (due to compiler optimizations)
valStr := string(val)
const similarityThreshold = 0.9
for k := range dupes {
// Avoid comparing strings of vastly different lengths.
if len(k)*10 < len(val)*9 || len(k)*10 > len(val)*11 {
if len(k)*10 < len(valStr)*9 || len(k)*10 > len(valStr)*11 {
continue
}

similarity := strutil.Similarity(string(val), k, metrics.NewLevenshtein())
similarity := strutil.Similarity(valStr, k, metrics.NewLevenshtein())

// close enough
if similarity > 0.9 {
if similarity > similarityThreshold {
ctx.Logger().V(2).Info("found similar duplicate", "val", val, "k", k, "similarity", similarity)
return true
}
Expand Down Expand Up @@ -660,7 +665,7 @@ func (e *Engine) reverifierWorker(ctx context.Context) {
wgDoneFn: wgDetect.Done,
}, res)

// Remove the detector from the map as we have already processed the result.
// Remove the detector and secret from the maps
delete(detectorsWithResult, detector)
}
chunkSecrets[string(val)] = struct{}{}
Expand Down
5 changes: 3 additions & 2 deletions pkg/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func TestReverifcationChunk(t *testing.T) {
WithConcurrency(1),
WithDecoders(decoders.DefaultDecoders()...),
WithDetectors(conf.Detectors...),
WithVerify(true),
WithVerify(false),
WithPrinter(new(discardPrinter)),
withReverificationTracking(),
)
Expand All @@ -246,7 +246,8 @@ func TestReverifcationChunk(t *testing.T) {

// Wait for all the chunks to be processed.
assert.Nil(t, e.Finish(ctx))
want := uint64(1)
// We want TWO secrets that match both the custom regexes.
want := uint64(2)
assert.Equal(t, want, e.GetMetrics().UnverifiedSecretsFound)

wantDupe := 1
Expand Down

0 comments on commit 5f4f6fa

Please sign in to comment.