diff --git a/core/cautils/scaninfo.go b/core/cautils/scaninfo.go index d0103f13..c9c057a3 100644 --- a/core/cautils/scaninfo.go +++ b/core/cautils/scaninfo.go @@ -321,6 +321,9 @@ func (scanInfo *ScanInfo) getScanningContext(input string) ScanningContext { return ContextCluster } + // Check if input is a URL (http:// or https://) + isURL := isHTTPURL(input) + // git url if _, err := giturl.NewGitURL(input); err == nil { if repo, err := CloneGitRepo(&input); err == nil { @@ -331,6 +334,18 @@ func (scanInfo *ScanInfo) getScanningContext(input string) ScanningContext { return ContextGitRemote } } + // If giturl.NewGitURL succeeded but cloning failed, the input is a git URL + // that couldn't be cloned. Don't treat it as a local path. + // The clone error was already logged by CloneGitRepo. + // Return ContextDir to prevent the URL from being joined with the current directory + // and to trigger a "no files found" error with the actual URL (not a mangled path). + return ContextDir + } + + // If it looks like a URL but wasn't recognized as a git URL, still don't treat it as a local path + if isURL { + logger.L().Error("URL provided but not recognized as a valid git repository. Ensure the URL is correct and accessible", helpers.String("url", input)) + return ContextDir } if !filepath.IsAbs(input) { // parse path @@ -456,3 +471,8 @@ func getAbsPath(p string) string { } return p } + +// isHTTPURL checks if the input string is an HTTP or HTTPS URL +func isHTTPURL(input string) bool { + return strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://") +} diff --git a/core/cautils/scaninfo_test.go b/core/cautils/scaninfo_test.go index 3ee367fa..18257af2 100644 --- a/core/cautils/scaninfo_test.go +++ b/core/cautils/scaninfo_test.go @@ -88,6 +88,16 @@ func TestGetScanningContext(t *testing.T) { input: os.TempDir(), want: ContextDir, }, + { + name: "self-hosted GitLab URL that can't be cloned", + input: "https://gitlab.private-domain.com/my-org/my-repo.git", + want: ContextDir, // Should return ContextDir when clone fails, not try to treat as local path + }, + { + name: "http URL that can't be cloned", + input: "http://gitlab.example.com/org/repo", + want: ContextDir, // Should return ContextDir when clone fails, not try to treat as local path + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/go.mod b/go.mod index 9380cf3b..9aad814f 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,7 @@ require ( github.com/json-iterator/go v1.1.12 github.com/jwalton/gchalk v1.3.0 github.com/kubescape/backend v0.0.20 - github.com/kubescape/go-git-url v0.0.30 + github.com/kubescape/go-git-url v0.0.31 github.com/kubescape/go-logger v0.0.25 github.com/kubescape/k8s-interface v0.0.195 github.com/kubescape/opa-utils v0.0.288 diff --git a/go.sum b/go.sum index 52ab9ba8..2ab2b2af 100644 --- a/go.sum +++ b/go.sum @@ -1781,8 +1781,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kubescape/backend v0.0.20 h1:E3nZGqWW8ELSh/n3ZRitlkmuZq33Lyx/42Lm4gpghhM= github.com/kubescape/backend v0.0.20/go.mod h1:FpazfN+c3Ucuvv4jZYCnk99moSBRNMVIxl5aWCZAEBo= -github.com/kubescape/go-git-url v0.0.30 h1:PIbg86ae0ftee/p/Tu/6CA1ju6VoJ51G3sQWNHOm6wg= -github.com/kubescape/go-git-url v0.0.30/go.mod h1:3ddc1HEflms1vMhD9owt/3FBES070UaYTUarcjx8jDk= +github.com/kubescape/go-git-url v0.0.31 h1:VZnvtdGLVc42cQaR7llQeGZz0PnOxcs+eDig2Q9PJss= +github.com/kubescape/go-git-url v0.0.31/go.mod h1:3ddc1HEflms1vMhD9owt/3FBES070UaYTUarcjx8jDk= github.com/kubescape/go-logger v0.0.25 h1:Bi6F0856LOlvjrbSKD+ZtKKzbfRXDifhVCjK8s3kI6U= github.com/kubescape/go-logger v0.0.25/go.mod h1:lk+R5/lAVJo4AgD4eYUJJfVTHf7ZChS73X1MFFbeInY= github.com/kubescape/k8s-interface v0.0.195 h1:pJ1PT3x3fd1WatLjyZbKAfE64PWtEbvxiFjOBKSBwuU=