@davoodmehraban89 / Factor-easy / commits / beed004
Fork 0

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

test(stage3): reject authenticated direct financial source mutation after posted server voucher

davoodmehraban89 committed Oct 23, 2026 at 03:14 UTC beed004490e377317930a5946020cf82835e6215
1 file changed +16
scripts/stage3-real-jwt-source-post.mjs
+16
@@ -102,6 +102,22 @@ for(const kind of ['sale','purchase']){
102 if(info.status!=='posted'||info.source!==sourceId||!info.number||Number(info.version)<2)
103 throw Error('JWT_POSTING_NOT_DURABLE '+kind);
104 posted++;
105 + // An attacker must not be able to amend the original financial source after
106 + // the server posted its journal. The API may return 2xx for an RLS-filtered
107 + // zero-row PATCH; the authoritative persisted row is the acceptance check.
108 + const attacked=await fetch(API+'/rest/v1/records?organization_id=eq.'+org+
109 + '&collection=eq.'+coll+'&id=eq.'+sourceId,{
110 + method:'PATCH',
111 + headers:{apikey:anon,authorization:'Bearer '+jwt,
112 + 'content-type':'application/json',Prefer:'return=representation'},
113 + body:JSON.stringify({data:{...source,grandTotal:source.grandTotal+17}})
114 + });
115 + const attackBody=await attacked.json().catch(()=>null);
116 + const persistedGrand=Number(db("SELECT data->>'grandTotal' FROM public.records WHERE organization_id="+
117 + quote(org)+"::uuid AND collection="+quote(coll)+" AND id="+quote(sourceId)));
118 + if(persistedGrand!==source.grandTotal||attackBody?.length>0&&attacked.ok)
119 + throw Error('JWT_POSTED_SOURCE_MUTATION_BYPASS '+kind+' HTTP_'+attacked.status);
120 + rejected++;
121 const retry=await rpc('finora_journal_autopost',args);
122 if(!retry.ok||retry.data?.replayed!==true||retry.data?.voucher?.number!==info.number)
123 throw Error('JWT_AUTOPOST_NOT_IDEMPOTENT '+kind+' '+retry.status);