fix: address performance and security findings
- Add PostgreSQL-backed backend and frontend tests to CI - Harden Android API logging and disable app backups - Update the performance report with resolved findings
This commit is contained in:
1 parent
a84a36349b
commit
937f51b4fb
35 files changed
+2566
-332
No files matched your search
+20
-13
@@ -5,58 +5,65 @@
|
||||
- **Analyzed:** 2026-10-16
|
||||
- **Commit:** 6aec03f on main
|
||||
- **Working tree:** clean (no uncommitted changes, no untracked files)
|
||||
- **Updated:** 2026-10-19 — Critical, High and Medium findings handled in the working tree (not committed); open Medium findings name their blocker in their status lines. A second Medium pass added database-backed backend tests, frontend helper tests and a CI Postgres service (Q-004, still open for Android tests) and re-checked Q-006.
|
||||
- **Stack:** Go 1.26 (net/http, pgx v5) API server; PostgreSQL; SvelteKit 2 / Svelte 5 static SPA (Vite 8, Tailwind 4, TanStack Query) embedded in the Go binary; Android client (Kotlin, Jetpack Compose, Room, Retrofit/OkHttp); Docker; GitHub Actions.
|
||||
- **Scope:** Read all first-party backend code (`backend/*.go`, including the five test files), the frontend data layer, routes, list components and Vite config, the Android API client, preferences, sync repository and app Gradle file, `README.md`, `.github/workflows/*`, `docker/Dockerfile`, and the shared auth, postgres and authclient code that CollabTable calls. Excluded: `node_modules`, `frontend/.svelte-kit`, the Gradle wrapper jar, generated build output, lock files (read for versions only), and the rest of `shared/`. The tracked SQLite files were inspected read-only from a copy. No tests, linters or builds were run.
|
||||
|
||||
## Summary
|
||||
|
||||
| Severity | Count |
|
||||
|---|---:|
|
||||
| Critical | 0 |
|
||||
| High | 0 |
|
||||
| Medium | 6 |
|
||||
| Low | 8 |
|
||||
| Info | 0 |
|
||||
| Severity | Count | Fixed | Not reproduced | Open |
|
||||
|---|---:|---:|---:|---:|
|
||||
| Critical | 0 | 0 | 0 | 0 |
|
||||
| High | 0 | 0 | 0 | 0 |
|
||||
| Medium | 6 | 4 | 0 | 2 |
|
||||
| Low | 8 | 0 | 0 | 8 |
|
||||
| Info | 0 | 0 | 0 | 0 |
|
||||
|
||||
The backend is small and readable, but its transactional and delete logic is duplicated across the REST and sync paths and has drifted apart. A few error-handling assumptions are wrong, and the data layer has essentially no test coverage.
|
||||
|
||||
## Issues
|
||||
|
||||
### Q-001 · Notification errors inside transactions abort the sync they were meant to tolerate · **Medium**
|
||||
### Q-001 · Notification errors inside transactions abort the sync they were meant to tolerate · **Medium** · Fixed
|
||||
- **Location:** `backend/notifications.go:51-57` (`ignoreNotificationError`); called inside transactions at `backend/sync.go:79`, `:126`, `:164`, `:206` and `backend/data.go:453`.
|
||||
- **Description:** `ignoreNotificationError` says a failed notification "must never roll back the data mutation". When the notification insert runs inside the transaction, PostgreSQL marks the transaction as aborted after any failing statement, so the next statement fails and the commit cannot succeed. The error is only logged, which makes the function look like it tolerates the failure, but the whole sync then returns `500 Sync failed` after rolling back. The comment is wrong for these paths.
|
||||
- **Impact:** A transient failure of a notification insert turns a successful write into a failed sync, and the client retries the same payload. Maintainers reading the comment will assume the opposite of what happens.
|
||||
- **Recommendation:** Isolate the notification insert in a savepoint (a nested `tx.Begin` in pgx, or `SAVEPOINT` and `ROLLBACK TO SAVEPOINT`), or write notifications after commit. Update the comment to match the actual behavior.
|
||||
- **Status:** Fixed 2026-10-19 — Notification inserts inside a transaction now run in a savepoint (`enqueueNotificationInTx`), so a failed insert rolls back only the notification and the data change still commits. Each such notification adds two statements (savepoint and release). Files: backend/notifications.go, backend/sync.go, backend/data.go.
|
||||
|
||||
### Q-002 · Delete semantics differ between REST endpoints and sync · **Medium**
|
||||
### Q-002 · Delete semantics differ between REST endpoints and sync · **Medium** · Fixed
|
||||
- **Location:** `backend/data.go:309-321` (REST list delete sets only `lists.isdeleted`); `backend/sync.go:81-91` (sync list delete also cascades to fields, items and values); `backend/data.go:420-455` (field delete cascades only when no active fields remain); `backend/data.go:609-621` (item delete updates the item, then deletes its values in a separate statement whose error is discarded at `:620`, and skips the notification at `:618-619` if the re-read fails).
|
||||
- **Description:** The same logical operations (delete a list, delete an item) leave different state depending on the entry point. A list deleted through REST keeps its fields, items and values active, while the same delete through sync removes the children. `/web/data` filters out deleted lists but still returns the active items and values that belong to them (`backend/web.go:31-72`). Item deletion is not atomic and can leave orphaned values if the second statement fails.
|
||||
- **Impact:** The dashboard, the Android client and sync can disagree about what exists, and orphaned values are silently accepted.
|
||||
- **Recommendation:** Implement `deleteList`, `deleteField` and `deleteItem` once, each inside a transaction, and call them from both REST and sync. Check every error.
|
||||
- **Status:** Fixed 2026-10-19 — REST and sync now share `cascadeDeleteList`, `cascadeDeleteField` and `cascadeDeleteItem`, each delete runs in one transaction, and the value-delete error is no longer discarded. REST list deletes now cascade to fields, items and values as sync already did, which is a client-visible change for REST callers. Files: backend/data.go, backend/sync.go.
|
||||
|
||||
### Q-003 · `handleSync` is a 200-line function, and cascade logic is duplicated · **Medium**
|
||||
### Q-003 · `handleSync` is a 200-line function, and cascade logic is duplicated · **Medium** · Fixed
|
||||
- **Location:** `backend/sync.go:45-249` (one function; the transaction closure spans `:61-209`). Duplicated cascade logic: `sync.go:128-143` and `backend/data.go:440-452`. Event-type selection repeated at `sync.go:72-78`, `:119-125` and `:157-163`. Default-value handling repeated at `sync.go:96-103` and `backend/data.go:349-354` and `:393-398`.
|
||||
- **Description:** Upserts, notifications, cascades and response assembly all live in one closure. The business rules for deletion, defaults and event types are implemented twice, once in the sync handler and once in the REST handlers. Q-002 is a direct result of this duplication.
|
||||
- **Impact:** Hard to test or change safely, and the two code paths have already diverged.
|
||||
- **Recommendation:** Extract a small repository layer (`upsertList`, `upsertField`, `upsertItem`, `upsertValue`, `deleteList`, `deleteField`, `deleteItem`) shared by the REST handlers and sync, and keep the handlers as thin request and response adapters. Move event-type selection into one helper.
|
||||
- **Status:** Fixed 2026-10-19 — `handleSync` is now a thin adapter over `applySyncRequest` and the per-table `applyLists`, `applyFields`, `applyItems` and `applyValues` helpers. The cascades (`cascadeDeleteList`, `cascadeDeleteField`, `cascadeDeleteItem`), `fieldDefaults` and `upsertValue` are shared by REST and sync, and sync picks its event type through one helper (`changeEventType`). The REST list, field and item create and update handlers keep their own SQL on purpose, because a plain `INSERT` rejects a duplicate id and switching it to the last-writer-wins upsert would change REST behaviour. Files: backend/sync.go, backend/data.go.
|
||||
|
||||
### Q-004 · Core data layer has no tests, and CI never starts a database · **Medium**
|
||||
### Q-004 · Core data layer has no tests, and CI never starts a database · **Medium** · Open
|
||||
- **Location:** `backend/routes_test.go:8-11` (registers routes with a nil database and asserts nothing); `backend/auth_test.go`, `env_test.go`, `models_test.go` and `sync_test.go` (cover env parsing, JSON timestamp parsing, timestamp normalization and the auth stub only); `.github/workflows/tests.yml` (Go job runs `go test ./...` with no Postgres service); the frontend has no unit tests (CI runs `npm run check` only); `CollabTableAndroid/app/src` has no test sources.
|
||||
- **Description:** No test exercises `handleSync`, the list, field, item and value handlers, `/web/data`, the CORS wrapper, the SPA fallback, or the frontend helpers in `listHelpers.js`. The one route test runs without assertions.
|
||||
- **Impact:** The most complex logic (sync merge, cascades, notification side effects, timestamp handling) is covered only by manual testing, so regressions like Q-001 and Q-002 are invisible.
|
||||
- **Recommendation:** Add `httptest` integration tests against a real Postgres (as a CI service container), starting with sync round trips and delete cascades. Give `routes_test.go` real assertions on methods and status codes. Add Vitest tests for `listHelpers.js`, and JVM unit tests for the Android sync merge logic.
|
||||
- **Status:** Open 2026-10-19 — Partly fixed: route and middleware tests assert real behaviour (`backend/routes_test.go`, `backend/server_test.go`). New database-backed tests in `backend/db_test.go` cover sync round trips between devices, the `updatedat >= since` delta, last-writer-wins, stale and newer deletes, delete cascades on the REST and sync paths, the value-cell guard, orphan values, the notification savepoint, `/web/data` ETags and API tokens. They need `TEST_DATABASE_URL` (each test uses its own schema and drops it) and are skipped without it; `tests.yml` now starts a PostgreSQL service container and sets it. A `node --test` suite covers `listHelpers.js` (`npm test`, no new dependency, also run in CI). Run against PostgreSQL 18 here: all pass, and breaking four of the rules made a test fail. Still open: Android JVM unit tests for the sync merge logic. JUnit is declared, but there is no JDK or Gradle here to compile or run them, so none were written. The CI change itself has not run.
|
||||
|
||||
### Q-005 · Setup documentation contradicts the configuration · **Medium**
|
||||
### Q-005 · Setup documentation contradicts the configuration · **Medium** · Fixed
|
||||
- **Location:** `README.md:52` (`SECRET` marked as optional, default "unset (push endpoint refuses every caller)"); `README.md:70-79` (Quick Start `docker run` sets no `SECRET`); `README.md:55` (`COOKIE_SECURE` described as marking cookies `Secure`); `shared/backend/auth/proxy.go:359-361` and `:838-841`; `backend/auth.go:113-120`; `frontend/vite.config.js:38-41` (dev proxy forwards only `/web` and `/health`).
|
||||
- **Description:** The auth service is built with `RequireSecret()` (`auth.go:118`). When `SECRET` is unset, `/api/auth/status` and every auth-gated route return `500 SECRET environment variable is not set.`, so the documented Quick Start produces a dashboard that cannot load. The README table also calls `SECRET` optional. The live session cookie's `Secure` and `SameSite` attributes come from AuthGuard (see Security S-013), so `COOKIE_SECURE` does not do what the README says. `npm run dev` proxies only `/web` and `/health`, but the dashboard calls `/api/auth/*` and `/api/tech-stack` (`frontend/src/lib/api.js`), so the dev server cannot reach the API.
|
||||
- **Impact:** Operators who follow the docs get a deployment that fails or looks secure when it is not, and developers hit a confusing dev setup.
|
||||
- **Recommendation:** Mark `SECRET` as required when auth is enabled, add it to the Quick Start, correct the `COOKIE_SECURE` description, and add `/api` to the Vite proxy.
|
||||
- **Status:** Fixed 2026-10-19 — README and backend README now mark `SECRET` as required unless `AUTH_DISABLED=true`, add it to the Quick Start, correct the `COOKIE_SECURE` description, and document `ALLOWED_ORIGINS`; the Vite dev proxy now forwards `/api`. Files: README.md, backend/README.md, frontend/vite.config.js.
|
||||
|
||||
### Q-006 · Client-supplied timestamps drive the change cursor and overwrite unconditionally · **Medium**
|
||||
### Q-006 · Client-supplied timestamps drive the change cursor and overwrite unconditionally · **Medium** · Open
|
||||
- **Location:** `backend/sync.go:20-43` (client timestamps accepted as sent, clamped only if in the future); upserts that overwrite regardless of the stored `updatedat`: `sync.go:64-69`, `:104-116`, `:149-156`, `:198-203`; the delta cursor filters on the stored value at `backend/data.go:75`, `:108`, `:145`, `:178`. Android: local edits stamped with device time (`CollabTableAndroid/app/src/main/java/com/collabtable/app/ui/screens/ListDetailViewModel.kt:148`), and the cursor is the server's `serverTimestamp` (`CollabTableAndroid/app/src/main/java/com/collabtable/app/data/repository/SyncRepository.kt:49-60`, `:283`).
|
||||
- **Description:** Two related problems. (1) A device that edits offline stamps the change with an old local time. If another device has already advanced its cursor past that time, the `updatedat >= since` filter never returns the change to it. (2) Upserts overwrite the stored row whatever its `updatedat`, so an older change that arrives later replaces a newer one. The Android client mitigates (1) by forcing a full down-sync at app start (`SyncRepository.kt:122`), so the devices affected are those that stay open for a long time.
|
||||
- **Impact:** Silent divergence between devices, and lost updates when two devices edit the same row offline. Dashboard users see stale data until a device restarts.
|
||||
- **Recommendation:** Assign server-side change tokens (a monotonically increasing sequence, or the server receive time) to every write and use that as the cursor, keeping client `updatedat` for display only. Alternatively, make each upsert conditional on `EXCLUDED.updatedat` being newer than the stored value, and document the last-writer-wins rule.
|
||||
- **Status:** Open 2026-10-19 — Re-checked, unchanged. Conditional upserts are in place and now covered by `backend/db_test.go` (`TestSyncLastWriterWinsByTimestamp`, `TestStaleDeleteDoesNotCascadeButNewerDeleteDoes`; an equal timestamp still applies). Still open: a change stamped with an old device clock can miss the `updatedat >= since` cursor. The fix needs a server-side change token on every row (a new column with a backfill, so a stored-data-format change) and a rollout plan. Decision needed: the token design (server receive time or a sequence) and whether the Android cursor changes with it.
|
||||
|
||||
### Q-007 · Validation and error mapping return misleading status codes and contract drift · **Low**
|
||||
- **Location:** `backend/data.go:248-255` (duplicate list id returns `500 Failed to create list` rather than `409`); `backend/data.go:549-558` (an item value for a missing item or field fails on a foreign key and returns `500`); `backend/data.go:589-607` (`PUT /api/items/{id}` accepts a body but only bumps `updatedat`, ignoring the body); `backend/server.go:292-316` (unknown `/api/...` paths return `index.html` with `200`).
|
||||
|
||||
Reference in new issue
Block a user