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): don't let a username-only peer-auth payload wipe a stored password

[._.]/ Adam Eivy2e48587bc4b9
diff --git a/server/routes/instances.js b/server/routes/instances.js
index 25e1c344cf..108e016b87 100644
--- a/server/routes/instances.js
+++ b/server/routes/instances.js
@@ -31,9 +31,11 @@ const router = Router();
// Optional HTTP Basic credential for a peer behind an auth proxy. `null` clears
// it; an object sets it. The service's sanitizePeerAuth does the final
-// normalize: a payload with both fields blank is a clear, but any non-blank
-// field (username-only or password-only) stores a credential — both are valid
-// Basic auth (username defaults to '' so a password-only credential works).
+// normalize: the password is the secret that defines the credential, so a
+// payload with a password stores it (username optional — password-only is
+// valid Basic auth), both fields blank is a clear, and a username-only payload
+// is ignored (it's most likely a redacted client peer being round-tripped, and
+// must not wipe a stored password).
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 35b7e3e0a3..1b77eb790f 100644
--- a/server/services/instances.js
+++ b/server/services/instances.js
@@ -63,16 +63,22 @@ function classifyProbeError(err, peer) {
}
// Normalize a credential object off a peer add/update payload. Returns:
-// - `undefined` for absent / malformed input (caller leaves the field as-is)
+// - `undefined` for absent / malformed input, or a username-only payload
+// (caller leaves the field as-is — see below)
// - `null` for an explicit clear (both fields blank)
// - `{ username, password }` for a credential to store
-// Username defaults to '' so a password-only credential is valid Basic auth.
+// The password is the secret that defines the credential; username defaults to
+// '' so a password-only credential is valid Basic auth. A payload with a
+// username but no password is treated as "ignore", NOT a blank-password store:
+// the client only ever receives a redacted peer (`{ username, hasPassword }`),
+// so round-tripping that shape back into a PATCH must not silently wipe a
+// working password. An explicit clear always goes through `auth: null`.
function sanitizePeerAuth(auth) {
if (auth === null) return null;
if (!auth || typeof auth !== 'object' || Array.isArray(auth)) return undefined;
const username = typeof auth.username === 'string' ? auth.username.trim() : '';
const password = typeof auth.password === 'string' ? auth.password : '';
- if (!username && !password) return null;
+ if (!password) return username ? undefined : null;
return { username, password };
}
diff --git a/server/services/instances.test.js b/server/services/instances.test.js
index ae9385d5c6..4425534161 100644
--- a/server/services/instances.test.js
+++ b/server/services/instances.test.js
@@ -280,6 +280,27 @@ describe('instances.js', () => {
expect(peer.auth).toBeNull();
});
+
+ it('should store a password-only credential (username defaults to "")', async () => {
+ readJSONFile.mockResolvedValue({ self: null, peers: [] });
+ fetch.mockRejectedValue(new Error('not reachable'));
+
+ const peer = await addPeer({ address: '10.0.0.11', auth: { password: 'tok3n' } });
+
+ expect(peer.auth).toEqual({ username: '', password: 'tok3n' });
+ });
+
+ it('should ignore a username-only payload (no blank-password store)', async () => {
+ readJSONFile.mockResolvedValue({ self: null, peers: [] });
+ fetch.mockRejectedValue(new Error('not reachable'));
+
+ const peer = await addPeer({ address: '10.0.0.12', auth: { username: 'alice' } });
+
+ // A username-only payload (e.g. a redacted client peer round-tripped back)
+ // must not store a blank-password credential — it's ignored, so a new peer
+ // is left with no auth.
+ expect(peer.auth).toBeNull();
+ });
});
describe('removePeer', () => {
@@ -406,6 +427,19 @@ describe('instances.js', () => {
expect(result.auth).toEqual({ username: 'bob', password: 'pw' });
});
+ it('should ignore a username-only payload rather than wiping a working password', async () => {
+ // The client only ever receives a redacted peer ({ username, hasPassword }).
+ // Round-tripping that shape back into a PATCH sends auth.username with no
+ // password — it must not clear the stored secret with a blank password.
+ const peers = [{ id: 'peer-1', name: 'host', enabled: true, auth: { username: 'bob', password: 'pw' } }];
+ readJSONFile.mockResolvedValue({ self: null, peers });
+
+ const result = await updatePeer('peer-1', { auth: { username: 'bob' } });
+
+ expect(result.auth).toEqual({ username: 'bob', password: 'pw' });
+ expect(disconnectFromPeer).not.toHaveBeenCalled();
+ });
+
it('should reconnect the relay and re-probe immediately when the credential changes', async () => {
// Peer is deep in backoff from prior 401s — the new credential must
// trigger an immediate probe, not wait out nextProbeAt.