Skip to content

Commit

Permalink
[bug] - Create a new context with timeout per request (#3163)
Browse files Browse the repository at this point in the history
* Create a new context with timeout per request

* match timeout

* use context timeout

* reduce timeout
  • Loading branch information
ahrav authored Aug 2, 2024
1 parent f939572 commit 0a3451a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
6 changes: 2 additions & 4 deletions pkg/detectors/privatekey/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"net"
"strings"
"time"

"golang.org/x/crypto/ssh"
)
Expand Down Expand Up @@ -35,8 +34,7 @@ func firstResponseFromSSH(ctx context.Context, parsedKey any, username, hostport

// Verify the server fingerprint to ensure that there is no MITM replay attack
config := &ssh.ClientConfig{
Timeout: 5 * time.Second,
User: username,
User: username,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
Expand Down Expand Up @@ -87,7 +85,7 @@ func firstResponseFromSSH(ctx context.Context, parsedKey any, username, hostport
}

func sshDialWithContext(ctx context.Context, network, addr string, config *ssh.ClientConfig) (*ssh.Client, error) {
d := net.Dialer{Timeout: config.Timeout}
d := net.Dialer{}
conn, err := d.DialContext(ctx, network, addr)
if err != nil {
return nil, fmt.Errorf("error dialing %s: %w", addr, err)
Expand Down
17 changes: 11 additions & 6 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,11 +882,15 @@ func (e *Engine) verificationOverlapWorker(ctx context.Context) {
// DO NOT VERIFY at this stage of the pipeline.
matchedBytes := detector.Matches()
for _, match := range matchedBytes {
ctx, cancel := context.WithTimeout(ctx, time.Second*2)
results, err := detector.FromData(ctx, false, match)
ctx.Logger().Error(
err, "error finding results in chunk during verification overlap",
"detector", detector.Key.Type().String(),
)
cancel()
if err != nil {
ctx.Logger().Error(
err, "error finding results in chunk during verification overlap",
"detector", detector.Key.Type().String(),
)
}

if len(results) == 0 {
continue
Expand Down Expand Up @@ -980,9 +984,7 @@ func (e *Engine) detectChunk(ctx context.Context, data detectableChunk) {
if e.printAvgDetectorTime {
start = time.Now()
}
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer common.Recover(ctx)
defer cancel()

isFalsePositive := detectors.GetFalsePositiveCheck(data.detector)

Expand All @@ -996,7 +998,10 @@ func (e *Engine) detectChunk(ctx context.Context, data detectableChunk) {
for _, matchBytes := range matches {
matchCount++
detectBytesPerMatch.Observe(float64(len(matchBytes)))

ctx, cancel := context.WithTimeout(ctx, time.Second*10)
results, err := data.detector.Detector.FromData(ctx, data.chunk.Verify, matchBytes)
cancel()
if err != nil {
ctx.Logger().Error(
err, "error finding results in chunk",
Expand Down

0 comments on commit 0a3451a

Please sign in to comment.