Security in PACT is designed database-first and server-authoritative. Security must NEVER depend solely on frontend UI behavior; the frontend is a presentation layer, NOT a security boundary.
Every feature MUST be implemented in the following strict vertical sequence:
1. Database Schema & Constraints
↓
2. Row Level Security (RLS) & Authorization
↓
3. Data Access Layer / Repository (Server-Side)
↓
4. Server Actions / API Endpoints
↓
5. Input Validation (Zod Schemas)
↓
6. Unit & Integration Tests
↓
7. Security Code Review
↓
8. Frontend UI Implementation
↓
9. Visual QA & Verification
For every security domain, PACT explicitly defines the 5 fundamental security dimensions:
| # | Security Domain | WHAT is protected? | WHO can access it? | WHERE is the boundary? | HOW is it enforced? | HOW will it be tested? |
|---|---|---|---|---|---|---|
| 1 | Authentication | User identity & session state | Authenticated user owning the session | Supabase Auth API / Middleware | JWT token verification on every server route | FUTURE TEST — Unauthenticated Request Rejection |
| 2 | Authorization | System features & data operations | Authorized resource owner only | Server Actions / API Gateway | Role & ownership check prior to operation | FUTURE TEST — Unauthorized Role Action Block |
| 3 | User Ownership | All user-created records (tasks, goals, expenses) | Explicit row owner (auth.uid() = user_id) |
PostgreSQL RLS Engine | USING (auth.uid() = user_id) DB policies |
FUTURE TEST — Cross-User Data Isolation |
| 4 | PostgreSQL RLS | All tables containing user_id |
Database engine level query filter | PostgreSQL Core DB Engine | RLS policies enabled on all user tables | FUTURE TEST — Direct SQL RLS Enforcement |
| 5 | Direct DB Attack Scenarios | Raw database rows & RPC functions | Authenticated owner only | Database connection pool | RLS policy evaluation inside Postgres | FUTURE TEST — Direct DB REST/RPC Attack |
| 6 | Cross-User SELECT | Reading another user's records | Owning user only | DB RLS SELECT Policy | CREATE POLICY ... FOR SELECT USING (auth.uid() = user_id) |
FUTURE TEST — Cross-User SELECT Prevention |
| 7 | Cross-User INSERT | Creating records under another user_id |
User creating their own record | DB RLS INSERT Policy | WITH CHECK (auth.uid() = user_id) |
FUTURE TEST — Forged userId INSERT Block |
| 8 | Cross-User UPDATE | Modifying another user's records | Owning user only | DB RLS UPDATE Policy | USING (auth.uid() = user_id) WITH CHECK (...) |
FUTURE TEST — Cross-User UPDATE Block |
| 9 | Cross-User DELETE | Deleting another user's records | Owning user only | DB RLS DELETE Policy | CREATE POLICY ... FOR DELETE USING (auth.uid() = user_id) |
FUTURE TEST — Cross-User DELETE Block |
| 10 | ID Manipulation | Accessing resources by guessing UUIDs | Owning user only | DB RLS + Server Validation | RLS filters out non-owned UUIDs even if guessed | FUTURE TEST — UUID Guessing Attack Defense |
| 11 | Forged userId | Injecting arbitrary user_id in API bodies |
System server context | Server API Boundary | user_id extracted from verified JWT, ignoring body user_id |
FUTURE TEST — Body userId Override Ignored |
| 12 | Forged Timestamps | completed_at, missed_at, created_at |
Server system clock only | Server Action / DB Trigger | Server-side now() generation; client timestamps rejected |
FUTURE TEST — Client Timestamp Injection Block |
| 13 | Forged Lifecycle State | Task status (completed, missed) |
Server state machine engine | Server Business Logic Layer | Status transitions executed solely by server state engine | FUTURE TEST — Arbitrary State Transition Block |
| 14 | Deadline Integrity | Commitment completion validity | Server evaluation engine | Server Boundary | Server compares trusted completed_at against deadline_at |
FUTURE TEST — Boundary Deadline Verification |
| 15 | Completion Integrity | Task completion status verification | Server action workflow | Database Transaction / RPC | Atomic check of deadline and task state inside DB transaction | FUTURE TEST — Concurrent Completion Race Condition |
| 16 | Consequence Confidentiality | Sensitive consequence payload data | Server engine (until revealed) | DB Data Access / RLS View | Column/Row filtering until is_revealed = true set by server |
FUTURE TEST — Unrevealed Consequence Exposure Attempt |
| 17 | Server-Side Enforcement | All security-critical decisions | Trusted server environment | Server Actions / API Routes | Business rules executed exclusively on server | FUTURE TEST — Client Logic Bypass Defeated |
| 18 | Input Validation | API request payloads & parameters | Validated request schemas | API Boundary | Zod schema parsing prior to controller execution | FUTURE TEST — Malformed Input Schema Rejection |
| 19 | Error Handling | System internal implementation details | Internal server logs only | API Error Middleware | Generic sanitized error responses returned to client | FUTURE TEST — Stack Trace & Leakage Verification |
| 20 | Account Enumeration | User registration status & emails | Public registration endpoint | Auth Handler | Uniform error messages ("Invalid credentials") across all auth failures | FUTURE TEST — Auth Response Timing & Uniformity |
| 21 | Rate Limiting | Public & authenticated API routes | Valid client traffic within quotas | API Gateway / Middleware | Rate limiter tracking IP & User ID request rates | FUTURE TEST — Rate Limit Threshold Enforcement |
| 22 | Session Security | JWT access tokens & refresh tokens | Valid user agent / browser | HTTP Headers / Secure Cookies | Short-lived JWTs, HTTPS-only, SameSite cookies | FUTURE TEST — Expired & Tampered Token Rejection |
| 23 | Integration Credentials | Third-party OAuth tokens | Backend sync job worker | Server-Only Storage Boundary | Tokens stored encrypted at rest; never sent to frontend | FUTURE TEST — Integration Credential Non-Exposure |
| 24 | Minimal Data Exposure | Database query responses | Client view layer | Service / Repository Layer | Explicit SELECT column lists; sensitive fields omitted | FUTURE TEST — Excessive Payload Column Pruning |
| 25 | Auditability | Security-critical state changes | System audit logs | Database Audit Triggers | Immutable audit log records for state transitions | FUTURE TEST — State Transition Audit Log Verification |
| 26 | Race Conditions | Concurrent state update requests | First valid atomic transaction | DB Transaction Isolation | Atomic SERIALIZABLE or FOR UPDATE DB locks |
FUTURE TEST — Parallel Completion Race Condition |
| 27 | Timezone Boundaries | Midnight calculation & daily tasks | Server timezone engine | Server Logic | Storage in UTC (TIMESTAMPTZ); conversion via user profile |
FUTURE TEST — Timezone Midnight Boundary Edge Cases |
| 28 | Financial Data Protection | Expense records & category data | Owning user only | DB RLS & Server Layer | Strict user RLS policies on expenses and expense_categories |
FUTURE TEST — Cross-User Financial Isolation |
-- Standard User Isolation Policy Blueprint
ALTER TABLE public.tasks ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can select own tasks"
ON public.tasks FOR SELECT
USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own tasks"
ON public.tasks FOR INSERT
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own non-trusted task fields"
ON public.tasks FOR UPDATE
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can delete own tasks"
ON public.tasks FOR DELETE
USING (auth.uid() = user_id);Consequence data confidentiality is enforced strictly at the database / data-access layer:
consequencestable rows containingencrypted_payloadare inaccessible to standard client queries whileis_revealed = false.- Transitioning
is_revealedtotrueis executed exclusively by trusted server-side cron workers or security-definer RPC stored procedures upon verified deadline failure.
For Phase 2C, Projects support optional parent Goal linkage. Security enforcement guarantees that a user cannot link a Project to a Goal owned by another user:
- Server Action Authorization:
createProjectActionandupdateProjectActionexplicitly querypublic.goalsmatchingid = goal_idANDuser_id = user.idprior to performing any database mutation. If the Goal is not owned by the authenticated user, the mutation fails immediately with"Selected Goal does not exist or does not belong to you.". - PostgreSQL RLS Boundary: PostgreSQL RLS policies on
public.projectsenforce:WITH CHECK (goal_id IS NULL OR EXISTS (SELECT 1 FROM public.goals g WHERE g.id = goal_id AND g.user_id = auth.uid())). - Adversarial Verification: Verified against the real remote Supabase database via
tests/adversarial-rls-audit.sql:- Cross-user Project
INSERTwith another user's Goal ID is blocked by RLS. - Cross-user Project
UPDATEsettinggoal_idto another user's Goal ID is blocked by RLS. - Independent projects with
goal_id = NULLoperate without restriction.
- Cross-user Project
For Phase 2D, Tasks support optional parent Goal (goal_id) and/or parent Project (project_id) linkage. Security enforcement guarantees that a user cannot link a Task to parent entities owned by another user:
- Server Action Authorization:
createTaskActionandupdateTaskActionindependently verify that providedgoal_idexists inpublic.goalsmatchinguser_id = user.idANDproject_idexists inpublic.projectsmatchinguser_id = user.idprior to executing any mutation. - PostgreSQL RLS Boundary: PostgreSQL RLS policies on
public.tasksenforce:WITH CHECK (auth.uid() = user_id AND (goal_id IS NULL OR EXISTS (SELECT 1 FROM public.goals g WHERE g.id = goal_id AND g.user_id = auth.uid())) AND (project_id IS NULL OR EXISTS (SELECT 1 FROM public.projects p WHERE p.id = project_id AND p.user_id = auth.uid()))). - Trusted Field Protection: PostgreSQL trigger
enforce_task_trusted_fields()enforces immutability ofcompleted_atandmissed_attimestamps for non-service_roleclients on bothINSERTandUPDATEstatements. - Adversarial Verification: Verified against real remote Supabase PostgreSQL database via
tests/adversarial-rls-audit.sql:- Cross-user Task
INSERT/UPDATEwith another user's Project ID is blocked by RLS (42501). - Cross-user Task
INSERT/UPDATEwith another user's Goal ID is blocked by RLS (42501). - Direct
INSERTorUPDATEofcompleted_atormissed_atis blocked by trigger. - Anonymous access to
public.tasksis blocked (0 rows /42501).
- Cross-user Task
For Phase 2E, temporal evaluation logic was isolated into a pure, deterministic engine (src/lib/time.ts). Security and temporal integrity guarantees include:
- UTC Instant Storage Invariance:
public.tasks.deadline_atcolumns store timestamps strictly asTIMESTAMPTZrepresenting absolute UTC instants. Changing a user profile's local timezone alters only wall-clock display formatting without modifying stored UTC instants. - Strict Canonical IANA Timezone Validation: Timezones are validated against canonical IANA identifiers (e.g.
Asia/Kolkata,America/New_York,Europe/London,UTC). Non-canonical 3-letter abbreviations (IST,PST,EST) are rejected at the Zod and application boundary (ianaTimezoneSchema). - One-Second Temporal Boundary Integrity:
isDeadlineReached(deadlineAt, clock)enforces standard 1-second precision (now >= deadlineAt), ensuring instant-level boundary consistency. - DST Spring-Forward & Fall-Back Edge Handling: Local wall-clock inputs are parsed cleanly via
localToUtc(). Nonexistent spring-forward local times are flagged (isNonexistent = true) with explicit error messaging. Ambiguous fall-back local times are flagged (isAmbiguous = true) to prevent silent time shift corruption. - Phase 2F Boundary Isolation: Pure temporal engine methods answer temporal state questions without triggering business state transitions (
completed_at,missed_at), keeping execution lifecycle strictly isolated for Phase 2F. Verified via unit test suite (tests/temporal-engine.test.ts).
For Phase 2F, Task execution lifecycle transitions were made fully server-authoritative and race-condition protected:
- Direct Status & Timestamp Mutation Block: Hardened database trigger
enforce_task_trusted_fields()blocks direct client REST/SQL attempts to mutatestatusto'completed'or'missed', as well as setting/updatingcompleted_atormissed_at. - Authoritative RPC Functions:
complete_task(p_task_id)andmark_task_missed(p_task_id)execute inside PostgreSQLSECURITY DEFINERfunctions withSET search_path = public. - Atomicity & Row Locking: Transactions execute
SELECT ... FOR UPDATEonpublic.tasksto lock the target row, preventing race conditions between concurrent requests. - Idempotency & State Invariants:
completedandmissedstates are terminal. Idempotent repeated calls preserve original authoritative transaction timestamps without overwriting data. - Adversarial Real Database Verification: Verified against real remote Supabase PostgreSQL database (
tests/adversarial-rls-audit.sqlSection 6, 29/29 checks passed).
For Phase 2I, Google OAuth was integrated into the existing server-authoritative Supabase Auth architecture:
- Supabase Auth Broker Security: Google OAuth flow uses Supabase Auth as the identity broker (
signInWithOAuth({ provider: 'google' })). The Google Client ID and Secret reside exclusively in the Supabase Dashboard. No Client Secret is present in frontend code, environment files, or repository assets. - Server-Side PKCE Code Exchange: The OAuth callback route (
/auth/callback/route.ts) receives the authorization code and executessupabase.auth.exchangeCodeForSession(code)server-side via@supabase/ssr, establishing verified HTTP-only session cookies. - Open Redirect Defense: All redirect target parameters (
next) pass throughvalidateSafeRedirect(). External protocols (http://,https://), protocol-relative URLs (//evil.com), and unauthorized internal routes are rejected, defaulting strictly to/app. - Database Identity & Profile Convergence: Authenticated identity is derived strictly from Supabase Auth (
auth.getUser()). First-time Google sign-ins triggerpublic.handle_new_user()in PostgreSQL, automatically initializingpublic.profileswith user metadata (full_name) without client privilege escalation. - Adversarial & Unit Verification: Verified via
tests/oauth-flow-validation.test.ts(10/10 open-redirect vectors blocked, provider contract verified).
For Phase 3 Milestone 4, the consequence resolution and waiver mechanics were built under the core principle: "PACT never assumes an accountability consequence was fulfilled."
- Anti-Forgery & State Integrity:
commitment_statustransitions to'fulfilled'or'waived'are strictly prohibited via direct clientUPDATE. The DB triggerprotect_accountability_commitment_immutability()blocks direct status manipulation.- Status changes occur exclusively through PostgreSQL
SECURITY DEFINERRPCs usingpact.internal_bypass = 'true'.
- Server-Authoritative Timed Sessions:
start_accountability_session(p_commitment_id)snapshots required duration and stamps server-authoritativestarted_at = transaction_timestamp().- Direct
INSERT,UPDATE, orDELETEonaccountability_verification_sessionsis blocked by DB triggerprotect_accountability_sessions_immutability(). fulfill_accountability_session(p_session_id, p_evidence_note)computes elapsed time strictly server-side (EXTRACT(EPOCH FROM now() - started_at)). Premature completion requests returnDURATION_NOT_MET.- Supporting activity evidence notes are required for timed sessions (max 5000 chars). Completed session evidence and timestamps are immutable.
- Atomic Weekly Waiver Quota:
- Users may waive an activated commitment up to a hard limit of 3 waivers per calendar week.
- Weekly quota is enforced atomically in
waive_accountability_commitment(p_commitment_id, p_confirmation_token)by acquiring a row-level lock (SELECT ... FROM public.profiles WHERE id = auth.uid() FOR UPDATE), preventing concurrent race conditions. - Week boundaries are calculated deterministically using the user's configured IANA timezone (
profiles.timezone). - The user-facing confirmation phrase is intentionally deferred; the server enforces validation of internal token
CONFIRM_WAIVER_V1. - Direct client
INSERT,UPDATE, orDELETEonaccountability_waiversis strictly blocked by triggerprotect_accountability_waivers_immutability().
- Append-Only Auditable History:
- Every fulfillment and waiver generates an append-only, immutable record in
public.accountability_events. - Cross-user SELECT, INSERT, UPDATE, and DELETE are blocked by RLS policies. Anonymous access is completely denied.
- Every fulfillment and waiver generates an append-only, immutable record in
- Adversarial Security Verification:
- Verified against the live remote Supabase PostgreSQL database across 84 real adversarial tests in
tests/adversarial-rls-audit.sql(27 new Milestone 4 checks covering fulfillment, timers, evidence, and weekly quotas).
- Verified against the live remote Supabase PostgreSQL database across 84 real adversarial tests in
For Phase 3 Milestone 5, the accountability resolution and consequence engine was finalized with exhaustive edge-case defenses and database-level hardening:
- Database-Level State Machine Invariant Enforcement:
- PostgreSQL trigger
trg_enforce_commitment_status_transitionsonpublic.task_accountability_commitmentsstrictly enforces the lifecycle progression:committed -> activated -> (fulfilled | waived). - Direct shortcuts (
committed -> fulfilled,committed -> waived) are rejected withP0001: Invalid commitment status transition: Commitment must be activated before being fulfilled or waived. - Modifications to terminal states (
fulfilled,waived) are strictly blocked withP0001: Invalid commitment status transition: Terminal commitment status cannot be modified.
- PostgreSQL trigger
- Snapshot Immutability Under Consequence Definition Lifecycle:
- Committed snapshots in
task_accountability_commitmentsare fully detached fromconsequence_definitions. - Deleting, archiving, or modifying reusable consequence definitions has zero impact on existing committed snapshots.
- Committed snapshots in
- Multi-Modal Server-Authoritative Verification Semantics:
timed_session: Duration enforced strictly server-side (now() - started_at >= required_duration_seconds), non-empty evidence notes required.written_reflection: Stored and validated viafulfill_written_reflection()with strict length constraints (20–5000 characters, whitespace trimmed).declaration: Handled viadeclare_accountability_fulfillment(), explicitly stampingis_self_declaration = trueandverified_objectively = falsewith non-empty statements (1–1000 characters), ensuring clear distinction from objective verification.task_completion: Handled viafulfill_task_completion_commitment(), requiring a real secondary PACT task owned by the authenticated user in terminalcompletedstatus. Circular self-reference (target_task_id = commit.task_id), incomplete tasks, and cross-user tasks are strictly rejected.custom: Structured, validated, and bounded; execution of arbitrary code, SQL, webhooks, or external network calls is strictly prohibited.
- Indefinite Consequence Persistence:
- Activated consequences remain in
activatedstatus indefinitely until explicitly fulfilled or waived; they never silently expire, decay, or self-forgive over time.
- Activated consequences remain in
- Defense-in-Depth Waiver Quota Trigger:
- PostgreSQL trigger
trg_enforce_weekly_waiver_quotaonpublic.accountability_waiversacts as a second-line defense behindwaive_accountability_commitment(), enforcing the 3-per-calendar-week limit in the user's IANA timezone at the table level.
- PostgreSQL trigger
- Concurrency & Race Condition Defenses:
- Concurrent resolution operations (e.g. simultaneous fulfill and waive) serialize via
SELECT ... FOR UPDATElocks on the target commitment and profile rows.
- Concurrent resolution operations (e.g. simultaneous fulfill and waive) serialize via
- Adversarial Real Database Verification:
- 103/103 adversarial security checks passed against the live remote Supabase PostgreSQL database (
tests/adversarial-rls-audit.sql), including 19 new checks (85–103) covering Milestone 5 state transitions, multi-modal fulfillment RPCs, circular task references, and terminal immutability.
- 103/103 adversarial security checks passed against the live remote Supabase PostgreSQL database (