Skip to content

Modern Docker Management, Designed for Everyone

fix: normalize git repo url, and allow changing of repo url (#2892)

Kyle Mendellc9f061a676d9
diff --git a/backend/pkg/gitutil/git.go b/backend/pkg/gitutil/git.go
index f8c34413b..915d052ff 100644
--- a/backend/pkg/gitutil/git.go
+++ b/backend/pkg/gitutil/git.go
@@ -10,6 +10,7 @@ import (
nethttp "net/http"
"os"
"path/filepath"
+ "regexp"
"slices"
"sort"
"strings"
@@ -20,6 +21,7 @@ import (
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport"
+ "github.com/go-git/go-git/v5/plumbing/transport/client"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/gofrs/flock"
@@ -27,11 +29,50 @@ import (
"golang.org/x/crypto/ssh/knownhosts"
)
+// go-git's file transport execs the git binary, which doesn't exist in the
+// distroless image. Unregister it so a repository URL can never reach it.
+func init() {
+ client.InstallProtocol("file", nil)
+}
+
// Client handles git operations
type Client struct {
workDir string
}
+var scpLikeURLPattern = regexp.MustCompile(`^[^@/]+@[^:/]+:`)
+
+const errUnsupportedURL = "repository URL must use http(s)://, ssh://, git://, or git@host:path"
+
+// normalizeURL coerces a repository URL into a form go-git resolves to a
+// network transport, never the file transport (which execs the git binary).
+// Schemeless host-style URLs (e.g. github.com/org/repo.git) get https://.
+func normalizeURL(raw string) (string, error) {
+ url := strings.TrimSpace(raw)
+ if url == "" {
+ return "", errors.New("repository URL is empty")
+ }
+
+ if scheme, _, found := strings.Cut(url, "://"); found {
+ switch strings.ToLower(scheme) {
+ case "http", "https", "ssh", "git":
+ return url, nil
+ default:
+ return "", errors.New(errUnsupportedURL)
+ }
+ }
+
+ if scpLikeURLPattern.MatchString(url) {
+ return url, nil
+ }
+
+ if strings.HasPrefix(url, "/") || strings.HasPrefix(url, ".") || strings.HasPrefix(url, "~") {
+ return "", errors.New(errUnsupportedURL)
+ }
+
+ return "https://" + url, nil
+}
+
// NewClient creates a new git client
func NewClient(workDir string) *Client {
return &Client{
@@ -254,6 +295,12 @@ func (c *Client) Clone(ctx context.Context, url, branch string, auth AuthConfig)
return "", err
}
+ url, err = normalizeURL(url)
+ if err != nil {
+ _ = os.RemoveAll(tmpDir)
+ return "", err
+ }
+
cloneOptions := &git.CloneOptions{
URL: url,
Progress: nil,
@@ -376,6 +423,11 @@ func (c *Client) listRemoteReferences(ctx context.Context, url string, auth Auth
return nil, err
}
+ url, err = normalizeURL(url)
+ if err != nil {
+ return nil, err
+ }
+
// Create a remote without cloning
rem := git.NewRemote(nil, &config.RemoteConfig{
Name: "origin",
diff --git a/backend/pkg/gitutil/git_test.go b/backend/pkg/gitutil/git_test.go
index b6a3fb940..725d6abe6 100644
--- a/backend/pkg/gitutil/git_test.go
+++ b/backend/pkg/gitutil/git_test.go
@@ -631,3 +631,42 @@ func generateTestPublicKeyVariant(t *testing.T) gossh.PublicKey {
}
return key
}
+
+func TestNormalizeURL(t *testing.T) {
+ cases := []struct {
+ name string
+ in string
+ want string
+ wantErr bool
+ }{
+ {name: "schemeless gets https prefix", in: "github.com/org/repo.git", want: "https://github.com/org/repo.git"},
+ {name: "https kept as-is", in: "https://github.com/org/repo.git", want: "https://github.com/org/repo.git"},
+ {name: "ssh kept as-is", in: "ssh://[email protected]/org/repo.git", want: "ssh://[email protected]/org/repo.git"},
+ {name: "git kept as-is", in: "git://github.com/org/repo.git", want: "git://github.com/org/repo.git"},
+ {name: "scp-like kept as-is", in: "[email protected]:org/repo.git", want: "[email protected]:org/repo.git"},
+ {name: "file scheme rejected", in: "file:///tmp/repo", wantErr: true},
+ {name: "unsupported scheme rejected", in: "ftp://github.com/org/repo.git", wantErr: true},
+ {name: "local path rejected", in: "/tmp/repo", wantErr: true},
+ {name: "relative path rejected", in: "./repo", wantErr: true},
+ {name: "home-relative path rejected", in: "~/repo", wantErr: true},
+ {name: "empty rejected", in: "", wantErr: true},
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ got, err := normalizeURL(tc.in)
+ if tc.wantErr {
+ if err == nil {
+ t.Fatalf("expected error, got %q", got)
+ }
+ return
+ }
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if got != tc.want {
+ t.Errorf("expected %q, got %q", tc.want, got)
+ }
+ })
+ }
+}
diff --git a/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte b/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte
index 623d4a295..7cc1c6adb 100644
--- a/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte
+++ b/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte
@@ -131,7 +131,7 @@
});
$effect(() => {
- if (!open || !syncToEdit || repositories.length === 0) return;
+ if (!open || !syncToEdit || repositories.length === 0 || selectedRepository) return;
const repo = repositories.find((r) => r.id === syncToEdit.repositoryId);
if (repo) {
selectedRepository = { value: repo.id, label: repo.name };