Intelligence, Advanced Charting & Trading Platform
security(outbox): second consent and active-lease preflight immediately before notification egress
davoodmehraban89 committed
Oct 23, 2026 at 02:33 UTC
199941d462a1f8e54bbbb1c2f34aab2c386727ba
1 file changed
+41
db/migrations/0006_alert_runtime_atomic.sql
+41
@@ -277,6 +277,45 @@ BEGIN
277
END;
278
$alert_claim$;
279
280
+-- Second consent checkpoint immediately before a sender attempts HTTPS.
281
+-- A claim-time check alone is insufficient: the user can revoke consent later.
282
+-- This function is side-effect-free on a valid row, returns ONLY a boolean,
283
+-- and never reads or reveals a chat ID, token or message payload.
284
+CREATE FUNCTION public.alert_notification_egress_allowed(
285
+ p_outbox_id UUID,
286
+ p_claim_token UUID,
287
+ p_at TIMESTAMPTZ DEFAULT now()
288
+) RETURNS BOOLEAN
289
+LANGUAGE plpgsql
290
+SECURITY INVOKER
291
+SET search_path = pg_catalog, public
292
+AS $egress_check$
293
+DECLARE
294
+ v_can_send BOOLEAN := FALSE;
295
+BEGIN
296
+ IF p_outbox_id IS NULL OR p_claim_token IS NULL OR p_at IS NULL THEN
297
+ RAISE EXCEPTION 'invalid outbound authorization parameters'
298
+ USING ERRCODE = '22023';
299
+ END IF;
300
+
301
+ -- Keep the outbox row locked during the check. The caller must use a
302
+ -- trusted backend role; no frontend, browser or PUBLIC access is granted.
303
+ SELECT true INTO v_can_send
304
+ FROM public.notification_outbox AS o
305
+ JOIN public.alert_notification_consents AS c
306
+ ON c.user_id = o.user_id
307
+ AND c.recipient_ref = o.recipient_ref
308
+ AND c.channel = o.channel
309
+ WHERE o.id = p_outbox_id AND o.lease_token = p_claim_token
310
+ AND o.status = 'LEASED'
311
+ AND o.leased_at <= p_at AND o.lease_until > p_at
312
+ AND c.consented_at <= p_at AND c.revoked_at IS NULL
313
+ FOR SHARE OF c;
314
+
315
+ RETURN COALESCE(v_can_send, FALSE);
316
+END;
317
+$egress_check$;
318
+
319
-- Persisted state is owned by backend service only. No default browser/anon
320
-- SELECT/INSERT/UPDATE or function EXECUTE grants are introduced.
321
REVOKE ALL ON public.alert_rule_runtime FROM PUBLIC;
@@ -285,5 +324,7 @@ REVOKE ALL ON FUNCTION public.alert_rule_commit(
324
UUID, TEXT, BIGINT, JSONB, TEXT, TEXT, UUID, TIMESTAMPTZ) FROM PUBLIC;
325
REVOKE ALL ON FUNCTION public.alert_notification_claim_consented(
326
TIMESTAMPTZ, INTEGER) FROM PUBLIC;
327
+REVOKE ALL ON FUNCTION public.alert_notification_egress_allowed(
328
+ UUID, UUID, TIMESTAMPTZ) FROM PUBLIC;
329
330
COMMIT;