From 8744d81db2fb87694ce8a08cad9f464ee8a0d0ed Mon Sep 17 00:00:00 2001 From: Michael Pursifull Date: Sun, 26 Jul 2026 14:31:15 -0500 Subject: [PATCH 1/5] fix(embeddeddolt): use stored peer credentials for federation sync bd federation add-peer promises stored credentials are used automatically when syncing, but the embedded Fetch/PushTo/PullFrom paths read only the DOLT_REMOTE_USER environment variable, so first sync against any authenticated Dolt server presented the wrong identity. This is the peer-path remainder of the class fixed for default remotes in #2543. withPeerAuth mirrors server mode's withPeerCredentials: peer-store lookup first, environment fallback for unknown remotes and credential-free peers (federation sync iterates every dolt remote, so non-peer remotes must keep their behavior). Stored credentials override the process-wide DOLT_REMOTE_USER/DOLT_REMOTE_PASSWORD pair as a unit under a federationEnvMutex, mirroring package dolt: the in-process Dolt engine reads them from the process environment, so the override is serialized and both variables are restored (or re-unset) afterward, including on callback failure. A stored empty field unsets its variable rather than inheriting an ambient value. Fixes #5080 Agent-Signature: claude-code-claude-fable-5-unknown-reasoning on behalf of Michael Pursifull --- internal/storage/embeddeddolt/federation.go | 57 +++++ .../federation_credentials_test.go | 232 ++++++++++++++++++ .../storage/embeddeddolt/version_control.go | 40 +-- 3 files changed, 312 insertions(+), 17 deletions(-) create mode 100644 internal/storage/embeddeddolt/federation_credentials_test.go diff --git a/internal/storage/embeddeddolt/federation.go b/internal/storage/embeddeddolt/federation.go index caf82a067..56ea12cfd 100644 --- a/internal/storage/embeddeddolt/federation.go +++ b/internal/storage/embeddeddolt/federation.go @@ -8,11 +8,13 @@ import ( "crypto/cipher" "crypto/rand" "database/sql" + "errors" "fmt" "io" "os" "path/filepath" "strings" + "sync" "time" "github.com/steveyegge/beads/internal/storage" @@ -145,6 +147,61 @@ func (s *EmbeddedDoltStore) GetFederationPeer(ctx context.Context, name string) return &row.Peer, nil } +// federationEnvMutex serializes mutation of the process-wide +// DOLT_REMOTE_USER/DOLT_REMOTE_PASSWORD pair, mirroring package dolt's +// federationEnvMutex: the in-process Dolt engine reads them from the process +// environment, so concurrent peer operations would otherwise observe each +// other's credentials. +var federationEnvMutex sync.Mutex + +// withPeerAuth runs fn with the remote-auth username for peer. Credentials +// stored by add-peer win and override the environment pair as a unit, so an +// ambient DOLT_REMOTE_PASSWORD never mixes with a stored username (or vice +// versa); remotes without a stored peer keep the environment fallback. +func (s *EmbeddedDoltStore) withPeerAuth(ctx context.Context, peer string, fn func(user string) error) error { + p, err := s.GetFederationPeer(ctx, peer) + if err != nil { + if errors.Is(err, storage.ErrNotFound) { + return fn(remoteAuthUser()) + } + return fmt.Errorf("resolve peer credentials: %w", err) + } + if p.Username == "" && p.Password == "" { + return fn(remoteAuthUser()) + } + + federationEnvMutex.Lock() + defer federationEnvMutex.Unlock() + + restoreUser := overrideEnv("DOLT_REMOTE_USER", p.Username) + restorePassword := overrideEnv("DOLT_REMOTE_PASSWORD", p.Password) + defer func() { + restorePassword() + restoreUser() + }() + + return fn(p.Username) +} + +// overrideEnv sets key to value (unsetting it when value is empty, so an +// ambient value cannot leak into an operation that stored an empty field) +// and returns a function restoring the prior state. +func overrideEnv(key, value string) func() { + prev, had := os.LookupEnv(key) + if value == "" { + _ = os.Unsetenv(key) + } else { + _ = os.Setenv(key, value) + } + return func() { + if had { + _ = os.Setenv(key, prev) + } else { + _ = os.Unsetenv(key) + } + } +} + func (s *EmbeddedDoltStore) ListFederationPeers(ctx context.Context) ([]*storage.FederationPeer, error) { var rows []*issueops.FederationPeerRow err := s.withConn(ctx, false, func(tx *sql.Tx) error { diff --git a/internal/storage/embeddeddolt/federation_credentials_test.go b/internal/storage/embeddeddolt/federation_credentials_test.go new file mode 100644 index 000000000..a850b1e28 --- /dev/null +++ b/internal/storage/embeddeddolt/federation_credentials_test.go @@ -0,0 +1,232 @@ +//go:build cgo + +package embeddeddolt + +import ( + "errors" + "os" + "path/filepath" + "sync" + "testing" + + "github.com/steveyegge/beads/internal/storage" +) + +func newPeerAuthTestStore(t *testing.T) *EmbeddedDoltStore { + t.Helper() + ctx := t.Context() + beadsDir := filepath.Join(t.TempDir(), ".beads") + store, err := Open(ctx, beadsDir, "fedauth", "main") + if err != nil { + t.Fatalf("Open: %v", err) + } + t.Cleanup(func() { store.Close() }) + return store +} + +// Regression test for GH#5080: credentials stored by add-peer must be the +// ones presented when syncing with that peer, overriding the environment +// pair as a unit; remotes without a stored peer keep the environment +// fallback. +func TestWithPeerAuth(t *testing.T) { + cases := []struct { + name string + peer *storage.FederationPeer + remote string + wantUser string + wantPwd string + wantPwdSet bool + wantUsrEnv string + wantUsrSet bool + }{ + { + name: "stored peer credentials win", + peer: &storage.FederationPeer{ + Name: "team", + RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", + Password: "peerpass", + }, + remote: "team", + wantUser: "peeruser", + wantPwd: "peerpass", wantPwdSet: true, + wantUsrEnv: "peeruser", wantUsrSet: true, + }, + { + name: "stored empty password does not inherit ambient password", + peer: &storage.FederationPeer{ + Name: "open-pwd", + RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", + }, + remote: "open-pwd", + wantUser: "peeruser", + wantPwd: "", wantPwdSet: false, + wantUsrEnv: "peeruser", wantUsrSet: true, + }, + { + name: "stored password with empty username does not inherit ambient user", + peer: &storage.FederationPeer{ + Name: "open-usr", + RemoteURL: "https://peer.example/peerdb", + Password: "peerpass", + }, + remote: "open-usr", + wantUser: "", + wantPwd: "peerpass", wantPwdSet: true, + wantUsrEnv: "", wantUsrSet: false, + }, + { + name: "unknown remote falls back to env", + remote: "not-a-peer", + wantUser: "envuser", + wantPwd: "envpass", wantPwdSet: true, + wantUsrEnv: "envuser", wantUsrSet: true, + }, + { + name: "credential-free peer falls back to env", + peer: &storage.FederationPeer{ + Name: "open-peer", + RemoteURL: "https://peer.example/peerdb", + }, + remote: "open-peer", + wantUser: "envuser", + wantPwd: "envpass", wantPwdSet: true, + wantUsrEnv: "envuser", wantUsrSet: true, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + ctx := t.Context() + store := newPeerAuthTestStore(t) + + t.Setenv("DOLT_REMOTE_USER", "envuser") + t.Setenv("DOLT_REMOTE_PASSWORD", "envpass") + + if tc.peer != nil { + if err := store.AddFederationPeer(ctx, tc.peer); err != nil { + t.Fatalf("AddFederationPeer: %v", err) + } + } + + var gotUser, gotPwd, gotUsrEnv string + var gotPwdSet, gotUsrSet bool + err := store.withPeerAuth(ctx, tc.remote, func(user string) error { + gotUser = user + gotPwd, gotPwdSet = os.LookupEnv("DOLT_REMOTE_PASSWORD") + gotUsrEnv, gotUsrSet = os.LookupEnv("DOLT_REMOTE_USER") + return nil + }) + if err != nil { + t.Fatalf("withPeerAuth: %v", err) + } + if gotUser != tc.wantUser { + t.Errorf("user = %q, want %q", gotUser, tc.wantUser) + } + if gotPwdSet != tc.wantPwdSet || gotPwd != tc.wantPwd { + t.Errorf("DOLT_REMOTE_PASSWORD during fn = %q (set=%v), want %q (set=%v)", gotPwd, gotPwdSet, tc.wantPwd, tc.wantPwdSet) + } + if gotUsrSet != tc.wantUsrSet || gotUsrEnv != tc.wantUsrEnv { + t.Errorf("DOLT_REMOTE_USER during fn = %q (set=%v), want %q (set=%v)", gotUsrEnv, gotUsrSet, tc.wantUsrEnv, tc.wantUsrSet) + } + if got := os.Getenv("DOLT_REMOTE_PASSWORD"); got != "envpass" { + t.Errorf("DOLT_REMOTE_PASSWORD after fn = %q, want restored %q", got, "envpass") + } + if got := os.Getenv("DOLT_REMOTE_USER"); got != "envuser" { + t.Errorf("DOLT_REMOTE_USER after fn = %q, want restored %q", got, "envuser") + } + }) + } +} + +func TestWithPeerAuth_RestoresEnvWhenCallbackFails(t *testing.T) { + ctx := t.Context() + store := newPeerAuthTestStore(t) + + t.Setenv("DOLT_REMOTE_PASSWORD", "envpass") + if err := store.AddFederationPeer(ctx, &storage.FederationPeer{ + Name: "team", RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", Password: "peerpass", + }); err != nil { + t.Fatalf("AddFederationPeer: %v", err) + } + + sentinel := errors.New("boom") + err := store.withPeerAuth(ctx, "team", func(string) error { return sentinel }) + if !errors.Is(err, sentinel) { + t.Fatalf("withPeerAuth error = %v, want sentinel", err) + } + if got := os.Getenv("DOLT_REMOTE_PASSWORD"); got != "envpass" { + t.Errorf("DOLT_REMOTE_PASSWORD after failed fn = %q, want restored %q", got, "envpass") + } +} + +func TestWithPeerAuth_RestoresAbsentEnv(t *testing.T) { + ctx := t.Context() + store := newPeerAuthTestStore(t) + + t.Setenv("DOLT_REMOTE_PASSWORD", "scratch") + _ = os.Unsetenv("DOLT_REMOTE_PASSWORD") + t.Setenv("DOLT_REMOTE_USER", "scratch") + _ = os.Unsetenv("DOLT_REMOTE_USER") + + if err := store.AddFederationPeer(ctx, &storage.FederationPeer{ + Name: "team", RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", Password: "peerpass", + }); err != nil { + t.Fatalf("AddFederationPeer: %v", err) + } + + if err := store.withPeerAuth(ctx, "team", func(string) error { return nil }); err != nil { + t.Fatalf("withPeerAuth: %v", err) + } + if v, set := os.LookupEnv("DOLT_REMOTE_PASSWORD"); set { + t.Errorf("DOLT_REMOTE_PASSWORD after fn = %q, want unset", v) + } + if v, set := os.LookupEnv("DOLT_REMOTE_USER"); set { + t.Errorf("DOLT_REMOTE_USER after fn = %q, want unset", v) + } +} + +func TestWithPeerAuth_ConcurrentPeersDoNotMixCredentials(t *testing.T) { + ctx := t.Context() + store := newPeerAuthTestStore(t) + + peers := map[string]string{"alpha": "alphapass", "beta": "betapass"} + for name, pwd := range peers { + if err := store.AddFederationPeer(ctx, &storage.FederationPeer{ + Name: name, RemoteURL: "https://peer.example/" + name, + Username: name + "-user", Password: pwd, + }); err != nil { + t.Fatalf("AddFederationPeer(%s): %v", name, err) + } + } + + var wg sync.WaitGroup + errs := make(chan error, 40) + for range 10 { + for name, pwd := range peers { + wg.Add(1) + go func(name, pwd string) { + defer wg.Done() + errs <- store.withPeerAuth(ctx, name, func(user string) error { + if got := os.Getenv("DOLT_REMOTE_PASSWORD"); got != pwd { + t.Errorf("peer %s observed DOLT_REMOTE_PASSWORD %q, want %q", name, got, pwd) + } + if want := name + "-user"; user != want { + t.Errorf("peer %s got user %q, want %q", name, user, want) + } + return nil + }) + }(name, pwd) + } + } + wg.Wait() + close(errs) + for err := range errs { + if err != nil { + t.Errorf("withPeerAuth: %v", err) + } + } +} diff --git a/internal/storage/embeddeddolt/version_control.go b/internal/storage/embeddeddolt/version_control.go index d02f04951..1230735f1 100644 --- a/internal/storage/embeddeddolt/version_control.go +++ b/internal/storage/embeddeddolt/version_control.go @@ -432,14 +432,18 @@ func (s *EmbeddedDoltStore) PullRemote(ctx context.Context, remote string) error } func (s *EmbeddedDoltStore) Fetch(ctx context.Context, peer string) error { - return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error { - return versioncontrolops.Fetch(ctx, db, peer, remoteAuthUser()) + return s.withPeerAuth(ctx, peer, func(user string) error { + return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error { + return versioncontrolops.Fetch(ctx, db, peer, user) + }) }) } func (s *EmbeddedDoltStore) PushTo(ctx context.Context, peer string) error { - return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error { - return versioncontrolops.Push(ctx, db, peer, s.branch, remoteAuthUser()) + return s.withPeerAuth(ctx, peer, func(user string) error { + return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error { + return versioncontrolops.Push(ctx, db, peer, s.branch, user) + }) }) } @@ -452,20 +456,22 @@ func (s *EmbeddedDoltStore) PullFrom(ctx context.Context, peer string) ([]storag preHead := s.preMergeHead(ctx) var conflicts []storage.Conflict - err := s.withMutatingPinnedDBConn(ctx, func(db versioncontrolops.DBConn) error { - if pullErr := versioncontrolops.Pull(ctx, db, peer, s.branch, remoteAuthUser()); pullErr != nil { - // bd-578h9.15: the settle machinery aborts a merge it cannot - // auto-resolve before returning, so dolt_conflicts is already - // empty here; the conflicts arrive captured pre-abort inside - // MergeConflictsError instead. - var mce *versioncontrolops.MergeConflictsError - if errors.As(pullErr, &mce) { - conflicts = mce.Conflicts - return nil + err := s.withPeerAuth(ctx, peer, func(user string) error { + return s.withMutatingPinnedDBConn(ctx, func(db versioncontrolops.DBConn) error { + if pullErr := versioncontrolops.Pull(ctx, db, peer, s.branch, user); pullErr != nil { + // bd-578h9.15: the settle machinery aborts a merge it cannot + // auto-resolve before returning, so dolt_conflicts is already + // empty here; the conflicts arrive captured pre-abort inside + // MergeConflictsError instead. + var mce *versioncontrolops.MergeConflictsError + if errors.As(pullErr, &mce) { + conflicts = mce.Conflicts + return nil + } + return fmt.Errorf("pull from %s: %w", peer, pullErr) } - return fmt.Errorf("pull from %s: %w", peer, pullErr) - } - return nil + return nil + }) }) if err != nil || len(conflicts) > 0 { // Conflicted pulls skip the recompute: the operator resolves first, From e87e5cbe3083d0f365b9968eeee986022b3d534d Mon Sep 17 00:00:00 2001 From: Michael Pursifull Date: Fri, 31 Jul 2026 15:42:07 -0500 Subject: [PATCH 2/5] docs(embeddeddolt): clarify federationEnvMutex is separate from package dolt's lock Review nit on #5085: the 'mirroring' phrasing read as if the two packages shared one mutex. State explicitly that they are separate locks and why per-package serialization suffices. --- internal/storage/embeddeddolt/federation.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/storage/embeddeddolt/federation.go b/internal/storage/embeddeddolt/federation.go index 56ea12cfd..d456c0bc4 100644 --- a/internal/storage/embeddeddolt/federation.go +++ b/internal/storage/embeddeddolt/federation.go @@ -148,10 +148,12 @@ func (s *EmbeddedDoltStore) GetFederationPeer(ctx context.Context, name string) } // federationEnvMutex serializes mutation of the process-wide -// DOLT_REMOTE_USER/DOLT_REMOTE_PASSWORD pair, mirroring package dolt's -// federationEnvMutex: the in-process Dolt engine reads them from the process -// environment, so concurrent peer operations would otherwise observe each -// other's credentials. +// DOLT_REMOTE_USER/DOLT_REMOTE_PASSWORD pair: the in-process Dolt engine +// reads them from the process environment, so concurrent peer operations +// would otherwise observe each other's credentials. This is a separate +// lock from package dolt's federationEnvMutex, not the same lock; the two +// stores never run remote operations concurrently, so serializing within +// each package suffices. var federationEnvMutex sync.Mutex // withPeerAuth runs fn with the remote-auth username for peer. Credentials From 575154c3204f62097fcb9c2988ad3f3a5e1e92e1 Mon Sep 17 00:00:00 2001 From: Michael Pursifull Date: Fri, 31 Jul 2026 22:51:53 -0500 Subject: [PATCH 3/5] feat(embeddeddolt): warn when a stored peer suppresses an ambient DOLT_REMOTE_PASSWORD A peer saved with a username but an empty password (Enter at the interactive prompt) deliberately suppresses an ambient DOLT_REMOTE_PASSWORD, the intended security default from the withPeerAuth unit rule. A setup that used to authenticate off the environment then fails with no hint about why (#5085 review, follow-up 4). Emit one stderr line when the resolved peer has a username, no stored password, and DOLT_REMOTE_PASSWORD set, naming the peer and how to store a password. Fired under federationEnvMutex before overrideEnv rewrites the pair, so the value read is the ambient one and not another peer's installed password. No credential behavior changes. Tests pin the exact rendered line and the three silent cases (stored password present, ambient unset, ambient empty), plus a direct-call row pinning the all-empty-peer boundary. Refs #5080 --- internal/storage/embeddeddolt/federation.go | 32 ++++ .../federation_credentials_test.go | 137 ++++++++++++++++++ 2 files changed, 169 insertions(+) diff --git a/internal/storage/embeddeddolt/federation.go b/internal/storage/embeddeddolt/federation.go index d456c0bc4..216be9063 100644 --- a/internal/storage/embeddeddolt/federation.go +++ b/internal/storage/embeddeddolt/federation.go @@ -175,6 +175,8 @@ func (s *EmbeddedDoltStore) withPeerAuth(ctx context.Context, peer string, fn fu federationEnvMutex.Lock() defer federationEnvMutex.Unlock() + warnStoredPeerSuppressesAmbientPassword(peer, p) + restoreUser := overrideEnv("DOLT_REMOTE_USER", p.Username) restorePassword := overrideEnv("DOLT_REMOTE_PASSWORD", p.Password) defer func() { @@ -185,6 +187,36 @@ func (s *EmbeddedDoltStore) withPeerAuth(ctx context.Context, peer string, fn fu return fn(p.Username) } +// federationWarnWriter receives the peer-credential diagnostics. Defaults to +// os.Stderr, matching the unconditional warnings this package writes from +// store.go; overridable in tests. +var federationWarnWriter io.Writer = os.Stderr + +// warnStoredPeerSuppressesAmbientPassword emits one line when a peer stores a +// username but no password while DOLT_REMOTE_PASSWORD is set. Suppressing the +// ambient password is the intended security default, but a setup that used to +// authenticate off the environment then fails with no hint about why (GH#5085 +// review). Call with federationEnvMutex held and before overrideEnv rewrites +// the pair, so the value read is the ambient one and not another peer's +// installed password. +func warnStoredPeerSuppressesAmbientPassword(peer string, p *storage.FederationPeer) { + // Defensive: withPeerAuth returns before this guard when both fields are empty. + if p.Username == "" || p.Password != "" { + return + } + // An ambient empty value is treated as no ambient password: unsetting it + // changes nothing the user could be puzzled by. + if os.Getenv("DOLT_REMOTE_PASSWORD") == "" { + return + } + fmt.Fprintf(federationWarnWriter, + "Warning: peer %[1]q stores a username with an empty password, "+ + "which overrides the ambient DOLT_REMOTE_PASSWORD for this operation; "+ + "store a password with 'bd federation add-peer %[1]s "+ + "--user %[2]s --password '.\n", + peer, p.Username) +} + // overrideEnv sets key to value (unsetting it when value is empty, so an // ambient value cannot leak into an operation that stored an empty field) // and returns a function restoring the prior state. diff --git a/internal/storage/embeddeddolt/federation_credentials_test.go b/internal/storage/embeddeddolt/federation_credentials_test.go index a850b1e28..e9f7a4d29 100644 --- a/internal/storage/embeddeddolt/federation_credentials_test.go +++ b/internal/storage/embeddeddolt/federation_credentials_test.go @@ -3,7 +3,9 @@ package embeddeddolt import ( + "bytes" "errors" + "io" "os" "path/filepath" "sync" @@ -15,6 +17,14 @@ import ( func newPeerAuthTestStore(t *testing.T) *EmbeddedDoltStore { t.Helper() ctx := t.Context() + + // Several cases here deliberately build the state that warns about a + // suppressed ambient password; keep that off the test log unless a test + // installs its own writer to assert on it. + prevWarn := federationWarnWriter + federationWarnWriter = io.Discard + t.Cleanup(func() { federationWarnWriter = prevWarn }) + beadsDir := filepath.Join(t.TempDir(), ".beads") store, err := Open(ctx, beadsDir, "fedauth", "main") if err != nil { @@ -140,6 +150,133 @@ func TestWithPeerAuth(t *testing.T) { } } +// Diagnostic from the GH#5085 review: a peer stored with a username and no +// password suppresses an ambient DOLT_REMOTE_PASSWORD (the security default), +// which turns a setup that used to authenticate off the environment into an +// auth failure with no visible cause. One line names the cause and the fix. +func TestWithPeerAuth_WarnsWhenStoredPeerSuppressesAmbientPassword(t *testing.T) { + // Pinned verbatim, not matched by substring: the peer name, the username, + // and the Warning: prefix are the parts an operator reads, so a wording + // change has to be made here deliberately. + const wantOpenPwdWarning = `Warning: peer "open-pwd" stores a username with an empty password, ` + + `which overrides the ambient DOLT_REMOTE_PASSWORD for this operation; ` + + `store a password with 'bd federation add-peer open-pwd ` + + `--user peeruser --password '.` + "\n" + + cases := []struct { + name string + peer *storage.FederationPeer + remote string + ambient string + ambientSet bool + // directPeer, when set, calls the warning helper itself instead of + // going through withPeerAuth, pinning the helper's own boundary. + directPeer *storage.FederationPeer + wantLine string + }{ + { + name: "username with empty stored password suppresses ambient password", + peer: &storage.FederationPeer{ + Name: "open-pwd", + RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", + }, + remote: "open-pwd", ambient: "envpass", ambientSet: true, + wantLine: wantOpenPwdWarning, + }, + { + name: "stored password suppresses nothing the operator can act on", + peer: &storage.FederationPeer{ + Name: "team", + RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", + Password: "peerpass", + }, + remote: "team", ambient: "envpass", ambientSet: true, + }, + { + name: "no ambient password to suppress", + peer: &storage.FederationPeer{ + Name: "open-pwd", + RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", + }, + remote: "open-pwd", + }, + { + name: "empty ambient password reads as no ambient password", + peer: &storage.FederationPeer{ + Name: "open-pwd", + RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", + }, + remote: "open-pwd", ambient: "", ambientSet: true, + }, + { + name: "peer without a username keeps the password puzzle out of scope", + peer: &storage.FederationPeer{ + Name: "open-usr", + RemoteURL: "https://peer.example/peerdb", + Password: "peerpass", + }, + remote: "open-usr", ambient: "envpass", ambientSet: true, + }, + { + name: "unknown remote keeps the environment fallback", + remote: "not-a-peer", ambient: "envpass", ambientSet: true, + }, + { + // withPeerAuth returns before the helper when a peer stores + // neither field, so a direct call is the only way to reach the + // helper's defensive empty-username clause. + name: "helper called directly with an all-empty peer stays silent", + directPeer: &storage.FederationPeer{}, + remote: "empty-peer", ambient: "envpass", ambientSet: true, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + ctx := t.Context() + + t.Setenv("DOLT_REMOTE_USER", "envuser") + t.Setenv("DOLT_REMOTE_PASSWORD", tc.ambient) + if !tc.ambientSet { + _ = os.Unsetenv("DOLT_REMOTE_PASSWORD") + } + + var warnings bytes.Buffer + captureWarnings := func() { + // Installed after any store: newPeerAuthTestStore points the + // writer at io.Discard for the rest of the suite. + prev := federationWarnWriter + federationWarnWriter = &warnings + t.Cleanup(func() { federationWarnWriter = prev }) + } + + if tc.directPeer != nil { + captureWarnings() + warnStoredPeerSuppressesAmbientPassword(tc.remote, tc.directPeer) + } else { + store := newPeerAuthTestStore(t) + if tc.peer != nil { + if err := store.AddFederationPeer(ctx, tc.peer); err != nil { + t.Fatalf("AddFederationPeer: %v", err) + } + } + captureWarnings() + if err := store.withPeerAuth(ctx, tc.remote, func(string) error { return nil }); err != nil { + t.Fatalf("withPeerAuth: %v", err) + } + } + + if got := warnings.String(); got != tc.wantLine { + t.Errorf("warning = %q, want %q", got, tc.wantLine) + } + }) + } +} + func TestWithPeerAuth_RestoresEnvWhenCallbackFails(t *testing.T) { ctx := t.Context() store := newPeerAuthTestStore(t) From 11db4f78227f950b3a77f10c712353bc7ef0e21d Mon Sep 17 00:00:00 2001 From: Michael Pursifull Date: Tue, 8 Sep 2026 10:34:04 -0500 Subject: [PATCH 4/5] merge: main into fix/peer-empty-password-warning Catches the branch up 329 commits. No conflicts: main did not touch internal/storage/embeddeddolt/federation.go or its credentials test, which are the only two files this branch changes. Verified on darwin/arm64: gofmt and go vet clean, and go test ./internal/storage/embeddeddolt/ passes. That package needs ICU headers to build here, so it was run with CGO_CPPFLAGS pointed at the brew icu4c prefix; without them the cgo dependency fails to compile on upstream main too. Refs: gastownhall/beads#5215 (review) From 69c7687a4dec4741e46b0ac0fc144b5250004946 Mon Sep 17 00:00:00 2001 From: Eddie the Engineer Date: Wed, 30 Sep 2026 05:32:18 +0000 Subject: [PATCH 5/5] fix(embeddeddolt): keep the tier and prompt for the password in the remedy Review fixup for #5215 (adopt-pr review loop, iteration 1). Both review lanes approved (scorecard 974/1000, no blockers or majors). This fixes the two gated minors, both in the add-peer command the new warning prints. - Change Impact: the printed command had no --sovereignty. add-peer upserts the whole row (issueops/federation.go: ON DUPLICATE KEY UPDATE ... sovereignty = VALUES(sovereignty)), so running it as printed on a T1-T4 peer cleared the tier (#5213, still open). The command now includes the peer's stored tier when it has one. The PR body says the --password form avoids #5213, but it does not: step 6 of #5213's own repro is add-peer with --password and without --sovereignty, and it clears the tier. The control below reproduces that. - Security Surface: the command put the secret on the command line ('--password '), where it ends up in shell history and in /proc//cmdline. add-peer already prompts for the password without echo when --user is given without --password, so the warning now points to the prompt, as the verifyPeerRemoteURL message in this file already does. The test gets a new case that pins the tiered wording verbatim, and the existing open-pwd pin is updated. Not changed: - Readability nit (shell-quote the username): doltutil.ShellQuote is only used by this package's go_test today. Using it here would add a go_library dependency and a BUILD.bazel deps change for a nit that does not gate. - Architectural Consistency (the server-mode internal/storage/dolt backend prints no such warning; does not gate): left for finalize to file as a follow-up bead. Validation (ambient BEADS_*/BD_*/DOLT_* unset, TMPDIR=/tmp): - go test -tags gms_pure_go -count=1 -run TestWithPeerAuth ./internal/storage/embeddeddolt/: all 8 tests pass, including all 8 subtests of the warning test. - Mutation: removing the branch that adds the tier fails only the new tiered subtest. The file was restored byte-identical. - E2E with bd built from this tree: a T2 peer that stores a username and an empty password, used with DOLT_REMOTE_PASSWORD set in the environment, prints one warning. Running the printed command and typing a password at the prompt keeps sovereignty T2 and stores the password (checked with dolt sql), and the warning no longer appears. Control: the old '--password' command on a T3 peer stores the password but clears the tier. - go vet -tags gms_pure_go ./internal/storage/embeddeddolt/, gofmt -l, git diff --check: clean. No imports or files were added, so there is no Bazel drift. Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR Co-Authored-By: Claude Opus 5.5 --- internal/storage/embeddeddolt/federation.go | 14 ++++++++--- .../federation_credentials_test.go | 25 ++++++++++++++++--- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/internal/storage/embeddeddolt/federation.go b/internal/storage/embeddeddolt/federation.go index bc07b8e27..905db8af3 100644 --- a/internal/storage/embeddeddolt/federation.go +++ b/internal/storage/embeddeddolt/federation.go @@ -220,12 +220,20 @@ func warnStoredPeerSuppressesAmbientPassword(peer string, p *storage.FederationP if os.Getenv("DOLT_REMOTE_PASSWORD") == "" { return } + // add-peer upserts the whole row, so a re-run without --sovereignty + // clears a stored tier (GH#5213): carry the stored tier into the + // suggested command. The password is left to add-peer's no-echo prompt + // rather than suggested as an argument that lands in shell history. + sovereignty := "" + if p.Sovereignty != "" { + sovereignty = " --sovereignty " + p.Sovereignty + } fmt.Fprintf(federationWarnWriter, "Warning: peer %[1]q stores a username with an empty password, "+ "which overrides the ambient DOLT_REMOTE_PASSWORD for this operation; "+ - "store a password with 'bd federation add-peer %[1]s "+ - "--user %[2]s --password '.\n", - peer, p.Username) + "store a password by re-running 'bd federation add-peer %[1]s "+ + "--user %[2]s%[3]s' and entering it at the prompt.\n", + peer, p.Username, sovereignty) } // verifyPeerRemoteURL fails closed when the live remote named peer does not diff --git a/internal/storage/embeddeddolt/federation_credentials_test.go b/internal/storage/embeddeddolt/federation_credentials_test.go index 09c301efa..1730a80f8 100644 --- a/internal/storage/embeddeddolt/federation_credentials_test.go +++ b/internal/storage/embeddeddolt/federation_credentials_test.go @@ -155,12 +155,16 @@ func TestWithPeerAuth(t *testing.T) { func TestWithPeerAuth_WarnsWhenStoredPeerSuppressesAmbientPassword(t *testing.T) { // Pinned verbatim, not matched by substring: the peer name, the username, - // and the Warning: prefix are the parts an operator reads, so a wording - // change has to be made here deliberately. + // the stored tier, and the Warning: prefix are the parts an operator + // reads, so a wording change has to be made here deliberately. const wantOpenPwdWarning = `Warning: peer "open-pwd" stores a username with an empty password, ` + `which overrides the ambient DOLT_REMOTE_PASSWORD for this operation; ` + - `store a password with 'bd federation add-peer open-pwd ` + - `--user peeruser --password '.` + "\n" + `store a password by re-running 'bd federation add-peer open-pwd ` + + `--user peeruser' and entering it at the prompt.` + "\n" + const wantTieredWarning = `Warning: peer "tiered" stores a username with an empty password, ` + + `which overrides the ambient DOLT_REMOTE_PASSWORD for this operation; ` + + `store a password by re-running 'bd federation add-peer tiered ` + + `--user peeruser --sovereignty T2' and entering it at the prompt.` + "\n" cases := []struct { name string @@ -183,6 +187,19 @@ func TestWithPeerAuth_WarnsWhenStoredPeerSuppressesAmbientPassword(t *testing.T) remote: "open-pwd", ambient: "envpass", ambientSet: true, wantLine: wantOpenPwdWarning, }, + { + // add-peer upserts sovereignty along with the credentials, so the + // suggested re-run must carry the stored tier or it clears it. + name: "suggested re-add keeps the stored sovereignty tier", + peer: &storage.FederationPeer{ + Name: "tiered", + RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", + Sovereignty: "T2", + }, + remote: "tiered", ambient: "envpass", ambientSet: true, + wantLine: wantTieredWarning, + }, { name: "stored password suppresses nothing the operator can act on", peer: &storage.FederationPeer{