Have custom detectors now use their own internal HTTP clients instead of a single global HTTP client across all custom detectors

This commit is contained in:
Charlie Gunyon
2026-09-08 14:12:21 +02:00
parent ea33cd04a6
commit 114a2f2b07
2 changed files with 15 additions and 11 deletions
+6 -4
View File
@@ -30,6 +30,7 @@ const maxTotalMatches = 100
// initialization).
type CustomRegexWebhook struct {
*custom_detectorspb.CustomRegex
httpClient *http.Client
}
// Ensure the Scanner satisfies the interface at compile time.
@@ -77,11 +78,12 @@ func NewWebhookCustomRegex(pb *custom_detectorspb.CustomRegex) (*CustomRegexWebh
ensurePrimaryRegexNameSet(pb)
// TODO: Copy only necessary data out of pb.
return &CustomRegexWebhook{pb}, nil
return &CustomRegexWebhook{
CustomRegex: pb,
httpClient: common.SaneHttpClient(),
}, nil
}
var httpClient = common.SaneHttpClient()
func (c *CustomRegexWebhook) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
regexMatches := make(map[string][][]string, len(c.GetRegex()))
@@ -307,7 +309,7 @@ func (c *CustomRegexWebhook) createResults(ctx context.Context, match map[string
if req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", "application/json")
}
resp, err := httpClient.Do(req)
resp, err := c.httpClient.Do(req)
if err != nil {
continue
}
@@ -11,6 +11,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/custom_detectorspb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb"
@@ -137,13 +138,14 @@ func TestCustomDetectorsParsing(t *testing.T) {
func TestFromData_InvalidRegEx(t *testing.T) {
c := &CustomRegexWebhook{
&custom_detectorspb.CustomRegex{
CustomRegex: &custom_detectorspb.CustomRegex{
Name: "Internal bi tool",
Keywords: []string{"secret_v1_", "pat_v2_"},
Regex: map[string]string{
"test": "!!?(?:?)[a-zA-Z0-9]{32}", // invalid regex
},
},
httpClient: common.SaneHttpClient(),
}
_, err := c.FromData(context.Background(), false, []byte("test"))
@@ -832,12 +834,12 @@ func TestVerificationWithConfigurableRanges(t *testing.T) {
t.Parallel()
tests := []struct {
name string
serverStatus int
successRanges []string
rotatedRanges []string
wantVerified bool
wantVerifyErr bool
name string
serverStatus int
successRanges []string
rotatedRanges []string
wantVerified bool
wantVerifyErr bool
}{
{
name: "backward compat: no ranges, 200 -> verified",