Intelligence, Advanced Charting & Trading Platform
security(Bale): resolve chat only via trusted opaque-recipient registry and bind SQL authorization to recipient and channel
davoodmehraban89 committed
Oct 23, 2026 at 02:42 UTC
aba6d1f86a55e1ab5895560c3462df65c4a54970
1 file changed
+14
-7
packages/market-domain/src/leased-bale-dispatch.mjs
+14
-7
@@ -8,8 +8,8 @@
8
* NOT PRODUCTION-AUTHORIZED: sender role and recipient ownership unverified.
9
*/
10
const uuid=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
11
-const beginSql='SELECT public.alert_notification_begin_egress($1::uuid,$2::uuid,$3::timestamptz) AS permitted';
12
-const egressSql='SELECT public.alert_notification_egress_allowed($1::uuid,$2::uuid,$3::timestamptz) AS permitted';
11
+const beginSql='SELECT public.alert_notification_begin_egress($1::uuid,$2::uuid,$3::uuid,$4::text,$5::timestamptz) AS permitted';
12
+const egressSql='SELECT public.alert_notification_egress_allowed($1::uuid,$2::uuid,$3::uuid,$4::text,$5::timestamptz) AS permitted';
13
const finishSql='SELECT public.notification_outbox_finish($1::uuid,$2::uuid,$3::text,$4::text,$5::timestamptz,60) AS outcome';
14
async function permitted(db,sql,params){
15
const result=await db.query(sql,params);
@@ -26,17 +26,24 @@ async function finish(db,{outboxId,claimToken,decision,messageId,at}){
26
return r.rows[0].outcome;
27
}
28
export async function dispatchLeasedBale({
29
- db,baleOneMessage,outboxId,claimToken,token,chatId,text,at
29
+ db,baleOneMessage,outboxId,claimToken,recipientRef,resolveRecipient,token,text,at
30
}={}){
31
if(typeof db?.query!=='function'||typeof baleOneMessage!=='function'||
32
- !uuid.test(outboxId)||!uuid.test(claimToken)||
32
+ !uuid.test(outboxId)||!uuid.test(claimToken)||!uuid.test(recipientRef)||
33
+ typeof resolveRecipient!=='function'||
34
typeof at!=='string'||!Number.isFinite(Date.parse(at)))
35
throw new TypeError('invalid server dispatch prerequisites');
36
if(typeof token!=='string'||token.length<12||
36
- typeof chatId!=='string'||!/^[1-9][0-9]{0,15}$/.test(chatId)||
37
typeof text!=='string'||text.length<1||text.length>1000)
38
throw new TypeError('invalid private dispatch data');
39
- const params=[outboxId,claimToken,at];
39
+ // The destination must be resolved from a trusted server-side opaque
40
+ // recipient registry, NEVER taken from untrusted event/HTTP input.
41
+ const resolved=await resolveRecipient({recipientRef,channel:'BALE'});
42
+ if(resolved?.verified!==true||resolved?.recipientRef!==recipientRef||
43
+ typeof resolved.chatId!=='string'||
44
+ !/^[1-9][0-9]{0,15}$/.test(resolved.chatId))
45
+ return {status:'BLOCKED',reason:'RECIPIENT_MAPPING_UNVERIFIED',sent:false};
46
+ const params=[outboxId,claimToken,recipientRef,'BALE',at];
47
// Linearization point: this SQL row UPDATE makes a second caller unable
48
// to obtain another send permit. No network call occurs before success.
49
if(!await permitted(db,beginSql,params))
@@ -49,7 +56,7 @@ export async function dispatchLeasedBale({
56
persistedOutcome:state,sent:false};
57
}
58
let outcome;
52
- try{outcome=await baleOneMessage({token,chatId,text})}
59
+ try{outcome=await baleOneMessage({token,chatId:resolved.chatId,text})}
60
catch{outcome={status:'UNKNOWN'}}
61
// Any ambiguous transport error is terminal UNKNOWN, not an auto retry.
62
const accepted=outcome?.status==='SENT'&&