diff --git a/.github/scripts/embedded-cmd-test-shards.txt b/.github/scripts/embedded-cmd-test-shards.txt index 3157db001..fe342055e 100644 --- a/.github/scripts/embedded-cmd-test-shards.txt +++ b/.github/scripts/embedded-cmd-test-shards.txt @@ -145,6 +145,7 @@ 100 60 TestEmbeddedCommentsCLI 100 60 TestEmbeddedRoutedWriteMissDoesNotCreateTargetDatabase +100 61 TestEmbeddedBootstrapCloneFailureScrubsCredentials 100 61 TestEmbeddedContributorRoutedWritesCommitInPlanning 100 61 TestEmbeddedUpdateRoutedStoreCommitsTargetHead diff --git a/cmd/bd/bootstrap.go b/cmd/bd/bootstrap.go index 25af0a9d4..1fa9c3cff 100644 --- a/cmd/bd/bootstrap.go +++ b/cmd/bd/bootstrap.go @@ -274,7 +274,11 @@ Examples: } if err := executeBootstrapPlan(plan, cfg, nonInteractive); err != nil { - return HandleError("Bootstrap failed: %v", err) + // The error can carry sync.remote verbatim, credentials and all: + // sanitizeURL passes through a URL net/url cannot parse (a + // password with an unencoded "#" or "?"), and Dolt's "invalid + // remote url:" repeats it. Scrub it where it is printed. + return HandleError("Bootstrap failed: %s", scrubURLCredentials(err.Error())) } return nil }, @@ -1656,14 +1660,21 @@ func isGitCodeRepoURL(rawURL string) bool { // urlHostname extracts the lowercase hostname from a URL without importing // net/url. Handles scheme://[user@]host[:port]/path and SCP git@host:path. +// +// The host part is cut out before userinfo is stripped through its last "@", +// so an "@" in a path, query or fragment never selects the host (mc-gcmer). +// For a scheme URL the host part is the authority, which ends at the first +// "/", "?" or "#" — the host net/url, and so Dolt, would dial. For SCP it is +// everything before the first ":", unless a "/" comes first, which makes it a +// local path (the split doltremote.FromGitURL makes). func urlHostname(rawURL string) string { if sep := strings.Index(rawURL, "://"); sep >= 0 { rest := rawURL[sep+3:] - if at := strings.Index(rest, "@"); at >= 0 { - rest = rest[at+1:] + if end := strings.IndexAny(rest, "/?#"); end >= 0 { + rest = rest[:end] } - if slash := strings.Index(rest, "/"); slash >= 0 { - rest = rest[:slash] + if at := strings.LastIndex(rest, "@"); at >= 0 { + rest = rest[at+1:] } if colon := strings.Index(rest, ":"); colon >= 0 { rest = rest[:colon] @@ -1671,10 +1682,9 @@ func urlHostname(rawURL string) string { return strings.ToLower(rest) } // SCP-style: git@github.com:org/repo - if at := strings.Index(rawURL, "@"); at >= 0 { - rest := rawURL[at+1:] - if colon := strings.Index(rest, ":"); colon >= 0 { - return strings.ToLower(rest[:colon]) + if colon := strings.Index(rawURL, ":"); colon >= 0 && !strings.Contains(rawURL[:colon], "/") { + if at := strings.LastIndex(rawURL[:colon], "@"); at >= 0 { + return strings.ToLower(rawURL[at+1 : colon]) } } return "" diff --git a/cmd/bd/bootstrap_embedded_test.go b/cmd/bd/bootstrap_embedded_test.go index e4f32a01d..39f42637f 100644 --- a/cmd/bd/bootstrap_embedded_test.go +++ b/cmd/bd/bootstrap_embedded_test.go @@ -295,3 +295,49 @@ func TestEmbeddedBootstrapConcurrent(t *testing.T) { } } } + +// TestEmbeddedBootstrapCloneFailureScrubsCredentials pins the credential scrub +// at the "Bootstrap failed:" print site (mc-gcmer). An unencoded "#" or "?" in +// the password ends the URL's authority early, so the remote is not a forge +// and is cloned as configured. The clone error then carries the password +// twice: sanitizeURL passes a URL net/url cannot parse through unchanged, and +// Dolt's "invalid remote url:" repeats it. +func TestEmbeddedBootstrapCloneFailureScrubsCredentials(t *testing.T) { + if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { + t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests") + } + t.Parallel() + + bd := buildEmbeddedBD(t) + + for _, tc := range []struct{ name, password string }{ + {"hash", "pa#ss"}, + {"question_mark", "pa?ss"}, + } { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + initGitRepoAt(t, dir) + beadsDir := filepath.Join(dir, ".beads") + if err := os.MkdirAll(beadsDir, 0o750); err != nil { + t.Fatalf("mkdir .beads: %v", err) + } + if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte("{}"), 0o644); err != nil { + t.Fatalf("write metadata.json: %v", err) + } + seedSyncRemote(t, beadsDir, "https://alice:"+tc.password+"@github.com/org/db") + + cmd := exec.Command(bd, "bootstrap", "--yes") + cmd.Dir = dir + cmd.Env = bdEnv(dir) + stdout, stderr, err := runCommandBuffers(t, cmd) + // A failure before the clone would pass the leak check below + // without reaching the print site. + if err == nil || !strings.Contains(stderr.String(), "Bootstrap failed: clone from remote:") { + t.Fatalf("bootstrap should fail at the clone, got err=%v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String()) + } + if strings.Contains(stdout.String()+stderr.String(), tc.password) { + t.Errorf("bootstrap printed the sync.remote password %q:\nstdout:\n%s\nstderr:\n%s", tc.password, stdout.String(), stderr.String()) + } + }) + } +} diff --git a/cmd/bd/bootstrap_guard_test.go b/cmd/bd/bootstrap_guard_test.go index 40df292a3..a128ebd44 100644 --- a/cmd/bd/bootstrap_guard_test.go +++ b/cmd/bd/bootstrap_guard_test.go @@ -60,6 +60,21 @@ func TestIsGitCodeRepoURL(t *testing.T) { // SCP-style to non-forge — allowed {"git@my-dolt.example.com:org/db", false}, + // Userinfo before a forge host is stripped, in either URL form + {"ssh://git@github.com/org/db", true}, + {"https://x-access-token:secret@github.com/org/db", true}, + {"git@github.com:org/db", true}, + // ...through the last "@" of the host part, as net/url (and so Dolt) reads it + {"https://a@b@github.com/org/db", true}, + {"a@b@github.com:org/db", true}, + + // An "@" outside the host part never selects the host (mc-gcmer) + {"https://my-dolt.example.com/x@github.com/path", false}, + {"https://my-dolt.example.com?x@github.com", false}, + {"https://my-dolt.example.com#x@github.com", false}, + {"my-dolt.example.com:org/x@github.com:db", false}, + {"./backups/me@github.com:db", false}, + // Edge: empty string {"", false}, } @@ -121,6 +136,9 @@ var forgeSyncRemoteForms = []struct { {"already git+https", "git+https://github.com/org/repo.git", "git+https://github.com/org/repo.git"}, {"git+ssh without user", "git+ssh://github.com/org/repo.git", "git+ssh://github.com/org/repo.git"}, {"gitlab", "https://gitlab.com/group/repo.git", "git+https://gitlab.com/group/repo.git"}, + // No .git suffix, so only the host can make it a forge: github.com, after + // the last "@" of the authority (mc-gcmer). + {"multi-@ userinfo", "https://a@b@github.com/org/repo", "git+https://a@b@github.com/org/repo"}, } // newForgeSyncRemoteWorkspace builds a workspace whose config.yaml carries