Skip to content

Find, verify, and analyze leaked credentials

feat(git): include line in github & gitlab links (#1466)

Richard Gomez1594fddf051a
diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.go
index 4c46e25b3..b8be0f50b 100644
--- a/pkg/sources/git/git.go
+++ b/pkg/sources/git/git.go
@@ -10,6 +10,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
+ "strconv"
"strings"
"time"
@@ -637,15 +638,23 @@ func normalizeConfig(scanOptions *ScanOptions, repo *git.Repository) (err error)
}
// GenerateLink crafts a link to the specific file from a commit. This works in most major git providers (Github/Gitlab)
-func GenerateLink(repo, commit, file string) string {
+func GenerateLink(repo, commit, file string, line int64) string {
// bitbucket links are commits not commit...
if strings.Contains(repo, "bitbucket.org/") {
return repo[:len(repo)-4] + "/commits/" + commit
}
- link := repo[:len(repo)-4] + "/blob/" + commit + "/" + file
-
+ var link string
if file == "" {
link = repo[:len(repo)-4] + "/commit/" + commit
+ } else {
+ link = repo[:len(repo)-4] + "/blob/" + commit + "/" + file
+
+ // Both GitHub and Gitlab support hyperlinking to a specific line with #L<number>, e.g.:
+ // https://github.com/trufflesecurity/trufflehog/blob/e856a6890d0da5a218f4f9283500b80043884641/go.mod#L169
+ // https://gitlab.com/pdftk-java/pdftk/-/blob/88559a08f34175b6fae76c40a88f0377f64a12d7/java/com/gitlab/pdftk_java/report.java#L893
+ if line > 0 && (strings.Contains(repo, "github") || strings.Contains(repo, "gitlab")) {
+ link += "#L" + strconv.FormatInt(line, 10)
+ }
}
return link
}
diff --git a/pkg/sources/git/git_test.go b/pkg/sources/git/git_test.go
index 3c00509cd..d8d54adfd 100644
--- a/pkg/sources/git/git_test.go
+++ b/pkg/sources/git/git_test.go
@@ -156,6 +156,7 @@ func Test_generateLink(t *testing.T) {
repo string
commit string
file string
+ line int64
}
tests := []struct {
name string
@@ -171,6 +172,16 @@ func Test_generateLink(t *testing.T) {
},
want: "https://github.com/trufflesec-julian/confluence-go-api/blob/047b4a2ba42fc5b6c0bd535c5307434a666db5ec/.gitignore",
},
+ {
+ name: "test link gen",
+ args: args{
+ repo: "https://github.com/trufflesec-julian/confluence-go-api.git",
+ commit: "047b4a2ba42fc5b6c0bd535c5307434a666db5ec",
+ file: ".gitignore",
+ line: int64(4),
+ },
+ want: "https://github.com/trufflesec-julian/confluence-go-api/blob/047b4a2ba42fc5b6c0bd535c5307434a666db5ec/.gitignore#L4",
+ },
{
name: "test link gen - no file",
args: args{
@@ -182,7 +193,7 @@ func Test_generateLink(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- if got := GenerateLink(tt.args.repo, tt.args.commit, tt.args.file); got != tt.want {
+ if got := GenerateLink(tt.args.repo, tt.args.commit, tt.args.file, tt.args.line); got != tt.want {
t.Errorf("generateLink() = %v, want %v", got, tt.want)
}
})
diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.go
index c7099f142..1ab628319 100644
--- a/pkg/sources/github/github.go
+++ b/pkg/sources/github/github.go
@@ -244,7 +244,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobID, sourceID int64,
File: sanitizer.UTF8(file),
Email: sanitizer.UTF8(email),
Repository: sanitizer.UTF8(repository),
- Link: git.GenerateLink(repository, commit, file),
+ Link: git.GenerateLink(repository, commit, file, line),
Timestamp: sanitizer.UTF8(timestamp),
Line: line,
Visibility: s.visibilityOf(aCtx, repository),
diff --git a/pkg/sources/gitlab/gitlab.go b/pkg/sources/gitlab/gitlab.go
index a32495925..9ce1057ff 100644
--- a/pkg/sources/gitlab/gitlab.go
+++ b/pkg/sources/gitlab/gitlab.go
@@ -127,7 +127,7 @@ func (s *Source) Init(_ context.Context, name string, jobId, sourceId int64, ver
File: sanitizer.UTF8(file),
Email: sanitizer.UTF8(email),
Repository: sanitizer.UTF8(repository),
- Link: git.GenerateLink(repository, commit, file),
+ Link: git.GenerateLink(repository, commit, file, line),
Timestamp: sanitizer.UTF8(timestamp),
Line: line,
},