-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
legacy_json.go
224 lines (199 loc) · 7.12 KB
/
legacy_json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package output
import (
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"strings"
gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/sirupsen/logrus"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/git"
)
func PrintLegacyJSON(ctx context.Context, r *detectors.ResultWithMetadata) {
var repo string
switch r.SourceType {
case sourcespb.SourceType_SOURCE_TYPE_GIT:
repo = r.SourceMetadata.GetGit().Repository
case sourcespb.SourceType_SOURCE_TYPE_GITHUB:
repo = r.SourceMetadata.GetGithub().Repository
case sourcespb.SourceType_SOURCE_TYPE_GITLAB:
repo = r.SourceMetadata.GetGitlab().Repository
default:
logrus.Errorf("unsupported source type for legacy json output: %s", r.SourceType)
}
// cloning the repo again here is not great and only works with unauthed repos
repoPath, remote, err := git.PrepareRepo(ctx, repo)
if err != nil || repoPath == "" {
logrus.WithError(err).Fatal("error preparing git repo for scanning")
}
if remote {
defer os.RemoveAll(repoPath)
}
legacy := ConvertToLegacyJSON(r, repoPath)
out, err := json.Marshal(legacy)
if err != nil {
logrus.WithError(err).Fatal("could not marshal result")
}
fmt.Println(string(out))
}
func ConvertToLegacyJSON(r *detectors.ResultWithMetadata, repoPath string) *LegacyJSONOutput {
var source LegacyJSONCompatibleSource
switch r.SourceType {
case sourcespb.SourceType_SOURCE_TYPE_GIT:
source = r.SourceMetadata.GetGit()
case sourcespb.SourceType_SOURCE_TYPE_GITHUB:
source = r.SourceMetadata.GetGithub()
case sourcespb.SourceType_SOURCE_TYPE_GITLAB:
source = r.SourceMetadata.GetGitlab()
default:
log.Fatalf("legacy JSON output can not be used with this source: %s", r.SourceName)
}
// The repo will be needed to gather info needed for the legacy output that isn't included in the new
// output format.
repo, err := gogit.PlainOpenWithOptions(repoPath, &gogit.PlainOpenOptions{DetectDotGit: true})
if err != nil {
logrus.WithError(err).Fatalf("could not open repo: %s", repoPath)
}
fileName := source.GetFile()
commitHash := plumbing.NewHash(source.GetCommit())
commit, err := repo.CommitObject(commitHash)
if err != nil {
log.Fatal(err)
}
diff := GenerateDiff(commit, fileName)
foundString := string(r.Result.Raw)
// Add highlighting to the offending bit of string.
printableDiff := strings.ReplaceAll(diff, foundString, fmt.Sprintf("\u001b[93m%s\u001b[0m", foundString))
// Load up the struct to match the old JSON format
output := &LegacyJSONOutput{
Branch: FindBranch(commit, repo),
Commit: commit.Message,
CommitHash: commitHash.String(),
Date: commit.Committer.When.Format("2006-01-02 15:04:05"),
Diff: diff,
Path: fileName,
PrintDiff: printableDiff,
Reason: r.Result.DetectorType.String(),
StringsFound: []string{foundString},
}
return output
}
// BranchHeads creates a map of branch names to their head commit. This can be used to find if a commit is an ancestor
// of a branch head.
func BranchHeads(repo *gogit.Repository) (map[string]*object.Commit, error) {
branches := map[string]*object.Commit{}
branchIter, err := repo.Branches()
if err != nil {
return branches, err
}
err = branchIter.ForEach(func(branchRef *plumbing.Reference) error {
branchName := branchRef.Name().String()
headHash, err := repo.ResolveRevision(plumbing.Revision(branchName))
if err != nil {
logrus.WithError(err).Errorf("unable to resolve head of branch: %s", branchRef.Name().String())
return nil
}
headCommit, err := repo.CommitObject(*headHash)
if err != nil {
logrus.WithError(err).Errorf("unable to get commit: %s", headCommit.String())
return nil
}
branches[branchName] = headCommit
return nil
})
return branches, err
}
// FindBranch returns the first branch a commit is a part of. Not the most accurate, but it should work similar to pre v3.0.
func FindBranch(commit *object.Commit, repo *gogit.Repository) string {
branches, err := BranchHeads(repo)
if err != nil {
logrus.WithError(err).Fatal("could not list branches")
}
for name, head := range branches {
isAncestor, err := commit.IsAncestor(head)
if err != nil {
logrus.WithError(err).Errorf("could not determine if %s is an ancestor of %s", commit.Hash.String(), head.Hash.String())
continue
}
if isAncestor {
return name
}
}
return ""
}
// GenerateDiff will take a commit and create a string diff between the commit and its first parent.
func GenerateDiff(commit *object.Commit, fileName string) string {
var diff string
// First grab the first parent of the commit. If there are none, we are at the first commit and should diff against
// an empty file.
parent, err := commit.Parent(0)
if err != object.ErrParentNotFound && err != nil {
logrus.WithError(err).Errorf("could not find parent of %s", commit.Hash.String())
}
// Now get the files from the commit and its parent.
var parentFile *object.File
if parent != nil {
parentFile, err = parent.File(fileName)
if err != nil && err != object.ErrFileNotFound {
logrus.WithError(err).Errorf("could not get previous version of file: %q", fileName)
return diff
}
}
commitFile, err := commit.File(fileName)
if err != nil {
logrus.WithError(err).Errorf("could not get current version of file: %q", fileName)
return diff
}
// go-git doesn't support creating a diff for just one file in a commit, so another package is needed to generate
// the diff.
dmp := diffmatchpatch.New()
var oldContent, newContent string
if parentFile != nil {
oldContent, err = parentFile.Contents()
if err != nil {
logrus.WithError(err).Errorf("could not get contents of previous version of file: %q", fileName)
}
}
// commitFile should never be nil at this point, but double-checking so we don't get a nil error.
if commitFile != nil {
newContent, _ = commitFile.Contents()
if err != nil {
logrus.WithError(err).Errorf("could not get contents of current version of file: %q", fileName)
}
}
// If anything has gone wrong here, we'll just be diffing two empty files.
diffs := dmp.DiffMain(oldContent, newContent, false)
patches := dmp.PatchMake(diffs)
// Put all the pieces of the diff together into one string.
for _, patch := range patches {
// The String() method URL escapes the diff, so it needs to be undone.
patchDiff, err := url.QueryUnescape(patch.String())
if err != nil {
logrus.WithError(err).Error("unable to unescape diff")
}
diff += patchDiff
}
return diff
}
type LegacyJSONOutput struct {
Branch string `json:"branch"`
Commit string `json:"commit"`
CommitHash string `json:"commitHash"`
Date string `json:"date"`
Diff string `json:"diff"`
Path string `json:"path"`
PrintDiff string `json:"printDiff"`
Reason string `json:"reason"`
StringsFound []string `json:"stringsFound"`
}
type LegacyJSONCompatibleSource interface {
GetCommit() string
GetFile() string
}