Skip to content

Commit 7dc056a

Browse files
authored
[bug] - Ensure detector HTTP clients share the same timeout set at runtime (#3946)
1 parent d3640fe commit 7dc056a

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ import (
2020
"github.com/go-logr/logr"
2121
"github.com/jpillora/overseer"
2222
"github.com/mattn/go-isatty"
23-
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple"
24-
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
25-
"github.com/trufflesecurity/trufflehog/v3/pkg/verificationcache"
2623
"go.uber.org/automaxprocs/maxprocs"
2724

2825
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer"
26+
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple"
2927
"github.com/trufflesecurity/trufflehog/v3/pkg/cleantemp"
3028
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
3129
"github.com/trufflesecurity/trufflehog/v3/pkg/config"
3230
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
31+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
3332
"github.com/trufflesecurity/trufflehog/v3/pkg/engine"
3433
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/defaults"
3534
"github.com/trufflesecurity/trufflehog/v3/pkg/feature"
@@ -39,6 +38,7 @@ import (
3938
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
4039
"github.com/trufflesecurity/trufflehog/v3/pkg/tui"
4140
"github.com/trufflesecurity/trufflehog/v3/pkg/updater"
41+
"github.com/trufflesecurity/trufflehog/v3/pkg/verificationcache"
4242
"github.com/trufflesecurity/trufflehog/v3/pkg/version"
4343
)
4444

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

448448
if *detectorTimeout != 0 {
449+
logger.Info("Setting detector timeout", "timeout", detectorTimeout.String())
449450
engine.SetDetectorTimeout(*detectorTimeout)
451+
detectors.OverrideDetectorTimeout(*detectorTimeout)
450452
}
451453
if *archiveMaxSize != 0 {
452454
handlers.SetArchiveMaxSize(int(*archiveMaxSize))

pkg/detectors/http.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"net"
77
"net/http"
8+
"sync"
89
"time"
910

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

16-
const DefaultResponseTimeout = 5 * time.Second
17+
// DefaultResponseTimeout is the default timeout for HTTP requests.
18+
const DefaultResponseTimeout = 10 * time.Second
1719

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

41+
var overrideOnce sync.Once
42+
43+
// OverrideDetectorTimeout overrides the default timeout for the detector HTTP clients.
44+
// It is guaranteed to only run once, subsequent calls will have no effect.
45+
// This should be called before any scans are started.
46+
func OverrideDetectorTimeout(timeout time.Duration) {
47+
overrideOnce.Do(func() {
48+
DetectorHttpClientWithLocalAddresses.Timeout = timeout
49+
DetectorHttpClientWithNoLocalAddresses.Timeout = timeout
50+
})
51+
}
52+
53+
54+
3955
// ClientOption defines a function type that modifies an http.Client.
4056
type ClientOption func(*http.Client)
4157

pkg/engine/engine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/adrg/strutil"
1414
"github.com/adrg/strutil/metrics"
1515
lru "github.com/hashicorp/golang-lru/v2"
16-
"github.com/trufflesecurity/trufflehog/v3/pkg/verificationcache"
1716
"google.golang.org/protobuf/proto"
1817

1918
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
@@ -29,9 +28,10 @@ import (
2928
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
3029
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
3130
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
31+
"github.com/trufflesecurity/trufflehog/v3/pkg/verificationcache"
3232
)
3333

34-
var detectionTimeout = 10 * time.Second
34+
var detectionTimeout = detectors.DefaultResponseTimeout
3535

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

0 commit comments

Comments
 (0)