@davoodmehraban89 / VELYQOR / commits / d869581
Fork 0

Intelligence, Advanced Charting & Trading Platform

security(outbox): add consent-aware claim that terminally suppresses revoked recipient delivery

davoodmehraban89 committed Oct 22, 2026 at 23:47 UTC d8695810d874ab30b19dcf0266bc5e91b5e15332
1 file changed +77
db/migrations/0006_alert_runtime_atomic.sql
+77
@@ -194,11 +194,88 @@ BEGIN
194 END;
195 $$;
196
197 +-- Optional future sender MUST claim through this consent-aware wrapper,
198 +-- not through the raw generic outbox claimant. A revoked consent permanently
199 +-- suppresses pending notification and never leaks it to a transport worker.
200 +-- The later live sender will require a second check just before HTTP send.
201 +CREATE FUNCTION public.alert_notification_claim_consented(
202 + p_now TIMESTAMPTZ DEFAULT now(),
203 + p_lease_seconds INTEGER DEFAULT 30
204 +) RETURNS TABLE (
205 + outbox_id UUID,
206 + event_key TEXT,
207 + channel TEXT,
208 + recipient_ref UUID,
209 + claim_token UUID,
210 + attempt_number INTEGER
211 +)
212 +LANGUAGE plpgsql
213 +SECURITY INVOKER
214 +SET search_path = pg_catalog, public
215 +AS $
216 +DECLARE
217 + v_claim RECORD;
218 + v_authorized BOOLEAN;
219 +BEGIN
220 + IF p_now IS NULL OR p_lease_seconds IS NULL
221 + OR p_lease_seconds NOT BETWEEN 1 AND 300 THEN
222 + RAISE EXCEPTION 'invalid consent-aware claim parameters'
223 + USING ERRCODE = '22023';
224 + END IF;
225 +
226 + SELECT * INTO v_claim
227 + FROM public.notification_outbox_claim(p_now, p_lease_seconds);
228 + IF NOT FOUND THEN RETURN; END IF;
229 +
230 + -- Check actual persisted user and recipient/owner binding rather than
231 + -- trusting the caller or relying solely on opt-in at enqueue time.
232 + SELECT true INTO v_authorized
233 + FROM public.notification_outbox AS o
234 + JOIN public.alert_notification_consents AS consent
235 + ON consent.user_id = o.user_id
236 + AND consent.recipient_ref = o.recipient_ref
237 + AND consent.channel = o.channel
238 + WHERE o.id = v_claim.outbox_id
239 + AND o.status = 'LEASED'
240 + AND o.lease_token = v_claim.claim_token
241 + AND consent.consented_at <= p_now
242 + AND consent.revoked_at IS NULL
243 + FOR SHARE OF consent;
244 +
245 + IF NOT COALESCE(v_authorized,false) THEN
246 + -- No network call is performed. UNKNOWN is terminal under PR #34 schema;
247 + -- the explicit reason distinguishes revoked consent from uncertain send.
248 + UPDATE public.notification_outbox AS o
249 + SET status = 'UNKNOWN',
250 + failure_reason = 'RECIPIENT_CONSENT_REVOKED_OR_MISSING',
251 + lease_token = NULL,
252 + leased_at = NULL,
253 + lease_until = NULL,
254 + next_retry_at = NULL,
255 + updated_at = p_now
256 + WHERE o.id = v_claim.outbox_id
257 + AND o.status = 'LEASED'
258 + AND o.lease_token = v_claim.claim_token;
259 + RETURN;
260 + END IF;
261 +
262 + outbox_id := v_claim.outbox_id;
263 + event_key := v_claim.event_key;
264 + channel := v_claim.channel;
265 + recipient_ref := v_claim.recipient_ref;
266 + claim_token := v_claim.claim_token;
267 + attempt_number := v_claim.attempt_number;
268 + RETURN NEXT;
269 +END;
270 +$;
271 +
272 -- Persisted state is owned by backend service only. No default browser/anon
273 -- SELECT/INSERT/UPDATE or function EXECUTE grants are introduced.
274 REVOKE ALL ON public.alert_rule_runtime FROM PUBLIC;
275 REVOKE ALL ON public.alert_notification_consents FROM PUBLIC;
276 REVOKE ALL ON FUNCTION public.alert_rule_commit(
277 UUID, TEXT, BIGINT, JSONB, TEXT, TEXT, UUID, TIMESTAMPTZ) FROM PUBLIC;
278 +REVOKE ALL ON FUNCTION public.alert_notification_claim_consented(
279 + TIMESTAMPTZ, INTEGER) FROM PUBLIC;
280
281 COMMIT;