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

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

test(stage3): require JWT-CAS update/delete rejection for posted sale and purchase

davoodmehraban89 committed Oct 23, 2026 at 14:49 UTC dfaa4ab07ee0c02fcf42fa48059bac49ac096500
1 file changed +30
scripts/stage3-real-jwt-source-post.mjs
+30
@@ -118,6 +118,36 @@ for(const kind of ['sale','purchase']){
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 + // The same authenticated owner must also be unable to bypass the freeze via
122 + // FINORA's normal optimistic-CAS sync endpoint (UPDATE or DELETE).
123 + const actualVersion=Number(db("SELECT version FROM public.records WHERE organization_id="+
124 + quote(org)+"::uuid AND collection="+quote(coll)+" AND id="+quote(sourceId)));
125 + if(!Number.isSafeInteger(actualVersion)||actualVersion<1)
126 + throw Error('JWT_SOURCE_VERSION_MISSING '+kind);
127 + const overwrite=await rpc('finora_sync_records_v2',{
128 + p_organization_id:org,
129 + p_upserts:[{collection:coll,id:sourceId,expected_version:actualVersion,
130 + data:{...source,grandTotal:source.grandTotal+31}}],
131 + p_deletes:[]
132 + });
133 + if(overwrite.ok||!String(overwrite.data?.message||'').includes('FINORA_POSTED_SOURCE_IMMUTABLE'))
134 + throw Error('JWT_CAS_POSTED_SOURCE_MUTATION_BYPASS '+kind+' '+overwrite.status+' '+
135 + JSON.stringify(overwrite.data).slice(0,300));
136 + if(Number(db("SELECT data->>'grandTotal' FROM public.records WHERE organization_id="+
137 + quote(org)+"::uuid AND collection="+quote(coll)+" AND id="+quote(sourceId)))!==source.grandTotal)
138 + throw Error('JWT_CAS_CHANGED_POSTED_SOURCE '+kind);
139 + rejected++;
140 + const deletion=await rpc('finora_sync_records_v2',{
141 + p_organization_id:org,p_upserts:[],
142 + p_deletes:[{collection:coll,id:sourceId,expected_version:actualVersion}]
143 + });
144 + if(deletion.ok||!String(deletion.data?.message||'').includes('FINORA_POSTED_SOURCE_IMMUTABLE'))
145 + throw Error('JWT_CAS_POSTED_SOURCE_DELETE_BYPASS '+kind+' '+deletion.status+' '+
146 + JSON.stringify(deletion.data).slice(0,300));
147 + if(db("SELECT count(*) FROM public.records WHERE organization_id="+
148 + quote(org)+"::uuid AND collection="+quote(coll)+" AND id="+quote(sourceId))!=='1')
149 + throw Error('JWT_POSTED_SOURCE_WAS_DELETED '+kind);
150 + rejected++;
151 const retry=await rpc('finora_journal_autopost',args);
152 if(!retry.ok||retry.data?.replayed!==true||retry.data?.voucher?.number!==info.number)
153 throw Error('JWT_AUTOPOST_NOT_IDEMPOTENT '+kind+' '+retry.status);