From 4a67d0d0288c1b4ee23e17fc896f47616931ba2d Mon Sep 17 00:00:00 2001 From: "gascity-eddie[bot]" <338826946+gascity-eddie[bot]@users.noreply.github.com> Date: Sat, 17 Oct 2026 18:07:50 +0000 Subject: [PATCH] fix(bootstrap): cut the host part before stripping userinfo (mc-gcmer) (#7616) * fix(bootstrap): cut the host part before stripping userinfo (mc-gcmer) urlHostname stripped userinfo through the first "@" anywhere in the URL, before cutting out the host part, so an "@" in a path, query or fragment picked the host: https://my-dolt.example.com/x@github.com/path read as github.com. isGitCodeRepoURL then called it a forge, and doltRemoteURL rewrote what is really a Dolt remotesapi URL to git+https:// (the GH#3339 class). The SCP branch made the same split, so both my-dolt.example.com:org/x@github.com:db and the local path ./backups/me@github.com:db read as github.com too. Cut the host part first, then strip userinfo through its last "@": - scheme URLs: the authority, ending at the first "/", "?" or "#". That is the host net/url dials, and Dolt's earl.Parse hands scheme URLs to net/url. Taking the last "@" matches net/url too, so https://a@b@github.com/org/db is now a forge, as Dolt would dial it, instead of going raw to the remotesapi client (the #4421 class). - SCP: everything before the first ":", unless a "/" comes first (a local path) - the split doltremote.FromGitURL makes. TestIsGitCodeRepoURL gains the path/query/fragment-@, SCP path-@, local-path-@ and last-@ cases, 7 of them red before this change, plus userinfo controls in both URL forms that stay green. Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 * fix(bootstrap): scrub credentials from the Bootstrap failed message (mc-gcmer) An unencoded "#" or "?" in a sync.remote password ends the URL's authority early, so urlHostname no longer reads the remote as a forge and bootstrap clones it as configured. The clone error then carries the URL, password included, twice: sanitizeURL passes through a URL net/url cannot parse, and Dolt's "invalid remote url:" repeats it. Scrub the error with scrubURLCredentials where "Bootstrap failed:" prints it. TestEmbeddedBootstrapCloneFailureScrubsCredentials pins this; it fails against the pre-fix binary for both "pa#ss" and "pa?ss", and the new test is registered in the embedded shard manifest. Also add a forgeSyncRemoteForms row for multi-@ userinfo without a .git suffix, so the routing pin covers a URL only its host can make a forge. The row fails with the base urlHostname. Review: PR #7616 synthesis F1 (minor) and F5 (nit). Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Eddie the Engineer <338826946+gascity-eddie[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- .github/scripts/embedded-cmd-test-shards.txt | 1 + cmd/bd/bootstrap.go | 28 ++++++++---- cmd/bd/bootstrap_embedded_test.go | 46 ++++++++++++++++++++ cmd/bd/bootstrap_guard_test.go | 18 ++++++++ 4 files changed, 84 insertions(+), 9 deletions(-) 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