Skip to content
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

Move detectors.IsKnownFalsePositive from the detectors and into the engine #2643

Merged
merged 8 commits into from
Apr 22, 2024
Prev Previous commit
Next Next commit
update filtering
  • Loading branch information
dustin-decker committed Mar 29, 2024
commit 3fce83e01528b220be6d1ed47863c3f8701ddfdb
6 changes: 4 additions & 2 deletions pkg/detectors/dotmailer/dotmailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package dotmailer

import (
"context"
regexp "github.com/wasilibs/go-re2"
"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"
Expand Down Expand Up @@ -54,6 +55,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
s1 := detectors.Result{
DetectorType: detectorspb.DetectorType_Dotmailer,
Raw: []byte(resMatch),
RawV2: []byte(resMatch + resPassMatch),
}
if verify {
timeout := 10 * time.Second
Expand All @@ -68,7 +70,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
defer res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode < 300 {
s1.Verified = true
}
}
}
}
results = append(results, s1)
Expand Down
41 changes: 21 additions & 20 deletions pkg/detectors/falsepositives.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@
var filteredResults []Result
for _, result := range results {
if !result.Verified {
if result.RawV2 != nil {
if StringShannonEntropy(string(result.RawV2)) >= entropy {
filteredResults = append(filteredResults, result)
}
} else {
if result.Raw != nil {
if StringShannonEntropy(string(result.Raw)) >= entropy {
filteredResults = append(filteredResults, result)
}
} else {
filteredResults = append(filteredResults, result)
}
} else {
filteredResults = append(filteredResults, result)
}
}
return filteredResults
Expand All @@ -129,25 +129,26 @@
func FilterKnownFalsePositives(results []Result, falsePositives []FalsePositive, wordCheck bool) []Result {
var filteredResults []Result
for _, result := range results {
switch result.DetectorType {
case detectorspb.DetectorType_CustomRegex:
filteredResults = append(filteredResults, result)
break
case detectorspb.DetectorType_GCP:
filteredResults = append(filteredResults, result)
break
default:
if result.RawV2 != nil {
if !IsKnownFalsePositive(string(result.RawV2), falsePositives, wordCheck) {
filteredResults = append(filteredResults, result)
}
} else {
if !IsKnownFalsePositive(string(result.Raw), falsePositives, wordCheck) {
if !result.Verified {
switch result.DetectorType {
case detectorspb.DetectorType_CustomRegex:
filteredResults = append(filteredResults, result)
break

Check failure on line 136 in pkg/detectors/falsepositives.go

View workflow job for this annotation

GitHub Actions / golangci-lint

S1023: redundant break statement (gosimple)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we breaking for these detector types? My understanding is that the effect will be that when a custom regex (or gcp) detector returns multiple unverified results, we'll ignore all but the first one. Is that intended? And even if it is, is this function the place for it? (It doesn't sound like it has anything to do with false positives.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lint action is actually flagging this as a "redundant break" because it is breaking out of the switch, not the for.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 thanks for correcting me!

case detectorspb.DetectorType_GCP:
filteredResults = append(filteredResults, result)
break

Check failure on line 139 in pkg/detectors/falsepositives.go

View workflow job for this annotation

GitHub Actions / golangci-lint

S1023: redundant break statement (gosimple)
default:
if result.Raw != nil {
if !IsKnownFalsePositive(string(result.Raw), falsePositives, wordCheck) {
filteredResults = append(filteredResults, result)
}
Copy link
Contributor

@rgmz rgmz Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would be useful to trace log that something was filtered? There have been a few times — most recently #2620 — where the cause of something not being detected wasn't immediately obvious, and required custom logging to see that the FP check was responsible.

(Incidentally, there's a lot of noise from this specific log)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rgmz in internal discussions we've actually been leaning towards expanding --results to include these in order to solve that problem. We're not quite there yet but it seems like a potentially good way to go.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that. It would be an intuitive expansion of the flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've extended the results flag with an additional filtered_unverified option. It just logs that it was omitted, it doesn't actually 'notify' on it though.

} else {
filteredResults = append(filteredResults, result)
}
}
} else {
filteredResults = append(filteredResults, result)
}

}
return filteredResults
}
Loading