Limit diff size to prevent out of control memory use. (#1035)
Lint / lint (push) Waiting to run
Release / Release (push) Waiting to run
Scan for secrets / test (push) Waiting to run
Snifftest / Run Snifftest (push) Waiting to run
Test / test (push) Waiting to run
Test / test-detectors (push) Waiting to run

* Limit diff size to prevent out of control memory use.

* Group consts
This commit is contained in:
Bill Rich
2023-01-23 10:14:10 -08:00
committed by GitHub
parent 2088f030f9
commit ac1dd23d37
2 changed files with 35 additions and 3 deletions
+11 -3
View File
@@ -17,8 +17,13 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
)
// DateFormat is the standard date format for git.
const DateFormat = "Mon Jan 02 15:04:05 2006 -0700"
const (
// DateFormat is the standard date format for git.
DateFormat = "Mon Jan 02 15:04:05 2006 -0700"
// MaxDiffSize is the maximum size for a diff. Larger diffs will be cut off.
MaxDiffSize = 1 * 1024 * 1024 * 1024 // 1GB
)
// Commit contains commit header info and diffs.
type Commit struct {
@@ -222,7 +227,10 @@ func FromReader(ctx context.Context, stdOut io.Reader, commitChan chan Commit) {
}
}
}
if currentDiff.Content.Len() > MaxDiffSize {
log.Debugf("Diff for %s exceeded MaxDiffSize(%d)", currentDiff.PathB, MaxDiffSize)
break
}
}
if currentDiff != nil && currentDiff.Content.Len() > 0 {
currentCommit.Diffs = append(currentCommit.Diffs, *currentDiff)
+24
View File
@@ -181,6 +181,30 @@ func TestMultiCommitContextDiff(t *testing.T) {
}
}
func TestMaxDiffSize(t *testing.T) {
bigBytes := bytes.Buffer{}
bigBytes.WriteString(singleCommitSingleDiff)
for i := 0; i <= MaxDiffSize/1024+10; i++ {
bigBytes.WriteString("+")
for n := 0; n < 1024; n++ {
bigBytes.Write([]byte("0"))
}
bigBytes.WriteString("\n")
}
bigReader := bytes.NewReader(bigBytes.Bytes())
commitChan := make(chan Commit)
go func() {
FromReader(context.TODO(), bigReader, commitChan)
}()
commit := <-commitChan
if commit.Diffs[0].Content.Len() > MaxDiffSize+1024 {
t.Errorf("diff did not match MaxDiffSize. Got: %d, expected (max): %d", commit.Diffs[0].Content.Len(), MaxDiffSize+1024)
}
}
const singleCommitSingleDiff = `commit 70001020fab32b1fcf2f1f0e5c66424eae649826 (HEAD -> master, origin/master, origin/HEAD)
Author: Dustin Decker <[email protected]>
Date: Mon Mar 15 23:27:16 2021 -0700