Skip to content

Commit

Permalink
[bug] - Ensure detector HTTP clients share the same timeout set at ru…
Browse files Browse the repository at this point in the history
…ntime (#3946)
  • Loading branch information
ahrav authored Feb 27, 2025
1 parent d3640fe commit 7dc056a
Showing 3 changed files with 24 additions and 6 deletions.
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -20,16 +20,15 @@ import (
"github.com/go-logr/logr"
"github.com/jpillora/overseer"
"github.com/mattn/go-isatty"
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/verificationcache"
"go.uber.org/automaxprocs/maxprocs"

"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer"
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple"
"github.com/trufflesecurity/trufflehog/v3/pkg/cleantemp"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/config"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/engine"
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/defaults"
"github.com/trufflesecurity/trufflehog/v3/pkg/feature"
@@ -39,6 +38,7 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
"github.com/trufflesecurity/trufflehog/v3/pkg/tui"
"github.com/trufflesecurity/trufflehog/v3/pkg/updater"
"github.com/trufflesecurity/trufflehog/v3/pkg/verificationcache"
"github.com/trufflesecurity/trufflehog/v3/pkg/version"
)

@@ -446,7 +446,9 @@ func run(state overseer.State) {
}

if *detectorTimeout != 0 {
logger.Info("Setting detector timeout", "timeout", detectorTimeout.String())
engine.SetDetectorTimeout(*detectorTimeout)
detectors.OverrideDetectorTimeout(*detectorTimeout)
}
if *archiveMaxSize != 0 {
handlers.SetArchiveMaxSize(int(*archiveMaxSize))
18 changes: 17 additions & 1 deletion pkg/detectors/http.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import (
"errors"
"net"
"net/http"
"sync"
"time"

"github.com/trufflesecurity/trufflehog/v3/pkg/feature"
@@ -13,7 +14,8 @@ import (
var DetectorHttpClientWithNoLocalAddresses *http.Client
var DetectorHttpClientWithLocalAddresses *http.Client

const DefaultResponseTimeout = 5 * time.Second
// DefaultResponseTimeout is the default timeout for HTTP requests.
const DefaultResponseTimeout = 10 * time.Second

func userAgent() string {
if len(feature.UserAgentSuffix.Load()) > 0 {
@@ -36,6 +38,20 @@ func init() {
)
}

var overrideOnce sync.Once

// OverrideDetectorTimeout overrides the default timeout for the detector HTTP clients.
// It is guaranteed to only run once, subsequent calls will have no effect.
// This should be called before any scans are started.
func OverrideDetectorTimeout(timeout time.Duration) {
overrideOnce.Do(func() {
DetectorHttpClientWithLocalAddresses.Timeout = timeout
DetectorHttpClientWithNoLocalAddresses.Timeout = timeout
})
}



// ClientOption defines a function type that modifies an http.Client.
type ClientOption func(*http.Client)

4 changes: 2 additions & 2 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@ import (
"github.com/adrg/strutil"
"github.com/adrg/strutil/metrics"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/trufflesecurity/trufflehog/v3/pkg/verificationcache"
"google.golang.org/protobuf/proto"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
@@ -29,9 +28,10 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
"github.com/trufflesecurity/trufflehog/v3/pkg/verificationcache"
)

var detectionTimeout = 10 * time.Second
var detectionTimeout = detectors.DefaultResponseTimeout

var errOverlap = errors.New(
"More than one detector has found this result. For your safety, verification has been disabled." +

0 comments on commit 7dc056a

Please sign in to comment.