diff --git a/web/src/app/api/sync/route.test.ts b/web/src/app/api/sync/route.test.ts index 5ab05e3..1d59dbf 100644 --- a/web/src/app/api/sync/route.test.ts +++ b/web/src/app/api/sync/route.test.ts @@ -251,6 +251,48 @@ describe('POST /api/sync: owner deletes', () => { ['tag', 'delete', B.tag, { id: B.tag }], ])).toEqual(['applied', 'applied', 'applied']); expect(await row('select count(*)::int c from sl_task_tags where task_id=$1', [B.task])).toEqual({ c: 0 }); + expect(await row('select deleted_at is not null d from sl_tasks where id=$1', [B.sub])).toEqual({ d: true }); + expect(await row('select deleted_at is not null d from sl_tags where id=$1', [B.tag])).toEqual({ d: true }); + }); +}); + +// Two defences guard each update and delete: the ownership check, then the +// userId filter of the .where(). Removing the check alone turns the result +// into `applied` (the filter still protects the row); removing both lets the +// write through. The filter alone cannot be removed observably while the +// check runs first. +describe('POST /api/sync: writes on another account\'s rows', () => { + const denied = { status: 'skipped', error: 'Entity not found or access denied' }; + const tagDenied = { status: 'skipped', error: 'Tag not found or access denied' }; + + it('refuses to update or delete another account\'s list', async () => { + const { A, B } = await seed(); + expect(await pushResults(B.userId, [ + ['list', 'update', A.list, { name: 'hijacked' }], + ['list', 'delete', A.list, { id: A.list }], + ])).toEqual([denied, denied]); + expect(await row('select user_id, name, deleted_at from sl_lists where id=$1', [A.list])) + .toEqual({ user_id: A.userId, name: 'A list', deleted_at: null }); + }); + + it('refuses to update or delete another account\'s task', async () => { + const { A, B } = await seed(); + expect(await pushResults(B.userId, [ + ['task', 'update', A.task, { title: 'hijacked' }], + ['task', 'delete', A.task, { id: A.task }], + ])).toEqual([denied, denied]); + expect(await row('select user_id, title, deleted_at from sl_tasks where id=$1', [A.task])) + .toEqual({ user_id: A.userId, title: 'A task', deleted_at: null }); + }); + + it('refuses to update or delete another account\'s tag', async () => { + const { A, B } = await seed(); + expect(await pushResults(B.userId, [ + ['tag', 'update', A.tag, { name: 'hijacked' }], + ['tag', 'delete', A.tag, { id: A.tag }], + ])).toEqual([tagDenied, tagDenied]); + expect(await row('select user_id, name, deleted_at from sl_tags where id=$1', [A.tag])) + .toEqual({ user_id: A.userId, name: 'a-tag', deleted_at: null }); }); });