Skip to content

Commit 17456f8

Browse files
Revert "[INS-397] Fix git version parser panic on non-numeric patch versions …" (#4903)
This reverts commit c120e8c.
1 parent 2b80819 commit 17456f8

9 files changed

Lines changed: 77 additions & 137 deletions

File tree

pkg/detectors/azureapimanagement/repositorykey/repositorykey.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ package repositorykey
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"net/url"
78
"os/exec"
9+
"strconv"
810
"strings"
911

1012
regexp "github.com/wasilibs/go-re2"
1113

1214
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple"
1315
logContext "github.com/trufflesecurity/trufflehog/v3/pkg/context"
1416
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
15-
"github.com/trufflesecurity/trufflehog/v3/pkg/gitcmd"
1617
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb"
1718
)
1819

@@ -99,8 +100,36 @@ func (s Scanner) Description() string {
99100
return "Azure API Management Repository Keys provide access to the API Management (APIM) configuration repository, allowing users to directly interact with and modify API definitions, policies, and settings. These keys enable programmatic access to APIM's Git-based repository, where configurations can be cloned, edited, and pushed back to apply changes. They are primarily used for managing API configurations as code, automating deployments, and synchronizing APIM settings across environments."
100101
}
101102

103+
func gitCmdCheck() error {
104+
if errors.Is(exec.Command("git").Run(), exec.ErrNotFound) {
105+
return fmt.Errorf("'git' command not found in $PATH. Make sure git is installed and included in $PATH")
106+
}
107+
108+
// Check the version is greater than or equal to 2.20.0
109+
out, err := exec.Command("git", "--version").Output()
110+
if err != nil {
111+
return fmt.Errorf("failed to check git version: %w", err)
112+
}
113+
114+
// Extract the version string using a regex to find the version numbers
115+
var regex = regexp.MustCompile(`\d+\.\d+\.\d+`)
116+
117+
versionStr := regex.FindString(string(out))
118+
versionParts := strings.Split(versionStr, ".")
119+
120+
// Parse version numbers
121+
major, _ := strconv.Atoi(versionParts[0])
122+
minor, _ := strconv.Atoi(versionParts[1])
123+
124+
// Compare with version 2.20.0<=x<3.0.0
125+
if major == 2 && minor >= 20 {
126+
return nil
127+
}
128+
return fmt.Errorf("git version is %s, but must be greater than or equal to 2.20.0, and less than 3.0.0", versionStr)
129+
}
130+
102131
func verifyUrlPassword(_ context.Context, repoUrl, user, password string) (bool, error) {
103-
if err := gitcmd.CheckVersion(); err != nil {
132+
if err := gitCmdCheck(); err != nil {
104133
return false, err
105134
}
106135

pkg/gitcmd/gitcmd.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

pkg/gitcmd/gitcmd_test.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

pkg/sources/git/cmd_check.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package git
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"regexp"
7+
"strconv"
8+
"strings"
9+
10+
"github.com/go-errors/errors"
11+
)
12+
13+
// Extract the version string using a regex to find the version numbers
14+
var regex = regexp.MustCompile(`\d+\.\d+\.\d+`)
15+
16+
// CmdCheck checks if git is installed and meets 2.20.0<=x<3.0.0 version requirements.
17+
func CmdCheck() error {
18+
if errors.Is(exec.Command("git").Run(), exec.ErrNotFound) {
19+
return fmt.Errorf("'git' command not found in $PATH. Make sure git is installed and included in $PATH")
20+
}
21+
22+
// Check the version is greater than or equal to 2.20.0
23+
out, err := exec.Command("git", "--version").Output()
24+
if err != nil {
25+
return fmt.Errorf("failed to check git version: %w", err)
26+
}
27+
28+
versionStr := regex.FindString(string(out))
29+
versionParts := strings.Split(versionStr, ".")
30+
31+
// Parse version numbers
32+
major, _ := strconv.Atoi(versionParts[0])
33+
minor, _ := strconv.Atoi(versionParts[1])
34+
35+
// Compare with version 2.20.0<=x<3.0.0
36+
if major == 2 && minor >= 20 {
37+
return nil
38+
}
39+
return fmt.Errorf("git version is %s, but must be greater than or equal to 2.20.0, and less than 3.0.0", versionStr)
40+
}

pkg/sources/git/git.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
3131
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
3232
"github.com/trufflesecurity/trufflehog/v3/pkg/feature"
33-
"github.com/trufflesecurity/trufflehog/v3/pkg/gitcmd"
3433
"github.com/trufflesecurity/trufflehog/v3/pkg/gitparse"
3534
"github.com/trufflesecurity/trufflehog/v3/pkg/handlers"
3635
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
@@ -215,7 +214,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobId sources.JobID, so
215214
concurrency = runtime.NumCPU()
216215
}
217216

