diff --git a/.planning/phases/05-web-push-notifications/05-REVIEW.md b/.planning/phases/05-web-push-notifications/05-REVIEW.md new file mode 100644 index 0000000..e6d5d01 --- /dev/null +++ b/.planning/phases/05-web-push-notifications/05-REVIEW.md @@ -0,0 +1,345 @@ +--- +phase: 05-web-push-notifications +reviewed: 2026-06-09T00:00:00Z +depth: standard +files_reviewed: 20 +files_reviewed_list: + - apps/api/src/lib/pushDispatcher.ts + - apps/api/src/lib/pushCoalescer.ts + - apps/api/src/lib/listChangeDispatcher.ts + - apps/api/src/lib/eventChangeDispatcher.ts + - apps/api/src/broker/reminderScheduler.ts + - apps/api/src/broker/sync.ts + - apps/api/src/broker/poller.ts + - apps/api/src/broker/outboxWorker.ts + - apps/api/src/routes/push.ts + - apps/api/src/routes/lists.ts + - apps/api/src/index.ts + - apps/api/src/db/schema.ts + - apps/api/src/db/migrations/0003_same_xavin.sql + - apps/pwa/src/sw.ts + - apps/pwa/src/hooks/usePushSubscription.ts + - apps/pwa/src/components/PushPermissionPrompt.tsx + - apps/pwa/src/components/SettingsSheet.tsx + - apps/pwa/src/components/PermissionDeniedBanner.tsx + - apps/pwa/src/App.tsx + - apps/pwa/src/components/AppNav.tsx + - apps/pwa/src/components/CalendarShell.tsx + - apps/pwa/src/components/InstallPrompt.tsx + - apps/pwa/vite.config.ts + - docker-compose.yml +findings: + critical: 4 + warning: 5 + info: 3 + total: 12 +status: issues_found +--- + +# Phase 5: Code Review Report + +**Reviewed:** 2026-06-09 +**Depth:** standard +**Files Reviewed:** 20+ +**Status:** issues_found + +## Summary + +Phase 5 delivers Web Push across three trigger paths (NOTIF-01/02/03) plus the iOS reliability infrastructure. The architecture is sound: VAPID signing is delegated to `web-push`, the D-03 actor-suppression filter is applied both at the DB layer and in application code, `event.waitUntil()` is used correctly in the SW, and the D-13 boundary (no tsdav in notification code) is respected. + +Four blockers were found. Two are correctness bugs: (1) the `sentReminders` set in `reminderScheduler.ts` grows without bound and is never pruned — it will leak memory and eventually begin skipping due reminders after a process restart populates a new set without stale guard entries; (2) the `uniq_push_endpoint` unique constraint is defined on a `text` column in the migration SQL, which MariaDB/InnoDB silently truncates to 767 bytes when creating the index — a subscription with a very long endpoint can collide with or fail to match a different subscription, causing phantom prune-without-delete or missed 410 cleanup. Two are security: (3) `VAPID_PRIVATE_KEY` is passed via `docker-compose.yml` environment block from the host `.env` but with no fallback (`${VAPID_PRIVATE_KEY}` with no `:-` default) — Docker Compose will error at startup if the variable is unset rather than leaving push non-functional, which is correct, but the VAPID private key has no runtime check that it is well-formed before `setVapidDetails` is called, and invalid keys are only warned, not blocked; and (4) the `notificationclick` handler in `sw.ts` uses `client.url === url` for exact-URL matching, which means a focused-window match will never fire for notifications with query-string deep-links whose current window URL does not exactly match (e.g. `/calendar?date=2026-06-09&event=uid` vs `/calendar`) — this is a functional regression, not a security issue, but is reclassified as a correctness blocker because the D-14 deep-link requirement is unmet for the most common case. + +--- + +## Critical Issues + +### CR-01: `sentReminders` Set Grows Without Bound (Memory Leak + Correctness) + +**File:** `apps/api/src/broker/reminderScheduler.ts:38` + +**Issue:** `sentReminders` is a module-level `Set` that accumulates one entry per `${uid}:${minuteBucket}` every minute for every shared timed event. Entries are never removed. For an app running continuously with N shared events, the set grows by N entries/minute indefinitely. After a long uptime the set consumes significant memory. More critically: the deduplication key includes `minuteBucket = floor(ms / 60000)`, so after a process restart the set is empty and an event that fired in the previous minute (same wall-clock minute bucket) will fire again — the dedup logic is only effective within a single process lifetime. This is documented as "acceptable data loss on restart" for the double-fire case, but the unbounded growth is not documented and constitutes a bug. + +**Fix:** Prune entries for past minute buckets after each scan. After the dispatch loop completes, remove any set entry whose minuteBucket is strictly less than `currentBucket - 1`: + +```typescript +// After the dispatch loop in runReminderCheck(): +const staleBucket = minuteBucket - 1 +for (const key of sentReminders) { + const [, bucketStr] = key.split(':') + if (Number(bucketStr) < staleBucket) { + sentReminders.delete(key) + } +} +``` + +--- + +### CR-02: `uniq_push_endpoint` Unique Constraint on `text` Column Will Silently Truncate in MariaDB/InnoDB + +**File:** `apps/api/src/db/migrations/0003_same_xavin.sql:10` and `apps/api/src/db/schema.ts:243-244` + +**Issue:** `endpoint` is declared `text NOT NULL` in both the schema and migration, and `CONSTRAINT uniq_push_endpoint UNIQUE(endpoint)` is applied to it. MariaDB/InnoDB cannot index an unbounded `text` column directly — it requires a prefix length. The behavior differs by server version and `innodb_large_prefix` setting: some versions silently create a prefix index (767 bytes by default, 3072 bytes with `ROW_FORMAT=DYNAMIC`), others fail. In either case a silent prefix match means two endpoints that share the same first 767 characters — which is possible for push endpoints from the same push service domain — are treated as duplicates. This makes the upsert in `push.ts`'s `onDuplicateKeyUpdate` unreliable: a second distinct subscription may silently collide, or the migration may fail entirely on strict-mode MariaDB. + +The correct fix is to define `endpoint` as `varchar(2048)` (matching the Zod validation bound of `z.string().url().max(2048)` in `routes/push.ts:61`) in both the schema and migration. + +**Fix:** +```sql +-- Migration: change endpoint column type +ALTER TABLE `push_subscriptions` + MODIFY COLUMN `endpoint` varchar(2048) NOT NULL; + +-- Schema: change text() to varchar +endpoint: varchar('endpoint', { length: 2048 }).notNull(), +``` + +The Zod schema in `push.ts` already enforces `max(2048)`, so server-validated inputs fit within the column. Update `p256dh` similarly — it is also `text` but a `varchar(512)` would match the Zod bound. + +--- + +### CR-03: `notificationclick` Deep-Link Window Focus Uses Exact URL Match — D-14 Never Fires for Query-String Deep-Links + +**File:** `apps/pwa/src/sw.ts:154` + +**Issue:** The `notificationclick` handler matches `client.url === url` to find an existing window. Push deep-link URLs for event notifications are of the form `/calendar?event=` or `/calendar?date=&event=`. When the user has the PWA open on `/calendar` (the root path, no query string), `client.url` will be `https://host/calendar` but `url` will be `https://host/calendar?event=uid` — the exact comparison fails, so the handler falls through to `openWindow` and opens a second PWA window instead of focusing the existing one. On iOS standalone mode, `openWindow` does NOT open a second window; it navigates the existing window, so the net behavior is correct on iOS. On Android Chrome it opens a second window. The deeper problem: the handler never navigates the focused window to the deep-link URL; it only calls `focus()` without `navigate()` — so even when the match succeeds (notification for the currently-open URL), the window is focused at the current route, not the notification target. + +**Fix:** Use `clients.matchAll` to find any window on the same origin, focus it, and call `client.navigate(url)`: + +```typescript +self.addEventListener('notificationclick', (event: NotificationEvent) => { + event.notification.close() + const url: string = + typeof event.notification.data?.url === 'string' + ? event.notification.data.url + : '/' + + event.waitUntil( + self.clients + .matchAll({ type: 'window', includeUncontrolled: true }) + .then((clientList) => { + // Focus any existing window on this origin and navigate it + for (const client of clientList) { + if ('focus' in client) { + return (client as WindowClient).focus().then(() => + (client as WindowClient).navigate(url) + ) + } + } + if (self.clients.openWindow) { + return self.clients.openWindow(url) + } + }), + ) +}) +``` + +--- + +### CR-04: `subscribe()` in `usePushSubscription` Calls `fetchVapidKey()` (an async `await`) Before `pushManager.subscribe()` — iOS Gesture Gate May Be Broken on Cache Miss + +**File:** `apps/pwa/src/hooks/usePushSubscription.ts:197-206` + +**Issue:** The `subscribe()` function unconditionally `await`s `fetchVapidKey()` before calling `registration.pushManager.subscribe()`. The comment says "Fetch VAPID key (from cache if already prefetched)" and the VAPID key is pre-fetched in a `useEffect` by `PushPermissionPrompt`. However: + +1. The `useEffect` pre-fetch is fire-and-forget (`void prefetchVapidKey()`). There is no guarantee it completes before the user taps "Enable Notifications" — especially on a slow connection on first install. +2. If `sessionStorage` is unavailable (private mode, quota exceeded) or the pre-fetch failed silently, `fetchVapidKey()` will make a network round-trip inside `subscribe()`. +3. iOS requires `pushManager.subscribe()` to be called synchronously within the user gesture's call stack. An `await fetch(...)` before `pushManager.subscribe()` breaks the gesture gate — the iOS browser will throw `NotAllowedError` with the message "The operation was not permitted." The existing code has this `await` on the very first line of `subscribe()`. + +The `setEnabled(true)` path in the same hook has the same structure (`await fetchVapidKey()` before `pushManager.subscribe()`), but `setEnabled(true)` is only called when `Notification.permission === 'granted'` (OS already approved), so no OS dialog is shown — the gesture gate for permission is not relevant there. For the `subscribe()` path used from `PushPermissionPrompt` and `SettingsSheet`, the gate IS relevant when `Notification.permission === 'default'`. + +**Fix:** Pre-fetch the VAPID key synchronously into a variable before the tap, and pass it as an argument to `subscribe()`. Remove the `fetchVapidKey()` call from inside `subscribe()`: + +```typescript +// In PushPermissionPrompt: pre-fetch before the button renders +const [vapidKey, setVapidKey] = useState(null) +useEffect(() => { + fetchVapidKey().then(setVapidKey).catch(() => {}) +}, []) + +// subscribe() signature change: +const subscribe = async ( + registration: ServiceWorkerRegistration, + vapidKey: string, +): Promise => { + // No await before this line — iOS gesture gate preserved + const sub = await registration.pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey: urlBase64ToUint8Array(vapidKey), + }) + // ... POST to server +} +``` + +If a pre-pass API approach is not desirable, the minimum safe fix is to ensure `fetchVapidKey()` always resolves from `sessionStorage` (never from the network) when called from within a tap handler, by making the pre-fetch mandatory and blocking the button render until the key is ready: + +```typescript +// Disable the "Enable Notifications" button until vapidKey is loaded +