Compare commits

...

7 Commits

Author SHA1 Message Date
4b00563016 feat: per-user data isolation (ownerId scoping) 2026-06-11 08:30:10 +00:00
bafdb88f5d docs: ownership model and ownerId in API contract
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 08:28:44 +00:00
f8955be4e9 feat(web): filter by ownerId and surface missing-role 401 state
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 08:28:25 +00:00
03fbe06a04 feat: scope every API endpoint to the token's sub; expose ownerId in DTOs
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 08:27:26 +00:00
0e16738624 feat: ownerOf(event) helper and ownerId in task DTO
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 08:26:23 +00:00
42abf35bff feat: scope all repo reads/writes to the caller's ownerId
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 08:26:07 +00:00
43f2d5b122 feat: add nullable owner_id columns to lists and tasks
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 08:24:28 +00:00
18 changed files with 1438 additions and 130 deletions

View File

@@ -29,16 +29,22 @@ the API, on shared PostgreSQL, behind Zitadel auth, deployed via Coolify.
## API ## API
Every `/api/**` route requires a valid Zitadel **access token** (`Authorization: Bearer …`) Every `/api/**` route requires a valid Zitadel **access token** (`Authorization: Bearer …`)
belonging to the owner. Missing/invalid/expired → `401`. No anonymous access. carrying the `user` project role. Missing/invalid/expired/role-less`401`. No anonymous access.
**Ownership:** every row carries `owner_id` = the writer's token `sub`. All reads and writes are
scoped server-side to the caller (`owner_id = sub OR owner_id IS NULL`); full-replace endpoints
only replace the caller's partition. `owner_id IS NULL` marks legacy pre-multi-user rows — visible
to all authorized users and adopted by the next write that touches them. DTOs expose this as a
nullable `ownerId`; any client-supplied `ownerId` is ignored.
| Method & path | Caller | Behaviour | | Method & path | Caller | Behaviour |
|---|---|---| |---|---|---|
| `PUT /api/lists` | desktop | Body `[{id,name}]` = full catalog. Upserts all, deletes lists not in payload (cascades tasks). → 200 | | `PUT /api/lists` | desktop | Body `[{id,name}]` = full catalog. Upserts all, deletes lists not in payload (cascades tasks). → 200 |
| `GET /api/lists` | web | → 200 `[{id,name}]` | | `GET /api/lists` | web | → 200 `[{id,name,ownerId}]` |
| `GET /api/lists/{id}/tasks` | web | → 200 tasks for the list; 404 if unknown | | `GET /api/lists/{id}/tasks` | web | → 200 tasks for the list; 404 if unknown |
| `POST /api/tasks` | web | Body `{title, description?, listId}`. Server-generated GUID, `source=web`, `consumed=false`. 404 if listId unknown. → 201 with the created task | | `POST /api/tasks` | web | Body `{title, description?, listId}`. Server-generated GUID, `source=web`, `consumed=false`. 404 if listId unknown. → 201 with the created task |
| `PUT /api/tasks/mirror` | desktop | Body `[{id, listId, title, description?}, ...]` = the FULL current Idle backlog (camelCase; `[]` is valid). Full-replace of the desktop-owned partition: upsert each (as `consumed=true`), delete any `consumed=true` task not in the array, leave web-created `consumed=false` tasks untouched. Mirrors `PUT /lists`. → 200 | | `PUT /api/tasks/mirror` | desktop | Body `[{id, listId, title, description?}, ...]` = the FULL current Idle backlog (camelCase; `[]` is valid). Full-replace of the desktop-owned partition: upsert each (as `consumed=true`), delete any `consumed=true` task not in the array, leave web-created `consumed=false` tasks untouched. Mirrors `PUT /lists`. → 200 |
| `GET /api/tasks?consumed=false` | desktop | → 200 `[{id, listId, title, description, createdAt}]` web tasks not yet imported (awaiting pull) | | `GET /api/tasks?consumed=false` | desktop | → 200 `[{id, listId, title, description, ownerId, createdAt}]` web tasks not yet imported (awaiting pull) |
| `POST /api/tasks/{id}/consume` | desktop | Sets `consumed=true` (imports a pulled web task into the desktop partition). Idempotent. → 200; 404 if unknown | | `POST /api/tasks/{id}/consume` | desktop | Sets `consumed=true` (imports a pulled web task into the desktop partition). Idempotent. → 200; 404 if unknown |
| `PUT /api/tasks/{id}` · `DELETE /api/tasks/{id}` | desktop | Legacy per-task upsert/delete — **superseded by `PUT /tasks/mirror`**. Kept for compatibility. | | `PUT /api/tasks/{id}` · `DELETE /api/tasks/{id}` | desktop | Legacy per-task upsert/delete — **superseded by `PUT /tasks/mirror`**. Kept for compatibility. |

View File

@@ -1,5 +1,15 @@
import type { ZitadelAuth } from "@kuns/zitadel-auth"; import type { ZitadelAuth } from "@kuns/zitadel-auth";
/** API call failure carrying the HTTP status (401 = authenticated but not authorized). */
export class ApiError extends Error {
constructor(
message: string,
public status: number,
) {
super(message);
}
}
// Access the bootstrap-provided Zitadel auth instance + a small JSON helper for /api calls. // Access the bootstrap-provided Zitadel auth instance + a small JSON helper for /api calls.
// By the time any component mounts, the plugin has gated auth, so `auth` is authenticated. // By the time any component mounts, the plugin has gated auth, so `auth` is authenticated.
// `auth.fetch` auto-attaches the Bearer access token. // `auth.fetch` auto-attaches the Bearer access token.
@@ -16,7 +26,7 @@ export function useAuth() {
} catch { } catch {
// non-JSON error body // non-JSON error body
} }
throw new Error(message); throw new ApiError(message, res.status);
} }
if (res.status === 204) return undefined as T; if (res.status === 204) return undefined as T;
return (await res.json()) as T; return (await res.json()) as T;

View File

