سامانه صدور فاکتور و حسابداری
test: add red-first RLS-authenticated adversarial voucher and sync scenarios
davoodmehraban89 committed
Oct 16, 2026 at 13:13 UTC
5c09d60713aa74fa964d70cd8b7208ff508e0204
1 file changed
+52
scripts/phase1a-adversarial-red.mjs
new
+52
@@ -0,0 +1,52 @@
1
+#!/usr/bin/env node
2
+// Isolated Supabase CLI red-first adversarial tests. NEVER point at Production.
3
+// Uses synthetic auth.users and SET ROLE authenticated, real RLS and real RPC.
4
+// Each test runs inside a transaction which is always rolled back on disconnect.
5
+import {spawnSync} from 'node:child_process';
6
+const USER_A='11111111-1111-4111-8111-111111111111';
7
+const USER_B='22222222-2222-4222-8222-222222222222';
8
+const ORG_A="(select organization_id from public.organization_members where user_id='"+USER_A+"' limit 1)";
9
+const ORG_B="(select organization_id from public.organization_members where user_id='"+USER_B+"' limit 1)";
10
+const seed=[
11
+ "begin;",
12
+ "insert into auth.users (id,instance_id,aud,role,email,email_confirmed_at,raw_user_meta_data) values",
13
+ " ('"+USER_A+"','00000000-0000-0000-0000-000000000000','authenticated','authenticated','phase1a-a@example.invalid',now(),'{}'::jsonb),",
14
+ " ('"+USER_B+"','00000000-0000-0000-0000-000000000000','authenticated','authenticated','phase1a-b@example.invalid',now(),'{}'::jsonb);",
15
+].join('\n');
16
+function rec(org,uid,coll,id,data){
17
+ return "insert into public.records(organization_id,owner_id,collection,id,data) values ("+org+",'"+uid+"','"+coll+"','"+id+"','"+JSON.stringify(data).replace(/'/g,"''")+"'::jsonb);";
18
+}
19
+const draft = {companyId:'COMP1',fiscalYearId:'FY1',date:'1405/07/24',number:'50',status:'draft',totalDebit:100,totalCredit:0};
20
+const posted = {...draft,status:'posted',number:'51',totalDebit:100,totalCredit:100,postedAt:'2026-10-16T09:00:00Z'};
21
+const jl=(id,voucherId,debit,credit)=>rec(ORG_A,USER_A,'journalLines',id,{voucherId,companyId:'COMP1',accountId:'ACC1',debit,credit});
22
+const tests=[
23
+ {id:'draft-to-posted-unbalanced', setup:[rec(ORG_A,USER_A,'journalVouchers','V001',draft),jl('L001','V001',100,0)], act:"update public.records set data=jsonb_set(data,'{status}','\"posted\"'::jsonb) where collection='journalVouchers' and id='V001';", policy:'block'},
24
+ {id:'direct-insert-posted',act:rec(ORG_A,USER_A,'journalVouchers','V002',posted),policy:'block'},
25
+ {id:'sync-direct-status-number-postedAt',setup:[rec(ORG_A,USER_A,'journalVouchers','V003',draft)],act:"select public.finora_sync_records(jsonb_build_array(jsonb_build_object('collection','journalVouchers','id','V003','data',jsonb_build_object('status','posted','number','999','postedAt','2026-10-16T09:00:00Z','companyId','COMP1','fiscalYearId','FY1','date','1405/07/24'))),'[]'::jsonb,"+ORG_A+");",policy:'block'},
26
+ {id:'direct-insert-line-under-posted',setup:[rec(ORG_A,USER_A,'journalVouchers','V004',posted),jl('L004a','V004',100,0),jl('L004b','V004',0,100)],act:jl('L004c','V004',1,0),policy:'block'},
27
+ {id:'direct-insert-dimension-under-posted',setup:[rec(ORG_A,USER_A,'journalVouchers','V005',posted),jl('L005a','V005',100,0),jl('L005b','V005',0,100)],act:rec(ORG_A,USER_A,'journalLineDimensions','LD005',{voucherId:'V005',journalLineId:'L005a',dimensionTypeId:'DT1',dimensionValueId:'DV1'}),policy:'block'},
28
+ {id:'cross-org-voucher-id',setup:[rec(ORG_B,USER_B,'journalVouchers','CROSS01',posted)],act:jl('CROSSJL','CROSS01',5,0),policy:'block'},
29
+ {id:'negative-rials',act:jl('BADNEG','NOTPOSTED',-1,0),policy:'block'},
30
+ {id:'fractional-rials',act:jl('BADDEC','NOTPOSTED',1.5,0),policy:'block'},
31
+ {id:'string-rials',act:jl('BADSTR','NOTPOSTED','100',0),policy:'block'},
32
+ {id:'null-rials',act:jl('BADNULL','NOTPOSTED',null,0),policy:'block'},
33
+ {id:'legacy-two-arg-sync',act:"select public.finora_sync_records('[{\"collection\":\"settings\",\"id\":\"main\",\"data\":{\"phase1aSynthetic\":true}}]'::jsonb,'[]'::jsonb);",policy:'allow'}
34
+];
35
+let fail=0,blocked=0,allowed=0,infra=0;
36
+for(const x of tests){
37
+ const start=[seed,...(x.setup||[])];
38
+ const sql=[...start,"set local role authenticated;","select set_config('request.jwt.claim.sub','"+USER_A+"',true);",x.act,"rollback;"].join('\n')+'\n';
39
+ const r=spawnSync('psql',['-X','-qAt','-v','ON_ERROR_STOP=1','-h','127.0.0.1','-p','54322','-U','postgres','-d','postgres'],{input:sql,env:{...process.env,PGPASSWORD:'postgres'},encoding:'utf8',timeout:25000});
40
+ const denied=r.status!==0, diagnostic=(r.stderr||'').split('\n').filter(Boolean).slice(-3).join(' ').slice(0,180);
41
+ // After the fix, financial prohibitions must report a specific FINORA_ validation error.
42
+ const targeted=/FINORA_/i.test(r.stderr||'');
43
+ let status;
44
+ if(x.policy==='block' && denied && targeted){status='PASS guarded';blocked++}
45
+ else if(x.policy==='allow'&&!denied){status='PASS compatible';allowed++}
46
+ else if(x.policy==='block'&&!denied){status='RED write accepted';fail++}
47
+ else if(x.policy==='allow'&&denied){status='RED legacy compatibility broken';fail++}
48
+ else{status='INFRA/OTHER ERROR (not an acceptable guard)';infra++;fail++}
49
+ console.log(JSON.stringify({case:x.id,result:status,exit:r.status,diagnostic}));
50
+}
51
+console.log(JSON.stringify({total:tests.length,blocked,allowed,fail,infra}));
52
+if(fail)process.exitCode=1;