سامانه صدور فاکتور و حسابداری
security: close legacy request replay and D3 bypass
davoodmehraban89 committed
Oct 2, 2026 at 01:11 UTC
377a50ba2e590f26faec8365407b5e286247b37b
1 file changed
+70
supabase/migrations/20261002010000_request_wave1_legacy_transition_guard.sql
new
+70
@@ -0,0 +1,70 @@
1
+-- Wave 1 / F-409 + F-410
2
+-- Re-authorize every legacy transition replay and prohibit the legacy transition API
3
+-- from mutating requests that have D3 step-runtime state.
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_next text;
17
+ v_from text;
18
+ v_event public.request_instance_events%rowtype;
19
+begin
20
+ if auth.uid() is null then raise exception 'authentication required' using errcode='28000'; end if;
21
+ if char_length(trim(coalesce(p_idempotency_key,'')))<8 then raise exception 'idempotency_key is required'; end if;
22
+
23
+ -- Lock and authorize the current caller before consulting replay evidence.
24
+ select * into v_row from public.request_instances r where r.id=p_request_instance_id for update;
25
+ if not found then raise exception 'request instance not found'; end if;
26
+ if v_row.organization_id not in (select private.current_organization_ids()) then raise exception 'organization access denied' using errcode='42501'; end if;
27
+ 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;
28
+ if not (private.has_org_capability(v_row.organization_id,'requests_workflow','approve') or private.has_org_capability(v_row.organization_id,'requests_workflow','edit')) then raise exception 'requests_workflow approve or edit capability required' using errcode='42501'; end if;
29
+
30
+ -- D3 requests are exclusively mutated through request_step_decide. The legacy
31
+ -- endpoint must never collapse quorum/step semantics into a direct status change.
32
+ if exists(select 1 from private.request_step_instances s where s.request_instance_id=v_row.id) then
33
+ raise exception 'D3 workflow requires request_step_decide' using errcode='55000';
34
+ end if;
35
+
36
+ select e.* into v_event
37
+ from public.request_instance_events e
38
+ where e.request_instance_id=v_row.id and e.idempotency_key=p_idempotency_key;
39
+ if found then
40
+ return query select v_row.id,v_event.to_status,v_event.revision;
41
+ return;
42
+ end if;
43
+
44
+ if p_expected_revision is null or p_expected_revision<>v_row.revision then raise exception 'stale revision'; end if;
45
+ v_from:=v_row.status;
46
+ v_next:=case lower(trim(p_action))
47
+ when 'approve' then 'approved'
48
+ when 'reject' then 'rejected'
49
+ when 'return' then 'returned'
50
+ when 'submit' then 'submitted'
51
+ when 'cancel' then 'cancelled'
52
+ else null end;
53
+ if v_next is null then raise exception 'unsupported transition action'; end if;
54
+ if v_from in ('approved','rejected','cancelled') then raise exception 'terminal request cannot transition'; end if;
55
+
56
+ update public.request_instances as ri
57
+ set status=v_next,revision=ri.revision+1,updated_at=now()
58
+ where ri.id=v_row.id
59
+ returning ri.* into v_row;
60
+
61
+ insert into public.request_instance_events(
62
+ organization_id,request_instance_id,actor_user_id,action,from_status,to_status,revision,note,idempotency_key
63
+ ) values(
64
+ v_row.organization_id,v_row.id,auth.uid(),lower(trim(p_action)),v_from,v_next,v_row.revision,p_note,p_idempotency_key
65
+ );
66
+ return query select v_row.id,v_next,v_row.revision;
67
+end $$;
68
+
69
+revoke all on function private.request_transition_instance(uuid,text,bigint,text,text) from public,anon;
70
+grant execute on function private.request_transition_instance(uuid,text,bigint,text,text) to authenticated;