fix(api): constrain client-writable sources and batch timestamps

Narrow the workout create schema's source to the client-writable set
(MANUAL, APPLE_HEALTH), matching the measurement write surfaces, so a
client cannot forge a server-owned source into the cross-source picker.
Apply the single-entry plausibility bound to the measurement batch
timestamps so a backdated or future-dated row can no longer slip past
the batch path. Regenerate the OpenAPI contract.
This commit is contained in:
Marc-André Bombeck committed 2026-07-01 23:45:08 +02:00
1 parent 86d80d2c04
commit 9aa0a3597a
4 files changed
+38 -14

No files matched your search

@@ -217,6 +217,18 @@ describe("createWorkoutSchema", () => {
).toThrow();
});
it("rejects a server-owned source a client cannot forge", () => {
// The client-writable set is exactly {MANUAL, APPLE_HEALTH}. WHOOP /
// FITBIT rows are minted server-side by the sync workers; letting a
// client attribute a row to them would pollute the per-source canonical
// workout picker with rows the integration never produced.
for (const forged of ["WHOOP", "FITBIT", "WITHINGS", "COMPUTED"]) {
expect(() =>
createWorkoutSchema.parse({ ...minimalRun, source: forged }),
).toThrow();
}
});
it("rejects a max heart rate below the resting floor", () => {
expect(() =>
createWorkoutSchema.parse({ ...minimalRun, maxHeartRate: 5 }),
+8 -2
View File
@@ -1,5 +1,5 @@
import { z } from "zod/v4";
import { measurementSourceEnum } from "./measurement";
import { writableMeasurementSourceEnum } from "./measurement";
/**
* Workout sport-type union.
@@ -166,7 +166,13 @@ export const createWorkoutSchema = z
stepCount: z.number().int().min(0).max(200_000).optional(),
elevationM: z.number().min(-500).max(10_000).optional(),
pauseDurationSec: z.number().int().min(0).max(86_400).optional(),
source: measurementSourceEnum.optional().default("MANUAL"),
// Narrowed to the client-writable subset ({MANUAL, APPLE_HEALTH}) — the
// same allowlist the measurement writes use (`WRITABLE_MEASUREMENT_SOURCES`
// / `batchSourceEnum`). Server-owned sources (WHOOP, FITBIT) write directly
// via `prisma.workout.upsert` with a hardcoded `source`, never through this
// schema, so a client cannot forge a row attributed to an integration it
// does not own.
source: writableMeasurementSourceEnum.optional().default("MANUAL"),
externalId: z.string().max(128).optional(),
externalSourceVersion: z.string().max(64).optional(),
metadata: z.record(z.string(), z.unknown()).optional(),