Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (claude): reconnect socket relay on peer credential change

[._.]/ Adam Eivye6e1c7a9d4c4
diff --git a/server/routes/instances.js b/server/routes/instances.js
index 77f40b3308..f08afed60a 100644
--- a/server/routes/instances.js
+++ b/server/routes/instances.js
@@ -31,7 +31,9 @@ const router = Router();
// Optional HTTP Basic credential for a peer behind an auth proxy. `null` clears
// it; an object sets it (username optional — a password-only credential is
-// valid Basic auth). The service's sanitizePeerAuth does the final normalize.
+// valid Basic auth). The service's sanitizePeerAuth does the final normalize,
+// and treats a username-only payload (no password) as a clear, not a store —
+// a password is required to store a credential.
const peerAuthSchema = z.object({
username: z.string().max(256).optional(),
password: z.string().max(2048).optional()
diff --git a/server/services/instances.js b/server/services/instances.js
index 27dba7a48e..37f573e15d 100644
--- a/server/services/instances.js
+++ b/server/services/instances.js
@@ -76,6 +76,15 @@ function sanitizePeerAuth(auth) {
return { username, password };
}
+// True when two credential values (null or { username, password }) are
+// equivalent — used to skip a needless socket-relay reconnect on a no-op
+// credential write.
+function sameAuth(a, b) {
+ if (!a && !b) return true;
+ if (!a || !b) return false;
+ return a.username === b.username && a.password === b.password;
+}
+
// Strip the locally-stored proxy credential before a peer record crosses the
// wire (e.g. the announce response echoes the matched local peer back to the
// announcing instance). The password is OUR secret for reaching THEM and must
@@ -258,6 +267,11 @@ export async function removePeer(id) {
export async function updatePeer(id, updates) {
let hostChanged = false;
+ // Credential edits must reconnect the live socket relay (it pins the
+ // Basic-auth header into extraHeaders at connect time), not just the next
+ // probe cycle — otherwise an online relayed peer keeps the stale credential
+ // until a natural reconnect. Tracked like hostChanged, consumed below.
+ let authChanged = false;
// Track false→true transitions for the per-record-subscribable categories
// so we can backfill-subscribe existing local records after the data write
// settles. Set inside withData (where we have the merged before/after
@@ -288,8 +302,9 @@ export async function updatePeer(id, updates) {
// is ignored so a stray payload can't wipe a working credential.
if (updates.auth !== undefined) {
const normalizedAuth = sanitizePeerAuth(updates.auth);
- if (normalizedAuth !== undefined) {
+ if (normalizedAuth !== undefined && !sameAuth(peer.auth, normalizedAuth)) {
peer.auth = normalizedAuth; // null clears, object sets
+ authChanged = true;
console.log(`🌐 Peer credential ${peer.auth ? 'set' : 'cleared'}: ${peer.name}`);
}
}
@@ -324,7 +339,7 @@ export async function updatePeer(id, updates) {
// Tear down the socket relay only after a real state transition so it can
// reconnect using the new URL on the next probe cycle. Invalid/no-op host
// writes no longer disrupt an already-healthy connection.
- if (updates.enabled === false || hostChanged) disconnectFromPeer(id);
+ if (updates.enabled === false || hostChanged || authChanged) disconnectFromPeer(id);
// Backfill-subscribe every local record of any kind whose category just
// flipped on. Fire-and-forget — `autoSubscribePeerToAllRecords` is
// idempotent + per-record-error tolerant, and we don't want to block the
diff --git a/server/services/instances.test.js b/server/services/instances.test.js
index 66971422e5..4d033e5682 100644
--- a/server/services/instances.test.js
+++ b/server/services/instances.test.js
@@ -387,6 +387,26 @@ describe('instances.js', () => {
expect(result.auth).toEqual({ username: 'bob', password: 'pw' });
});
+ it('should reconnect the socket relay when the credential changes', async () => {
+ const peers = [{ id: 'peer-1', name: 'host', enabled: true, auth: { username: 'bob', password: 'old' } }];
+ readJSONFile.mockResolvedValue({ self: null, peers });
+
+ await updatePeer('peer-1', { auth: { username: 'bob', password: 'new' } });
+
+ // Relay pins the Basic header at connect time, so a credential change
+ // must tear it down to reconnect with the new extraHeaders.
+ expect(disconnectFromPeer).toHaveBeenCalledWith('peer-1');
+ });
+
+ it('should NOT reconnect the socket relay on a no-op credential write', async () => {
+ const peers = [{ id: 'peer-1', name: 'host', enabled: true, auth: { username: 'bob', password: 'pw' } }];
+ readJSONFile.mockResolvedValue({ self: null, peers });
+
+ await updatePeer('peer-1', { auth: { username: 'bob', password: 'pw' } });
+
+ expect(disconnectFromPeer).not.toHaveBeenCalled();
+ });
+
it('should not disconnect when enabling a peer', async () => {
const peers = [{ id: 'peer-1', name: 'host', enabled: false }];
readJSONFile.mockResolvedValue({ self: null, peers });