Skip to content

fix(consent): lock ACEPTO, tighten log grants and widen clinical detection - #10

Merged
juancodedev merged 7 commits into
mainfrom
feat/consent-follow-ups
Nov 10, 2026
Merged

juancodedev merged 7 commits into
mainfrom
feat/consent-follow-ups

Conversation

@juancodedev

@juancodedev juancodedev commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Closes #7

Summary

Follow-ups from the #6 reviews, plus one permission found while verifying production.

  • ACEPTO takes the per-sender lock (migration 009). The grant now goes through record_sender_consent. It takes the same advisory lock as BAJA and ELIMINAR, and refuses the grant when the consent text is no longer recorded as shown. A grant can no longer land after an erasure. The bot then shows the full text again.
  • The consent log is written only through locked functions. service_role loses direct INSERT on consent_events. anon and authenticated lose the default grant on consent_events_id_seq.
  • ELIMINAR keeps the current rate window. The Edge Function passes the window, which replaces the 2-hour literal that assumed a one-hour window.
  • A published policy must name ACEPTO, BAJA and ELIMINAR. A check constraint enforces this; drafts are not checked.
  • Clinical detection.
    • Matching ignores accents and case.
    • Newly detected: "deprimido/a", "no puedo dormir", "insomnio", and common psychiatric drugs.
    • Any mention of panic is clinical except the everyday "me da pánico".
    • The refusal check is derived from the referral text.
  • Crisis with an unreadable consent stays fail-closed. The helpline is sent and the crisis marker is not stored. This is now documented and pinned by a test.
  • Maintainability.
    • Replies are built from the command constants.
    • The purge job uses the function's default retention.
    • erased is renamed to consentWithdrawn.
    • The purge SQL test passes on a non-empty database.

Before deploying

  1. Apply migration 009, then deploy webhook right away. Until then, ACEPTO and ELIMINAR reply that they could not be processed and confirm nothing. With the policy still unpublished, only ELIMINAR is exposed.
  2. 009 adds a check that aborts the whole migration if a published policy lacks one of the three words. Production holds only the unpublished draft, and it names all three (checked on 2026-11-10).
  3. Optional: the scheduled purge job still passes interval '90 days' explicitly, which is equivalent. Re-running supabase/scripts/schedule_retention_purge.sql makes it follow the default.

Test plan

  • Edge tests: 62 passed (deno test supabase/functions/_shared/phase1.test.ts, type-checked)
  • npx tsc --noEmit clean; Node tests: 13 passed
  • SQL tests pass on Postgres 17 with migrations 001–009 (fresh and populated databases). New cases:
    • record_sender_consent with and without a prompt, and after ELIMINAR
    • direct INSERT refused for service_role
    • the exact counter ELIMINAR keeps
    • a null window is rejected
    • sequence and function grants
    • publishing without the command words
    • the 90-day default
  • Independent verification: S1–S10 met, no blockers.
    • 009 applied over populated data leaves consent_events unchanged (same count and md5).
    • A concurrent record_sender_consent blocks on the advisory lock held by erase_sender_data and then returns false.
    • Reply texts are byte-identical to main.
    • Clinical detection differs from main only for the requested phrases.
  • Not observed on a real Supabase project; grants were checked on plain Postgres with simulated Supabase default privileges.

…t rate window on ELIMINAR

ACEPTO now goes through record_sender_consent, which takes the same advisory
lock as BAJA and ELIMINAR and refuses the grant when the consent text is no
longer recorded as shown. The service role loses direct INSERT on
consent_events, erase_sender_data receives the current rate-limit window
from the Edge Function, client roles lose the sequence grant, and a
published policy must name ACEPTO, BAJA and ELIMINAR.

Refs #7
…e 'pánico'

Clinical matching is now accent-insensitive and covers 'deprimido/a',
'no puedo dormir', insomnia and common psychiatric medication, while
'pánico' alone no longer counts. The clinical refusal check is derived
from the referral text, and the fail-closed crisis storage on an
unreadable consent is documented and pinned by a test.

Refs #7
…etention in one place

Reply texts take ACEPTO, BAJA and ELIMINAR from the gate constants, the
purge job uses the function's default retention, and 'siento pánico',
'tengo pánico' and 'trastorno de pánico' are clinical again. Tests restore
the injected Claude client in finally and check that publishing the
draft publishes it.

Refs #7
…day 'me da pánico'

Also pin the purge's 90-day default in the SQL test, compare the clinical
referral marker case-insensitively, and correct the retention notes.

Refs #7
@juancodedev
juancodedev merged commit 2723fe8 into main Nov 10, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consent and data rights: follow-ups from the #6 reviews

1 participant