Skip to content

Commit

Permalink
use injected client for timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
rosecodym committed Jul 20, 2023
1 parent 7a7021b commit ad505c0
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 16 deletions.
4 changes: 2 additions & 2 deletions pkg/common/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ func SaneHttpClient() *http.Client {
}

// SaneHttpClientTimeOut adds a custom timeout for some scanners
func SaneHttpClientTimeOut(timeOutSeconds int64) *http.Client {
func SaneHttpClientTimeOut(timeoutMs int64) *http.Client {
httpClient := &http.Client{}
httpClient.Timeout = time.Second * time.Duration(timeOutSeconds)
httpClient.Timeout = time.Millisecond * time.Duration(timeoutMs)
httpClient.Transport = NewCustomTransport(nil)
return httpClient
}
11 changes: 2 additions & 9 deletions pkg/detectors/alchemy/alchemy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ func TestAlchemy_FromChunk(t *testing.T) {
},
{
name: "found, would be verified if not for timeout",
s: Scanner{},
s: Scanner{client: common.SaneHttpClientTimeOut(1)},
args: args{
ctx: timeoutContext(1 * time.Microsecond),
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a alchemy secret %s within", secret)),
verify: true,
},
Expand Down Expand Up @@ -158,10 +158,3 @@ func BenchmarkFromData(benchmark *testing.B) {
})
}
}

func timeoutContext(timeout time.Duration) context.Context {
c, _ := context.WithTimeout(context.Background(), timeout)
// The cancellation function is discarded for test ergonomics - this is expected to be used with a short timeout,
// and it's test code anyway.
return c
}
2 changes: 1 addition & 1 deletion pkg/detectors/getemail/getemail.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Scanner struct{}
var _ detectors.Detector = (*Scanner)(nil)

var (
client = common.SaneHttpClientTimeOut(5)
client = common.SaneHttpClientTimeOut(5000)

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"getemail"}) + `\b([a-zA-Z0-9-]{20})\b`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Scanner struct{}
var _ detectors.Detector = (*Scanner)(nil)

var (
client = common.SaneHttpClientTimeOut(5)
client = common.SaneHttpClientTimeOut(5000)

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(`(https:\/\/[a-zA-Z-0-9]+\.webhook\.office\.com\/webhookb2\/[a-zA-Z-0-9]{8}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{12}\@[a-zA-Z-0-9]{8}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{12}\/IncomingWebhook\/[a-zA-Z-0-9]{32}\/[a-zA-Z-0-9]{8}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{12})`)
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/scrapersite/scrapersite.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Scanner struct{}
var _ detectors.Detector = (*Scanner)(nil)

var (
client = common.SaneHttpClientTimeOut(10)
client = common.SaneHttpClientTimeOut(10000)

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"scrapersite"}) + `\b([a-zA-Z0-9]{45})\b`)
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/screenshotlayer/screenshotlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Scanner struct{}
var _ detectors.Detector = (*Scanner)(nil)

var (
client = common.SaneHttpClientTimeOut(10)
client = common.SaneHttpClientTimeOut(10000)

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"screenshotlayer"}) + `\b([a-zA-Z0-9_]{32})\b`)
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/zenserp/zenserp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Scanner struct{}
var _ detectors.Detector = (*Scanner)(nil)

var (
client = common.SaneHttpClientTimeOut(5)
client = common.SaneHttpClientTimeOut(5000)

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"zenserp"}) + `\b([0-9a-z-]{36})\b`)
Expand Down

0 comments on commit ad505c0

Please sign in to comment.