218-
if err = gitcmd.CheckVersion(); err != nil {
217+
if err = CmdCheck(); err != nil {
219218
return err
220219
}
221220

@@ -615,7 +614,7 @@ func executeClone(ctx context.Context, params cloneParams) (*git.Repository, err
615614
//
616615
// Pinging using other authentication methods is only unimplemented because there's been no pressing need for it yet.
617616
func PingRepoUsingToken(ctx context.Context, token, gitUrl, user string) error {
618-
if err := gitcmd.CheckVersion(); err != nil {
617+
if err := CmdCheck(); err != nil {
619618
return err
620619
}
621620
lsUrl, err := GitURLParse(gitUrl)

pkg/sources/github/github.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
2929
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
3030
"github.com/trufflesecurity/trufflehog/v3/pkg/feature"
31-
"github.com/trufflesecurity/trufflehog/v3/pkg/gitcmd"
3231
"github.com/trufflesecurity/trufflehog/v3/pkg/giturl"
3332
"github.com/trufflesecurity/trufflehog/v3/pkg/handlers"
3433
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
@@ -214,7 +213,7 @@ func (c *filteredRepoCache) wantRepo(s string) bool {
214213

215214
// Init returns an initialized GitHub source.
216215
func (s *Source) Init(aCtx context.Context, name string, jobID sources.JobID, sourceID sources.SourceID, verify bool, connection *anypb.Any, concurrency int) error {
217-
err := gitcmd.CheckVersion()
216+
err := git.CmdCheck()
218217
if err != nil {
219218
return err
220219
}

pkg/sources/github_experimental/github_experimental.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"google.golang.org/protobuf/types/known/anypb"
1010

1111
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
12-
"github.com/trufflesecurity/trufflehog/v3/pkg/gitcmd"
1312
"github.com/trufflesecurity/trufflehog/v3/pkg/giturl"
1413
"github.com/trufflesecurity/trufflehog/v3/pkg/log"
1514
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
@@ -69,7 +68,7 @@ func (s *Source) JobID() sources.JobID {
6968

7069
// Init returns an initialized GitHubExperimental source.
7170
func (s *Source) Init(aCtx context.Context, name string, jobID sources.JobID, sourceID sources.SourceID, verify bool, connection *anypb.Any, concurrency int) error {
72-
err := gitcmd.CheckVersion()
71+
err := git.CmdCheck()
7372
if err != nil {
7473
return err
7574
}

pkg/sources/gitlab/gitlab.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
1414
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
1515
"github.com/trufflesecurity/trufflehog/v3/pkg/feature"
16-
"github.com/trufflesecurity/trufflehog/v3/pkg/gitcmd"
1716
"github.com/trufflesecurity/trufflehog/v3/pkg/giturl"
1817
"github.com/trufflesecurity/trufflehog/v3/pkg/log"
1918
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
@@ -167,7 +166,7 @@ func (s *Source) Init(ctx context.Context, name string, jobId sources.JobID, sou
167166
s.jobPool = &errgroup.Group{}
168167
s.jobPool.SetLimit(concurrency)
169168

170-
if err := gitcmd.CheckVersion(); err != nil {
169+
if err := git.CmdCheck(); err != nil {
171170
return err
172171
}
173172

pkg/sources/huggingface/huggingface.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple"
2020
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
2121
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
22-
"github.com/trufflesecurity/trufflehog/v3/pkg/gitcmd"
2322
"github.com/trufflesecurity/trufflehog/v3/pkg/giturl"
2423
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
2524
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
@@ -170,7 +169,7 @@ func (c *filteredRepoCache) includeRepo(s string) bool {
170169

171170
// Init returns an initialized HuggingFace source.
172171
func (s *Source) Init(ctx context.Context, name string, jobID sources.JobID, sourceID sources.SourceID, verify bool, connection *anypb.Any, concurrency int) error {
173-
err := gitcmd.CheckVersion()
172+
err := git.CmdCheck()
174173
if err != nil {
175174
return err
176175
}

0 commit comments

Comments
 (0)