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

fix: keep the settings ledger write off the queued save turn and revoke sessions once the hash is on disk

[._.]/ Adam Eivy431ec57d4e68
diff --git a/server/routes/settings.test.js b/server/routes/settings.test.js
index be001d7d98..9d4325113d 100644
--- a/server/routes/settings.test.js
+++ b/server/routes/settings.test.js
@@ -1529,6 +1529,7 @@ describe('Settings routes — atomic partial saves (#11045)', () => {
timezone: 'UTC',
});
expect(emitted).toHaveBeenCalledTimes(3);
+ await actual.flushSettingsLedger();
const { recordUserAction } = await import('../services/userActions.js');
expect(recordUserAction).toHaveBeenCalledWith(expect.objectContaining({ type: 'settings.update', actor: 'user' }));
} finally {
diff --git a/server/services/auth.js b/server/services/auth.js
index 75c1ba8120..5657a24db0 100644
--- a/server/services/auth.js
+++ b/server/services/auth.js
@@ -218,6 +218,30 @@ export const getPasswordRiskStatus = async () => {
return { enabled, revision: settings.passwordRiskRevision || 'initial' };
};
+// Persist an auth-config change and revoke every session once the change is on
+// disk. The revocation is keyed on what settings.json actually holds, not on
+// `updateSettingsWith` resolving: save() still runs post-write steps (the cache
+// event, listeners) after the file is replaced, so a failure there used to
+// report the change as failed while the new hash was already live and every
+// pre-existing session — including a stolen token — stayed valid (#12018).
+// `isWritten(auth)` receives the on-disk `secrets.auth` (or null) and decides
+// whether the intended change landed; the original error still propagates.
+const writeAuthConfig = async (mutate, isWritten) => {
+ let written = false;
+ try {
+ await updateSettingsWith(mutate);
+ written = true;
+ } finally {
+ if (!written) {
+ const { settings } = await readSettingsStrict();
+ written = isWritten(settings?.secrets?.auth ?? null);
+ }
+ // Existing sessions are invalidated on every auth change — the user (or
+ // anyone holding a stolen token) starts over.
+ if (written) await revokeAllSessions();
+ }
+};
+
// Set or replace the password. When `currentPassword` is provided we verify it
// against the stored hash first; pass `null` for the first-time set. Returns a
// fresh session token so the caller can stay signed in after a change.
@@ -240,7 +264,7 @@ export const setPassword = async ({ newPassword, currentPassword = null }) => {
}
const salt = randomBytes(SALT_BYTES).toString('hex');
const passwordHash = await hashPassword(newPassword, salt);
- await updateSettingsWith((settings) => ({
+ await writeAuthConfig((settings) => ({
...settings,
secrets: {
...(settings.secrets || {}),
@@ -253,10 +277,7 @@ export const setPassword = async ({ newPassword, currentPassword = null }) => {
},
},
passwordRiskRevision: randomBytes(16).toString('hex'),
- }));
- // Existing sessions are invalidated on password change — the user (or anyone
- // holding a stolen token) starts over.
- await revokeAllSessions();
+ }), (auth) => auth?.passwordHash === passwordHash);
return createSession();
};
@@ -272,12 +293,11 @@ export const clearPassword = async ({ currentPassword }) => {
if (!ok) {
throw new ServerError('Current password is incorrect', { status: 401, code: 'AUTH_BAD_CURRENT' });
}
- await updateSettingsWith((settings) => {
+ await writeAuthConfig((settings) => {
const secrets = { ...(settings.secrets || {}) };
delete secrets.auth;
return { ...settings, secrets, passwordRiskRevision: randomBytes(16).toString('hex') };
- });
- await revokeAllSessions();
+ }, (auth) => auth === null);
return { enabled: false };
};
diff --git a/server/services/auth.test.js b/server/services/auth.test.js
index 9b5f03c192..0b25600a57 100644
--- a/server/services/auth.test.js
+++ b/server/services/auth.test.js
@@ -389,6 +389,28 @@ describe('auth service', () => {
expect(raw).toContain('tokenHash');
});
+ it('revokes old sessions when a step after the hash write fails (#12018)', async () => {
+ const auth = await import('./auth.js');
+ const { settingsEvents, readSettingsStrict } = await import('./settings.js');
+ const first = await auth.setPassword({ newPassword: 'correct-horse' });
+ const before = (await readSettingsStrict()).settings.secrets.auth.passwordHash;
+ // settings:updated fires after settings.json is replaced; a listener that
+ // throws is the post-write failure (the ledger used to be another).
+ const explode = () => { throw new Error('post-write listener failed'); };
+ settingsEvents.on('settings:updated', explode);
+ try {
+ await expect(auth.setPassword({ newPassword: 'second-attempt', currentPassword: 'correct-horse' }))
+ .rejects.toThrow('post-write listener failed');
+ } finally {
+ settingsEvents.off('settings:updated', explode);
+ }
+ // The hash changed on disk, so the session minted under the old password
+ // must be gone even though the call reported failure.
+ expect((await readSettingsStrict()).settings.secrets.auth.passwordHash).not.toBe(before);
+ expect(await auth.verifySession(first.token)).toBe(false);
+ expect(await auth.verifyPassword('second-attempt')).toBe(true);
+ });
+
it('emits sessions:revoked-all on every auth-state change so the socket layer can kick connections', async () => {
vi.useFakeTimers();
const auth = await import('./auth.js');
diff --git a/server/services/settings.js b/server/services/settings.js
index 3fc491ed3f..b4630c77dd 100644
--- a/server/services/settings.js
+++ b/server/services/settings.js
@@ -296,10 +296,26 @@ const save = async (settings, { actor = 'system', skipUserAction = false } = {},
// on every visit, and a log full of "changed nothing" rows would bury the
// changes that matter.
if (!skipUserAction && change && change.keysChanged.length > 0) {
- const happenedAt = new Date().toISOString();
- const changedKeys = change.keysChanged.join(',');
- const { recordUserAction } = await import('./userActions.js');
- await recordUserAction({
+ recordSettingsChange(actor, change);
+ }
+ return cleaned;
+};
+
+// Ledger writes in flight, so `flushSettingsLedger` can await them.
+const pendingLedgerWrites = new Set();
+
+// Record a settings change in the operator-action ledger (#5594) as detached
+// bookkeeping: the row is a Postgres write, and awaiting it inside the queued
+// save turn made a DB outage stall every later settings write for the pool
+// connect timeout and then reject a save whose file was already on disk — which
+// in turn skipped setPassword's session revocation (#12018). The ledger is an
+// audit trail, not part of the save's success contract, so the chain is not
+// awaited by the caller and owns its own rejection.
+const recordSettingsChange = (actor, change) => {
+ const happenedAt = new Date().toISOString();
+ const changedKeys = change.keysChanged.join(',');
+ const write = import('./userActions.js')
+ .then(({ recordUserAction }) => recordUserAction({
type: 'settings.update',
actor,
summary: `Updated settings: ${change.keysChanged.join(', ')}`,
@@ -312,11 +328,19 @@ const save = async (settings, { actor = 'system', skipUserAction = false } = {},
// to a single row. Deterministic (not random) so a genuine duplicate still
// dedupes.
dedupeKey: `settings.update:${happenedAt}:${changedKeys.slice(0, 200)}`,
- });
- }
- return cleaned;
+ }))
+ .catch((error) => {
+ console.error(`❌ Settings ledger write failed for keys ${changedKeys}: ${error.message}`);
+ })
+ .finally(() => pendingLedgerWrites.delete(write));
+ pendingLedgerWrites.add(write);
};
+// Await every ledger write scheduled so far. The writes are detached from the
+// save that scheduled them, so a caller that must observe the row (a test, a
+// shutdown drain) waits here rather than on the save's promise.
+export const flushSettingsLedger = () => Promise.all([...pendingLedgerWrites]).then(() => undefined);
+
/**
* `getSettings`, but reporting whether the snapshot came from a CORRUPT read.
*
diff --git a/server/services/settings.test.js b/server/services/settings.test.js
index 4b52c8eaa8..d295406083 100644
--- a/server/services/settings.test.js
+++ b/server/services/settings.test.js
@@ -35,7 +35,8 @@ import { mkdtempSync, rmSync } from 'fs';
import { join as joinPath } from 'path';
import { tmpdir } from 'os';
import { atomicWrite, tryReadFile, tryReadFileStrict } from '../lib/fileUtils.js';
-import { getSettings, updateSettings, updateSettingsWith, withLiveSettingsRestore, reloadSettings, settingsEvents, __resetSettingsCache, readSettingsStrict } from './settings.js';
+import { recordUserAction } from './userActions.js';
+import { getSettings, updateSettings, updateSettingsWith, withLiveSettingsRestore, reloadSettings, settingsEvents, flushSettingsLedger, __resetSettingsCache, readSettingsStrict } from './settings.js';
describe('settings.js', () => {
beforeEach(() => {
@@ -277,6 +278,38 @@ describe('settings.js', () => {
});
});
+ describe('ledger write is detached from the save turn (#12018)', () => {
+ it('resolves the save and keeps the file write when the ledger rejects', async () => {
+ const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
+ recordUserAction.mockRejectedValueOnce(new Error('connect ECONNREFUSED'));
+ try {
+ await expect(updateSettings({ theme: 'dark' }, { actor: 'user' })).resolves.toEqual({ theme: 'dark' });
+ expect(atomicWrite).toHaveBeenCalledTimes(1);
+ await flushSettingsLedger();
+ expect(recordUserAction).toHaveBeenCalledTimes(1);
+ expect(consoleError).toHaveBeenCalledWith(expect.stringContaining('Settings ledger write failed for keys theme: connect ECONNREFUSED'));
+ } finally {
+ consoleError.mockRestore();
+ }
+ });
+
+ it('does not hold the write queue while a ledger write hangs', async () => {
+ let current = {};
+ tryReadFile.mockImplementation(async () => JSON.stringify(current));
+ atomicWrite.mockImplementation(async (_path, content) => { current = JSON.parse(content); });
+ // A ledger write that never settles (Postgres pool waiting on a dead host).
+ recordUserAction.mockReturnValueOnce(new Promise(() => {}));
+
+ await updateSettings({ first: 1 }, { actor: 'user' });
+ await expect(updateSettings({ second: 2 }, { actor: 'user' })).resolves.toEqual({ first: 1, second: 2 });
+ expect(current).toEqual({ first: 1, second: 2 });
+ // Both saves scheduled their row (the second's dispatch is a tick behind
+ // its resolved save); the first is still in flight.
+ await new Promise((resolve) => setImmediate(resolve));
+ expect(recordUserAction).toHaveBeenCalledTimes(2);
+ });
+ });
+
describe('updateSettingsWith', () => {
it('hands the mutator the current stripped settings and persists its return', async () => {
tryReadFile.mockResolvedValue(JSON.stringify({ civitai: { apiKey: 'old', other: 'keep' } }));
diff --git a/server/services/settings.userActions.test.js b/server/services/settings.userActions.test.js
index a430f274da..3ca7d772be 100644
--- a/server/services/settings.userActions.test.js
+++ b/server/services/settings.userActions.test.js
@@ -29,10 +29,17 @@ vi.mock('../lib/fileUtils.js', async () => {
return makePathsProxy(actual, { dataRoot: tempRoot });
});
-const { saveSettings, updateSettings, updateSettingsWith, reloadSettings, __resetSettingsCache } =
+const { saveSettings, updateSettings, updateSettingsWith, reloadSettings, flushSettingsLedger, __resetSettingsCache } =
await import('./settings.js');
const { listUserActions } = await import('./userActions.js');
+// The ledger row is detached bookkeeping (#12018): a resolved save does not
+// mean the row has landed, so drain the in-flight writes before reading back.
+const listSettingsUpdates = async () => {
+ await flushSettingsLedger();
+ return listUserActions({ type: 'settings.update' });
+};
+
const SETTINGS_FILE = join(tempRoot, 'settings.json');
const seed = (settings) => writeFileSync(SETTINGS_FILE, JSON.stringify(settings, null, 2));
@@ -49,7 +56,7 @@ describe('settings save → user_action_events (#5594)', () => {
it('records the changed keys with before/after and the caller-supplied actor', async () => {
await updateSettingsWith((current) => ({ ...current, timezone: 'America/Los_Angeles' }), { actor: 'user' });
- const [event] = await listUserActions({ type: 'settings.update' });
+ const [event] = await listSettingsUpdates();
expect(event).toMatchObject({ type: 'settings.update', actor: 'user', success: true });
expect(event.payload.keysChanged).toEqual(['timezone']);
expect(event.payload.changes.timezone).toEqual({ from: 'UTC', to: 'America/Los_Angeles' });
@@ -61,7 +68,7 @@ describe('settings save → user_action_events (#5594)', () => {
it('defaults to actor "system" for every non-route caller', async () => {
await updateSettings({ theme: 'dark' });
- const [event] = await listUserActions({ type: 'settings.update' });
+ const [event] = await listSettingsUpdates();
expect(event.actor).toBe('system');
expect(event.payload.keysChanged).toEqual(['theme']);
});
@@ -69,7 +76,7 @@ describe('settings save → user_action_events (#5594)', () => {
it('threads the actor through saveSettings and updateSettings too', async () => {
await saveSettings({ timezone: 'UTC', theme: 'dark' }, { actor: 'user' });
await updateSettings({ theme: 'light' }, { actor: 'user' });
- const events = await listUserActions({ type: 'settings.update' });
+ const events = await listSettingsUpdates();
expect(events).toHaveLength(2);
expect(events.every((e) => e.actor === 'user')).toBe(true);
});
@@ -77,7 +84,7 @@ describe('settings save → user_action_events (#5594)', () => {
it('lists a secret-shaped key as changed but withholds its value', async () => {
await updateSettings({ civitaiApiKey: 'EXAMPLE-not-a-real-key' }, { actor: 'user' });
- const [event] = await listUserActions({ type: 'settings.update' });
+ const [event] = await listSettingsUpdates();
expect(event.payload.keysChanged).toEqual(['civitaiApiKey']);
expect(event.payload.redactedKeys).toEqual(['changes.civitaiApiKey']);
expect(event.payload.changes).not.toHaveProperty('civitaiApiKey');
@@ -87,18 +94,18 @@ describe('settings save → user_action_events (#5594)', () => {
it('records the fact of a change for a non-scalar slice without copying it in', async () => {
await updateSettings({ backup: { enabled: true, destination: '/example/backups' } }, { actor: 'user' });
- const [event] = await listUserActions({ type: 'settings.update' });
+ const [event] = await listSettingsUpdates();
expect(event.payload.changes.backup).toEqual({ changed: true });
});
it('writes nothing for a no-op save', async () => {
await updateSettings({ timezone: 'UTC' }, { actor: 'user' });
- expect(await listUserActions({ type: 'settings.update' })).toEqual([]);
+ expect(await listSettingsUpdates()).toEqual([]);
// Bypass probe: the same call with a real change DOES record, so the
// assertion above is about the no-op, not about the hook being dead.
await updateSettings({ timezone: 'Europe/Berlin' }, { actor: 'user' });
- expect(await listUserActions({ type: 'settings.update' })).toHaveLength(1);
+ expect(await listSettingsUpdates()).toHaveLength(1);
});
it('keeps two same-millisecond saves apart in the dedupe key', async () => {
@@ -108,7 +115,7 @@ describe('settings save → user_action_events (#5594)', () => {
await updateSettings({ theme: 'dark' }, { actor: 'user' });
await updateSettings({ locale: 'en-GB' }, { actor: 'user' });
- const events = await listUserActions({ type: 'settings.update' });
+ const events = await listSettingsUpdates();
expect(events).toHaveLength(2);
expect(new Set(events.map((e) => e.dedupeKey)).size).toBe(2);
expect(events.every((e) => e.dedupeKey.startsWith('settings.update:'))).toBe(true);
@@ -116,12 +123,12 @@ describe('settings save → user_action_events (#5594)', () => {
it('skipUserAction suppresses the generic settings.update row', async () => {
await updateSettings({ timezone: 'Europe/Berlin' }, { actor: 'user', skipUserAction: true });
- expect(await listUserActions({ type: 'settings.update' })).toEqual([]);
+ expect(await listSettingsUpdates()).toEqual([]);
});
it('records nothing for reloadSettings — a restore is not an operator edit', async () => {
seed({ timezone: 'Asia/Tokyo', theme: 'dark' });
await reloadSettings();
- expect(await listUserActions({ type: 'settings.update' })).toEqual([]);
+ expect(await listSettingsUpdates()).toEqual([]);
});
});