سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 3, 22:00 UTC
security: harden legacy request transitions
davoodmehraban89 committed
Oct 2, 2026 at 01:12 UTC
6ba5bc84446a1df1f3b2c8285e5de86eb7ad65bf
1 file changed
+115
supabase/migrations/20261002003000_request_legacy_transition_hardening.sql
new
+115
@@ -0,0 +1,115 @@
1
+-- Wave 1 remediation: close legacy request-transition authorization and workflow-bypass paths.
2
+-- F-409/F-410: legacy transition RPC must never bypass D3 step runtime, and idempotent
3
+-- replays must be authorized for the same actor before returning evidence.
4
+
5
+create or replace function private.request_transition_instance(
6
+ p_request_instance_id uuid,
7
+ p_action text,
8
+ p_expected_revision bigint,
9
+ p_idempotency_key text,
10
+ p_note text default null
11
+) returns table(request_instance_id uuid,status text,revision bigint)
12
+language plpgsql security definer set search_path=''
13
+as $$
14
+declare
15
+ v_row public.request_instances%rowtype;
16
+ v_event public.request_instance_events%rowtype;
17
+ v_action text;
18
+ v_next text;
19
+ v_from text;
20
+begin
21
+ if auth.uid() is null then
22
+ raise exception 'authentication required' using errcode='28000';
23
+ end if;
24
+ if char_length(trim(coalesce(p_idempotency_key,''))) < 8 then
25
+ raise exception 'idempotency_key is required';
26
+ end if;
27
+
28
+ -- Lock and authorize the request before consulting idempotency evidence. The old
29
+ -- implementation returned an event first, which could disclose another actor's result.
30
+ select * into v_row
31
+ from public.request_instances r
32
+ where r.id=p_request_instance_id
33
+ for update;
34
+ if not found then raise exception 'request instance not found'; end if;
35
+ if v_row.organization_id not in (select private.current_organization_ids()) then
36
+ raise exception 'organization access denied' using errcode='42501';
37
+ end if;
38
+ if not private.organization_has_module_entitlement(v_row.organization_id,'requests_workflow',true) then
39
+ raise exception 'active requests_workflow entitlement required' using errcode='42501';
40
+ end if;
41
+
42
+ -- D3 workflows are governed exclusively by request_step_decide. Keeping this legacy
43
+ -- endpoint usable for them would let an approver skip ordered/quorum/condition controls.
44
+ if exists(select 1 from private.request_step_instances s where s.request_instance_id=v_row.id) then
45
+ raise exception 'legacy transition disabled for step workflow' using errcode='42501';
46
+ end if;
47
+
48
+ v_action:=lower(trim(coalesce(p_action,'')));
49
+ if v_action not in ('approve','reject','return','submit','cancel') then
50
+ raise exception 'unsupported transition action';
51
+ end if;
52
+
53
+ -- Decision actions require approve. Edit alone is not an approval authority.
54
+ if v_action in ('approve','reject','return') then
55
+ if not private.has_org_capability(v_row.organization_id,'requests_workflow','approve') then
56
+ raise exception 'requests_workflow approve capability required' using errcode='42501';
57
+ end if;
58
+ elsif v_action='submit' then
59
+ if not (v_row.requester_user_id=auth.uid() or private.has_org_capability(v_row.organization_id,'requests_workflow','edit')) then
60
+ raise exception 'requester or requests_workflow edit capability required' using errcode='42501';
61
+ end if;
62
+ else
63
+ if not (v_row.requester_user_id=auth.uid() or private.has_org_capability(v_row.organization_id,'requests_workflow','edit')) then
64
+ raise exception 'requester or requests_workflow edit capability required' using errcode='42501';
65
+ end if;
66
+ end if;
67
+
68
+ select e.* into v_event
69
+ from public.request_instance_events e
70
+ where e.request_instance_id=v_row.id
71
+ and e.organization_id=v_row.organization_id
72
+ and e.idempotency_key=p_idempotency_key;
73
+ if found then
74
+ if v_event.actor_user_id<>auth.uid() or v_event.action<>v_action then
75
+ raise exception 'idempotency key already used by another actor or action' using errcode='42501';
76
+ end if;
77
+ return query select v_row.id,v_event.to_status,v_event.revision;
78
+ return;
79
+ end if;
80
+
81
+ if p_expected_revision is null or p_expected_revision<>v_row.revision then
82
+ raise exception 'stale revision';
83
+ end if;
84
+ if v_row.status in ('approved','rejected','cancelled') then
85
+ raise exception 'terminal request cannot transition';
86
+ end if;
87
+
88
+ -- Explicit legacy state machine. In particular, a returned request cannot be approved
89
+ -- without being resubmitted, and draft/returned requests cannot be rejected directly.
90
+ v_next:=case
91
+ when v_action='approve' and v_row.status='submitted' then 'approved'
92
+ when v_action='reject' and v_row.status='submitted' then 'rejected'
93
+ when v_action='return' and v_row.status='submitted' then 'returned'
94
+ when v_action='submit' and v_row.status in ('draft','returned') then 'submitted'
95
+ when v_action='cancel' and v_row.status in ('draft','submitted','returned') then 'cancelled'
96
+ else null
97
+ end;
98
+ if v_next is null then raise exception 'transition not allowed from current status'; end if;
99
+
100
+ v_from:=v_row.status;
101
+ update public.request_instances as ri
102
+ set status=v_next,revision=ri.revision+1,updated_at=now()
103
+ where ri.id=v_row.id
104
+ returning ri.* into v_row;
105
+
106
+ insert into public.request_instance_events(
107
+ organization_id,request_instance_id,actor_user_id,action,from_status,to_status,revision,note,idempotency_key
108
+ ) values(
109
+ v_row.organization_id,v_row.id,auth.uid(),v_action,v_from,v_next,v_row.revision,p_note,p_idempotency_key
110
+ );
111
+ return query select v_row.id,v_next,v_row.revision;
112
+end $$;
113
+
114
+revoke all on function private.request_transition_instance(uuid,text,bigint,text,text) from public,anon;
115
+grant execute on function private.request_transition_instance(uuid,text,bigint,text,text) to authenticated;