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

refactor: share object validation across storage and creative tools (#7868)

[._.]/ Adam Eivy65f5aa8e2c20
diff --git a/server/lib/collectionStore.js b/server/lib/collectionStore.js
index 44d50e77aa..433ced201d 100644
--- a/server/lib/collectionStore.js
+++ b/server/lib/collectionStore.js
@@ -85,18 +85,9 @@ import { join } from 'path';
import { readdir, lstat, rm } from 'fs/promises';
import { atomicWrite, readJSONFile, readJSONFileStrict, unreadableStoreError, ensureDir } from './fileUtils.js';
import { createFileWriteQueue, createRecordWriteQueue } from './fileWriteQueue.js';
+import { isPlainObject, POLLUTING_KEYS } from './objects.js';
import { isVitestRunner } from './runtimeEnv.js';
-const isPlainObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
-
-// Reserved names that must never become a record-directory id. They pass the
-// default (and any typical) `idPattern` — a legacy JSON map can carry them as
-// own keys — but naming a record `__proto__`/`constructor`/`prototype` invites
-// prototype-pollution confusion and a weird on-disk dir. Rejecting them in
-// `isValidId` (below) makes the store the single owner of "what id may I hold,"
-// so consumers don't each re-guard the triple.
-const RESERVED_IDS = new Set(['__proto__', 'constructor', 'prototype']);
-
/**
* The type-index `config` slot — cross-record state owned by the collection as
* a whole. See the header doc block for the convention. All keys are optional;
@@ -163,7 +154,9 @@ export function createCollectionStore({
// uses readdir as the source of truth whenever the collection dir exists.
const knownIds = new Set();
- const isValidId = (id) => typeof id === 'string' && idPattern.test(id) && !RESERVED_IDS.has(id);
+ // Reject prototype-polluting names even when they pass the id pattern, so
+ // consumers do not each need to guard legacy JSON keys used as record ids.
+ const isValidId = (id) => typeof id === 'string' && idPattern.test(id) && !POLLUTING_KEYS.has(id);
// Per-record write queue. Tail-chained per `id` so two writes against the
// same record serialize while writes against different records proceed in
diff --git a/server/lib/eidoverseFoundationSandbox.js b/server/lib/eidoverseFoundationSandbox.js
index 05dbf7d258..3480986f04 100644
--- a/server/lib/eidoverseFoundationSandbox.js
+++ b/server/lib/eidoverseFoundationSandbox.js
@@ -49,15 +49,13 @@
* injected so this module never imports the service layer.
*/
-import { canonicalStringify } from './objects.js';
+import { canonicalStringify, isPlainObject } from './objects.js';
import { generateDistrictTemplatePlacement } from './eidoverseCreativeToolkit.js';
// Declared beside the schemas that carry the label rather than here, so this
// module can keep reaching the creative toolkit without closing an ESM cycle
// back through it — see `derivedContributionId`'s own note.
import { controllerDefinitionIdFromBody, derivedContributionId } from './eidoverseFoundations.js';
-const isPlainObject = (value) => Boolean(value) && typeof value === 'object' && !Array.isArray(value);
-
const refuse = (refusal) => ({ contribution: null, refusal });
const accept = (contribution) => ({ contribution, refusal: null });
diff --git a/server/lib/eidoverseFoundations.js b/server/lib/eidoverseFoundations.js
index ad0e345523..6bffdec825 100644
--- a/server/lib/eidoverseFoundations.js
+++ b/server/lib/eidoverseFoundations.js
@@ -66,7 +66,7 @@
import { createHash } from 'node:crypto';
import { z } from 'zod';
-import { canonicalStringify } from './objects.js';
+import { canonicalStringify, isPlainObject } from './objects.js';
import { describeJsonPath, federationSafetyFindings, walkJsonText } from './federationSafety.js';
/** Ownership layers, widest-shared first. See the module header. */
@@ -152,8 +152,6 @@ const contributionIdSchema = z.string().trim().min(1).max(120);
/** A shipped-definition / foundation slug. Mirrors `foundationIdSchema`. */
const CONTROLLER_DEFINITION_SLUG = /^[a-z0-9][a-z0-9-]{0,63}$/;
-const isPlainObject = (value) => Boolean(value) && typeof value === 'object' && !Array.isArray(value);
-
/**
* The shipped controller definition id a `controller` foundation names, or
* `null` when its body names none. Exported because `eidoverseFoundationSandbox.js`
diff --git a/server/lib/loraDataset.js b/server/lib/loraDataset.js
index 2010860f47..c44027540c 100644
--- a/server/lib/loraDataset.js
+++ b/server/lib/loraDataset.js
@@ -12,6 +12,7 @@
* `server/services/loraDatasets.js`.
*/
+import { isPlainObject } from './objects.js';
import { escapeRegExp } from './textUtils.js';
export const LORA_DATASET_SCHEMA_VERSION = 1;
@@ -38,7 +39,6 @@ const TRIGGER_WORD_RE = /^[a-z0-9_]{2,64}$/;
export const isValidTriggerWord = (word) => typeof word === 'string' && TRIGGER_WORD_RE.test(word);
const trim = (s) => (typeof s === 'string' ? s.trim() : '');
-const isPlainObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
const oneOf = (v, list, fallback) => (list.includes(v) ? v : fallback);
const isoOrNull = (v) => (typeof v === 'string' && v ? v : null);
diff --git a/server/services/fableLoom/editorial.js b/server/services/fableLoom/editorial.js
index 1125e2dbb8..adb6623a21 100644
--- a/server/services/fableLoom/editorial.js
+++ b/server/services/fableLoom/editorial.js
@@ -46,6 +46,7 @@ import {
sanitizeLoom,
} from './records.js';
import { trimTo, isNonBlankStr } from '../../lib/textUtils.js';
+import { isPlainObject } from '../../lib/objects.js';
const REVIEW_SEVERITIES = new Set(['high', 'medium', 'low']);
const REVIEW_CATEGORIES = new Set([
@@ -635,7 +636,6 @@ const collectionHasContent = (value) => (
|| (value && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length > 0)
);
-const isPlainObject = (value) => value && typeof value === 'object' && !Array.isArray(value);
const isOptionalId = (value) => value === undefined
|| (typeof value === 'string' && value.trim().length > 0);
const isPlanItem = (value) => isPlainObject(value)