Skip to content

Find, verify, and analyze leaked credentials

Log when a detector ignores the timeout (#3201)

Cody Rose239f35921dad
diff --git a/pkg/engine/ahocorasick/ahocorasickcore.go b/pkg/engine/ahocorasick/ahocorasickcore.go
index d6ce0dd96..9ec2da626 100644
--- a/pkg/engine/ahocorasick/ahocorasickcore.go
+++ b/pkg/engine/ahocorasick/ahocorasickcore.go
@@ -24,6 +24,17 @@ type DetectorKey struct {
customDetectorName string
}
+func (k DetectorKey) Loggable() map[string]any {
+ res := map[string]any{"type": k.detectorType.String()}
+ if k.version > 0 {
+ res["version"] = k.version
+ }
+ if k.customDetectorName != "" {
+ res["name"] = k.customDetectorName
+ }
+ return res
+}
+
// Type returns the detector type of the key.
func (k DetectorKey) Type() detectorspb.DetectorType { return k.detectorType }
diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go
index b96448a22..d1c163852 100644
--- a/pkg/engine/engine.go
+++ b/pkg/engine/engine.go
@@ -29,6 +29,8 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
)
+const detectionTimeout = 10 * time.Second
+
var errOverlap = errors.New(
"More than one detector has found this result. For your safety, verification has been disabled." +
"You can override this behavior by using the --allow-verification-overlap flag.",
@@ -986,6 +988,8 @@ func (e *Engine) detectChunk(ctx context.Context, data detectableChunk) {
}
defer common.Recover(ctx)
+ ctx = context.WithValue(ctx, "detector", data.detector.Key.Loggable())
+
isFalsePositive := detectors.GetFalsePositiveCheck(data.detector)
var matchCount int
@@ -999,14 +1003,15 @@ func (e *Engine) detectChunk(ctx context.Context, data detectableChunk) {
matchCount++
detectBytesPerMatch.Observe(float64(len(matchBytes)))
- ctx, cancel := context.WithTimeout(ctx, time.Second*10)
+ ctx, cancel := context.WithTimeout(ctx, detectionTimeout)
+ t := time.AfterFunc(detectionTimeout+1*time.Second, func() {
+ ctx.Logger().Error(nil, "a detector ignored the context timeout")
+ })
results, err := data.detector.Detector.FromData(ctx, data.chunk.Verify, matchBytes)
+ t.Stop()
cancel()
if err != nil {
- ctx.Logger().Error(
- err, "error finding results in chunk",
- "detector", data.detector.Key.Type().String(),
- )
+ ctx.Logger().Error(err, "error finding results in chunk")
continue
}