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(auth): preserve concurrent settings secrets (#12017)

Adam Eivy757dade47b6b
diff --git a/server/services/auth.js b/server/services/auth.js
index bbb62f5ed4..75c1ba8120 100644
--- a/server/services/auth.js
+++ b/server/services/auth.js
@@ -18,7 +18,7 @@ import {
parseSessionCookies,
sessionCookieNameFor,
} from '../../lib/portosAuthCore.js';
-import { getSettings, readSettingsStrict, settingsEvents, updateSettings } from './settings.js';
+import { getSettings, readSettingsStrict, settingsEvents, updateSettingsWith } from './settings.js';
import { ServerError } from '../lib/errorHandler.js';
// Auth gates the PortOS UI + API behind a single user-set password. PortOS is
@@ -240,16 +240,20 @@ export const setPassword = async ({ newPassword, currentPassword = null }) => {
}
const salt = randomBytes(SALT_BYTES).toString('hex');
const passwordHash = await hashPassword(newPassword, salt);
- const settings = await getSettings();
- const secrets = { ...(settings.secrets || {}) };
- secrets.auth = {
- enabled: true,
- kdf: 'scrypt',
- passwordHash,
- salt,
- updatedAt: new Date().toISOString(),
- };
- await updateSettings({ secrets, passwordRiskRevision: randomBytes(16).toString('hex') });
+ await updateSettingsWith((settings) => ({
+ ...settings,
+ secrets: {
+ ...(settings.secrets || {}),
+ auth: {
+ enabled: true,
+ kdf: 'scrypt',
+ passwordHash,
+ salt,
+ updatedAt: new Date().toISOString(),
+ },
+ },
+ passwordRiskRevision: randomBytes(16).toString('hex'),
+ }));
// Existing sessions are invalidated on password change — the user (or anyone
// holding a stolen token) starts over.
await revokeAllSessions();
@@ -268,10 +272,11 @@ export const clearPassword = async ({ currentPassword }) => {
if (!ok) {
throw new ServerError('Current password is incorrect', { status: 401, code: 'AUTH_BAD_CURRENT' });
}
- const settings = await getSettings();
- const secrets = { ...(settings.secrets || {}) };
- delete secrets.auth;
- await updateSettings({ secrets, passwordRiskRevision: randomBytes(16).toString('hex') });
+ await updateSettingsWith((settings) => {
+ const secrets = { ...(settings.secrets || {}) };
+ delete secrets.auth;
+ return { ...settings, secrets, passwordRiskRevision: randomBytes(16).toString('hex') };
+ });
await revokeAllSessions();
return { enabled: false };
};
diff --git a/server/services/auth.test.js b/server/services/auth.test.js
index bcafec8c73..9b5f03c192 100644
--- a/server/services/auth.test.js
+++ b/server/services/auth.test.js
@@ -3,6 +3,7 @@ import { rmSync } from 'fs';
import { mockPathsDataRoot } from '../lib/mockPathsDataRoot.js';
const { tempRoot, makeProxy, cleanup } = mockPathsDataRoot({ prefix: 'portos-auth-' });
+const { authTestHooks } = vi.hoisted(() => ({ authTestHooks: { hashGate: null } }));
vi.mock('../lib/fileUtils.js', async () => {
const actual = await vi.importActual('../lib/fileUtils.js');
@@ -16,8 +17,11 @@ vi.mock('../lib/fileUtils.js', async () => {
vi.mock('../../lib/portosAuthCore.js', async () => {
const actual = await vi.importActual('../../lib/portosAuthCore.js');
const testParams = { N: 1024, r: 8, p: 1, maxmem: 8 * 1024 * 1024 };
- const hashPassword = (password, salt) =>
- actual.__hashPasswordWithParamsForTests(password, salt, testParams);
+ const hashPassword = async (password, salt) => {
+ const hash = await actual.__hashPasswordWithParamsForTests(password, salt, testParams);
+ if (authTestHooks.hashGate) await authTestHooks.hashGate();
+ return hash;
+ };
return {
...actual,
hashPassword,
@@ -43,6 +47,7 @@ const resetSettings = () => {
beforeEach(() => {
vi.resetModules();
+ authTestHooks.hashGate = null;
resetSettings();
});
@@ -131,6 +136,81 @@ describe('auth service', () => {
expect(await auth.isAuthEnabled()).toBe(false);
});
+ it('preserves a concurrent credential write when setting the password', async () => {
+ const auth = await import('./auth.js');
+ const { getSettings, updateSettingsWith } = await import('./settings.js');
+ let releaseWrite;
+ let writeStarted;
+ const started = new Promise((resolve) => { writeStarted = resolve; });
+ const heldWrite = updateSettingsWith(async (settings) => {
+ writeStarted();
+ await new Promise((resolve) => { releaseWrite = resolve; });
+ return {
+ ...settings,
+ secrets: { ...(settings.secrets || {}), integration: { apiKey: 'example-key' } },
+ };
+ });
+ await started;
+
+ let releaseHash;
+ let notifyHashStarted;
+ const hashStarted = new Promise((resolve) => { notifyHashStarted = resolve; });
+ authTestHooks.hashGate = () => {
+ notifyHashStarted();
+ return new Promise((resolve) => { releaseHash = resolve; });
+ };
+ const passwordChange = auth.setPassword({ newPassword: 'correct-horse' });
+ await hashStarted;
+ authTestHooks.hashGate = null;
+ releaseHash();
+ await new Promise((resolve) => setImmediate(resolve));
+ releaseWrite();
+ await Promise.all([heldWrite, passwordChange]);
+
+ const settings = await getSettings();
+ expect(settings.secrets.integration.apiKey).toBe('example-key');
+ expect(settings.secrets.auth.enabled).toBe(true);
+ });
+
+ it('preserves a concurrent credential write when clearing the password', async () => {
+ const auth = await import('./auth.js');
+ const { getSettings, updateSettingsWith } = await import('./settings.js');
+ await auth.setPassword({ newPassword: 'correct-horse' });
+
+ let releaseWrite;
+ let writeStarted;
+ const started = new Promise((resolve) => { writeStarted = resolve; });
+ const heldWrite = updateSettingsWith(async (settings) => {
+ writeStarted();
+ await new Promise((resolve) => { releaseWrite = resolve; });
+ return {
+ ...settings,
+ secrets: { ...(settings.secrets || {}), integration: { apiKey: 'example-key' } },
+ };
+ });
+ await started;
+
+ let releaseHash;
+ let notifyHashStarted;
+ const hashStarted = new Promise((resolve) => { notifyHashStarted = resolve; });
+ authTestHooks.hashGate = () => {
+ notifyHashStarted();
+ return new Promise((resolve) => { releaseHash = resolve; });
+ };
+ const passwordClear = auth.clearPassword({ currentPassword: 'correct-horse' });
+ await hashStarted;
+ authTestHooks.hashGate = null;
+ releaseHash();
+ await new Promise((resolve) => setImmediate(resolve));
+ releaseWrite();
+ await Promise.all([heldWrite, passwordClear]);
+
+ const settings = await getSettings();
+ expect(settings.secrets.integration.apiKey).toBe('example-key');
+ expect(settings.secrets.auth).toBeUndefined();
+ expect(await auth.isAuthEnabled()).toBe(false);
+ });
+
it('revokes individual sessions', async () => {
const auth = await import('./auth.js');
await auth.setPassword({ newPassword: 'correct-horse' });