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(security): close the remaining prototype-key paths in POST mastery and usage

Adam Eivy1ce874e7b769
diff --git a/server/lib/postValidation.js b/server/lib/postValidation.js
index 7467287c86..55868c14f7 100644
--- a/server/lib/postValidation.js
+++ b/server/lib/postValidation.js
@@ -50,7 +50,8 @@ const fillBlankAnswerSchema = z.object({
value: z.string().nullable(),
expected: z.string().optional(),
correct: z.boolean().optional(),
- element: z.string().nullable().optional(),
+ // Copied onto the question's element by expandMemoryQuestionResults.
+ element: masteryKeySchema.nullable().optional(),
});
const questionResultSchema = z.object({
diff --git a/server/lib/postValidation.test.js b/server/lib/postValidation.test.js
index 8e5730bcc5..3d6b55a330 100644
--- a/server/lib/postValidation.test.js
+++ b/server/lib/postValidation.test.js
@@ -413,6 +413,8 @@ describe('questionResultSchema chunkId/element (issue #2016)', () => {
});
expect(postSessionSubmitSchema.safeParse(session({ chunkId: key })).success).toBe(false);
expect(postSessionSubmitSchema.safeParse(session({ element: key })).success).toBe(false);
+ // Each fill-blank answer's element is merged as its own mastery key.
+ expect(postSessionSubmitSchema.safeParse(session({ answered: [{ index: 0, value: 'b', element: key }] })).success).toBe(false);
expect(memoryPracticeSchema.safeParse({ mode: 'sequence', chunkId: key, results: [{ correct: true }] }).success).toBe(false);
expect(memoryPracticeSchema.safeParse({ mode: 'element-flash', results: [{ correct: true, element: key }] }).success).toBe(false);
expect(memoryPracticeSchema.safeParse({ mode: 'sequence', chunkId: 'verse-1', results: [{ correct: true, chunkId: 'verse-1' }] }).success).toBe(true);
diff --git a/server/services/usage.js b/server/services/usage.js
index a5dfc59955..634e72ec75 100644
--- a/server/services/usage.js
+++ b/server/services/usage.js
@@ -391,9 +391,16 @@ export async function applyHistoricalUsageCorrections(corrections = []) {
&& Boolean(usageData.dailyActivity?.[correction.day]?.byProvider?.[correction.providerId]);
const pendingSiblings = (correction) => (correction?.siblings?.length > 0)
&& !usageData.siblingReconciledRuns[correction.runId];
+ // Transcript-derived provider ids and model names index the same plain-object
+ // maps as live recording, so a correction naming a prototype key is skipped.
+ const usableRecord = (record) => !record
+ || ((!record.providerId || isUsageKey(record.providerId)) && (!record.model || usageModelKey(record.model) !== null));
const eligible = corrections.filter((correction) => {
const { runId, day: dayKey, providerId } = correction || {};
if (!runId || !dayKey || !providerId) return false;
+ if (POLLUTING_KEYS.has(runId) || POLLUTING_KEYS.has(dayKey) || !isUsageKey(providerId)) return false;
+ const records = [correction, ...[correction.measured].flat(), ...(correction.siblings || [])];
+ if (!records.every(usableRecord)) return false;
return pendingParent(correction) || pendingSiblings(correction);
});
const providerScopes = new Map();
diff --git a/server/services/usage.test.js b/server/services/usage.test.js
index 577d683aa2..5051f5fec6 100644
--- a/server/services/usage.test.js
+++ b/server/services/usage.test.js
@@ -1123,6 +1123,27 @@ describe('usage.js — historical transcript corrections (#3156)', () => {
await loadUsage();
});
+ it('skips a correction whose provider or model names a prototype key', async () => {
+ const correction = (overrides) => ({
+ runId: 'run-example-proto', day: dayKey, providerId, model,
+ estimate: { messages: 1, tokensIn: 20, tokensOut: 50 },
+ measured: [{ providerId, model, messages: 2, tokensIn: 100, tokensOut: 200, source: 'measured' }],
+ ...overrides,
+ });
+ const before = structuredClone(getUsage());
+ expect(await applyHistoricalUsageCorrections([
+ correction({ measured: [{ providerId, model: '__proto__', messages: 2, tokensOut: 200 }] }),
+ correction({ siblings: [{ providerId: '__proto__', model, messages: 1, tokensOut: 5 }], siblingScanned: true }),
+ correction({ model: 'constructor' }),
+ ])).toMatchObject({ corrected: 0 });
+ expect(Object.hasOwn(Object.prototype, 'messages')).toBe(false);
+ expect(Object.hasOwn(Object.prototype, 'tokensOut')).toBe(false);
+ const after = getUsage();
+ for (const key of ['dailyActivity', 'byProvider', 'byModel', 'totalTokens', 'totalMessages']) {
+ expect(after[key]).toEqual(before[key]);
+ }
+ });
+
it('replaces the configured-provider estimate, rebuilds flat totals, and is idempotent', async () => {
const correction = {
runId: 'run-example-1',