Skip to content

Commit 8f3b2fd

Browse files
authored
only include a valid git url in sarif output (#365)
1 parent ba025b9 commit 8f3b2fd

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

formatters/sarif/sarif.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"net/url"
9+
"regexp"
810
"strings"
911

1012
"github.com/boostsecurityio/poutine/results"
@@ -46,11 +48,18 @@ func (f *Format) Format(ctx context.Context, packages []*models.PackageInsights)
4648
"purl": pkg.Purl,
4749
}
4850

51+
sourceGitRepoURI := pkg.GetSourceGitRepoURI()
52+
53+
versionControlProvenance := sarif.NewVersionControlDetails().
54+
WithRevisionID(pkg.SourceGitCommitSha).
55+
WithBranch(pkg.SourceGitRef)
56+
57+
if IsValidGitURL(sourceGitRepoURI) {
58+
versionControlProvenance = versionControlProvenance.
59+
WithRepositoryURI(sourceGitRepoURI)
60+
}
4961
run.AddVersionControlProvenance(
50-
sarif.NewVersionControlDetails().
51-
WithRepositoryURI(pkg.GetSourceGitRepoURI()).
52-
WithRevisionID(pkg.SourceGitCommitSha).
53-
WithBranch(pkg.SourceGitRef),
62+
versionControlProvenance,
5463
)
5564

5665
findingsByPurl := make(map[string][]results.Finding)
@@ -120,3 +129,25 @@ func (f *Format) Format(ctx context.Context, packages []*models.PackageInsights)
120129
func (f *Format) FormatWithPath(ctx context.Context, packages []*models.PackageInsights, pathAssociations map[string][]*models.RepoInfo) error {
121130
return errors.New("not implemented")
122131
}
132+
133+
// IsValidGitURL validates if a string is a valid Git URL (HTTP(S) or SSH format)
134+
func IsValidGitURL(gitURL string) bool {
135+
if strings.HasPrefix(gitURL, "http://") || strings.HasPrefix(gitURL, "https://") {
136+
parsedURL, err := url.Parse(gitURL)
137+
if err != nil {
138+
return false
139+
}
140+
return parsedURL.Host != "" && parsedURL.Path != ""
141+
}
142+
143+
if strings.HasPrefix(gitURL, "ssh://") {
144+
parsedURL, err := url.Parse(gitURL)
145+
if err != nil {
146+
return false
147+
}
148+
return parsedURL.Host != "" && parsedURL.Path != ""
149+
}
150+
151+
sshPattern := regexp.MustCompile(`^[a-zA-Z0-9_-]+@[a-zA-Z0-9._-]+:[a-zA-Z0-9/._-]+$`)
152+
return sshPattern.MatchString(gitURL)
153+
}

formatters/sarif/sarif_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package sarif
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestIsValidGitURL(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
gitURL string
13+
isValid bool
14+
}{
15+
{
16+
name: "Valid HTTPS Git URL",
17+
gitURL: "https://github.com/user/repo.git",
18+
isValid: true,
19+
},
20+
{
21+
name: "Valid SSH Git URL",
22+
gitURL: "ssh://[email protected]/user/repo.git",
23+
isValid: true,
24+
},
25+
{
26+
name: "Valid Git URL without .git",
27+
gitURL: "https://gitlab.com/user/repo",
28+
isValid: true,
29+
},
30+
{
31+
name: "Invalid Git URL - missing scheme",
32+
gitURL: "github.com/user/repo.git",
33+
isValid: false,
34+
},
35+
{
36+
name: "Invalid Git URL - empty",
37+
gitURL: "",
38+
isValid: false,
39+
},
40+
}
41+
for _, tt := range tests {
42+
t.Run(tt.name, func(t *testing.T) {
43+
isValid := IsValidGitURL(tt.gitURL)
44+
require.Equal(t, tt.isValid, isValid)
45+
})
46+
}
47+
}

0 commit comments

Comments
 (0)