Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

api: a push with nothing of the profile in it is a wipe, not a sync

Boris 'B' Kurktchiev07dece792002
diff --git a/api/openapi.yaml b/api/openapi.yaml
index 38973c8c7..e2867f055 100644
--- a/api/openapi.yaml
+++ b/api/openapi.yaml
@@ -560,10 +560,11 @@ paths:
- type: 'null'
'400':
description: >-
- `state` missing or not an object, `state` an array, or `workouts`/`routines`
- set to anything other than an array or null. Entries inside those two arrays
- that are not objects (`null`, numbers, nested arrays) are dropped before the
- write rather than refused, so a client with a damaged copy still syncs.
+ `state` missing or not an object, `state` an array or an object with nothing of
+ the profile in it (`_rev`/`_ts` do not count — both would empty the profile), or
+ `workouts`/`routines` set to anything other than an array or null. Entries inside
+ those two arrays that are not objects (`null`, numbers, nested arrays) are dropped
+ before the write rather than refused, so a client with a damaged copy still syncs.
content:
application/json:
schema: { $ref: '#/components/schemas/Error' }
diff --git a/api/server.js b/api/server.js
index ed769ef1b..c02b3fa7a 100644
--- a/api/server.js
+++ b/api/server.js
@@ -926,6 +926,17 @@ const routes = {
if (!user) return json(res, 401, { error: 'not signed in' });
const body = await readBody(req);
if (!body.state || typeof body.state !== 'object') return json(res, 400, { error: 'state required' });
+ // An object with nothing of the profile in it empties the document with the counter left
+ // intact: what lands on disk is `{"_rev":n+1}`, every routine, workout and weigh-in gone, and
+ // the next poll reports a revision the client accepts as its own. `_rev` and `_ts` do not
+ // count towards "something of the profile" — both are bookkeeping this route writes or echoes
+ // itself, so `{}` and `{"_rev":5}` are the same push and get the same refusal. Nothing
+ // shipped sends either: the web and mobile clients push a state built on DEF
+ // (frontend/src/store/useStore.js), which always carries its keys. An array is a document
+ // loss of its own and is left to the `invalid state` check below, which already refuses it.
+ if (!Array.isArray(body.state) && !Object.keys(body.state).some(k => k !== '_rev' && k !== '_ts')) {
+ return json(res, 400, { error: 'state required' });
+ }
// The reminder tick and the admin routes iterate these two on the server's side, so a truthy
// non-array would throw there on every pass for as long as it sat on disk. Absent or null is
// fine — every client fills its own defaults. An array is `typeof 'object'` but no document:
diff --git a/api/test/server-data-rev.test.js b/api/test/server-data-rev.test.js
index 8b3eab316..e7b73f6c5 100644
--- a/api/test/server-data-rev.test.js
+++ b/api/test/server-data-rev.test.js
@@ -110,3 +110,35 @@ test('GET/PUT /api/data: revisions, conditional writes and the legacy overwrite'
assert.equal(r.body.error, 'invalid state');
assert.equal(onDisk()._rev, 4);
});
+
+// `{}` keeps every rule this route has and still empties the profile: it is
+// an object, it is not an array, `workouts` and `routines` are absent (which is legal — a client
+// fills its own defaults), so the document on disk becomes `{"_rev":n+1}` with every routine,
+// workout and weigh-in gone, and the revision keeps counting so the next poll sees nothing wrong.
+// No shipped client sends it: the web and mobile clients push a state built on DEF, which always
+// carries its keys.
+test('PUT /api/data refuses an empty object, which would wipe the profile and keep counting', async t => {
+ const h = await startServer(t);
+ const uid = 'u_rev_1';
+ const put = async body => { const r = await fetch(`${h.api}/api/data`, { method: 'PUT', headers: headers(uid), body: JSON.stringify(body) }); return { status: r.status, body: await r.json() }; };
+ const rev = async () => (await fetch(`${h.api}/api/data/rev`, { headers: headers(uid) }).then(r => r.json())).rev;
+ const onDisk = () => JSON.parse(fs.readFileSync(path.join(h.dataDir, `state-${uid}.json`), 'utf8'));
+
+ assert.equal((await put({ state: { _ts: 100, workouts: [{ id: 'w1', d: '2026-09-01' }], routines: [] } })).status, 200);
+ assert.equal(await rev(), 1);
+
+ // `_rev` and `_ts` are this route's own bookkeeping — it stamps the one and echoes the other —
+ // so a document carrying nothing but those is the same empty push wearing a hat, and did the
+ // same damage: `{"_rev":5}` wrote `{"_rev":2}` over the profile.
+ for (const state of [{}, { _rev: 5 }, { _ts: Date.now() }, { _rev: 5, _ts: Date.now() }]) {
+ const r = await put({ state, baseRev: 1 });
+ assert.equal(r.status, 400, `state: ${JSON.stringify(state)}`);
+ assert.equal(r.body.error, 'state required');
+ }
+ assert.equal(await rev(), 1, 'nothing was written');
+ assert.deepEqual(onDisk().workouts.map(w => w.id), ['w1'], 'the profile is still there');
+
+ // …and a document that carries one real key alongside them is a profile, and goes through.
+ assert.equal((await put({ state: { _rev: 99, _ts: 1, routines: [] }, baseRev: 1 })).status, 200);
+ assert.equal(await rev(), 2);
+});
diff --git a/api/test/server-reminder.test.js b/api/test/server-reminder.test.js
index 51fef782f..d17f88973 100644
--- a/api/test/server-reminder.test.js
+++ b/api/test/server-reminder.test.js
@@ -69,9 +69,11 @@ test('PUT /api/data refuses a state whose workouts or routines is not an array',
}
assert.equal(fs.existsSync(file), false, 'nothing landed on disk');
- // the shapes real clients send still go through: arrays, the field left out, or null
+ // the shapes real clients send still go through: arrays, the field left out, or null.
+ // (Something of the profile has to be in there — a document that is only `_ts`/`_rev` is the
+ // empty push the route refuses, so the "left out" case carries a real key of its own.)
assert.equal((await put({ _ts: 2, workouts: [], routines: [] })).status, 200);
- assert.equal((await put({ _ts: 3 })).status, 200);
+ assert.equal((await put({ _ts: 3, unit: 'kg' })).status, 200);
assert.equal((await put({ _ts: 4, workouts: null, routines: null })).status, 200);
assert.equal(JSON.parse(fs.readFileSync(file, 'utf8'))._ts, 4);
});