Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
add tests (#785)
Bill Rich3fe916fe1e4e
diff --git a/pkg/gitparse/gitparse.go b/pkg/gitparse/gitparse.goindex 939b28165..0f41bf1fc 100644--- a/pkg/gitparse/gitparse.go+++ b/pkg/gitparse/gitparse.go@@ -37,6 +37,38 @@ type Diff struct {IsBinary bool}+// Equal compares the content of two Commits to determine if they are the same.+func (c1 *Commit) Equal(c2 *Commit) bool {+ switch {+ case c1.Hash != c2.Hash:+ return false+ case c1.Author != c2.Author:+ return false+ case !c1.Date.Equal(c2.Date):+ return false+ case c1.Message.String() != c2.Message.String():+ return false+ case len(c1.Diffs) != len(c2.Diffs):+ return false+ }+ for i := range c1.Diffs {+ d1 := c1.Diffs[i]+ d2 := c2.Diffs[i]+ switch {+ case d1.PathB != d2.PathB:+ return false+ case d1.LineStart != d2.LineStart:+ return false+ case d1.Content.String() != d2.Content.String():+ return false+ case d1.IsBinary != d2.IsBinary:+ return false+ }+ }+ return true++}+// RepoPath parses the output of the `git log` command for the `source` path.func RepoPath(ctx context.Context, source string, head string) (chan Commit, error) {args := []string{"-C", source, "log", "-p", "-U0", "--full-history", "--diff-filter=AM", "--date=format:%a %b %d %H:%M:%S %Y %z"}@@ -136,7 +168,7 @@ func FromReader(ctx context.Context, stdOut io.Reader, commitChan chan Commit) {currentCommit.Hash = string(line[7:47])}case isAuthorLine(line):- currentCommit.Author = string(line[8:])+ currentCommit.Author = strings.TrimRight(string(line[8:]), "\n")case isDateLine(line):date, err := time.Parse(DateFormat, strings.TrimSpace(string(line[6:])))if err != nil {diff --git a/pkg/gitparse/gitparse_test.go b/pkg/gitparse/gitparse_test.goindex 827299e8f..ff91b7c85 100644--- a/pkg/gitparse/gitparse_test.go+++ b/pkg/gitparse/gitparse_test.go@@ -1,7 +1,12 @@package gitparseimport (+ "bytes"+ "strings""testing"+ "time"++ "github.com/trufflesecurity/trufflehog/v3/pkg/context")type testCase struct {@@ -78,3 +83,205 @@ func TestBinaryPathParse(t *testing.T) {}}++func TestSingleCommitSingleDiff(t *testing.T) {+ r := bytes.NewReader([]byte(singleCommitSingleDiff))+ commitChan := make(chan Commit)+ date, _ := time.Parse(DateFormat, "Mon Mar 15 23:27:16 2021 -0700")+ content := bytes.NewBuffer([]byte(singleCommitSingleDiffDiff))+ builder := strings.Builder{}+ builder.Write([]byte(singleCommitSingleDiffMessage))+ expected := []Commit{+ {+ Hash: "70001020fab32b1fcf2f1f0e5c66424eae649826",+ Author: "Dustin Decker <[email protected]>",+ Date: date,+ Message: builder,+ Diffs: []Diff{+ {+ PathB: "aws",+ LineStart: 1,+ Content: *content,+ IsBinary: false,+ },+ },+ },+ }+ go func() {+ FromReader(context.TODO(), r, commitChan)+ }()+ i := 0+ for commit := range commitChan {+ if len(expected) < i {+ t.Errorf("Commit does not match. Wrong number of commits.")+ }++ if !commit.Equal(&expected[i]) {+ t.Errorf("Commit does not match. Got: %v, expected: %v", commit, expected)+ }+ i+++ }+}++func TestMultiCommitBrokenDiff(t *testing.T) {+ r := bytes.NewReader([]byte(singleCommitBrokenDiff))+ commitChan := make(chan Commit)+ dateOne, _ := time.Parse(DateFormat, "Mon Mar 15 23:27:16 2021 -0700")+ dateTwo, _ := time.Parse(DateFormat, "Wed Dec 12 18:19:21 2018 -0800")+ diffOneA := bytes.NewBuffer([]byte(singleCommitBrokenDiffDiffOneA))+ diffTwoA := bytes.NewBuffer([]byte(singleCommitBrokenDiffDiffTwoA))+ diffTwoB := bytes.NewBuffer([]byte(singleCommitBrokenDiffDiffTwoB))+ messageOne := strings.Builder{}+ messageOne.Write([]byte(singleCommitBrokenDiffMessageOne))+ messageTwo := strings.Builder{}+ messageTwo.Write([]byte(singleCommitBrokenDiffMessageTwo))+ expected := []Commit{+ {+ Hash: "70001020fab32b1fcf2f1f0e5c66424eae649826",+ Author: "Dustin Decker <[email protected]>",+ Date: dateOne,+ Message: messageOne,+ Diffs: []Diff{+ {+ PathB: "aws",+ LineStart: 1,+ Content: *diffOneA,+ IsBinary: false,+ },+ },+ },+ {+ Hash: "84e9c75e388ae3e866e121087ea2dd45a71068f2",+ Author: "Dylan Ayrey <[email protected]>",+ Date: dateTwo,+ Message: messageTwo,+ Diffs: []Diff{+ {+ PathB: "aws",+ LineStart: 3,+ Content: *diffTwoA,+ IsBinary: false,+ },+ {+ PathB: "aws",+ LineStart: 7,+ Content: *diffTwoB,+ IsBinary: false,+ },+ },+ },+ }+ go func() {+ FromReader(context.TODO(), r, commitChan)+ }()+ i := 0+ for commit := range commitChan {+ if len(expected) < i {+ t.Errorf("Commit does not match. Wrong number of commits.")+ }++ if !commit.Equal(&expected[i]) {+ t.Errorf("Commit does not match. Got: %v, expected: %v", commit, expected)+ }+ i+++ }+}++const singleCommitSingleDiff = `commit 70001020fab32b1fcf2f1f0e5c66424eae649826 (HEAD -> master, origin/master, origin/HEAD)+Author: Dustin Decker <[email protected]>+Date: Mon Mar 15 23:27:16 2021 -0700++ Update aws++diff --git a/aws b/aws+index 2ee133b..12b4843 100644+--- a/aws++++ b/aws+@@ -1,7 +1,5 @@+-blah blaj+-+-this is the secret: [Default]+-Access key Id: AKIAILE3JG6KMS3HZGCA+-Secret Access Key: 6GKmgiS3EyIBJbeSp7sQ+0PoJrPZjPUg8SF6zYz7+-+-okay thank you bye++[default]++aws_access_key_id = AKIAXYZDQCEN4B6JSJQI++aws_secret_access_key = Tg0pz8Jii8hkLx4+PnUisM8GmKs3a2DK+9qz/lie++output = json++region = us-east-2+`+const singleCommitSingleDiffMessage = `Update aws+`++const singleCommitSingleDiffDiff = `[default]+aws_access_key_id = AKIAXYZDQCEN4B6JSJQI+aws_secret_access_key = Tg0pz8Jii8hkLx4+PnUisM8GmKs3a2DK+9qz/lie+output = json+region = us-east-2+`+const singleCommitBrokenDiff = `commit 70001020fab32b1fcf2f1f0e5c66424eae649826 (HEAD -> master, origin/master, origin/HEAD)+Author: Dustin Decker <[email protected]>+Date: Mon Mar 15 23:27:16 2021 -0700++ Update aws++diff --git a/aws b/aws+index 2ee133b..12b4843 100644+--- a/aws++++ b/aws+@@ -1,7 +1,5 @@+-blah blaj+-+-this is the secret: [Default]+-Access key Id: AKIAILE3JG6KMS3HZGCA+-Secret Access Key: 6GKmgiS3EyIBJbeSp7sQ+0PoJrPZjPUg8SF6zYz7+-+-okay thank you bye++[default]++aws_access_key_id = AKIAXYZDQCEN4B6JSJQI++aws_secret_access_key = Tg0pz8Jii8hkLx4+PnUisM8GmKs3a2DK+9qz/lie++output = json++region = us-east-2++commit 84e9c75e388ae3e866e121087ea2dd45a71068f2+Author: Dylan Ayrey <[email protected]>+Date: Wed Dec 12 18:19:21 2018 -0800++ Update aws again++diff --git a/aws b/aws+index 239b415..2ee133b 100644+--- a/aws++++ b/aws+@@ -3 +3,3 @@ blah blaj+-this is the secret: AKIA2E0A8F3B244C9986++this is the secret: [Default]++Access key Id: AKIAILE3JG6KMS3HZGCA++Secret Access Key: 6GKmgiS3EyIBJbeSp7sQ+0PoJrPZjPUg8SF6zYz7+@@ -5 +7 @@ this is the secret: AKIA2E0A8F3B244C9986+-okay thank you bye+\ No newline at end of file++okay thank you bye+`++const singleCommitBrokenDiffMessageOne = `Update aws+`++const singleCommitBrokenDiffMessageTwo = `Update aws again+`++const singleCommitBrokenDiffDiffOneA = `[default]+aws_access_key_id = AKIAXYZDQCEN4B6JSJQI+aws_secret_access_key = Tg0pz8Jii8hkLx4+PnUisM8GmKs3a2DK+9qz/lie+output = json+region = us-east-2+`++const singleCommitBrokenDiffDiffTwoA = `this is the secret: [Default]+Access key Id: AKIAILE3JG6KMS3HZGCA+Secret Access Key: 6GKmgiS3EyIBJbeSp7sQ+0PoJrPZjPUg8SF6zYz7+`++const singleCommitBrokenDiffDiffTwoB = `okay thank you bye+`