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([patch-settings-slice-helper]): guard against non-object nodes and empty path segments

[._.]/ Adam Eivy39a17c158b62
diff --git a/client/src/services/apiSystem.js b/client/src/services/apiSystem.js
index 619933af5c..aa499b7ed1 100644
--- a/client/src/services/apiSystem.js
+++ b/client/src/services/apiSystem.js
@@ -39,28 +39,38 @@ export const updateSettings = (data, options) => request('/settings', {
...options
});
+const isPlainObject = (v) =>
+ v != null && typeof v === 'object' && !Array.isArray(v);
+
// PUT /api/settings shallow-merges top-level keys only — patching
// `{ imageGen: { local: { pythonPath } } }` directly clobbers the rest of
// `imageGen`. patchSettingsSlice fetches current settings, walks to the slice
// at a dotted path, shallow-merges `partial` into it, and PUTs the rebuilt
// top-level key so sibling subkeys survive. `options` flows through to the
// final updateSettings (e.g. `{ silent: true }`).
+//
+// Non-object values along the path (a hand-edited settings.json with an
+// unexpected primitive/array at a slice or parent) are treated as absent so
+// they don't spread into character-indexed or numeric-indexed keys.
export const patchSettingsSlice = async (slicePath, partial, options = {}) => {
if (typeof slicePath !== 'string' || !slicePath) {
throw new Error('patchSettingsSlice: slicePath required');
}
- if (partial == null || typeof partial !== 'object' || Array.isArray(partial)) {
+ if (!isPlainObject(partial)) {
throw new Error('patchSettingsSlice: partial must be a plain object');
}
const segments = slicePath.split('.');
+ if (!segments.every(Boolean)) {
+ throw new Error('patchSettingsSlice: slicePath has empty segments');
+ }
const current = await getSettings({ silent: true }).catch(() => ({}));
let existing = current;
for (const seg of segments) existing = existing?.[seg];
- let updated = { ...(existing || {}), ...partial };
+ let updated = { ...(isPlainObject(existing) ? existing : {}), ...partial };
for (let i = segments.length - 1; i > 0; i--) {
let parent = current;
for (let j = 0; j < i; j++) parent = parent?.[segments[j]];
- updated = { ...(parent || {}), [segments[i]]: updated };
+ updated = { ...(isPlainObject(parent) ? parent : {}), [segments[i]]: updated };
}
return updateSettings({ [segments[0]]: updated }, options);
};
diff --git a/client/src/services/apiSystem.test.js b/client/src/services/apiSystem.test.js
index 7aee670e42..aceb2023d3 100644
--- a/client/src/services/apiSystem.test.js
+++ b/client/src/services/apiSystem.test.js
@@ -106,4 +106,34 @@ describe('patchSettingsSlice', () => {
await expect(patchSettingsSlice('imageGen', [])).rejects.toThrow(/plain object/);
await expect(patchSettingsSlice('imageGen', 'foo')).rejects.toThrow(/plain object/);
});
+
+ it('throws on empty path segments', async () => {
+ await expect(patchSettingsSlice('imageGen..local', { x: 1 })).rejects.toThrow(/empty segments/);
+ await expect(patchSettingsSlice('imageGen.', { x: 1 })).rejects.toThrow(/empty segments/);
+ await expect(patchSettingsSlice('.imageGen', { x: 1 })).rejects.toThrow(/empty segments/);
+ });
+
+ it('treats a non-object slice value as absent (does not spread strings into chars)', async () => {
+ mockSettings({ imageGen: { local: 'oops-not-an-object' } });
+ await patchSettingsSlice('imageGen.local', { pythonPath: '/x' });
+ const putCall = request.mock.calls.find(([, opts]) => opts?.method === 'PUT');
+ const body = JSON.parse(putCall[1].body);
+ expect(body).toEqual({ imageGen: { local: { pythonPath: '/x' } } });
+ });
+
+ it('treats a non-object parent as absent', async () => {
+ mockSettings({ imageGen: 'oops' });
+ await patchSettingsSlice('imageGen.local', { pythonPath: '/x' });
+ const putCall = request.mock.calls.find(([, opts]) => opts?.method === 'PUT');
+ const body = JSON.parse(putCall[1].body);
+ expect(body).toEqual({ imageGen: { local: { pythonPath: '/x' } } });
+ });
+
+ it('treats an array at the slice as absent', async () => {
+ mockSettings({ imageGen: { local: [1, 2, 3] } });
+ await patchSettingsSlice('imageGen.local', { pythonPath: '/x' });
+ const putCall = request.mock.calls.find(([, opts]) => opts?.method === 'PUT');
+ const body = JSON.parse(putCall[1].body);
+ expect(body).toEqual({ imageGen: { local: { pythonPath: '/x' } } });
+ });
});