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 (codex): don't cache a corrupt settings read so verifyPassword self-heals

[._.]/ Adam Eivy7428669138c7
diff --git a/server/services/settings.js b/server/services/settings.js
index 913d3c21d3..dca76586d4 100644
--- a/server/services/settings.js
+++ b/server/services/settings.js
@@ -204,14 +204,28 @@ const save = async (settings) => {
export const getSettings = async () => {
if (settingsCache === null) {
- // stripStoreKeys builds a fresh top-level object over the just-parsed
- // loadRaw() graph, which nothing else references — safe to cache directly.
- const loaded = stripStoreKeys(await loadRaw());
+ const raw = await tryReadFile(SETTINGS_FILE);
+ // Distinguish a CORRUPT (present but unparseable) settings.json from an
+ // absent/empty one so a corrupt read does NOT poison the cache with `{}`
+ // (issue #2684). Caching a corrupt-derived empty object would strand every
+ // consumer — verifyPassword, schedulers, feature reads — on empty settings
+ // until a save() or restart, defeating the no-restart self-heal that the
+ // auth fail-closed path promises. A malformed file (`raw` present but not a
+ // JSON object) is the corrupt case handled here; `tryReadFile` collapses an
+ // absent OR unreadable file to null, which we treat as an empty, cacheable
+ // snapshot (absent is the fresh-install default; the rarer unreadable case is
+ // still failed CLOSED by isAuthEnabled's stricter readSettingsStrict).
+ const parsed = raw === null ? {} : safeJSONParse(raw, null);
+ const corrupt = raw !== null && !isPlainObject(parsed);
+ const loaded = stripStoreKeys(isPlainObject(parsed) ? parsed : {});
// A save()/reloadSettings() may have populated the cache via the
// settings:updated listener while this cold read was awaiting the disk read.
- // Only fill if still empty, so an older on-disk snapshot can't clobber that
- // fresher in-memory value and strand consumers on stale settings.
- if (settingsCache === null) settingsCache = loaded;
+ // Prefer that fresher in-memory value over our (older) on-disk snapshot.
+ if (settingsCache !== null) return structuredClone(settingsCache);
+ // On a corrupt read, hand back the empty snapshot WITHOUT caching it, so the
+ // very next call re-reads and picks up a repair immediately.
+ if (corrupt) return structuredClone(loaded);
+ settingsCache = loaded;
}
// Hand out a private deep copy so a caller mutating nested settings in place
// can't corrupt the shared cache — matching the prior per-call
diff --git a/server/services/settings.test.js b/server/services/settings.test.js
index a3f4915500..23f54184c4 100644
--- a/server/services/settings.test.js
+++ b/server/services/settings.test.js
@@ -59,6 +59,20 @@ describe('settings.js', () => {
expect(result).toEqual({});
});
+ it('does not cache a corrupt read, so a repaired file is picked up next call (#2684)', async () => {
+ // A malformed settings.json must NOT poison the cache with {} — otherwise
+ // verifyPassword() and every other consumer stay stranded on empty settings
+ // (rejecting the correct password) until a save or restart.
+ tryReadFile.mockResolvedValueOnce('{ truncated');
+ const first = await getSettings();
+ expect(first).toEqual({});
+
+ // File repaired: the next read must reflect it, proving {} was not cached.
+ tryReadFile.mockResolvedValue(JSON.stringify({ secrets: { auth: { enabled: true, passwordHash: 'h', salt: 's' } } }));
+ const second = await getSettings();
+ expect(second.secrets.auth.enabled).toBe(true);
+ });
+
it('should handle complex nested settings', async () => {
const mockSettings = {
display: {