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): preserve omitted write-only tokens on PUT (#1821)

[._.]/ Adam Eivy3146a7ee1403
diff --git a/server/routes/settings.js b/server/routes/settings.js
index 058a62fc3c..9737dda247 100644
--- a/server/routes/settings.js
+++ b/server/routes/settings.js
@@ -1,6 +1,6 @@
import { Router } from 'express';
import { z } from 'zod';
-import { getSettings, updateSettings } from '../services/settings.js';
+import { getSettings, updateSettingsWith } from '../services/settings.js';
import { getAiAssignments, updateAiAssignment } from '../services/aiAssignments.js';
import {
setCodexParallelLimit,
@@ -58,6 +58,29 @@ const redactExternalTokens = (settings) => {
return next;
};
+// Write-path counterpart to the redaction above. Because GET /api/settings no
+// longer returns these tokens, a client that GETs settings, rebuilds a full
+// top-level object and PUTs it back (e.g. `patchSettingsSlice('imageGen.local',
+// …)`) would otherwise drop the persisted token — `updateSettings` shallow-
+// merges top-level keys, so an incoming `imageGen`/`civitai` replaces the
+// stored object wholesale. PUT /api/settings is never the write path for these
+// tokens (the dedicated /setup/hf-token and /loras/auth/civitai routes are), so
+// re-inject the persisted value whenever an incoming parent object omits it.
+// A parent absent from the patch needs nothing — the top-level merge keeps the
+// stored object (token included) untouched.
+const preserveWriteOnlyTokens = (next, current) => {
+ const carryOver = (parentKey, tokenKey) => {
+ const incoming = next[parentKey];
+ const stored = current?.[parentKey]?.[tokenKey];
+ if (isPlainObject(incoming) && !(tokenKey in incoming) && stored !== undefined) {
+ next[parentKey] = { ...incoming, [tokenKey]: stored };
+ }
+ };
+ carryOver('imageGen', 'hfToken');
+ carryOver('civitai', 'apiKey');
+ return next;
+};
+
// Single sanitizer every settings response (GET load + PUT save) runs through,
// so a leak can't reappear on one path after being closed on the other: strip
// the top-level `secrets` hierarchy, redact external tokens (#1821), then
@@ -156,7 +179,11 @@ router.put('/', asyncHandler(async (req, res) => {
// require. Secrets are write-only through their dedicated routes
// (/api/auth/password, /api/github/secrets, etc.).
const { secrets: _ignoredSecrets, catalogUserTypes: _ignoredTypes, ...settingsPatch } = req.body || {};
- const merged = await updateSettings(settingsPatch);
+ // updateSettingsWith (not updateSettings) so we can re-inject persisted
+ // write-only tokens the incoming patch omits, against the freshest snapshot
+ // inside the write queue (see preserveWriteOnlyTokens).
+ const merged = await updateSettingsWith((current) =>
+ preserveWriteOnlyTokens({ ...current, ...settingsPatch }, current));
// The queue caches codex.parallelLimit in-process; sync it from the
// merged value so a save takes effect without a restart and without
// re-reading the file.
diff --git a/server/routes/settings.secretsStrip.test.js b/server/routes/settings.secretsStrip.test.js
index 86603c2fb4..00bccc9f1c 100644
--- a/server/routes/settings.secretsStrip.test.js
+++ b/server/routes/settings.secretsStrip.test.js
@@ -148,4 +148,46 @@ describe('GET /api/settings — external token redaction', () => {
expect(persisted.imageGen?.hfToken).toBe('hf_secret123');
expect(persisted.civitai?.apiKey).toBe('civ_secret456');
});
+
+ // Because GET no longer returns the tokens, a client that rebuilds a full
+ // top-level object from a GET and PUTs it back (e.g. patchSettingsSlice)
+ // sends an `imageGen`/`civitai` object WITHOUT the token. The server must
+ // re-inject the persisted token so an unrelated slice save doesn't silently
+ // delete the credential.
+ it('preserves a persisted hfToken when a PUT replaces imageGen without it', async () => {
+ seedSettings({ imageGen: { hfToken: 'hf_keepme', mode: 'local' } });
+ const app = await buildApp();
+ const res = await request(app)
+ .put('/api/settings')
+ .send({ imageGen: { mode: 'external', local: { pythonPath: '/usr/bin/python3' } } });
+ expect(res.status).toBe(200);
+ const persisted = readSettingsFile();
+ expect(persisted.imageGen?.hfToken).toBe('hf_keepme');
+ expect(persisted.imageGen?.mode).toBe('external');
+ expect(persisted.imageGen?.local?.pythonPath).toBe('/usr/bin/python3');
+ // And the save response still doesn't echo the preserved token.
+ expect(res.body.imageGen?.hfToken).toBeUndefined();
+ });
+
+ it('preserves a persisted civitai.apiKey when a PUT replaces civitai without it', async () => {
+ seedSettings({ civitai: { apiKey: 'civ_keepme', autoDownload: true } });
+ const app = await buildApp();
+ const res = await request(app)
+ .put('/api/settings')
+ .send({ civitai: { autoDownload: false } });
+ expect(res.status).toBe(200);
+ const persisted = readSettingsFile();
+ expect(persisted.civitai?.apiKey).toBe('civ_keepme');
+ expect(persisted.civitai?.autoDownload).toBe(false);
+ });
+
+ it('leaves a persisted token untouched when the PUT omits its parent entirely', async () => {
+ seedSettings({ imageGen: { hfToken: 'hf_keepme' }, timezone: 'UTC' });
+ const app = await buildApp();
+ const res = await request(app).put('/api/settings').send({ timezone: 'America/Los_Angeles' });
+ expect(res.status).toBe(200);
+ const persisted = readSettingsFile();
+ expect(persisted.imageGen?.hfToken).toBe('hf_keepme');
+ expect(persisted.timezone).toBe('America/Los_Angeles');
+ });
});
diff --git a/server/routes/settings.test.js b/server/routes/settings.test.js
index 01e477a168..963cd56916 100644
--- a/server/routes/settings.test.js
+++ b/server/routes/settings.test.js
@@ -11,6 +11,12 @@ vi.mock('../services/settings.js', () => ({
store = { ...store, ...patch };
return { ...store };
}),
+ // The PUT handler uses updateSettingsWith so it can re-inject persisted
+ // write-only tokens omitted by the patch (see preserveWriteOnlyTokens).
+ updateSettingsWith: vi.fn(async (mutate) => {
+ store = await mutate({ ...store });
+ return { ...store };
+ }),
}));
vi.mock('../services/aiAssignments.js', () => ({
getAiAssignments: vi.fn(async () => ({})),