سامانه صدور فاکتور و حسابداری
test: add adversarial legacy transition security cases
davoodmehraban89 committed
Oct 2, 2026 at 01:12 UTC
eaf24a4862142ee46d862bf29283e6aaef69f074
1 file changed
+99
supabase/tests/request_wave1_legacy_transition_security.sql
new
+99
@@ -0,0 +1,99 @@
1
+\set ON_ERROR_STOP on
2
+
3
+set role authenticated;
4
+select set_config('request.jwt.claim.sub','11111111-1111-1111-1111-111111111111',false);
5
+select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
6
+select set_config('request.jwt.claim.member_active','true',false);
7
+select set_config('request.jwt.claim.entitled','true',false);
8
+select set_config('request.jwt.claim.write','true',false);
9
+select set_config('request.jwt.claim.capabilities','read,configure,create,edit,approve',false);
10
+
11
+create temporary table wave1_ids(k text primary key,v uuid);
12
+insert into wave1_ids
13
+select 'legacy_type',request_type_version_id from public.request_publish_type_version(
14
+ 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','W1_LEGACY','Legacy security',null,
15
+ '{"schemaVersion":1,"fields":[]}'::jsonb,
16
+ '{"schemaVersion":1}'::jsonb,
17
+ 0
18
+);
19
+insert into wave1_ids values(
20
+ 'legacy_instance',
21
+ public.request_create_instance(
22
+ 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
23
+ (select v from wave1_ids where k='legacy_type'),
24
+ '{}'::jsonb,
25
+ 'wave1-create-legacy-0001'
26
+ )
27
+);
28
+select * from public.request_transition_instance(
29
+ (select v from wave1_ids where k='legacy_instance'),
30
+ 'approve',1,'wave1-transition-legacy-0001','first authorized transition'
31
+);
32
+-- Authorized replay remains stable even though the supplied expected revision is stale.
33
+select * from public.request_transition_instance(
34
+ (select v from wave1_ids where k='legacy_instance'),
35
+ 'approve',1,'wave1-transition-legacy-0001','authorized retry'
36
+);
37
+
38
+-- F-409: a caller who no longer has transition capability must not learn/replay
39
+-- a prior successful idempotency result.
40
+select set_config('request.jwt.claim.sub','22222222-2222-2222-2222-222222222222',false);
41
+select set_config('request.jwt.claim.capabilities','read',false);
42
+do $$
43
+begin
44
+ begin
45
+ perform * from public.request_transition_instance(
46
+ (select v from wave1_ids where k='legacy_instance'),
47
+ 'approve',1,'wave1-transition-legacy-0001','unauthorized replay'
48
+ );
49
+ raise exception 'F-409: unauthorized idempotent replay unexpectedly succeeded';
50
+ exception when insufficient_privilege then null;
51
+ end;
52
+end $$;
53
+
54
+-- F-410: even a fully-authorized approver cannot bypass D3 step/quorum state
55
+-- through the legacy transition endpoint.
56
+select set_config('request.jwt.claim.sub','11111111-1111-1111-1111-111111111111',false);
57
+select set_config('request.jwt.claim.capabilities','read,configure,create,edit,approve',false);
58
+insert into wave1_ids
59
+select 'd3_type',request_type_version_id from public.request_publish_type_version(
60
+ 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','W1_D3','D3 bypass guard',null,
61
+ '{"schemaVersion":1,"fields":[]}'::jsonb,
62
+ '{"schemaVersion":2,"steps":[{"key":"manager","title":"Manager","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve"}]}'::jsonb,
63
+ 0
64
+);
65
+insert into wave1_ids values(
66
+ 'd3_instance',
67
+ public.request_create_instance(
68
+ 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
69
+ (select v from wave1_ids where k='d3_type'),
70
+ '{}'::jsonb,
71
+ 'wave1-create-d3-0000001'
72
+ )
73
+);
74
+do $$
75
+begin
76
+ begin
77
+ perform * from public.request_transition_instance(
78
+ (select v from wave1_ids where k='d3_instance'),
79
+ 'approve',1,'wave1-legacy-bypass-0001','must fail'
80
+ );
81
+ raise exception 'F-410: D3 request bypassed step runtime through legacy transition';
82
+ exception when object_not_in_prerequisite_state then
83
+ if position('D3 workflow requires request_step_decide' in sqlerrm)=0 then raise; end if;
84
+ end;
85
+end $$;
86
+
87
+reset role;
88
+do $$
89
+declare legacy_id uuid; d3_id uuid;
90
+begin
91
+ legacy_id=(select v from wave1_ids where k='legacy_instance');
92
+ d3_id=(select v from wave1_ids where k='d3_instance');
93
+ if (select status from public.request_instances where id=legacy_id)<>'approved' then raise exception 'legacy transition status drifted'; end if;
94
+ if (select revision from public.request_instances where id=legacy_id)<>2 then raise exception 'legacy replay mutated revision'; end if;
95
+ if (select count(*) from public.request_instance_events where request_instance_id=legacy_id and idempotency_key='wave1-transition-legacy-0001')<>1 then raise exception 'legacy replay duplicated evidence'; end if;
96
+ if (select status from public.request_instances where id=d3_id)<>'submitted' then raise exception 'D3 bypass attempt mutated request status'; end if;
97
+ if (select revision from public.request_instances where id=d3_id)<>1 then raise exception 'D3 bypass attempt mutated revision'; end if;
98
+ if not exists(select 1 from private.request_step_instances where request_instance_id=d3_id and state='active') then raise exception 'D3 active step missing after bypass attempt'; end if;
99
+end $$;