سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 3, 19:30 UTC
security: harden D3a idempotent replay authorization
davoodmehraban89 committed
Oct 1, 2026 at 22:48 UTC
db4c5c73114d1d65c55f6ab386dfe67545ac2204
1 file changed
+56
supabase/migrations/20261001230500_request_workflow_d3a_security_hardening.sql
new
+56
@@ -0,0 +1,56 @@
1
+-- D3a hardening: authorize tenant/module access before idempotent replay and cover private step FK.
2
+create index if not exists request_step_instances_organization_idx on private.request_step_instances(organization_id);
3
+
4
+create or replace function private.request_step_decide(p_request_instance_id uuid,p_decision text,p_expected_revision bigint,p_idempotency_key text,p_note text default null)
5
+returns table(request_instance_id uuid,status text,revision bigint,step_key text,step_state text,approvals_received integer,approvals_required integer)
6
+language plpgsql security definer set search_path=''
7
+as $$
8
+declare
9
+ v_row public.request_instances%rowtype; v_step private.request_step_instances%rowtype; v_vote private.request_step_votes%rowtype; v_next private.request_step_instances%rowtype; v_decision text; v_approvals integer; v_status text;
10
+begin
11
+ if auth.uid() is null then raise exception 'authentication required' using errcode='28000'; end if;
12
+ if char_length(trim(coalesce(p_idempotency_key,'')))<8 then raise exception 'idempotency_key is required'; end if;
13
+ select * into v_row from public.request_instances r where r.id=p_request_instance_id;
14
+ if not found then raise exception 'request instance not found'; end if;
15
+ if v_row.organization_id not in (select private.current_organization_ids()) then raise exception 'organization access denied' using errcode='42501'; end if;
16
+ if not private.organization_has_module_entitlement(v_row.organization_id,'requests_workflow',true) then raise exception 'active requests_workflow entitlement required' using errcode='42501'; end if;
17
+
18
+ select * into v_vote from private.request_step_votes v where v.request_instance_id=p_request_instance_id and v.idempotency_key=p_idempotency_key;
19
+ if found then
20
+ select * into v_step from private.request_step_instances s where s.id=v_vote.step_instance_id;
21
+ select count(*)::integer into v_approvals from private.request_step_votes x where x.step_instance_id=v_step.id and x.decision='approve';
22
+ return query select v_row.id,v_row.status,v_row.revision,v_step.step_key,v_step.state,v_approvals,v_step.required_approvals; return;
23
+ end if;
24
+
25
+ select * into v_row from public.request_instances r where r.id=p_request_instance_id for update;
26
+ if p_expected_revision is null or p_expected_revision<>v_row.revision then raise exception 'stale revision'; end if;
27
+ if v_row.status in ('approved','rejected','cancelled') then raise exception 'terminal request cannot transition'; end if;
28
+ select * into v_step from private.request_step_instances s where s.request_instance_id=v_row.id and s.state='active' order by s.step_index limit 1 for update;
29
+ if not found then raise exception 'active workflow step not found'; end if;
30
+ if not private.has_org_capability(v_row.organization_id,'requests_workflow',v_step.required_capability) then raise exception 'active step capability required' using errcode='42501'; end if;
31
+ v_decision:=lower(trim(coalesce(p_decision,'')));
32
+ if v_decision not in ('approve','reject') then raise exception 'unsupported step decision'; end if;
33
+ begin
34
+ insert into private.request_step_votes(organization_id,request_instance_id,step_instance_id,actor_user_id,decision,note,idempotency_key) values(v_row.organization_id,v_row.id,v_step.id,auth.uid(),v_decision,p_note,p_idempotency_key) returning * into v_vote;
35
+ exception when unique_violation then
36
+ if exists(select 1 from private.request_step_votes x where x.step_instance_id=v_step.id and x.actor_user_id=auth.uid()) then raise exception 'actor already decided this step'; end if;
37
+ raise;
38
+ end;
39
+ if v_decision='reject' then
40
+ update private.request_step_instances set state='rejected',completed_at=now() where id=v_step.id returning * into v_step;
41
+ v_status:='rejected';
42
+ else
43
+ select count(*)::integer into v_approvals from private.request_step_votes x where x.step_instance_id=v_step.id and x.decision='approve';
44
+ if v_approvals>=v_step.required_approvals then
45
+ update private.request_step_instances set state='completed',completed_at=now() where id=v_step.id returning * into v_step;
46
+ select * into v_next from private.request_step_instances s where s.request_instance_id=v_row.id and s.state='pending' and s.step_index>v_step.step_index order by s.step_index limit 1 for update;
47
+ if found then update private.request_step_instances set state='active',activated_at=now() where id=v_next.id; v_status:='submitted'; else v_status:='approved'; end if;
48
+ else v_status:='submitted'; end if;
49
+ end if;
50
+ update public.request_instances as ri set status=v_status,revision=ri.revision+1,updated_at=now() where ri.id=v_row.id returning ri.* into v_row;
51
+ insert into public.request_instance_events(organization_id,request_instance_id,actor_user_id,action,from_status,to_status,revision,note,idempotency_key) values(v_row.organization_id,v_row.id,auth.uid(),'step_'||v_decision||':'||v_step.step_key,'submitted',v_status,v_row.revision,p_note,p_idempotency_key);
52
+ select count(*)::integer into v_approvals from private.request_step_votes x where x.step_instance_id=v_step.id and x.decision='approve';
53
+ return query select v_row.id,v_row.status,v_row.revision,v_step.step_key,v_step.state,v_approvals,v_step.required_approvals;
54
+end $$;
55
+revoke all on function private.request_step_decide(uuid,text,bigint,text,text) from public,anon;
56
+grant execute on function private.request_step_decide(uuid,text,bigint,text,text) to authenticated;