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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: guard request- and peer-keyed counter maps against prototype key names (#12498)
[._.]/ Adam Eivy89a48063d50a
diff --git a/server/lib/README.md b/server/lib/README.mdindex 8c58a20727..d07aa14449 100644--- a/server/lib/README.md+++ b/server/lib/README.md@@ -731,7 +731,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub| `syncManifest.js` | Wire contract for the snapshot-sync MANIFEST leg — the fix for a category that is always dirty AND a map of per-instance slots (`usage`), where one instance advancing dragged every other instance's digest across the wire. `isManifestEnvelope(value)` validates a `{ data: { instances }, checksum }` response (a legacy peer that 404s the endpoint fails it, and the puller falls back to the whole snapshot); `diffManifestSlots(remoteInstances, localInstances)` returns the sorted slot ids whose REMOTE LWW stamp is strictly newer than ours — exactly the slots worth fetching. Comparison goes through `lwwTimestamp.js`, so a tie breaks to what we already hold. Consumed by `syncOrchestrator.syncDataCategoryFromPeer`; served by `dataSync.getManifest`. || `mapWithConcurrency.js` | Generic bounded-concurrency async mapper that preserves input order while capping in-flight work. || `markedSection.js` | Marker-delimited section replacement (pure). `buildMarkers(id)` → `{ start, end }` HTML-comment marker pair; `replaceMarkedSection(content, body, markers)` splices/replaces/removes an auto-generated region without touching surrounding user content (idempotent); `extractMarkedSection` / `hasMarkedSection` read it back. Powers the daily-log activity-digest auto-drafts (#2155) via `brainJournal.upsertAutoSection()`. |-| `objects.js` | Object utilities — `deepMerge` (recursive merge w/ array replacement), `isPlainObject` (non-null, non-array `object` guard for JSON / LLM payloads), `POLLUTING_KEYS` (shared `__proto__`/`constructor`/`prototype` denylist for sanitizers), `canonicalStringify` (recursive sorted-key JSON serialization for cross-machine content hashing), `isEmptyScalar` (true for null/undefined/whitespace-string/empty-array — merge gap-fill gate), `boundedByJsonChars` (longest prefix of a list whose JSON encoding fits a character budget — the shared prompt-catalog truncation loop). |+| `objects.js` | Object utilities — `deepMerge` (recursive merge w/ array replacement), `isPlainObject` (non-null, non-array `object` guard for JSON / LLM payloads), `POLLUTING_KEYS` (shared `__proto__`/`constructor`/`prototype` denylist for sanitizers), `bucketFor(map, key, init)` (get-or-create a bucket for an externally supplied key; `null` for `POLLUTING_KEYS`, `hasOwn` lookup), `canonicalStringify` (recursive sorted-key JSON serialization for cross-machine content hashing), `isEmptyScalar` (true for null/undefined/whitespace-string/empty-array — merge gap-fill gate), `boundedByJsonChars` (longest prefix of a list whose JSON encoding fits a character budget — the shared prompt-catalog truncation loop). || `openapiSpec.js` | Builds two OpenAPI 3.0.3 documents: the Settings-controlled exposed public surface and the complete internal HTTP inventory. Detailed operations reuse canonical Zod contracts; generated operations are visibly labeled rather than claiming unmodeled schemas. || `openapiDowngrade.js` | `OPENAPI_VERSION` plus `toOpenApi30Schema` / `toOpenApi30Operation`, which rewrite Zod's draft-2020-12 output (null unions, numeric exclusive bounds, `const`, tuples, `propertyNames`) into the OpenAPI 3.0.3 dialect. Apply ONLY at the OpenAPI document boundary — the AsyncAPI payloads, CoS provider tool definitions, and tool resource read the same schemas as plain JSON Schema, where these rewrites silently widen bounds and drop null branches. || `orchestrationProfile.js` | Orchestrated CoS execution (#5992) — `ORCHESTRATION_MODES` / `ORCHESTRATION_ROLES` (architect, implementer, reviewer), `normalizeOrchestrationProfile` / `normalizeOrchestrationMode`, `isOrchestratedTask` / `roleAssignment` (both inert on a `direct` task, so a stored profile stays opt-in), the six-part `SPEC_PARTS` contract a delegated context-free lane needs, plus `parseReasoningDirective` / `resolveStepEffort` (spec rung → role default → run effort; an unsupported rung is an error, never a silent downgrade). |diff --git a/server/lib/objects.js b/server/lib/objects.jsindex 14cbae0744..dad74f906c 100644--- a/server/lib/objects.js+++ b/server/lib/objects.js@@ -54,6 +54,17 @@ export const isEmptyScalar = (v) => v === null || v === undefined || (typeof v =// it (drift risk on security-relevant constants).export const POLLUTING_KEYS = new Set(['__proto__', 'constructor', 'prototype']);+// Get-or-create a bucket in a map keyed by an externally supplied string (request+// body, upload, peer payload, model output). Returns null for POLLUTING_KEYS so the+// caller skips the record; a bare `map[key] ||= …` would return Object.prototype for+// `__proto__` and bump a counter on every object in the process. `hasOwn` keeps+// inherited names such as `toString` from being mistaken for an existing bucket.+export const bucketFor = (map, key, init) => {+ if (POLLUTING_KEYS.has(key)) return null;+ if (!Object.hasOwn(map, key)) map[key] = init();+ return map[key];+};+export const deepMerge = (base, patch) => {if (!isPlainObject(patch)) return patch === undefined ? base : patch;const out = { ...(isPlainObject(base) ? base : {}) };diff --git a/server/lib/objects.test.js b/server/lib/objects.test.jsindex b7056b46e8..fd4c9cf04b 100644--- a/server/lib/objects.test.js+++ b/server/lib/objects.test.js@@ -1,5 +1,27 @@import { describe, it, expect } from 'vitest';-import { deepMerge, isEmptyScalar, isPlainObject, canonicalStringify } from './objects.js';+import { deepMerge, isEmptyScalar, isPlainObject, canonicalStringify, bucketFor } from './objects.js';++describe('bucketFor', () => {+ // Guard for request/peer-keyed maps: a polluting key must never reach the+ // assignment, and an inherited name like `toString` must still get its own bucket.+ it('refuses prototype-polluting keys and leaves Object.prototype untouched', () => {+ const map = {};+ for (const key of ['__proto__', 'constructor', 'prototype']) {+ expect(bucketFor(map, key, () => ({ count: 0 }))).toBeNull();+ }+ expect(Object.keys(map)).toEqual([]);+ expect(Object.getPrototypeOf(map)).toBe(Object.prototype);+ expect(Object.prototype).not.toHaveProperty('count');+ });++ it('creates an own bucket for inherited Object.prototype names', () => {+ const map = {};+ const bucket = bucketFor(map, 'toString', () => ({ count: 0 }));+ bucket.count++;+ expect(Object.hasOwn(map, 'toString')).toBe(true);+ expect(bucketFor(map, 'toString', () => ({ count: 0 })).count).toBe(1);+ });+});describe('isPlainObject', () => {it('returns true for plain `{}`-shaped values', () => {diff --git a/server/services/appleHealthClinical.js b/server/services/appleHealthClinical.jsindex eb17a0c363..53fde50f45 100644--- a/server/services/appleHealthClinical.js+++ b/server/services/appleHealthClinical.js@@ -10,6 +10,7 @@ import { atomicWrite, safeJSONParse } from '../lib/fileUtils.js';import { getBloodTests } from './meatspaceHealth.js';import { join } from 'path';import { PATHS, ensureDir } from '../lib/fileUtils.js';+import { POLLUTING_KEYS } from '../lib/objects.js';const BLOOD_TESTS_FILE = join(PATHS.meatspace, 'blood-tests.json');@@ -159,8 +160,11 @@ export function parseFhirLabResult(jsonStr) {const displayName = resource.code?.text || loincEntry?.display || '';// Map to key: LOINC lookup first, then fallback to snake_case of display name- const key = (loincCode && LOINC_TO_KEY[loincCode]) || toSnakeCase(displayName);- if (!key) return null;+ // hasOwn: LOINC_TO_KEY inherits `constructor` & co., which would otherwise map an export's code to a function.+ const mapped = loincCode && Object.hasOwn(LOINC_TO_KEY, loincCode) ? LOINC_TO_KEY[loincCode] : null;+ const key = mapped || toSnakeCase(displayName);+ // The key indexes byDate[date] and refRanges, so a polluting name would write through to Object.prototype.+ if (!key || POLLUTING_KEYS.has(key)) return null;// Extract reference range if availableconst refRange = resource.referenceRange?.[0];diff --git a/server/services/appleHealthClinical.test.js b/server/services/appleHealthClinical.test.jsindex 65702da2ef..576bb278f1 100644--- a/server/services/appleHealthClinical.test.js+++ b/server/services/appleHealthClinical.test.js@@ -132,6 +132,21 @@ describe('parseFhirLabResult', () => {// =============================================================================describe('processClinicalRecords', () => {+ it('keeps export-supplied names from writing through to Object.prototype', () => {+ const jsons = [+ // A LOINC code inherited from Object.prototype must not map to a function key.+ makeFhirObservation({ code: 'constructor', display: 'Hostile', value: 1, date: '2025-01-15T10:00:00Z' }),+ // A display name that snake-cases to a polluting key is skipped, not stored.+ makeFhirObservation({ display: 'Prototype', value: 2, date: '2025-01-15T10:00:00Z', refLow: 0, refHigh: 2 }),+ makeFhirObservation({ display: 'Glucose', value: 95, date: '2025-01-15T10:00:00Z' }),+ ];+ const result = processClinicalRecords(jsons);+ expect(result.tests).toEqual([{ date: '2025-01-15', hostile: 1, glucose: 95 }]);+ expect(result.referenceRanges).toEqual({});+ expect(Object.prototype).not.toHaveProperty('value');+ expect(Object.prototype).not.toHaveProperty('label');+ });+it('groups results by date and collects reference ranges', () => {const jsons = [makeFhirObservation({ code: '2345-7', display: 'Glucose', value: 95, date: '2025-01-15T10:00:00Z', refLow: 70, refHigh: 99 }),diff --git a/server/services/appleHealthXml.js b/server/services/appleHealthXml.jsindex 138e1e9473..6c27c5a3ea 100644--- a/server/services/appleHealthXml.js+++ b/server/services/appleHealthXml.js@@ -10,6 +10,7 @@ import { createReadStream } from 'fs';import { unlink } from 'fs/promises';import { extractDateStr, readDayFile, writeDayFile, upsertPoints, queueHealthDayMutation } from './appleHealthIngest.js';import { createAppleHealthRecordStream } from './appleHealthXmlParser.js';+import { bucketFor } from '../lib/objects.js';// === Mapping Tables ===@@ -311,9 +312,11 @@ export async function importAppleHealthXml(filePath, io = null) {const { metricName, dateStr, dataPoint } = normalized;- if (!dayBuckets[dateStr]) dayBuckets[dateStr] = {};- if (!dayBuckets[dateStr][metricName]) dayBuckets[dateStr][metricName] = [];- dayBuckets[dateStr][metricName].push(dataPoint);+ // dateStr and metricName both come from the uploaded export.+ const metrics = bucketFor(dayBuckets, dateStr, () => ({}));+ const points = metrics && bucketFor(metrics, metricName, () => []);+ if (!points) return;+ points.push(dataPoint);allDays.add(dateStr);processedRecords++;diff --git a/server/services/cosAgentFeedback.js b/server/services/cosAgentFeedback.jsindex 7ce5767138..36e7b9c1ca 100644--- a/server/services/cosAgentFeedback.js+++ b/server/services/cosAgentFeedback.js@@ -21,6 +21,7 @@ import {recordArchivedCompletions,} from './cosAgentCompletionIndex.js';import { ServerError } from '../lib/errorHandler.js';+import { bucketFor } from '../lib/objects.js';import { recordUserAction } from './userActions.js';import {FEEDBACK_RATINGS,@@ -393,11 +394,10 @@ export async function getFeedbackStats() {metadata: a.metadata,taskType: a.metadata?.taskType,});- if (!byTaskType[taskType]) {- byTaskType[taskType] = { positive: 0, negative: 0, neutral: 0, total: 0 };- }- byTaskType[taskType][a.feedback.rating]++;- byTaskType[taskType].total++;+ const bucket = bucketFor(byTaskType, taskType, () => ({ positive: 0, negative: 0, neutral: 0, total: 0 }));+ if (!bucket) return;+ bucket[a.feedback.rating]++;+ bucket.total++;});// Recent feedback (last 10 with comments)diff --git a/server/services/feedbackLoop.js b/server/services/feedbackLoop.jsindex c4dade8f91..faf7dae325 100644--- a/server/services/feedbackLoop.js+++ b/server/services/feedbackLoop.js@@ -10,6 +10,7 @@ import { join } from 'path';import { v4 as uuidv4 } from '../lib/uuid.js';import { atomicWrite, ensureDir, readJSONFile, PATHS } from '../lib/fileUtils.js';import { createFileWriteQueue } from '../lib/fileWriteQueue.js';+import { bucketFor } from '../lib/objects.js';import { digitalTwinEvents } from './digital-twin.js';const DIGITAL_TWIN_DIR = PATHS.digitalTwin;@@ -197,11 +198,10 @@ export async function getFeedbackStats() {const byProvider = {};for (const e of entries) {const key = e.model || e.providerId || 'unknown';- if (!byProvider[key]) {- byProvider[key] = { total: 0, sounds_like_me: 0, not_quite: 0, doesnt_sound_like_me: 0 };- }- byProvider[key].total++;- byProvider[key][e.validation]++;+ const bucket = bucketFor(byProvider, key, () => ({ total: 0, sounds_like_me: 0, not_quite: 0, doesnt_sound_like_me: 0 }));+ if (!bucket) continue;+ bucket.total++;+ bucket[e.validation]++;}return {@@ -239,11 +239,13 @@ async function recalculateWeightsLocked() {const docScores = {};for (const e of entries) {for (const doc of e.documentsUsed) {- if (!docScores[doc]) docScores[doc] = { positive: 0, neutral: 0, negative: 0, total: 0 };- docScores[doc].total++;- if (e.validation === 'sounds_like_me') docScores[doc].positive++;- else if (e.validation === 'not_quite') docScores[doc].neutral++;- else docScores[doc].negative++;+ // `doc` comes from stored feedback, which can carry any string.+ const scores = bucketFor(docScores, doc, () => ({ positive: 0, neutral: 0, negative: 0, total: 0 }));+ if (!scores) continue;+ scores.total++;+ if (e.validation === 'sounds_like_me') scores.positive++;+ else if (e.validation === 'not_quite') scores.neutral++;+ else scores.negative++;}}diff --git a/server/services/feedbackLoop.test.js b/server/services/feedbackLoop.test.jsindex 057c150209..4d06ee8097 100644--- a/server/services/feedbackLoop.test.js+++ b/server/services/feedbackLoop.test.js@@ -35,6 +35,20 @@ afterAll(cleanup);const merge = async (remote) => (await sync.applyDigitalTwinRemote({ feedback: remote })).count > 0;+describe('document attribution keyed by stored strings', () => {+ it('recalculates weights without writing a __proto__ document key onto Object.prototype', async () => {+ const entries = Array.from({ length: 5 }, (_, i) => ({+ ...entry(`e${i}`, `2026-01-0${i + 1}T00:00:00.000Z`, 'sounds_like_me'),+ documentsUsed: ['__proto__'],+ }));+ await writeFile(FILE, JSON.stringify(doc(entries, '2026-01-06T00:00:00.000Z')));+ const result = await feedback.recalculateWeights();+ expect(result.adjustments).toEqual({});+ expect(Object.prototype).not.toHaveProperty('total');+ expect(Object.prototype).not.toHaveProperty('positive');+ });+});+describe('feedback peer merge vs local submit', () => {it('keeps synced entries when a local submit follows a peer merge (stale cache)', async () => {await feedback.submitFeedback({ contentType: 'enrichment', validation: 'sounds_like_me', contentSnippet: 'a' }); // primes cachediff --git a/server/services/peerUsage.js b/server/services/peerUsage.jsindex 4b2a942659..c85109b20c 100644--- a/server/services/peerUsage.js+++ b/server/services/peerUsage.js@@ -27,7 +27,7 @@import { join } from 'path';import { atomicWrite, readJSONFile, dataPath, PATHS } from '../lib/fileUtils.js';-import { isPlainObject } from '../lib/objects.js';+import { isPlainObject, POLLUTING_KEYS } from '../lib/objects.js';import { createMutex } from '../lib/asyncMutex.js';import { compareNewerWins, parseTsMs } from '../lib/lwwTimestamp.js';import { mergeTombstones, normalizeTombstones, recordTombstone, isTombstoned } from '../lib/tombstones.js';@@ -340,7 +340,8 @@ export async function applyUsageRemote(remoteData) {let changed = 0;for (const [key, rawEntry] of Object.entries(incoming)) {- if (!isNonBlankStr(key) || key === selfId) continue;+ // A peer-chosen key of `__proto__` would replace this map's prototype on assignment.+ if (!isNonBlankStr(key) || key === selfId || POLLUTING_KEYS.has(key)) continue;const entry = sanitizeEntry(rawEntry, key);if (!entry) continue;// A digest CAPTURED after the deletion is a machine that came back, anddiff --git a/server/services/peerUsage.test.js b/server/services/peerUsage.test.jsindex 3331ceab3e..4c2b931204 100644--- a/server/services/peerUsage.test.js+++ b/server/services/peerUsage.test.js@@ -307,6 +307,18 @@ describe('usage sync manifest (#5759)', () => {});describe('untrusted digest handling', () => {+ it('skips a peer-chosen __proto__ instance key instead of re-parenting the instance map', async () => {+ // Computed key: a literal `__proto__:` would set the prototype and never reach Object.entries.+ // instanceId must match the key, or sanitizeEntry drops the entry before the store is touched.+ const payload = { instances: { ['__proto__']: peerEntry({ instanceId: '__proto__' }), 'inst-peer': peerEntry() } };+ expect(Object.hasOwn(payload.instances, '__proto__')).toBe(true);+ expect(await applyUsageRemote(payload)).toEqual({ applied: true, count: 1 });+ const stored = await readStoreFile();+ expect(Object.getPrototypeOf(stored.instances)).toBe(Object.prototype);+ expect(Object.keys(stored.instances)).toEqual(['inst-peer']);+ expect(({}).capturedAt).toBeUndefined();+ });+// canonicalStringify (this category's checksum) recurses in JS, with a far// smaller depth budget than native JSON.stringify — so a digest that survives// atomicWrite but blows that recursion would 500 the snapshot endpoint for