@davoodmehraban89 / VELYQOR / commits / da4ccad
Fork 0

Intelligence, Advanced Charting & Trading Platform

security(data): require independent server-side license registry evidence; ignore caller-supplied permission flags

davoodmehraban89 committed Oct 23, 2026 at 10:55 UTC da4ccad02f6da96a68896aeb08f90f4e59766c95
1 file changed +40 -14
packages/market-domain/src/alert-postgres-coordinator.mjs
+40 -14
@@ -41,9 +41,9 @@ function ensureEvent(event,rule,quote,gate,next){
41 gate.accepted?.price!==quote.price)
42 throw new Error('UNVERIFIED_EVENT_PROVENANCE');
43 }
44 -function validateArgs({db,domain,principal,rule,recipient,observation,now}){
44 +function validateArgs({db,domain,sourceRights,principal,rule,recipient,observation,now}){
45 if(typeof db?.query!=='function'||typeof domain?.evaluateQuoteGate!=='function'||
46 - typeof domain?.stepAlert!=='function')throw new TypeError('trusted database/domain functions required');
46 + typeof domain?.stepAlert!=='function'||typeof sourceRights!=='function')throw new TypeError('trusted database/domain functions required');
47 if(principal?.kind!=='VERIFIED_SERVER_PRINCIPAL'||principal.verified!==true||
48 !ID.test(principal.userId))
49 throw new TypeError('verified server user required');
@@ -61,12 +61,6 @@ function validateArgs({db,domain,principal,rule,recipient,observation,now}){
61 observation.symbol!==rule.symbol||observation.provider!==rule.provider||
62 observation.unit!==rule.unit||observation.side!==rule.legs[0].side)
63 throw new TypeError('observation must match trusted source, unit and exit side');
64 - // The persisted gate/high-water state contains historical provider prices.
65 - // A display/alert license alone is NOT a right to store this state in Neon.
66 - if(observation.permission?.publicDisplay!==true||
67 - observation.permission?.derivedAlerts!==true||
68 - observation.permission?.historicalStorage!==true)
69 - throw new Error('PRICE_STORAGE_OR_ALERT_RIGHTS_UNVERIFIED');
64 if(typeof now!=='string'||!Number.isFinite(Date.parse(now)))
65 throw new TypeError('valid server time required');
66 }
@@ -76,14 +70,46 @@ function validateArgs({db,domain,principal,rule,recipient,observation,now}){
70 * retries are allowed after STALE_REVISION to resolve Worker races.
71 */
72 export async function evaluateAndPersistAlert({
79 - db,domain,principal,rule,recipient,observation,now,
73 + db,domain,sourceRights,principal,rule,recipient,observation,now,
74 gateOptions={},maxAttempts=3
75 }){
82 - validateArgs({db,domain,principal,rule,recipient,observation,now});
76 + validateArgs({db,domain,sourceRights,principal,rule,recipient,observation,now});
77 if(!Number.isInteger(maxAttempts)||maxAttempts<1||maxAttempts>5)
78 throw new RangeError('maxAttempts');
79 + // Entitlements MUST come from a separate, trusted server-side contract
80 + // registry. Never accept observation.permission supplied by an API/browser
81 + // as evidence of redisplay, alert creation or historical state storage.
82 + let rights;
83 + try {
84 + rights=await sourceRights({
85 + provider:rule.provider,symbol:rule.symbol,unit:rule.unit,
86 + side:observation.side,timestamp:observation.timestamp,now
87 + });
88 + } catch {
89 + throw new Error('SOURCE_RIGHTS_REGISTRY_UNAVAILABLE');
90 + }
91 + const granted=rights?.origin==='TRUSTED_SERVER_CONTRACT_REGISTRY' &&
92 + rights.verified===true &&
93 + rights.provider===rule.provider && rights.symbol===rule.symbol &&
94 + rights.unit===rule.unit && rights.side===observation.side &&
95 + rights.publicDisplay===true && rights.derivedAlerts===true &&
96 + rights.historicalStorage===true &&
97 + typeof rights.evidenceRef==='string' &&
98 + /^[A-Za-z0-9:._/-]{8,256}$/.test(rights.evidenceRef) &&
99 + typeof rights.effectiveFrom==='string' &&
100 + typeof rights.expiresAt==='string';
101 + const start=granted?Date.parse(rights.effectiveFrom):NaN;
102 + const expiry=granted?Date.parse(rights.expiresAt):NaN;
103 + const requested=Date.parse(now),observed=Date.parse(observation.timestamp);
104 + if(!granted||!Number.isFinite(start)||!Number.isFinite(expiry)||
105 + !Number.isFinite(observed)||start>observed||start>requested||
106 + expiry<observed||expiry<requested||start>=expiry)
107 + throw new Error('PRICE_STORAGE_OR_ALERT_RIGHTS_UNVERIFIED');
108 + const licensedObservation={...observation,permission:{
109 + publicDisplay:true,derivedAlerts:true,historicalStorage:true
110 + }};
111 const userId=principal.userId,ruleId=rule.id;
86 - const gateConfig={...gateOptions,nowMs:Date.parse(now)};
112 + const gateConfig={...gateOptions,nowMs:requested};
113 for(let attempt=0;attempt<maxAttempts;attempt++){
114 const r=await db.query(sqlRead,[userId,ruleId]);
115 if(!Array.isArray(r?.rows)||r.rows.length>1)
@@ -97,15 +123,15 @@ export async function evaluateAndPersistAlert({
123 if(previousAlert?.phase==='TRIGGERED'||previousAlert?.phase==='NOTIFIED'||
124 previousAlert?.phase==='CLOSED')
125 return {outcome:'ALREADY_TERMINAL',revision,outboxId:null,reason:'NOT_REARMED'};
100 - const evaluation=domain.evaluateQuoteGate(snapshot?.gate??null,observation,gateConfig);
126 + const evaluation=domain.evaluateQuoteGate(snapshot?.gate??null,licensedObservation,gateConfig);
127 if(!evaluation||typeof evaluation.status!=='string'||!evaluation.state)
128 throw new Error('INVALID_DOMAIN_GATE');
103 - const computed=domain.stepAlert(rule,previousAlert,observation,evaluation);
129 + const computed=domain.stepAlert(rule,previousAlert,licensedObservation,evaluation);
130 if(!computed||!computed.state||!Array.isArray(computed.events)||
131 computed.events.length>1)
132 throw new Error('INVALID_DOMAIN_EVALUATION');
133 const event=computed.events[0]??null;
108 - if(event)ensureEvent(event,rule,observation,evaluation,computed);
134 + if(event)ensureEvent(event,rule,licensedObservation,evaluation,computed);
135 // Suspensions and recovery are preserved durably, even with no alert event.
136 if(!event&&computed.state.phase!=='ARMED')
137 throw new Error('EVENTLESS_TERMINAL_TRANSITION');