Treasury & Bank Transfer Management System — سامانه مدیریت خزانهداری و حوالههای بانکی
fix: harden tenant isolation in production schema
Davood Mehraban committed
Oct 5, 2026 at 20:15 UTC
b79cc715bf7f07b9482fb7b32e5b8842aff58e33
3 files changed
+24
-18
migrations/001_production_core.sql
+21
-15
@@ -1,18 +1,24 @@
1
BEGIN;
2
CREATE TABLE tenants (id text PRIMARY KEY, name text NOT NULL, created_at timestamptz NOT NULL DEFAULT now());
3
-CREATE TABLE users (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), role text NOT NULL CHECK(role IN ('MAKER','CHECKER','TREASURY_ADMIN','AUDITOR')), active boolean NOT NULL DEFAULT true);
4
-CREATE TABLE bank_accounts (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), iban text NOT NULL, name text NOT NULL, UNIQUE(tenant_id, iban));
5
-CREATE TABLE beneficiaries (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), iban text NOT NULL, name text NOT NULL, whitelisted boolean NOT NULL DEFAULT false, UNIQUE(tenant_id, iban));
6
-CREATE TABLE payments (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), maker_id text NOT NULL REFERENCES users(id), beneficiary_id text NOT NULL REFERENCES beneficiaries(id), source_account_id text NOT NULL REFERENCES bank_accounts(id), amount_irr bigint NOT NULL CHECK(amount_irr>=0), payment_date date NOT NULL, client_reference text NOT NULL, idempotency_key text NOT NULL, state text NOT NULL, version integer NOT NULL DEFAULT 1, UNIQUE (tenant_id, idempotency_key));
7
-CREATE TABLE approvals (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), payment_id text NOT NULL REFERENCES payments(id), checker_id text NOT NULL REFERENCES users(id), approved_version integer NOT NULL, created_at timestamptz NOT NULL DEFAULT now());
8
-CREATE TABLE bank_statements (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), bank_account_id text NOT NULL REFERENCES bank_accounts(id), source_record_id text NOT NULL, amount_irr bigint NOT NULL, occurred_at timestamptz NOT NULL, reference text, UNIQUE(tenant_id,bank_account_id,source_record_id));
9
-CREATE TABLE audit_events (sequence bigserial PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), actor_id text, action text NOT NULL, entity_type text NOT NULL, entity_id text NOT NULL, payload jsonb NOT NULL DEFAULT '{}'::jsonb, created_at timestamptz NOT NULL DEFAULT now());
10
-ALTER TABLE users ENABLE ROW LEVEL SECURITY; ALTER TABLE bank_accounts ENABLE ROW LEVEL SECURITY; ALTER TABLE beneficiaries ENABLE ROW LEVEL SECURITY; ALTER TABLE payments ENABLE ROW LEVEL SECURITY; ALTER TABLE approvals ENABLE ROW LEVEL SECURITY; ALTER TABLE bank_statements ENABLE ROW LEVEL SECURITY; ALTER TABLE audit_events ENABLE ROW LEVEL SECURITY;
11
-CREATE POLICY users_tenant ON users USING (tenant_id = current_setting('app.tenant_id', true));
12
-CREATE POLICY bank_accounts_tenant ON bank_accounts USING (tenant_id = current_setting('app.tenant_id', true));
13
-CREATE POLICY beneficiaries_tenant ON beneficiaries USING (tenant_id = current_setting('app.tenant_id', true));
14
-CREATE POLICY payments_tenant ON payments USING (tenant_id = current_setting('app.tenant_id', true));
15
-CREATE POLICY approvals_tenant ON approvals USING (tenant_id = current_setting('app.tenant_id', true));
16
-CREATE POLICY bank_statements_tenant ON bank_statements USING (tenant_id = current_setting('app.tenant_id', true));
17
-CREATE POLICY audit_events_tenant ON audit_events USING (tenant_id = current_setting('app.tenant_id', true));
3
+CREATE TABLE users (id text NOT NULL, tenant_id text NOT NULL REFERENCES tenants(id), role text NOT NULL CHECK(role IN ('MAKER','CHECKER','TREASURY_ADMIN','AUDITOR')), active boolean NOT NULL DEFAULT true, PRIMARY KEY(id), UNIQUE(tenant_id,id));
4
+CREATE TABLE bank_accounts (id text NOT NULL, tenant_id text NOT NULL REFERENCES tenants(id), iban text NOT NULL, name text NOT NULL, PRIMARY KEY(id), UNIQUE(tenant_id,id), UNIQUE(tenant_id,iban));
5
+CREATE TABLE beneficiaries (id text NOT NULL, tenant_id text NOT NULL REFERENCES tenants(id), iban text NOT NULL, name text NOT NULL, whitelisted boolean NOT NULL DEFAULT false, PRIMARY KEY(id), UNIQUE(tenant_id,id), UNIQUE(tenant_id,iban));
6
+CREATE TABLE payments (id text NOT NULL, tenant_id text NOT NULL REFERENCES tenants(id), maker_id text NOT NULL, beneficiary_id text NOT NULL, source_account_id text NOT NULL, amount_irr bigint NOT NULL CHECK(amount_irr>=0), payment_date date NOT NULL, client_reference text NOT NULL, idempotency_key text NOT NULL, state text NOT NULL, version integer NOT NULL DEFAULT 1 CHECK(version>0), PRIMARY KEY(id), UNIQUE(tenant_id,id), UNIQUE (tenant_id, idempotency_key), FOREIGN KEY (tenant_id, maker_id) REFERENCES users(tenant_id,id), FOREIGN KEY (tenant_id, beneficiary_id) REFERENCES beneficiaries(tenant_id,id), FOREIGN KEY (tenant_id, source_account_id) REFERENCES bank_accounts(tenant_id,id));
7
+CREATE TABLE approvals (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), payment_id text NOT NULL, checker_id text NOT NULL, approved_version integer NOT NULL CHECK(approved_version>0), created_at timestamptz NOT NULL DEFAULT now(), FOREIGN KEY(tenant_id,payment_id) REFERENCES payments(tenant_id,id), FOREIGN KEY(tenant_id,checker_id) REFERENCES users(tenant_id,id));
8
+CREATE TABLE bank_statements (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), bank_account_id text NOT NULL, source_record_id text NOT NULL, amount_irr bigint NOT NULL, occurred_at timestamptz NOT NULL, reference text, UNIQUE(tenant_id,bank_account_id,source_record_id), FOREIGN KEY(tenant_id,bank_account_id) REFERENCES bank_accounts(tenant_id,id));
9
+CREATE TABLE audit_events (sequence bigserial PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), actor_id text, action text NOT NULL, entity_type text NOT NULL, entity_id text NOT NULL, payload jsonb NOT NULL DEFAULT '{}'::jsonb, created_at timestamptz NOT NULL DEFAULT now(), FOREIGN KEY(tenant_id,actor_id) REFERENCES users(tenant_id,id));
10
+ALTER TABLE users ENABLE ROW LEVEL SECURITY; ALTER TABLE users FORCE ROW LEVEL SECURITY;
11
+ALTER TABLE bank_accounts ENABLE ROW LEVEL SECURITY; ALTER TABLE bank_accounts FORCE ROW LEVEL SECURITY;
12
+ALTER TABLE beneficiaries ENABLE ROW LEVEL SECURITY; ALTER TABLE beneficiaries FORCE ROW LEVEL SECURITY;
13
+ALTER TABLE payments ENABLE ROW LEVEL SECURITY; ALTER TABLE payments FORCE ROW LEVEL SECURITY;
14
+ALTER TABLE approvals ENABLE ROW LEVEL SECURITY; ALTER TABLE approvals FORCE ROW LEVEL SECURITY;
15
+ALTER TABLE bank_statements ENABLE ROW LEVEL SECURITY; ALTER TABLE bank_statements FORCE ROW LEVEL SECURITY;
16
+ALTER TABLE audit_events ENABLE ROW LEVEL SECURITY; ALTER TABLE audit_events FORCE ROW LEVEL SECURITY;
17
+CREATE POLICY users_tenant ON users USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
18
+CREATE POLICY bank_accounts_tenant ON bank_accounts USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
19
+CREATE POLICY beneficiaries_tenant ON beneficiaries USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
20
+CREATE POLICY payments_tenant ON payments USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
21
+CREATE POLICY approvals_tenant ON approvals USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
22
+CREATE POLICY bank_statements_tenant ON bank_statements USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
23
+CREATE POLICY audit_events_tenant ON audit_events USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
24
COMMIT;
src/platform/persistence/schema-contract.test.ts
+2
-2
@@ -1,3 +1,3 @@
1
import {describe,expect,it} from 'vitest'; import {verifyProductionSchema} from './schema-contract';
2
-const schema=`CREATE TABLE tenants; CREATE TABLE users; CREATE TABLE bank_accounts; CREATE TABLE beneficiaries; CREATE TABLE payments; CREATE TABLE approvals; CREATE TABLE bank_statements; CREATE TABLE audit_events; ALTER TABLE payments ENABLE ROW LEVEL SECURITY; CREATE POLICY payments_tenant ON payments; UNIQUE (tenant_id, idempotency_key);`;
3
-describe('production schema contract',()=>{it('requires core tables, payment RLS and tenant idempotency uniqueness',()=>expect(verifyProductionSchema(schema)).toEqual([]));it('reports unsafe schema',()=>expect(verifyProductionSchema('CREATE TABLE payments;')).toContain('payments-rls'))});
2
+const schema=`CREATE TABLE tenants; CREATE TABLE users; CREATE TABLE bank_accounts; CREATE TABLE beneficiaries; CREATE TABLE payments; CREATE TABLE approvals; CREATE TABLE bank_statements; CREATE TABLE audit_events; ALTER TABLE payments ENABLE ROW LEVEL SECURITY; ALTER TABLE payments FORCE ROW LEVEL SECURITY; CREATE POLICY payments_tenant ON payments USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true)); UNIQUE (tenant_id, idempotency_key); FOREIGN KEY (tenant_id, maker_id) REFERENCES users(tenant_id,id);`;
3
+describe('production schema contract',()=>{it('requires core tables, forced payment RLS, write policy, idempotency and tenant-bound FK',()=>expect(verifyProductionSchema(schema)).toEqual([]));it('reports unsafe schema',()=>{const e=verifyProductionSchema('CREATE TABLE payments;');expect(e).toContain('payments-rls');expect(e).toContain('payments-force-rls');expect(e).toContain('payments-with-check');expect(e).toContain('tenant-bound-maker-fk')})});
src/platform/persistence/schema-contract.ts
+1
-1
@@ -1,2 +1,2 @@
1
const required=['tenants','users','bank_accounts','beneficiaries','payments','approvals','bank_statements','audit_events'];
2
-export function verifyProductionSchema(sql:string){const s=sql.toLowerCase().replace(/\s+/g,' ');const errors:string[]=[];for(const table of required)if(!s.includes(`table ${table}`))errors.push(`table-${table}`);if(!s.includes('alter table payments enable row level security'))errors.push('payments-rls');if(!s.includes('policy payments_tenant'))errors.push('payments-tenant-policy');if(!/unique\s*\(\s*tenant_id\s*,\s*idempotency_key\s*\)/.test(s))errors.push('tenant-idempotency-unique');return errors}
2
+export function verifyProductionSchema(sql:string){const s=sql.toLowerCase().replace(/\s+/g,' ');const errors:string[]=[];for(const table of required)if(!s.includes(`table ${table}`))errors.push(`table-${table}`);if(!s.includes('alter table payments enable row level security'))errors.push('payments-rls');if(!s.includes('alter table payments force row level security'))errors.push('payments-force-rls');if(!s.includes('policy payments_tenant'))errors.push('payments-tenant-policy');if(!/policy payments_tenant[\s\S]*with check\s*\(\s*tenant_id\s*=\s*current_setting/.test(s))errors.push('payments-with-check');if(!/unique\s*\(\s*tenant_id\s*,\s*idempotency_key\s*\)/.test(s))errors.push('tenant-idempotency-unique');if(!/foreign key\s*\(\s*tenant_id\s*,\s*maker_id\s*\)\s*references users\s*\(\s*tenant_id\s*,\s*id\s*\)/.test(s))errors.push('tenant-bound-maker-fk');return errors}