@@ -1,7 +1,10 @@
<script setup lang="ts"> <script setup lang="ts">
import { ApiError } from "~/composables/useAuth";
interface List { interface List {
id: string; id: string;
name: string; name: string;
ownerId?: string | null;
} }
interface Task { interface Task {
id: string; id: string;
@@ -10,6 +13,7 @@ interface Task {
description: string | null; description: string | null;
source: string; source: string;
consumed: boolean; consumed: boolean;
ownerId?: string | null;
createdAt: string; createdAt: string;
} }
@@ -22,6 +26,21 @@ const loadingLists = ref(true);
const loadingTasks = ref(false); const loadingTasks = ref(false);
const error = ref<string | null>(null); const error = ref<string | null>(null);
// Authenticated but the token lacks the required Zitadel project role → API answers 401.
const missingRole = ref(false);
// Defense-in-depth: the server already scopes every query by the token's sub; additionally
// hide anything not owned by the current user. Absent ownerId = legacy (pre-multi-user) row.
const mine = (x: { ownerId?: string | null }) => !x.ownerId || x.ownerId === auth.user?.sub;
function handleApiError(e: unknown) {
if (e instanceof ApiError && e.status === 401) {
missingRole.value = true;
} else {
error.value = (e as Error).message;
}
}
const title = ref(""); const title = ref("");
const description = ref(""); const description = ref("");
const showNote = ref(false); const showNote = ref(false);
@@ -43,12 +62,12 @@ async function refreshLists() {
loadingLists.value = true; loadingLists.value = true;
error.value = null; error.value = null;
try { try {
lists.value = await api<List[]>("/lists"); lists.value = (await api<List[]>("/lists")).filter(mine);
if (lists.value.length && !lists.value.some((l) => l.id === selectedId.value)) { if (lists.value.length && !lists.value.some((l) => l.id === selectedId.value)) {
await selectList(lists.value[0]!.id); await selectList(lists.value[0]!.id);
} }
} catch (e) { } catch (e) {
error.value = (e as Error).message; handleApiError(e);
} finally { } finally {
loadingLists.value = false; loadingLists.value = false;
} }
@@ -66,9 +85,9 @@ async function refreshTasks() {
loadingTasks.value = true; loadingTasks.value = true;
error.value = null; error.value = null;
try { try {
tasks.value = await api<Task[]>(`/lists/${selectedId.value}/tasks`); tasks.value = (await api<Task[]>(`/lists/${selectedId.value}/tasks`)).filter(mine);
} catch (e) { } catch (e) {
error.value = (e as Error).message; handleApiError(e);
} finally { } finally {
loadingTasks.value = false; loadingTasks.value = false;
} }
@@ -95,7 +114,7 @@ async function addTask() {
showNote.value = false; showNote.value = false;
titleInput.value?.focus(); titleInput.value?.focus();
} catch (e) { } catch (e) {
error.value = (e as Error).message; handleApiError(e);
} finally { } finally {
adding.value = false; adding.value = false;
} }
@@ -135,7 +154,19 @@ onMounted(() => {
<main class="content"> <main class="content">
<p v-if="error" class="error" role="alert">{{ error }}</p> <p v-if="error" class="error" role="alert">{{ error }}</p>
<template v-if="loadingLists"> <template v-if="missingRole">
<div class="empty">
<p class="empty-mark"></p>
<p class="empty-title">No access yet</p>
<p class="muted">
You're signed in, but your account is missing the “user” role for ClaudeDo.
Ask the admin to grant it in Zitadel, then sign in again.
</p>
<button class="link" @click="auth.logout()">Sign out</button>
</div>
</template>
<template v-else-if="loadingLists">
<p class="muted">Loading lists…</p> <p class="muted">Loading lists…</p>
</template> </template>
@@ -172,7 +203,7 @@ onMounted(() => {
</template> </template>
</main> </main>
<footer v-if="lists.length" class="dock"> <footer v-if="lists.length && !missingRole" class="dock">
<button <button
v-if="lists.length > 1" v-if="lists.length > 1"
class="list-pill" class="list-pill"

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,5 @@
// GET /api/lists (web) — all lists. // GET /api/lists (web) — the caller's lists (plus legacy unowned).
export default defineEventHandler(async () => { export default defineEventHandler(async (event) => {
return getLists(getSql()); const rows = await getLists(getSql(), ownerOf(event));
return rows.map((r) => ({ id: r.id, name: r.name, ownerId: r.owner_id }));
}); });

View File

@@ -1,4 +1,6 @@
// PUT /api/lists (desktop) — full-replace catalog. Upsert all supplied; delete the rest. // PUT /api/lists (desktop) — full-replace of the caller's catalog. Upsert all supplied;
// delete the caller's lists not present. Any client-supplied ownerId is ignored — the
// server stamps ownership from the verified token.
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const body = await readBody(event); const body = await readBody(event);
if ( if (
@@ -9,6 +11,7 @@ export default defineEventHandler(async (event) => {
} }
await replaceLists( await replaceLists(
getSql(), getSql(),
ownerOf(event),
body.map((l) => ({ id: l.id, name: l.name })), body.map((l) => ({ id: l.id, name: l.name })),
); );
return { ok: true }; return { ok: true };

View File

@@ -1,10 +1,11 @@
// GET /api/lists/:id/tasks (web) — Idle tasks for a list. 404 if the list is unknown. // GET /api/lists/:id/tasks (web) — the caller's Idle tasks for a list. 404 if the list is unknown (to the caller).
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const id = getRouterParam(event, "id")!; const id = getRouterParam(event, "id")!;
const ownerId = ownerOf(event);
const sql = getSql(); const sql = getSql();
if (!(await listExists(sql, id))) { if (!(await listExists(sql, ownerId, id))) {
throw createError({ statusCode: 404, statusMessage: "list not found" }); throw createError({ statusCode: 404, statusMessage: "list not found" });
} }
const rows = await getTasksForList(sql, id); const rows = await getTasksForList(sql, ownerId, id);
return rows.map(toTaskDto); return rows.map(toTaskDto);
}); });

View File

@@ -1,11 +1,12 @@
// GET /api/tasks?consumed=false (desktop) — web-created tasks not yet imported. // GET /api/tasks?consumed=false (desktop) — the caller's web-created tasks not yet imported.
export default defineEventHandler(async () => { export default defineEventHandler(async (event) => {
const rows = await getUnconsumed(getSql()); const rows = await getUnconsumed(getSql(), ownerOf(event));
return rows.map((r) => ({ return rows.map((r) => ({
id: r.id, id: r.id,
listId: r.list_id, listId: r.list_id,
title: r.title, title: r.title,
description: r.description, description: r.description,
ownerId: r.owner_id,
createdAt: new Date(r.created_at).toISOString(), createdAt: new Date(r.created_at).toISOString(),
})); }));
}); });

View File

@@ -1,4 +1,4 @@
// POST /api/tasks (web) — create an Idle task with a server-generated GUID. // POST /api/tasks (web) — create an Idle task with a server-generated GUID, owned by the caller.
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const body = await readBody(event); const body = await readBody(event);
const title = typeof body?.title === "string" ? body.title.trim() : ""; const title = typeof body?.title === "string" ? body.title.trim() : "";
@@ -9,12 +9,13 @@ export default defineEventHandler(async (event) => {
const description = const description =
typeof body?.description === "string" && body.description.trim() ? body.description : null; typeof body?.description === "string" && body.description.trim() ? body.description : null;
const ownerId = ownerOf(event);
const sql = getSql(); const sql = getSql();
if (!(await listExists(sql, listId))) { if (!(await listExists(sql, ownerId, listId))) {
throw createError({ statusCode: 404, statusMessage: "list not found" }); throw createError({ statusCode: 404, statusMessage: "list not found" });
} }
const row = await createWebTask(sql, { listId, title, description }); const row = await createWebTask(sql, ownerId, { listId, title, description });
setResponseStatus(event, 201); setResponseStatus(event, 201);
return toTaskDto(row); return toTaskDto(row);
}); });

View File

@@ -1,6 +1,6 @@
// DELETE /api/tasks/:id (desktop) — task left Idle on desktop. Idempotent. // DELETE /api/tasks/:id (desktop) — task left Idle on desktop. Idempotent, scoped to the caller's rows.
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
await deleteTask(getSql(), getRouterParam(event, "id")!); await deleteTask(getSql(), ownerOf(event), getRouterParam(event, "id")!);
setResponseStatus(event, 204); setResponseStatus(event, 204);
return null; return null;
}); });

