|
5 | 5 | "errors" |
6 | 6 | "fmt" |
7 | 7 | "io" |
| 8 | + "net/url" |
| 9 | + "regexp" |
8 | 10 | "strings" |
9 | 11 |
|
10 | 12 | "github.com/boostsecurityio/poutine/results" |
@@ -46,11 +48,18 @@ func (f *Format) Format(ctx context.Context, packages []*models.PackageInsights) |
46 | 48 | "purl": pkg.Purl, |
47 | 49 | } |
48 | 50 |
|
| 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 | + } |
49 | 61 | run.AddVersionControlProvenance( |
50 | | - sarif.NewVersionControlDetails(). |
51 | | - WithRepositoryURI(pkg.GetSourceGitRepoURI()). |
52 | | - WithRevisionID(pkg.SourceGitCommitSha). |
53 | | - WithBranch(pkg.SourceGitRef), |
| 62 | + versionControlProvenance, |
54 | 63 | ) |
55 | 64 |
|
56 | 65 | findingsByPurl := make(map[string][]results.Finding) |
@@ -120,3 +129,25 @@ func (f *Format) Format(ctx context.Context, packages []*models.PackageInsights) |
120 | 129 | func (f *Format) FormatWithPath(ctx context.Context, packages []*models.PackageInsights, pathAssociations map[string][]*models.RepoInfo) error { |
121 | 130 | return errors.New("not implemented") |
122 | 131 | } |
| 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 | +} |
0 commit comments