@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / ba9d760
Fork 0

سامانه صدور فاکتور و حسابداری

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 4, 06:45 UTC

test(phase1): add production-safe read-only access catalog checks

davoodmehraban89 committed Oct 13, 2026 at 13:00 UTC ba9d7607f8717e4f910714369da3d9b566d8c28a
2 files changed +111
docs/FINORA_PHASE1_SECURITY_READONLY_AUDIT_2026-10-13.md new
+43
@@ -0,0 +1,43 @@
1 +# Finora Phase 1 — Live permission and SECURITY DEFINER read-only audit
2 +**Observed:** 2026-10-13 · **Scope:** independent, metadata-only Phase 1 slice while owner defers F0-01 startup slowness. **Status:** READ-ONLY METADATA VERIFIED; AUTHENTICATED NEGATIVE TESTS OPEN; PHASE 1 NOT ACCEPTED.
3 +
4 +## Authority and version
5 +- Owner explicitly prioritizes continuing Finora phases now; Phase 0 login slowness remains open and deferred, **not accepted**.
6 +- Canonical protected main at audit start: `1fa5cf99be9eb790d1192b7597d2dbf65cc9d3fa`. GitHub same SHA Quality `38357143810`, Security `38357143834`, Release `38357450781`, gated Pages `38357495885` SUCCESS. Exact production user-login stability is unresolved and separate.
7 +- Production Supabase project `hcsixhqbyuhpshfwqpjx` was **ACTIVE_HEALTHY**, PostgreSQL 17.6.1.166, **82 migration ledger records**, last `20261012075507 fix_identity_bootstrap_license_before_membership`. This audit does not authorize schema changes or infer migration SQL equivalence.
8 +
9 +## Directly verified live catalog metadata
10 +| Control | Observed | Interpretation |
11 +|---|---|---|
12 +| Public tables with RLS enabled | **18/18** | All inspected public base tables use RLS; no assurance policy bodies cannot be bypassed |
13 +| Private tables with RLS enabled | **3/10** | Seven private tables do not enable RLS; needs threat-model/privilege review |
14 +| Direct SELECT on private tables by `anon` or `authenticated` | **0/10** | No direct table SELECT grants found for either role, regardless of RLS |
15 +| Private schema USAGE | `anon=false`, `authenticated=true` | Do not equate table-grant absence with all possible RPC/function paths being inaccessible |
16 +| Public SECURITY DEFINER functions | **27** total; **21** executable by `authenticated`, **0** by `anon` | Matches the 21 authenticated-callable function advisor warnings; these are review candidates, not 21 proven exploits |
17 +| Private SECURITY DEFINER functions | **66** total; **43** executable by `authenticated`, **0** by `anon` | Private helper execution policy and schemas require separate API exposure/capability review |
18 +| Explicit function search_path | **93/93** public+private SECURITY DEFINER | Fixed search_path is present, but not a sufficient authorization proof |
19 +
20 +This is a snapshot of catalog metadata from live queries, **not** an exhaustive adversarial user simulation. Count of `SECURITY DEFINER` includes trigger/helpers; only public `authenticated`-callable functions are the 21 directly reported API-exposure warnings.
21 +
22 +## Source-level sample (not an authorization penetration test)
23 +- Six `public.admin_*license*`/user-capacity mutators inspected via `pg_get_functiondef` enforce `public.is_admin()` and validate inputs. The checker itself relies on `auth.uid()` and `public.profiles.role='admin'`. A compromised global admin role or missing tenant constraints elsewhere is **not** ruled out by source review.
24 +- Sampled `public.organization_create_invitation`, `organization_accept_invitation` and `office_refer_correspondence_v2` include `auth.uid()`, organizational ownership/capability checks and membership checks. Real two-identity negative tests remain mandatory.
25 +- Supabase security advisor flags `rls_enabled_no_policy` for three private office-workflow tables and `auth_leaked_password_protection` as a separate warning. Warnings are actionable review inputs; enabling protection or changing RLS/Auth configuration requires explicit owner approval.
26 +
27 +## Executable repeatable read-only check
28 +File: `supabase/tests/phase1_permissions_readonly.sql`. It reads only `pg_catalog`, `has_table_privilege`, and `has_function_privilege`. Status `PASS` means the listed *metadata contract* holds, `FAIL` means drift/violation needing investigation, and `REVIEW` is intentionally not a failure or a vulnerability claim. It does not query real tenant tables, authenticate users, or mutate data.
29 +
30 +Expected initial run: `PUBLIC_ALL_RLS PASS (18/18)`; `PRIVATE_NO_CLIENT_DIRECT_READ PASS (0 of 10)`; `PUBLIC_DEFINER_NO_ANON_EXEC PASS (0 of 27)`; `DEFINER_EXPLICIT_SEARCH_PATH PASS (93/93)`; `PUBLIC_AUTHENTICATED_DEFINER_REVIEW REVIEW (21)`; `PRIVATE_RLS_COVERAGE_REVIEW REVIEW (3/10)`. Counts reflect this deployment and should not be hard-coded as invariants.
31 +
32 +## Production migration archive discrepancy, held for review
33 +- Live migration `20261007165208 phone_identity_profile` is recorded in Supabase.
34 +- Current `main` has **no** archived file for that operation; stale [PR #58](https://github.com/davoodmehraban89/Factor-easy/pull/58) includes `supabase/migrations/20261007163500_phone_identity_profile.sql`, a *different migration timestamp*, bundled with historical admin/frontend/Auth changes and presently unmerged. **No proof of SQL-body parity**, and **no authority** to rename, reapply or merge PR #58 unreviewed.
35 +- Required next audit: recover verified executed SQL text/metadata from appropriate production migration source and compare exact semantics/recorded ledger version to PR archive offline. Do not execute historical DDL against production.
36 +
37 +## Acceptance boundaries / next safely executable slices
38 +1. Independently check all 21 public authenticated-callable SECURITY DEFINER functions for authorization, organization ID spoofing, parameter validation and search_path; classify each with evidence.
39 +2. Build isolated two-user read-only/negative tests for tenant separation, offboarding/revocation and access to invitation/office/request functions; **requires separately authorized durable test users**. Never use live production financial rows for disposable tests.
40 +3. Verify recovery/backup in isolated approved scope and reconciliation of 82 live migrations against repository archives. Password-leak protection, Auth/RLS/role/schema changes and privileged probes require separate approval.
41 +4. Continue independent Phase 2 accounting invariant review after approved Phase 1 contracts; do not authorize historical cutover or financial writes yet.
42 +
43 +**No runtime change, database migration, Auth/RLS/grant change, new users, or financial record mutation in this slice.** Rollback of deliverables is a docs/test-file-only revert. F0-01, Phase 0 and overall Phase 1 acceptance remain OPEN.
supabase/tests/phase1_permissions_readonly.sql new
+68
@@ -0,0 +1,68 @@
1 +-- Finora Phase 1 production-catalog access audit (READ-ONLY).
2 +-- SELECT metadata only. No customer rows, JWTs, passwords, mutation or auth bypass.
3 +-- Run with the authorized Supabase read-only SQL interface; do NOT run service changes.
4 +WITH
5 +pub AS (
6 + SELECT c.oid,c.relrowsecurity
7 + FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace
8 + WHERE n.nspname='public' AND c.relkind IN ('r','p')
9 +),
10 +prv AS (
11 + SELECT c.oid,c.relrowsecurity
12 + FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace
13 + WHERE n.nspname='private' AND c.relkind IN ('r','p')
14 +),
15 +def AS (
16 + SELECT p.oid,n.nspname AS schema_name,coalesce(array_to_string(p.proconfig,','),'') AS config
17 + FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace
18 + WHERE p.prosecdef AND n.nspname IN ('public','private')
19 +),
20 +checks AS (
21 + SELECT 10 AS sort_order,'PUBLIC_ALL_RLS' AS check_id,
22 + CASE WHEN COUNT(*)>0 AND COUNT(*)=COUNT(*) FILTER (WHERE relrowsecurity)
23 + THEN 'PASS' ELSE 'FAIL' END AS status,
24 + (COUNT(*) FILTER (WHERE relrowsecurity))::text||'/'||COUNT(*)::text AS observed,
25 + 'Every public table has RLS enabled' AS requirement
26 + FROM pub
27 + UNION ALL
28 + SELECT 20,'PRIVATE_NO_CLIENT_DIRECT_READ',
29 + CASE WHEN COUNT(*)>0 AND COUNT(*) FILTER (WHERE
30 + pg_catalog.has_table_privilege('anon',oid,'SELECT')
31 + OR pg_catalog.has_table_privilege('authenticated',oid,'SELECT'))=0
32 + THEN 'PASS' ELSE 'FAIL' END,
33 + (COUNT(*) FILTER (WHERE
34 + pg_catalog.has_table_privilege('anon',oid,'SELECT')
35 + OR pg_catalog.has_table_privilege('authenticated',oid,'SELECT')))::text||' of '||COUNT(*)::text||' have direct client SELECT',
36 + 'No anon/authenticated direct SELECT grant on private tables'
37 + FROM prv
38 + UNION ALL
39 + SELECT 30,'PUBLIC_DEFINER_NO_ANON_EXEC',
40 + CASE WHEN COUNT(*)>0 AND COUNT(*) FILTER
41 + (WHERE pg_catalog.has_function_privilege('anon',oid,'EXECUTE'))=0
42 + THEN 'PASS' ELSE 'FAIL' END,
43 + (COUNT(*) FILTER (WHERE pg_catalog.has_function_privilege('anon',oid,'EXECUTE')))::text||' of '||COUNT(*)::text||' callable by anon',
44 + 'No exposed-schema SECURITY DEFINER functions callable by anon'
45 + FROM def WHERE schema_name='public'
46 + UNION ALL
47 + SELECT 40,'DEFINER_EXPLICIT_SEARCH_PATH',
48 + CASE WHEN COUNT(*)>0 AND COUNT(*)=COUNT(*) FILTER (WHERE config LIKE '%search_path=%')
49 + THEN 'PASS' ELSE 'FAIL' END,
50 + (COUNT(*) FILTER (WHERE config LIKE '%search_path=%'))::text||'/'||COUNT(*)::text AS observed,
51 + 'All inspected SECURITY DEFINER routines pin search_path'
52 + FROM def
53 + UNION ALL
54 + SELECT 50,'PUBLIC_AUTHENTICATED_DEFINER_REVIEW',
55 + 'REVIEW',
56 + (COUNT(*) FILTER (WHERE pg_catalog.has_function_privilege('authenticated',oid,'EXECUTE')))::text||
57 + ' publicly exposed SECURITY DEFINER functions callable by authenticated',
58 + 'Require per-function authorization/source review; not automatically a flaw'
59 + FROM def WHERE schema_name='public'
60 + UNION ALL
61 + SELECT 60,'PRIVATE_RLS_COVERAGE_REVIEW',
62 + 'REVIEW',
63 + (COUNT(*) FILTER (WHERE relrowsecurity))::text||'/'||COUNT(*)::text AS observed,
64 + 'Private tables without RLS need separate schema/grant/RPC-path analysis'
65 + FROM prv
66 +)
67 +SELECT check_id,status,observed,requirement
68 +FROM checks ORDER BY sort_order;