View File

@@ -1,4 +1,4 @@
// PUT /api/tasks/:id (desktop) — idempotent upsert mirroring a desktop Idle task. // PUT /api/tasks/:id (desktop) — idempotent upsert mirroring a desktop Idle task, owned by the caller.
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const id = getRouterParam(event, "id")!; const id = getRouterParam(event, "id")!;
const body = await readBody(event); const body = await readBody(event);
@@ -9,12 +9,13 @@ export default defineEventHandler(async (event) => {
} }
const description = typeof body?.description === "string" ? body.description : null; const description = typeof body?.description === "string" ? body.description : null;
const ownerId = ownerOf(event);
const sql = getSql(); const sql = getSql();
if (!(await listExists(sql, listId))) { if (!(await listExists(sql, ownerId, listId))) {
throw createError({ statusCode: 404, statusMessage: "list not found" }); throw createError({ statusCode: 404, statusMessage: "list not found" });
} }
const { created } = await upsertDesktopTask(sql, id, { listId, title, description }); const { created } = await upsertDesktopTask(sql, ownerId, id, { listId, title, description });
setResponseStatus(event, created ? 201 : 200); setResponseStatus(event, created ? 201 : 200);
return { id }; return { id };
}); });

View File

@@ -1,6 +1,6 @@
// POST /api/tasks/:id/consume (desktop) — mark a web task imported. Idempotent. // POST /api/tasks/:id/consume (desktop) — mark the caller's web task imported. Idempotent.
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const ok = await consume(getSql(), getRouterParam(event, "id")!); const ok = await consume(getSql(), ownerOf(event), getRouterParam(event, "id")!);
if (!ok) { if (!ok) {
throw createError({ statusCode: 404, statusMessage: "task not found" }); throw createError({ statusCode: 404, statusMessage: "task not found" });
} }

View File

@@ -1,7 +1,8 @@
// PUT /api/tasks/mirror (desktop) — full-replace of the desktop's current Idle backlog. // PUT /api/tasks/mirror (desktop) — full-replace of the caller's desktop Idle backlog.
// Body: [{ id, listId, title, description? }, ...] (camelCase). An empty array is valid and // Body: [{ id, listId, title, description? }, ...] (camelCase). An empty array is valid and
// clears the desktop-owned partition. Mirrors PUT /lists. Web-created tasks awaiting pull // clears the caller's desktop-owned partition. Mirrors PUT /lists. Web-created tasks awaiting
// (consumed=false) are never touched here. // pull (consumed=false) and other users' rows are never touched. Any client-supplied ownerId
// on items is ignored — ownership comes from the verified token.
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const body = await readBody(event); const body = await readBody(event);
if (!Array.isArray(body)) { if (!Array.isArray(body)) {
@@ -28,16 +29,17 @@ export default defineEventHandler(async (event) => {
}); });
} }
const ownerId = ownerOf(event);
const sql = getSql(); const sql = getSql();
// Every referenced list must exist (lists are full-replaced before tasks are mirrored). // Every referenced list must exist for the caller (lists are full-replaced before tasks are mirrored).
const listIds = [...new Set(items.map((t) => t.listId))]; const listIds = [...new Set(items.map((t) => t.listId))];
for (const id of listIds) { for (const id of listIds) {
if (!(await listExists(sql, id))) { if (!(await listExists(sql, ownerId, id))) {
throw createError({ statusCode: 400, statusMessage: `unknown listId: ${id}` }); throw createError({ statusCode: 400, statusMessage: `unknown listId: ${id}` });
} }
} }
await mirrorDesktopTasks(sql, items); await mirrorDesktopTasks(sql, ownerId, items);
return { ok: true, count: items.length }; return { ok: true, count: items.length };
}); });

View File

