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

Log when a detector ignores the timeout #3201

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/engine/ahocorasick/ahocorasickcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
15 changes: 10 additions & 5 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down
Loading