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: refuse settings saves when settings.json is unreadable instead of overwriting it

Adam Eivyc98d72be8d90
diff --git a/server/routes/settings.js b/server/routes/settings.js
index e655f0e8bc..f28c7ccca3 100644
--- a/server/routes/settings.js
+++ b/server/routes/settings.js
@@ -714,7 +714,7 @@ const saveSettingsPatch = async (body, slicePatch) => {
mergeFederationSlice(next, current),
current,
);
- }, { actor: 'user', ...(slicePatch ? { requireReadable: true } : {}) });
+ }, { actor: 'user' });
if (settingsPatch.instanceFeatures?.eidoverse !== undefined) {
const { reconcileEidoverseRuntime } = await import('../services/eidoverseRuntime.js');
await reconcileEidoverseRuntime()
diff --git a/server/services/settings.js b/server/services/settings.js
index 6c02dc7177..139b96992b 100644
--- a/server/services/settings.js
+++ b/server/services/settings.js
@@ -381,16 +381,32 @@ export const getSettingsWithStatus = async () => {
export const getSettings = async () => (await getSettingsWithStatus()).settings;
export const saveSettings = (settings, options) => queueWrite(() => save(settings, options));
+// The merge base for every partial write, read strictly inside the queued turn.
+// loadRaw() reads a present-but-unreadable or malformed file as `{}`, and a
+// partial write merged onto that replaced every setting (auth secret included)
+// with just its patch. A confirmed-absent file (fresh install) is a valid empty
+// base; anything else refuses until the file is repaired or restored.
+const loadWriteBase = async () => {
+ const { corrupt, settings } = await readSettingsStrict();
+ if (corrupt) {
+ const err = new Error('Cannot update settings: data/settings.json is unreadable or malformed — repair it or restore it from a backup');
+ err.code = 'SETTINGS_UNREADABLE';
+ throw err;
+ }
+ return hydratePrivateKeys(settings, PATHS.data);
+};
+
// Merge against the unstripped on-disk snapshot so save() sees every
// MortalLoom store key in one place — guaranteeing exactly one warning
// per updateSettings call, only when the write succeeds. The whole
// read-merge-write runs inside one queued turn so it merges against the
// freshest persisted snapshot, not a stale pre-image.
export const updateSettings = (patch, options) => queueWrite(async () => {
- const raw = await loadRaw();
+ const raw = await loadWriteBase();
+ const previous = structuredClone(stripStoreKeys(raw));
const incoming = isPlainObject(patch) ? patch : {};
const merged = { ...raw, ...incoming };
- return save(merged, options);
+ return save(merged, options, previous);
});
// Read-modify-write INSIDE the write queue. For callers whose merge semantics
@@ -403,19 +419,11 @@ export const updateSettings = (patch, options) => queueWrite(async () => {
// saveSettings(...)` pattern: there, a concurrent `updateSettings` landing
// between the external read and the queued write was clobbered by the caller's
// stale pre-image. Here the read and the write share one queued turn, so the
-// mutator always sees the latest persisted snapshot.
-// Pass { requireReadable: true } to refuse unreadable/malformed snapshots rather
-// than using the legacy empty fallback; a confirmed absent file is still valid.
+// mutator always sees the latest persisted snapshot. An unreadable or malformed
+// file refuses (loadWriteBase) rather than handing the mutator an empty base.
export const updateSettingsWith = (mutate, options) => queueWrite(async () => {
- // Partial mutations must never turn a failed read into an empty base. Keep
- // ordinary readers/legacy writers compatible, but opt this contract into the
- // strict reader inside the same queued turn as the mutation and write.
- const strict = options?.requireReadable ? await readSettingsStrict() : null;
- if (strict?.corrupt) throw new Error('Cannot update settings: persisted settings are unreadable or malformed');
- const current = stripStoreKeys(strict
- ? await hydratePrivateKeys(strict.settings, PATHS.data)
- : await loadRaw());
- const previous = strict ? structuredClone(current) : undefined;
+ const current = stripStoreKeys(await loadWriteBase());
+ const previous = structuredClone(current);
const next = await mutate(current);
// Guard the mutator's return BEFORE persisting: unlike the old
// saveSettings(objectLiteral) callers, a mutator with a missing `return`
diff --git a/server/services/settings.test.js b/server/services/settings.test.js
index f3c411bba2..b9b7e94f0d 100644
--- a/server/services/settings.test.js
+++ b/server/services/settings.test.js
@@ -384,6 +384,30 @@ describe('settings.js', () => {
});
});
+ describe('partial writes refuse an unreadable or malformed file', () => {
+ // Before this contract a failed read merged the patch onto `{}`, so one
+ // unrelated save replaced every persisted setting, including secrets.auth.
+ const writers = [
+ ['updateSettings', () => updateSettings({ theme: 'dark' })],
+ ['updateSettingsWith', () => updateSettingsWith((current) => ({ ...current, theme: 'dark' }))],
+ ];
+
+ for (const [name, write] of writers) {
+ it(`${name} refuses malformed content and an unreadable file, then recovers on a clean read`, async () => {
+ tryReadFile.mockResolvedValue('{ "secrets": { "auth": ');
+ await expect(write()).rejects.toMatchObject({ code: 'SETTINGS_UNREADABLE' });
+
+ tryReadFileStrict.mockResolvedValueOnce({ ok: false, value: null });
+ await expect(write()).rejects.toMatchObject({ code: 'SETTINGS_UNREADABLE' });
+ expect(atomicWrite).not.toHaveBeenCalled();
+
+ tryReadFile.mockResolvedValue(JSON.stringify({ secrets: { auth: { enabled: true } } }));
+ await write();
+ expect(JSON.parse(atomicWrite.mock.calls[0][1])).toEqual({ secrets: { auth: { enabled: true } }, theme: 'dark' });
+ });
+ }
+ });
+
describe('live settings restore boundary', () => {
it('drains admitted mutations and gives waiting mutations the restored warm-store base', async () => {
let disk = { exampleSetting: 'before' };