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: avoid negative Basic auth cache entries (#5135)

[._.]/ Adam Eivy34cff821747b
diff --git a/server/services/authGate.js b/server/services/authGate.js
index 338aa002d8..3319971f9a 100644
--- a/server/services/authGate.js
+++ b/server/services/authGate.js
@@ -29,23 +29,29 @@ const PUBLIC_API_PATHS = new Set([
// future routes that live outside `/api`.
const GATED_NON_API_PREFIXES = ['/sdapi/'];
-// Short-lived cache for Basic-auth scrypt results so probe cycles (3 parallel
-// HTTP requests every 30s) don't re-run scrypt each time. Keyed by sha256 of
-// the password so the plaintext never lives in the Map. Flushed on any
-// settings write (covers password rotation).
+// Short-lived cache for successful Basic-auth scrypt results so probe cycles
+// (3 parallel HTTP requests every 30s) don't re-run scrypt each time. Failed
+// results are deliberately not cached so a corrected credential is retried
+// immediately. Keyed by sha256 so plaintext never lives in the Map. Flushed
+// on any settings write (covers password rotation).
const basicAuthCache = new Map();
const BASIC_AUTH_CACHE_TTL_MS = 60_000;
settingsEvents.on('settings:updated', () => basicAuthCache.clear());
const verifyBasicPassword = async (password) => {
+ if (typeof password !== 'string' || password.length === 0) return false;
const key = createHash('sha256').update(password).digest('hex');
const cached = basicAuthCache.get(key);
if (cached && cached.expiresAt > Date.now()) return cached.ok;
const ok = await verifyPassword(password);
- basicAuthCache.set(key, { ok, expiresAt: Date.now() + BASIC_AUTH_CACHE_TTL_MS });
+ if (ok) {
+ basicAuthCache.set(key, { ok: true, expiresAt: Date.now() + BASIC_AUTH_CACHE_TTL_MS });
+ }
return ok;
};
+export const __testing = { verifyBasicPassword };
+
const isPublicPath = (path) => {
if (PUBLIC_API_PATHS.has(path)) return true;
// /api/* and /data/* are always gated. /sdapi/* (and any future non-/api
diff --git a/server/services/authGate.test.js b/server/services/authGate.test.js
index 3e5fdfa66f..60454575f0 100644
--- a/server/services/authGate.test.js
+++ b/server/services/authGate.test.js
@@ -1,5 +1,5 @@
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
-import { writeFileSync, rmSync } from 'fs';
+import { readFileSync, writeFileSync, rmSync } from 'fs';
import { join } from 'path';
import { mockPathsDataRoot } from '../lib/mockPathsDataRoot.js';
import { bindSettingsFile } from '../lib/settingsTestUtil.js';
@@ -425,6 +425,35 @@ describe('authGate HTTP Basic auth (peer federation)', () => {
expect(result.called).toBe(false);
expect(result.res.statusCode).toBe(401);
});
+
+ it('does not cache failed password verification', async () => {
+ const auth = await import('./auth.js');
+ await auth.setPassword({ newPassword: 'future-secret' });
+ const futureSettings = JSON.parse(readFileSync(join(tempRoot, 'settings.json'), 'utf-8'));
+ await auth.setPassword({
+ newPassword: 'current-secret',
+ currentPassword: 'future-secret',
+ });
+ const { __testing } = await import('./authGate.js');
+
+ expect(await __testing.verifyBasicPassword('future-secret')).toBe(false);
+
+ // Replace the backing credential without emitting settings:updated. A failed
+ // verification must not shadow the now-valid password for the cache TTL.
+ await mergeSettingsFile({ secrets: futureSettings.secrets });
+ expect(await __testing.verifyBasicPassword('future-secret')).toBe(true);
+ });
+
+ it.each([null, undefined, 123, {}, []])(
+ 'rejects malformed Basic password value %j without throwing',
+ async (password) => {
+ const auth = await import('./auth.js');
+ await auth.setPassword({ newPassword: 'peer-secret' });
+ const { __testing } = await import('./authGate.js');
+
+ await expect(__testing.verifyBasicPassword(password)).resolves.toBe(false);
+ },
+ );
});
describe('socketAuthGate middleware', () => {