fragJulia
Changelog

2026-11-21 — Document counter: no cross-user increments

increment_document_llm_call could be called for any user id by anyone, including callers without an account. It now only counts for the caller. Applied to the live database and verified.

Problem

increment_document_llm_call(p_user_id, p_day) (apps/web/scripts/005_document_llm_rate_limit.sql) is SECURITY DEFINER and executable by anon and authenticated (Supabase security advisor, lints 0028 and 0029, read 2026-11-21). Anyone could raise another user's counter to the daily limit of 10 (apps/web/lib/documents/doc-rate-limit.ts:3) and lock them out of /api/documents/simplify and /api/documents/summarize. Listed as a follow-up in the live migration run.

Change

supabase/migrations/20261121_document_llm_counter_lockdown.sql: the function refuses API callers (anon, authenticated) whose p_user_id is not their own auth.uid(), and pins search_path. Same pattern as 20261121_post_count_client_lockdown.sql. Both document routes call it with the signed-in user's own id and client (simplify/route.ts:176, summarize/route.ts:114), so they are unaffected.

Applied to live

Project oxixxrzdrryfztfxlady, 2026-11-21, as migration 20261121_document_llm_counter_lockdown.

Verification, each inside a transaction that was rolled back:

  • Before applying (function replaced inside the transaction): own id counts (two calls, counter = 2); another user's id is refused for authenticated; any id is refused for anon; no row for the other user.
  • After applying: own id counts; another user's id is refused.
  • No test rows left (user_document_llm_calls has 0 rows).

Other advisor findings, not changed

  • admin_user_view is flagged as a SECURITY DEFINER view. Its definition returns rows only when the caller's own profile has is_admin = true, and only id, pseudonym, is_admin, timestamps. Left as is.
  • 12 functions without a pinned search_path; 15 SECURITY DEFINER functions executable by anon. Not reviewed one by one here.
  • Leaked-password protection is off in Supabase Auth (dashboard setting).

On this page