@davoodmehraban89 / Factor-easy / commits / 42a044f
Fork 0

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

fix(finance): attribute scope-move guard audit to source tenant

davoodmehraban89 committed Nov 15, 2026 at 23:25 UTC 42a044f7b53a53fa7b8a3a82fa3c5d87e0004da0
4 files changed +120 -2
PROJECT_STATE.md
+5 -1
@@ -340,4 +340,8 @@ Owner requests layer-by-layer click/functional testing of all controls and compa
340 ### 2026-11-15 — Scope-move audit attribution
341 - On commit `46892da0e08699e3bdead693f5e62c10f4ed39d5`, WP1 passed baseline RED, all preceding green fixtures including the 1,000-event test and `wp1_reversal_dimensions.sql`.
342 - `wp1_scope_move_and_null.sql` failed only at its final audit-to-off relocation audit-row assertion. The fixture expected the audit event under destination organization B, but the acting user belongs to source organization A. The tenant-scoped audit writer correctly denied cross-tenant audit insertion.
343 -- Corrected the fixture to require the audit event in source organization A with source actor and voucher ID. No guard behavior or negative assertion was weakened; rerun the full workflow.
\ No newline at end of file
343 +- Corrected the fixture to require the audit event in source organization A with source actor and voucher ID. No guard behavior or negative assertion was weakened; rerun the full workflow.
344 +
345 +### 2026-11-15 — Source tenant audit attribution fix
346 +- Latest WP1 run on `7b0d058e4eb8f3a791a68c2a82e043a0067ff4a8` passed reversal dimensions, then failed in `wp1_scope_move_and_null.sql` at the final audit-to-off move. This is a real function issue, not a fixture issue: `guard_server_journal_fields` passed NEW organization to `log_guard`, while the actor is authorized in OLD/source organization.
347 +- Added candidate migration `20261115150000_wp1_scope_move_audit_source_tenant.sql` to attribute identity-move guard audit to OLD organization; added conservative DOWN. No production deployment. Exact-head WP1 UP/DOWN/UP and full security/quality CI pending.
\ No newline at end of file
docs/audits/WP1_DB_ONLY_INTEGRATION_CANDIDATE_2026-11-15_FA.md
+5 -1
@@ -74,4 +74,8 @@ This was a fixture-actor error, not evidence that the reversal RPC is broken. Th
74
75 After correcting the foreign-tenant reversal fixture, `wp1_reversal_dimensions.sql` passed in full. The next fixture `wp1_scope_move_and_null.sql` then reached its final audit-to-off relocation scenario and failed because the test expected the guard event in the destination organization. The source migration invokes the guard logger with the NEW organization on UPDATE, but the actor remains a member of the source organization; the tenant-scoped audit writer correctly refuses to let that actor write an audit row into the destination tenant.
76
77 -The event is evidence of an attempted mutation of the source tenant’s posted journal identity. The test assertion is corrected to require the event in the source organization, with the source actor and voucher ID. The relocation behavior and all negative assertions remain unchanged. This is a test expectation correction based on the intended audit ownership, not a production-function change. Full exact-head CI is still required.
\ No newline at end of file
77 +The event is evidence of an attempted mutation of the source tenant’s posted journal identity. The test assertion is corrected to require the event in the source organization, with the source actor and voucher ID. The relocation behavior and all negative assertions remain unchanged. This is a test expectation correction based on the intended audit ownership, not a production-function change. Full exact-head CI is still required.
78 +
79 +## Source-tenant audit attribution correction — 2026-11-15
80 +
81 +The exact-head WP1 rerun confirmed `wp1_reversal_dimensions.sql` passes and exposed a real guard-function issue in `wp1_scope_move_and_null.sql`: when a posted voucher is moved between organizations while the source is audit and destination is off, `finora_phase1a_guard_server_journal_fields` derives the logger organization from NEW row scope. It then tries to write the guard event into the destination tenant, where the source actor is not a member; the tenant-scoped logger correctly rejects this. Added migration `20261115150000_wp1_scope_move_audit_source_tenant.sql` to attribute identity-move guard events to OLD/source organization, plus a conservative DOWN restoring the prior function. Existing fixture expects source tenant, source actor and voucher ID. Exact-head CI must validate UP/DOWN and all regressions before any merge.
\ No newline at end of file
supabase/migrations/20261115150000_wp1_scope_move_audit_source_tenant.sql new
+59
@@ -0,0 +1,59 @@
1 +-- Attribute posted-journal identity-move guard events to the source tenant.
2 +-- The actor is authorized in the source tenant; writing the event to NEW tenant
3 +-- would be a cross-tenant audit write and must not be attempted.
4 +CREATE OR REPLACE FUNCTION private.finora_phase1a_guard_server_journal_fields()
5 +RETURNS trigger LANGUAGE plpgsql SECURITY INVOKER SET search_path=''
6 +AS $fn$
7 +DECLARE
8 + v_finora_mode text;
9 + v_finora_org uuid;
10 + v_audit_org uuid;
11 + v_finora_msg text;
12 + v_finora_state text;
13 + v_is_server boolean := current_user IN ('postgres','supabase_admin','service_role');
14 + v_new_status text;
15 +BEGIN
16 + v_finora_org:=CASE WHEN TG_OP='DELETE' THEN old.organization_id ELSE new.organization_id END;
17 + v_finora_mode:=private.finora_phase1a_mode(v_finora_org);
18 + IF TG_OP='UPDATE' AND private.finora_phase1a_mode(old.organization_id)='enforce' THEN v_finora_mode:='enforce'; END IF;
19 + IF TG_OP='UPDATE' AND v_finora_mode='off' AND private.finora_phase1a_mode(old.organization_id)='audit' THEN v_finora_mode:='audit'; END IF;
20 + IF TG_OP='UPDATE' AND old.collection='journalVouchers' AND old.data->>'status' IN ('posted','reversed')
21 + AND (old.organization_id,old.owner_id,old.collection,old.id) IS DISTINCT FROM (new.organization_id,new.owner_id,new.collection,new.id)
22 + AND private.finora_phase1a_mode(old.organization_id)<>'off' THEN
23 + RAISE EXCEPTION 'FINORA_POSTED_JOURNAL_IDENTITY_IMMUTABLE' USING ERRCODE='23514'; END IF;
24 + IF v_finora_mode='off' THEN RETURN new; END IF;
25 + IF TG_OP NOT IN ('INSERT','UPDATE') OR new.collection<>'journalVouchers' THEN RETURN new; END IF;
26 + v_new_status:=new.data->>'status';
27 + IF NOT v_is_server THEN
28 + IF TG_OP='INSERT' THEN
29 + IF new.data ?| ARRAY['status','number','postedAt','postedBy','reversedAt','reversalVoucherId'] THEN
30 + RAISE EXCEPTION 'FINORA_SERVER_ONLY_JOURNAL_FIELDS' USING ERRCODE='P0001';
31 + END IF;
32 + ELSIF (new.data->'status') IS DISTINCT FROM (old.data->'status')
33 + OR (new.data->'number') IS DISTINCT FROM (old.data->'number')
34 + OR (new.data->'postedAt') IS DISTINCT FROM (old.data->'postedAt')
35 + OR (new.data->'postedBy') IS DISTINCT FROM (old.data->'postedBy')
36 + OR (new.data->'reversedAt') IS DISTINCT FROM (old.data->'reversedAt')
37 + OR (new.data->'reversalVoucherId') IS DISTINCT FROM (old.data->'reversalVoucherId') THEN
38 + RAISE EXCEPTION 'FINORA_SERVER_ONLY_JOURNAL_FIELDS' USING ERRCODE='P0001';
39 + END IF;
40 + END IF;
41 + IF v_new_status IS NOT NULL AND v_new_status NOT IN ('draft','posted','reversed') THEN
42 + RAISE EXCEPTION 'FINORA_INVALID_JOURNAL_STATUS' USING ERRCODE='23514';
43 + END IF;
44 + RETURN new;
45 +EXCEPTION WHEN OTHERS THEN
46 + IF v_finora_mode='audit' THEN
47 + GET STACKED DIAGNOSTICS v_finora_msg=MESSAGE_TEXT,v_finora_state=RETURNED_SQLSTATE;
48 + v_audit_org:=CASE
49 + WHEN TG_OP='UPDATE' AND old.collection='journalVouchers'
50 + AND (old.organization_id,old.owner_id,old.collection,old.id)
51 + IS DISTINCT FROM (new.organization_id,new.owner_id,new.collection,new.id)
52 + THEN old.organization_id ELSE v_finora_org END;
53 + PERFORM private.finora_phase1a_log_guard(v_audit_org,
54 + CASE WHEN TG_OP='DELETE' THEN old.id ELSE new.id END,v_finora_msg,v_finora_state);
55 + RETURN new;
56 + END IF;
57 + RAISE;
58 +END
59 +$fn$;
supabase/rollback/20261115150000_wp1_scope_move_audit_source_tenant.sql new
+51
@@ -0,0 +1,51 @@
1 +-- Roll back source-tenant audit attribution to the prior candidate behavior.
2 +CREATE OR REPLACE FUNCTION private.finora_phase1a_guard_server_journal_fields()
3 +RETURNS trigger LANGUAGE plpgsql SECURITY INVOKER SET search_path=''
4 +AS $fn$
5 +DECLARE
6 + v_finora_mode text;
7 + v_finora_org uuid;
8 + v_finora_msg text;
9 + v_finora_state text;
10 + v_is_server boolean := current_user IN ('postgres','supabase_admin','service_role');
11 + v_new_status text;
12 +BEGIN
13 + v_finora_org:=CASE WHEN TG_OP='DELETE' THEN old.organization_id ELSE new.organization_id END;
14 + v_finora_mode:=private.finora_phase1a_mode(v_finora_org);
15 + IF TG_OP='UPDATE' AND private.finora_phase1a_mode(old.organization_id)='enforce' THEN v_finora_mode:='enforce'; END IF;
16 + IF TG_OP='UPDATE' AND v_finora_mode='off' AND private.finora_phase1a_mode(old.organization_id)='audit' THEN v_finora_mode:='audit'; END IF;
17 + IF TG_OP='UPDATE' AND old.collection='journalVouchers' AND old.data->>'status' IN ('posted','reversed')
18 + AND (old.organization_id,old.owner_id,old.collection,old.id) IS DISTINCT FROM (new.organization_id,new.owner_id,new.collection,new.id)
19 + AND private.finora_phase1a_mode(old.organization_id)<>'off' THEN
20 + RAISE EXCEPTION 'FINORA_POSTED_JOURNAL_IDENTITY_IMMUTABLE' USING ERRCODE='23514'; END IF;
21 + IF v_finora_mode='off' THEN RETURN new; END IF;
22 + IF TG_OP NOT IN ('INSERT','UPDATE') OR new.collection<>'journalVouchers' THEN RETURN new; END IF;
23 + v_new_status:=new.data->>'status';
24 + IF NOT v_is_server THEN
25 + IF TG_OP='INSERT' THEN
26 + IF new.data ?| ARRAY['status','number','postedAt','postedBy','reversedAt','reversalVoucherId'] THEN
27 + RAISE EXCEPTION 'FINORA_SERVER_ONLY_JOURNAL_FIELDS' USING ERRCODE='P0001';
28 + END IF;
29 + ELSIF (new.data->'status') IS DISTINCT FROM (old.data->'status')
30 + OR (new.data->'number') IS DISTINCT FROM (old.data->'number')
31 + OR (new.data->'postedAt') IS DISTINCT FROM (old.data->'postedAt')
32 + OR (new.data->'postedBy') IS DISTINCT FROM (old.data->'postedBy')
33 + OR (new.data->'reversedAt') IS DISTINCT FROM (old.data->'reversedAt')
34 + OR (new.data->'reversalVoucherId') IS DISTINCT FROM (old.data->'reversalVoucherId') THEN
35 + RAISE EXCEPTION 'FINORA_SERVER_ONLY_JOURNAL_FIELDS' USING ERRCODE='P0001';
36 + END IF;
37 + END IF;
38 + IF v_new_status IS NOT NULL AND v_new_status NOT IN ('draft','posted','reversed') THEN
39 + RAISE EXCEPTION 'FINORA_INVALID_JOURNAL_STATUS' USING ERRCODE='23514';
40 + END IF;
41 + RETURN new;
42 +EXCEPTION WHEN OTHERS THEN
43 + IF v_finora_mode='audit' THEN
44 + GET STACKED DIAGNOSTICS v_finora_msg=MESSAGE_TEXT,v_finora_state=RETURNED_SQLSTATE;
45 + PERFORM private.finora_phase1a_log_guard(v_finora_org,
46 + CASE WHEN TG_OP='DELETE' THEN old.id ELSE new.id END,v_finora_msg,v_finora_state);
47 + RETURN new;
48 + END IF;
49 + RAISE;
50 +END
51 +$fn$;