@@ -7,6 +7,7 @@ export interface TaskDto {
description: string | null; description: string | null;
source: string; source: string;
consumed: boolean; consumed: boolean;
ownerId: string | null;
createdAt: string; createdAt: string;
} }
@@ -18,6 +19,7 @@ export function toTaskDto(row: TaskRow): TaskDto {
description: row.description, description: row.description,
source: row.source, source: row.source,
consumed: row.consumed, consumed: row.consumed,
ownerId: row.owner_id,
createdAt: new Date(row.created_at).toISOString(), createdAt: new Date(row.created_at).toISOString(),
}; };
} }

View File

@@ -6,6 +6,7 @@ type Sql = ReturnType<typeof postgres>;
export interface ListRow { export interface ListRow {
id: string; id: string;
name: string; name: string;
owner_id: string | null;
} }
export interface TaskRow { export interface TaskRow {
@@ -15,52 +16,68 @@ export interface TaskRow {
description: string | null; description: string | null;
source: string; source: string;
consumed: boolean; consumed: boolean;
owner_id: string | null;
created_at: Date; created_at: Date;
updated_at: Date; updated_at: Date;
} }
export async function getLists(sql: Sql): Promise<ListRow[]> { // Ownership model: every read/write is scoped to the caller's Zitadel sub (ownerId).
return sql<ListRow[]>`select id, name from lists order by name`; // owner_id NULL = legacy row from before multi-user — visible to every authorized user
// and adopted (stamped with the caller's sub) by the caller's next write that touches it.
// Upserts use `on conflict … where` so they can never modify a row owned by someone else.
export async function getLists(sql: Sql, ownerId: string): Promise<ListRow[]> {
return sql<ListRow[]>`
select id, name, owner_id from lists
where owner_id = ${ownerId} or owner_id is null
order by name`;
} }
/** Full-replace catalog: upsert all supplied, delete any list not present (cascades tasks). */ /** Full-replace of the caller's catalog: upsert all supplied, delete the caller's (and legacy) lists not present (cascades tasks). */
export async function replaceLists( export async function replaceLists(
sql: Sql, sql: Sql,
ownerId: string,
lists: { id: string; name: string }[], lists: { id: string; name: string }[],
): Promise<void> { ): Promise<void> {
await sql.begin(async (tx) => { await sql.begin(async (tx) => {
for (const l of lists) { for (const l of lists) {
await tx` await tx`
insert into lists (id, name, updated_at) insert into lists (id, name, owner_id, updated_at)
values (${l.id}, ${l.name}, now()) values (${l.id}, ${l.name}, ${ownerId}, now())
on conflict (id) do update set name = excluded.name, updated_at = now()`; on conflict (id) do update set name = excluded.name, owner_id = ${ownerId}, updated_at = now()
where lists.owner_id = ${ownerId} or lists.owner_id is null`;
} }
if (lists.length) { const ids = lists.map((l) => l.id);
const ids = lists.map((l) => l.id); if (ids.length) {
await tx`delete from lists where id <> all(${ids})`; await tx`delete from lists where (owner_id = ${ownerId} or owner_id is null) and id <> all(${ids})`;
} else { } else {
await tx`delete from lists`; await tx`delete from lists where owner_id = ${ownerId} or owner_id is null`;
} }
}); });
} }
export async function listExists(sql: Sql, id: string): Promise<boolean> { export async function listExists(sql: Sql, ownerId: string, id: string): Promise<boolean> {
const rows = await sql`select 1 from lists where id = ${id}`; const rows = await sql`
select 1 from lists where id = ${id} and (owner_id = ${ownerId} or owner_id is null)`;
return rows.length > 0; return rows.length > 0;
} }
export async function getTasksForList(sql: Sql, listId: string): Promise<TaskRow[]> { export async function getTasksForList(sql: Sql, ownerId: string, listId: string): Promise<TaskRow[]> {
return sql<TaskRow[]>`select * from tasks where list_id = ${listId} order by created_at`; return sql<TaskRow[]>`
select * from tasks
where list_id = ${listId} and (owner_id = ${ownerId} or owner_id is null)
order by created_at`;
} }
export async function createWebTask( export async function createWebTask(
sql: Sql, sql: Sql,
ownerId: string,
t: { listId: string; title: string; description: string | null }, t: { listId: string; title: string; description: string | null },
): Promise<TaskRow> { ): Promise<TaskRow> {
const id = randomUUID(); const id = randomUUID();
const [row] = await sql<TaskRow[]>` const [row] = await sql<TaskRow[]>`
insert into tasks (id, list_id, title, description, source, consumed) insert into tasks (id, list_id, title, description, source, consumed, owner_id)
values (${id}, ${t.listId}, ${t.title}, ${t.description}, 'web', false) values (${id}, ${t.listId}, ${t.title}, ${t.description}, 'web', false, ${ownerId})
returning *`; returning *`;
return row; return row;
} }
@@ -68,64 +85,76 @@ export async function createWebTask(
/** Idempotent upsert of a desktop Idle task by id. Returns whether a row was inserted. */ /** Idempotent upsert of a desktop Idle task by id. Returns whether a row was inserted. */
export async function upsertDesktopTask( export async function upsertDesktopTask(
sql: Sql, sql: Sql,
ownerId: string,
id: string, id: string,
t: { listId: string; title: string; description?: string | null }, t: { listId: string; title: string; description?: string | null },
): Promise<{ created: boolean }> { ): Promise<{ created: boolean }> {
const [row] = await sql<{ created: boolean }[]>` const [row] = await sql<{ created: boolean }[]>`
insert into tasks (id, list_id, title, description, source) insert into tasks (id, list_id, title, description, source, owner_id)
values (${id}, ${t.listId}, ${t.title}, ${t.description ?? null}, 'desktop') values (${id}, ${t.listId}, ${t.title}, ${t.description ?? null}, 'desktop', ${ownerId})
on conflict (id) do update set on conflict (id) do update set
list_id = excluded.list_id, list_id = excluded.list_id,
title = excluded.title, title = excluded.title,
description = excluded.description, description = excluded.description,
owner_id = ${ownerId},
updated_at = now() updated_at = now()
where tasks.owner_id = ${ownerId} or tasks.owner_id is null
returning (xmax = 0) as created`; returning (xmax = 0) as created`;
return { created: row.created }; // row is undefined when the id exists but belongs to another user: blocked no-op.
return { created: row?.created ?? false };
} }
/** /**
* Full-replace of the desktop-owned partition (the desktop's current Idle backlog). * Full-replace of the caller's desktop-owned partition (their current Idle backlog).
* Mirrors `replaceLists`: upsert every supplied task as desktop-owned (consumed=true), and * Mirrors `replaceLists`: upsert every supplied task as desktop-owned (consumed=true), and
* delete any desktop-owned (consumed=true) task not in the payload. Web-created tasks still * delete any of the caller's (or legacy) desktop-owned tasks not in the payload. Web-created
* awaiting pull (consumed=false) are left untouched. An empty array clears the partition. * tasks still awaiting pull (consumed=false) and other users' rows are left untouched.
*/ */
export async function mirrorDesktopTasks( export async function mirrorDesktopTasks(
sql: Sql, sql: Sql,
ownerId: string,
items: { id: string; listId: string; title: string; description?: string | null }[], items: { id: string; listId: string; title: string; description?: string | null }[],
): Promise<void> { ): Promise<void> {
await sql.begin(async (tx) => { await sql.begin(async (tx) => {
for (const t of items) { for (const t of items) {
await tx` await tx`
insert into tasks (id, list_id, title, description, source, consumed) insert into tasks (id, list_id, title, description, source, consumed, owner_id)
values (${t.id}, ${t.listId}, ${t.title}, ${t.description ?? null}, 'desktop', true) values (${t.id}, ${t.listId}, ${t.title}, ${t.description ?? null}, 'desktop', true, ${ownerId})
on conflict (id) do update set on conflict (id) do update set
list_id = excluded.list_id, list_id = excluded.list_id,
title = excluded.title, title = excluded.title,
description = excluded.description, description = excluded.description,
source = 'desktop', source = 'desktop',
consumed = true, consumed = true,
updated_at = now()`; owner_id = ${ownerId},
updated_at = now()
where tasks.owner_id = ${ownerId} or tasks.owner_id is null`;
} }
const ids = items.map((t) => t.id); const ids = items.map((t) => t.id);
if (ids.length) { if (ids.length) {
await tx`delete from tasks where consumed = true and id <> all(${ids})`; await tx`delete from tasks
where consumed = true and (owner_id = ${ownerId} or owner_id is null) and id <> all(${ids})`;
} else { } else {
await tx`delete from tasks where consumed = true`; await tx`delete from tasks where consumed = true and (owner_id = ${ownerId} or owner_id is null)`;
} }
}); });
} }
/** Web-created tasks the desktop has not yet imported. */ /** The caller's web-created tasks the desktop has not yet imported. */
export async function getUnconsumed(sql: Sql): Promise<TaskRow[]> { export async function getUnconsumed(sql: Sql, ownerId: string): Promise<TaskRow[]> {
return sql<TaskRow[]>` return sql<TaskRow[]>`
select * from tasks where source = 'web' and consumed = false order by created_at`; select * from tasks
where source = 'web' and consumed = false and (owner_id = ${ownerId} or owner_id is null)
order by created_at`;
} }
export async function consume(sql: Sql, id: string): Promise<boolean> { export async function consume(sql: Sql, ownerId: string, id: string): Promise<boolean> {
const res = await sql`update tasks set consumed = true, updated_at = now() where id = ${id}`; const res = await sql`
update tasks set consumed = true, updated_at = now()
where id = ${id} and (owner_id = ${ownerId} or owner_id is null)`;
return res.count > 0; return res.count > 0;
} }
export async function deleteTask(sql: Sql, id: string): Promise<void> { export async function deleteTask(sql: Sql, ownerId: string, id: string): Promise<void> {
await sql`delete from tasks where id = ${id}`; await sql`delete from tasks where id = ${id} and (owner_id = ${ownerId} or owner_id is null)`;
} }

View File

@@ -20,4 +20,8 @@ create table if not exists tasks (
create index if not exists idx_tasks_list_id on tasks(list_id); create index if not exists idx_tasks_list_id on tasks(list_id);
create index if not exists idx_tasks_unconsumed on tasks(consumed) where consumed = false; create index if not exists idx_tasks_unconsumed on tasks(consumed) where consumed = false;
-- Multi-user plumbing: rows are owned by a Zitadel sub. NULL = legacy/unowned (pre-multi-user).
alter table lists add column if not exists owner_id text;
alter table tasks add column if not exists owner_id text;
`; `;

10
server/utils/session.ts Normal file
View File

@@ -0,0 +1,10 @@
import { createError, type H3Event } from "h3";
/** The authenticated caller's Zitadel sub — the ownership key for all row scoping. */
export function ownerOf(event: H3Event): string {
const sub = (event.context.user as { sub?: unknown } | undefined)?.sub;
if (typeof sub !== "string" || !sub) {
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
}
return sub;
}

View File

@@ -4,6 +4,9 @@ import * as repo from "../server/utils/repo";
const sql = postgres(process.env.DATABASE_URL!, { max: 1, onnotice: () => {} }); const sql = postgres(process.env.DATABASE_URL!, { max: 1, onnotice: () => {} });
const U1 = "user-1";
const U2 = "user-2";
beforeEach(async () => { beforeEach(async () => {
await sql`truncate lists cascade`; await sql`truncate lists cascade`;
}); });
@@ -14,54 +17,56 @@ afterAll(async () => {
describe("lists", () => { describe("lists", () => {
it("full-replace upserts supplied and deletes missing", async () => { it("full-replace upserts supplied and deletes missing", async () => {
await repo.replaceLists(sql, [ await repo.replaceLists(sql, U1, [
{ id: "a", name: "A" }, { id: "a", name: "A" },
{ id: "b", name: "B" }, { id: "b", name: "B" },
]); ]);
await repo.replaceLists(sql, [{ id: "a", name: "A2" }]); // b removed, a renamed await repo.replaceLists(sql, U1, [{ id: "a", name: "A2" }]); // b removed, a renamed
const got = await repo.getLists(sql); const got = await repo.getLists(sql, U1);
expect(got).toEqual([{ id: "a", name: "A2" }]); expect(got).toEqual([{ id: "a", name: "A2", owner_id: U1 }]);
}); });
it("empty payload clears all lists", async () => { it("empty payload clears all my lists", async () => {
await repo.replaceLists(sql, [{ id: "a", name: "A" }]); await repo.replaceLists(sql, U1, [{ id: "a", name: "A" }]);
await repo.replaceLists(sql, []); await repo.replaceLists(sql, U1, []);
expect(await repo.getLists(sql)).toEqual([]); expect(await repo.getLists(sql, U1)).toEqual([]);
}); });
it("deleting a list cascades its tasks", async () => { it("deleting a list cascades its tasks", async () => {
await repo.replaceLists(sql, [{ id: "a", name: "A" }]); await repo.replaceLists(sql, U1, [{ id: "a", name: "A" }]);
await repo.upsertDesktopTask(sql, "t1", { listId: "a", title: "x" }); await repo.upsertDesktopTask(sql, U1, "t1", { listId: "a", title: "x" });
await repo.replaceLists(sql, []); // removes a + cascades t1 await repo.replaceLists(sql, U1, []); // removes a + cascades t1
expect(await repo.getUnconsumed(sql)).toEqual([]); expect(await repo.getUnconsumed(sql, U1)).toEqual([]);
}); });
it("listExists validates listId", async () => { it("listExists validates listId within the caller's scope", async () => {
await repo.replaceLists(sql, [{ id: "L", name: "List" }]); await repo.replaceLists(sql, U1, [{ id: "L", name: "List" }]);
expect(await repo.listExists(sql, "L")).toBe(true); expect(await repo.listExists(sql, U1, "L")).toBe(true);
expect(await repo.listExists(sql, "ghost")).toBe(false); expect(await repo.listExists(sql, U1, "ghost")).toBe(false);
expect(await repo.listExists(sql, U2, "L")).toBe(false); // not my list
}); });
}); });
describe("tasks", () => { describe("tasks", () => {
beforeEach(async () => { beforeEach(async () => {
await repo.replaceLists(sql, [{ id: "L", name: "List" }]); await repo.replaceLists(sql, U1, [{ id: "L", name: "List" }]);
}); });
it("web create returns generated GUID, source=web, consumed=false", async () => { it("web create returns generated GUID, source=web, consumed=false, stamped owner", async () => {
const t = await repo.createWebTask(sql, { listId: "L", title: "buy milk", description: null }); const t = await repo.createWebTask(sql, U1, { listId: "L", title: "buy milk", description: null });
expect(t.id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/); expect(t.id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
expect(t.source).toBe("web"); expect(t.source).toBe("web");
expect(t.consumed).toBe(false); expect(t.consumed).toBe(false);
expect(t.title).toBe("buy milk"); expect(t.title).toBe("buy milk");
expect(t.owner_id).toBe(U1);
}); });
it("desktop upsert is idempotent by id; source=desktop on insert", async () => { it("desktop upsert is idempotent by id; source=desktop on insert", async () => {
const a = await repo.upsertDesktopTask(sql, "fixed-id", { listId: "L", title: "one" }); const a = await repo.upsertDesktopTask(sql, U1, "fixed-id", { listId: "L", title: "one" });
expect(a.created).toBe(true); expect(a.created).toBe(true);
const b = await repo.upsertDesktopTask(sql, "fixed-id", { listId: "L", title: "two", description: "d" }); const b = await repo.upsertDesktopTask(sql, U1, "fixed-id", { listId: "L", title: "two", description: "d" });
expect(b.created).toBe(false); expect(b.created).toBe(false);
const tasks = await repo.getTasksForList(sql, "L"); const tasks = await repo.getTasksForList(sql, U1, "L");
expect(tasks).toHaveLength(1); expect(tasks).toHaveLength(1);
expect(tasks[0].title).toBe("two"); expect(tasks[0].title).toBe("two");
expect(tasks[0].description).toBe("d"); expect(tasks[0].description).toBe("d");
@@ -69,90 +74,165 @@ describe("tasks", () => {
}); });
it("getUnconsumed returns only web tasks with consumed=false", async () => { it("getUnconsumed returns only web tasks with consumed=false", async () => {
await repo.createWebTask(sql, { listId: "L", title: "web1", description: null }); await repo.createWebTask(sql, U1, { listId: "L", title: "web1", description: null });
await repo.upsertDesktopTask(sql, "d1", { listId: "L", title: "desk1" }); await repo.upsertDesktopTask(sql, U1, "d1", { listId: "L", title: "desk1" });
const u = await repo.getUnconsumed(sql); const u = await repo.getUnconsumed(sql, U1);
expect(u).toHaveLength(1); expect(u).toHaveLength(1);
expect(u[0].title).toBe("web1"); expect(u[0].title).toBe("web1");
}); });
it("consume sets consumed=true and is reflected in getUnconsumed", async () => { it("consume sets consumed=true and is reflected in getUnconsumed", async () => {
const t = await repo.createWebTask(sql, { listId: "L", title: "c", description: null }); const t = await repo.createWebTask(sql, U1, { listId: "L", title: "c", description: null });
expect(await repo.consume(sql, t.id)).toBe(true); expect(await repo.consume(sql, U1, t.id)).toBe(true);
expect(await repo.getUnconsumed(sql)).toEqual([]); expect(await repo.getUnconsumed(sql, U1)).toEqual([]);
}); });
it("consume on unknown id returns false", async () => { it("consume on unknown id returns false", async () => {
expect(await repo.consume(sql, "nope")).toBe(false); expect(await repo.consume(sql, U1, "nope")).toBe(false);
}); });
it("deleteTask is idempotent", async () => { it("deleteTask is idempotent", async () => {
const t = await repo.createWebTask(sql, { listId: "L", title: "c", description: null }); const t = await repo.createWebTask(sql, U1, { listId: "L", title: "c", description: null });
await repo.deleteTask(sql, t.id); await repo.deleteTask(sql, U1, t.id);
await repo.deleteTask(sql, t.id); // no throw on absent await repo.deleteTask(sql, U1, t.id); // no throw on absent
expect(await repo.getTasksForList(sql, "L")).toEqual([]); expect(await repo.getTasksForList(sql, U1, "L")).toEqual([]);
}); });
}); });
describe("mirrorDesktopTasks (desktop Idle full-replace)", () => { describe("mirrorDesktopTasks (desktop Idle full-replace)", () => {
beforeEach(async () => { beforeEach(async () => {
await repo.replaceLists(sql, [ await repo.replaceLists(sql, U1, [
{ id: "L", name: "List" }, { id: "L", name: "List" },
{ id: "L2", name: "List 2" }, { id: "L2", name: "List 2" },
]); ]);
}); });
it("inserts items as desktop-owned (consumed=true, source=desktop)", async () => { it("inserts items as desktop-owned (consumed=true, source=desktop, stamped owner)", async () => {
await repo.mirrorDesktopTasks(sql, [ await repo.mirrorDesktopTasks(sql, U1, [
{ id: "m1", listId: "L", title: "one", description: "d1" }, { id: "m1", listId: "L", title: "one", description: "d1" },
{ id: "m2", listId: "L2", title: "two" }, { id: "m2", listId: "L2", title: "two" },
]); ]);
const t = await repo.getTasksForList(sql, "L"); const t = await repo.getTasksForList(sql, U1, "L");
expect(t).toHaveLength(1); expect(t).toHaveLength(1);
expect(t[0]).toMatchObject({ id: "m1", title: "one", description: "d1", source: "desktop", consumed: true }); expect(t[0]).toMatchObject({ id: "m1", title: "one", description: "d1", source: "desktop", consumed: true, owner_id: U1 });
expect(await repo.getTasksForList(sql, "L2")).toHaveLength(1); expect(await repo.getTasksForList(sql, U1, "L2")).toHaveLength(1);
}); });
it("upserts existing items by id (title/listId/description)", async () => { it("upserts existing items by id (title/listId/description)", async () => {
await repo.mirrorDesktopTasks(sql, [{ id: "m1", listId: "L", title: "one" }]); await repo.mirrorDesktopTasks(sql, U1, [{ id: "m1", listId: "L", title: "one" }]);
await repo.mirrorDesktopTasks(sql, [{ id: "m1", listId: "L2", title: "renamed", description: "x" }]); await repo.mirrorDesktopTasks(sql, U1, [{ id: "m1", listId: "L2", title: "renamed", description: "x" }]);
const onL = await repo.getTasksForList(sql, "L"); const onL = await repo.getTasksForList(sql, U1, "L");
const onL2 = await repo.getTasksForList(sql, "L2"); const onL2 = await repo.getTasksForList(sql, U1, "L2");
expect(onL).toHaveLength(0); expect(onL).toHaveLength(0);
expect(onL2).toHaveLength(1); expect(onL2).toHaveLength(1);
expect(onL2[0]).toMatchObject({ title: "renamed", description: "x" }); expect(onL2[0]).toMatchObject({ title: "renamed", description: "x" });
}); });
it("deletes desktop tasks whose id is not in the array", async () => { it("deletes desktop tasks whose id is not in the array", async () => {
await repo.mirrorDesktopTasks(sql, [ await repo.mirrorDesktopTasks(sql, U1, [
{ id: "m1", listId: "L", title: "one" }, { id: "m1", listId: "L", title: "one" },
{ id: "m2", listId: "L", title: "two" }, { id: "m2", listId: "L", title: "two" },
]); ]);
await repo.mirrorDesktopTasks(sql, [{ id: "m1", listId: "L", title: "one" }]); // m2 dropped await repo.mirrorDesktopTasks(sql, U1, [{ id: "m1", listId: "L", title: "one" }]); // m2 dropped
const t = await repo.getTasksForList(sql, "L"); const t = await repo.getTasksForList(sql, U1, "L");
expect(t.map((x) => x.id)).toEqual(["m1"]); expect(t.map((x) => x.id)).toEqual(["m1"]);
}); });
it("empty array deletes all desktop tasks", async () => { it("empty array deletes all my desktop tasks", async () => {
await repo.mirrorDesktopTasks(sql, [{ id: "m1", listId: "L", title: "one" }]); await repo.mirrorDesktopTasks(sql, U1, [{ id: "m1", listId: "L", title: "one" }]);
await repo.mirrorDesktopTasks(sql, []); await repo.mirrorDesktopTasks(sql, U1, []);
expect(await repo.getTasksForList(sql, "L")).toEqual([]); expect(await repo.getTasksForList(sql, U1, "L")).toEqual([]);
}); });
it("does NOT touch web-created tasks awaiting pull (consumed=false)", async () => { it("does NOT touch web-created tasks awaiting pull (consumed=false)", async () => {
const web = await repo.createWebTask(sql, { listId: "L", title: "web", description: null }); const web = await repo.createWebTask(sql, U1, { listId: "L", title: "web", description: null });
await repo.mirrorDesktopTasks(sql, [{ id: "m1", listId: "L", title: "desk" }]); await repo.mirrorDesktopTasks(sql, U1, [{ id: "m1", listId: "L", title: "desk" }]);
// web task survives the mirror (and is still unconsumed) const unconsumed = await repo.getUnconsumed(sql, U1);
const unconsumed = await repo.getUnconsumed(sql);
expect(unconsumed.map((x) => x.id)).toEqual([web.id]); expect(unconsumed.map((x) => x.id)).toEqual([web.id]);
expect((await repo.getTasksForList(sql, "L")).map((x) => x.id).sort()).toEqual([web.id, "m1"].sort()); expect((await repo.getTasksForList(sql, U1, "L")).map((x) => x.id).sort()).toEqual([web.id, "m1"].sort());
}); });
it("claims a web task when its id is mirrored (becomes consumed=true)", async () => { it("claims a web task when its id is mirrored (becomes consumed=true)", async () => {
const web = await repo.createWebTask(sql, { listId: "L", title: "web", description: null }); const web = await repo.createWebTask(sql, U1, { listId: "L", title: "web", description: null });
await repo.mirrorDesktopTasks(sql, [{ id: web.id, listId: "L", title: "web (imported)" }]); await repo.mirrorDesktopTasks(sql, U1, [{ id: web.id, listId: "L", title: "web (imported)" }]);
expect(await repo.getUnconsumed(sql)).toEqual([]); // no longer awaiting pull expect(await repo.getUnconsumed(sql, U1)).toEqual([]); // no longer awaiting pull
const t = await repo.getTasksForList(sql, "L"); const t = await repo.getTasksForList(sql, U1, "L");
expect(t[0]).toMatchObject({ id: web.id, title: "web (imported)", consumed: true }); expect(t[0]).toMatchObject({ id: web.id, title: "web (imported)", consumed: true });
}); });
}); });
describe("owner isolation", () => {
it("lists are invisible to other users; legacy (NULL owner) lists visible to everyone", async () => {
await repo.replaceLists(sql, U1, [{ id: "mine", name: "Mine" }]);
await sql`insert into lists (id, name) values ('legacy', 'Old')`; // pre-multi-user row
expect((await repo.getLists(sql, U1)).map((l) => l.id).sort()).toEqual(["legacy", "mine"]);
expect((await repo.getLists(sql, U2)).map((l) => l.id)).toEqual(["legacy"]);
});
it("replaceLists never deletes or renames another user's lists", async () => {
await repo.replaceLists(sql, U1, [{ id: "a", name: "A" }]);
await repo.replaceLists(sql, U2, [{ id: "b", name: "B" }]); // U2's full catalog: just b
expect((await repo.getLists(sql, U1)).map((l) => l.id)).toEqual(["a"]);
expect((await repo.getLists(sql, U2)).map((l) => l.id)).toEqual(["b"]);
// U2 upserting U1's id must not steal/rename it
await repo.replaceLists(sql, U2, [{ id: "a", name: "stolen" }, { id: "b", name: "B" }]);
const u1Lists = await repo.getLists(sql, U1);
expect(u1Lists).toEqual([{ id: "a", name: "A", owner_id: U1 }]);
});
it("replaceLists adopts legacy lists (stamps the caller as owner)", async () => {
await sql`insert into lists (id, name) values ('legacy', 'Old')`;
await repo.replaceLists(sql, U1, [{ id: "legacy", name: "Old" }]);
expect((await repo.getLists(sql, U2)).map((l) => l.id)).toEqual([]); // no longer legacy
expect((await repo.getLists(sql, U1))[0]).toMatchObject({ id: "legacy", owner_id: U1 });
});
it("tasks are invisible to other users; legacy tasks visible to everyone", async () => {
await repo.replaceLists(sql, U1, [{ id: "L", name: "List" }]);
await repo.createWebTask(sql, U1, { listId: "L", title: "mine", description: null });
await sql`insert into tasks (id, list_id, title, source) values ('legacy-t', 'L', 'old', 'web')`;
expect((await repo.getUnconsumed(sql, U2)).map((t) => t.id)).toEqual(["legacy-t"]);
expect((await repo.getTasksForList(sql, U2, "L")).map((t) => t.id)).toEqual(["legacy-t"]);
expect(await repo.getUnconsumed(sql, U1)).toHaveLength(2);
});
it("one user's mirror never deletes another user's desktop tasks", async () => {
await repo.replaceLists(sql, U1, [{ id: "L1", name: "U1 List" }]);
await repo.replaceLists(sql, U2, [{ id: "L2", name: "U2 List" }]);
await repo.mirrorDesktopTasks(sql, U1, [{ id: "m1", listId: "L1", title: "u1 task" }]);
await repo.mirrorDesktopTasks(sql, U2, [{ id: "m2", listId: "L2", title: "u2 task" }]);
// U2's full-replace (only m2) must not have wiped U1's m1:
expect((await repo.getTasksForList(sql, U1, "L1")).map((t) => t.id)).toEqual(["m1"]);
// and an empty mirror from U2 clears only U2's partition:
await repo.mirrorDesktopTasks(sql, U2, []);
expect((await repo.getTasksForList(sql, U1, "L1")).map((t) => t.id)).toEqual(["m1"]);
expect(await repo.getTasksForList(sql, U2, "L2")).toEqual([]);
});
it("mirror upsert cannot claim another user's task by id", async () => {
await repo.replaceLists(sql, U1, [{ id: "L1", name: "U1 List" }]);
await repo.replaceLists(sql, U2, [{ id: "L2", name: "U2 List" }]);
const web = await repo.createWebTask(sql, U1, { listId: "L1", title: "u1 web", description: null });
await repo.mirrorDesktopTasks(sql, U2, [{ id: web.id, listId: "L2", title: "hijack" }]);
// U1's web task is untouched and still awaiting pull:
const u1 = await repo.getUnconsumed(sql, U1);
expect(u1.map((t) => t.id)).toEqual([web.id]);
expect(u1[0].title).toBe("u1 web");
});
it("upsertDesktopTask cannot overwrite another user's task", async () => {
await repo.replaceLists(sql, U1, [{ id: "L", name: "List" }]);
await repo.upsertDesktopTask(sql, U1, "shared-id", { listId: "L", title: "u1 owns this" });
const res = await repo.upsertDesktopTask(sql, U2, "shared-id", { listId: "L", title: "u2 steal" });
expect(res.created).toBe(false);
expect((await repo.getTasksForList(sql, U1, "L"))[0].title).toBe("u1 owns this");
});
it("consume and delete are scoped to the caller", async () => {
await repo.replaceLists(sql, U1, [{ id: "L", name: "List" }]);
const t = await repo.createWebTask(sql, U1, { listId: "L", title: "w", description: null });
expect(await repo.consume(sql, U2, t.id)).toBe(false);
await repo.deleteTask(sql, U2, t.id); // no-op for U2
expect((await repo.getUnconsumed(sql, U1)).map((x) => x.id)).toEqual([t.id]);
});
});