feat(notifications): send product notifications with a rights check right before each send

This commit is contained in:
shira committed 2026-10-09 20:38:39 +03:00
1 parent a856066316
commit 623d41b969
59 files changed
+4125 -1126

No files matched your search

@@ -0,0 +1,188 @@
-- +goose Up
-- A notification names the record it is about (its subject), so the sending
-- worker can re-check the rights the notice depends on before it calls the
-- transport. The subject is fixed when the request is stored and is copied to
-- the delivery, where the content trigger keeps it unchanged. The three
-- columns are set together. Only the kind invitation exists so far. A
-- notification without a subject stays valid: the sending worker cancels it
-- with source_closed, because it has no right to check.
--
-- Everything here is additive: existing rows keep NULL subjects.
ALTER TABLE mail_requests
ADD COLUMN subject_kind text,
ADD COLUMN subject_id uuid,
ADD COLUMN subject_organization_id uuid,
ADD CONSTRAINT mail_requests_subject_complete CHECK (
(subject_kind IS NULL) = (subject_id IS NULL)
AND (subject_kind IS NULL) = (subject_organization_id IS NULL)
AND (subject_kind IS NULL OR subject_kind IN ('invitation'))
);
COMMENT ON COLUMN mail_requests.subject_kind IS
'The kind of record the notification is about, NULL when it names none; the sending worker checks the rights of that kind.';
COMMENT ON COLUMN mail_requests.subject_id IS
'The record the notification is about, for example the invitation; an opaque ID, never an address or a contact.';
COMMENT ON COLUMN mail_requests.subject_organization_id IS
'The organization the subject record belongs to, for example the organization that sent the invitation.';
ALTER TABLE deliveries
ADD COLUMN subject_kind text,
ADD COLUMN subject_id uuid,
ADD COLUMN subject_organization_id uuid,
ADD CONSTRAINT deliveries_subject_complete CHECK (
(subject_kind IS NULL) = (subject_id IS NULL)
AND (subject_kind IS NULL) = (subject_organization_id IS NULL)
AND (subject_kind IS NULL OR subject_kind IN ('invitation'))
AND (subject_kind IS NULL OR kind = 'notification')
);
COMMENT ON COLUMN deliveries.subject_kind IS
'The kind of record a notification is about; set only for a notification, NULL when it names none. The product sending worker re-checks its rights before the send.';
COMMENT ON COLUMN deliveries.subject_id IS
'The record a notification is about; an opaque ID, never an address or a contact.';
COMMENT ON COLUMN deliveries.subject_organization_id IS
'The organization the subject record belongs to.';
-- The subject is part of what the delivery sends and is fixed, like the
-- recipient and the content. This is the function of migration 00012 with the
-- three subject columns added to the unchanged tuple.
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION deliveries_refuse_content_change() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF (
NEW.id, NEW.source_event_id, NEW.recipient_user_id,
NEW.recipient_email, NEW.kind, NEW.template_version, NEW.subject,
NEW.content_sha256,
NEW.idempotency_key, NEW.created_at, NEW.email_token_id,
NEW.subject_kind, NEW.subject_id, NEW.subject_organization_id
) IS DISTINCT FROM (
OLD.id, OLD.source_event_id, OLD.recipient_user_id,
OLD.recipient_email, OLD.kind, OLD.template_version, OLD.subject,
OLD.content_sha256,
OLD.idempotency_key, OLD.created_at, OLD.email_token_id,
OLD.subject_kind, OLD.subject_id, OLD.subject_organization_id
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'deliveries_content_immutable',
TABLE = 'deliveries',
MESSAGE = 'delivery content and recipient are immutable';
END IF;
IF (NEW.text_body, NEW.html_body) IS DISTINCT FROM (OLD.text_body, OLD.html_body) THEN
IF NOT (
OLD.body_redacted_at IS NULL AND NEW.body_redacted_at IS NOT NULL
AND NEW.text_body = '' AND NEW.html_body = ''
AND NEW.status IN ('accepted', 'rejected', 'cancelled')
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'deliveries_content_immutable',
TABLE = 'deliveries',
MESSAGE = 'delivery content and recipient are immutable';
END IF;
ELSIF OLD.body_redacted_at IS DISTINCT FROM NEW.body_redacted_at THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'deliveries_content_immutable',
TABLE = 'deliveries',
MESSAGE = 'delivery content and recipient are immutable';
END IF;
IF (
(NEW.provider, NEW.provider_account)
IS DISTINCT FROM (OLD.provider, OLD.provider_account)
) AND (OLD.attempt_count <> 0 OR NEW.attempt_count <> 0) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'deliveries_provider_pinned',
TABLE = 'deliveries',
MESSAGE = 'the provider account of a delivery is fixed after its first attempt';
END IF;
RETURN NEW;
END;
$$;
-- +goose StatementEnd
-- +goose Down
-- Subjects name records whose rights a later version checks; dropping them
-- would send notices that were not checked. Refuse while any row names one.
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM deliveries WHERE subject_kind IS NOT NULL)
OR EXISTS (SELECT 1 FROM mail_requests WHERE subject_kind IS NOT NULL) THEN
RAISE EXCEPTION USING
ERRCODE = 'feature_not_supported',
MESSAGE = 'notification subjects are stored; cancel or export the notifications that name one before this rollback';
END IF;
END;
$$;
-- +goose StatementEnd
-- The function of migration 00012 without the subject columns.
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION deliveries_refuse_content_change() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF (
NEW.id, NEW.source_event_id, NEW.recipient_user_id,
NEW.recipient_email, NEW.kind, NEW.template_version, NEW.subject,
NEW.content_sha256,
NEW.idempotency_key, NEW.created_at, NEW.email_token_id
) IS DISTINCT FROM (
OLD.id, OLD.source_event_id, OLD.recipient_user_id,
OLD.recipient_email, OLD.kind, OLD.template_version, OLD.subject,
OLD.content_sha256,
OLD.idempotency_key, OLD.created_at, OLD.email_token_id
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'deliveries_content_immutable',
TABLE = 'deliveries',
MESSAGE = 'delivery content and recipient are immutable';
END IF;
IF (NEW.text_body, NEW.html_body) IS DISTINCT FROM (OLD.text_body, OLD.html_body) THEN
IF NOT (
OLD.body_redacted_at IS NULL AND NEW.body_redacted_at IS NOT NULL
AND NEW.text_body = '' AND NEW.html_body = ''
AND NEW.status IN ('accepted', 'rejected', 'cancelled')
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'deliveries_content_immutable',
TABLE = 'deliveries',
MESSAGE = 'delivery content and recipient are immutable';
END IF;
ELSIF OLD.body_redacted_at IS DISTINCT FROM NEW.body_redacted_at THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'deliveries_content_immutable',
TABLE = 'deliveries',
MESSAGE = 'delivery content and recipient are immutable';
END IF;
IF (
(NEW.provider, NEW.provider_account)
IS DISTINCT FROM (OLD.provider, OLD.provider_account)
) AND (OLD.attempt_count <> 0 OR NEW.attempt_count <> 0) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'deliveries_provider_pinned',
TABLE = 'deliveries',
MESSAGE = 'the provider account of a delivery is fixed after its first attempt';
END IF;
RETURN NEW;
END;
$$;
-- +goose StatementEnd
ALTER TABLE deliveries DROP CONSTRAINT deliveries_subject_complete;
ALTER TABLE deliveries
DROP COLUMN subject_organization_id,
DROP COLUMN subject_id,
DROP COLUMN subject_kind;
ALTER TABLE mail_requests DROP CONSTRAINT mail_requests_subject_complete;
ALTER TABLE mail_requests
DROP COLUMN subject_organization_id,
DROP COLUMN subject_id,
DROP COLUMN subject_kind;
@@ -0,0 +1,111 @@
-- +goose Up
-- Product notifications get their own sending worker. It claims them through
-- deliveries_product_due_idx, re-checks the rights of their subject before
-- the transport is called, and cancels a notice whose basis no longer holds
-- with one of the reasons below. A cancelled notification loses its bodies,
-- as a token mail does, so a withdrawn notice keeps no text in the table.
-- The service worker and deliveries_due_idx do not change.
ALTER TABLE deliveries DROP CONSTRAINT deliveries_cancel_reason_matches_status;
ALTER TABLE deliveries ADD CONSTRAINT deliveries_cancel_reason_matches_status CHECK (
(status = 'cancelled') = (cancel_reason IS NOT NULL)
AND (
cancel_reason IS NULL
OR cancel_reason IN (
'token_missing',
'token_purpose_mismatch',
'token_owner_mismatch',
'token_consumed',
'token_revoked',
'token_expired',
'account_not_current',
'recipient_mismatch',
'content_invalid',
'processing_failed',
'rights_revoked',
'recipient_ineligible',
'source_closed'
)
)
);
-- A redacted mail has no bodies. A token mail may be redacted once it is
-- terminal; a notification once it is cancelled.
ALTER TABLE deliveries DROP CONSTRAINT deliveries_body_present;
ALTER TABLE deliveries ADD CONSTRAINT deliveries_body_present CHECK (
(body_redacted_at IS NULL AND (text_body <> '' OR html_body <> ''))
OR (
body_redacted_at IS NOT NULL
AND text_body = '' AND html_body = ''
AND (
(
kind IN ('verification', 'password_reset')
AND status IN ('accepted', 'rejected', 'cancelled')
)
OR (kind = 'notification' AND status = 'cancelled')
)
)
);
-- Serves the claim of due product notifications. The predicate names the
-- kind literal the product claim repeats, so an idle poll of either worker
-- reads only its own index and never the other lane's backlog.
CREATE INDEX deliveries_product_due_idx
ON deliveries (next_attempt_at, id)
WHERE
status IN ('pending', 'waiting_quota', 'unknown')
AND next_attempt_at IS NOT NULL
AND kind = 'notification';
-- +goose Down
DROP INDEX deliveries_product_due_idx;
-- Rows that name the reasons of this version, or redacted notifications,
-- cannot satisfy the checks of the earlier schema. Refuse before changing
-- them, so the rollback never rewrites a cancellation.
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM deliveries
WHERE cancel_reason IN ('rights_revoked', 'recipient_ineligible', 'source_closed')
OR (kind = 'notification' AND body_redacted_at IS NOT NULL)
) THEN
RAISE EXCEPTION USING
ERRCODE = 'feature_not_supported',
MESSAGE = 'notifications cancelled by a rights check are stored; export or resend them before this rollback';
END IF;
END;
$$;
-- +goose StatementEnd
ALTER TABLE deliveries DROP CONSTRAINT deliveries_body_present;
ALTER TABLE deliveries ADD CONSTRAINT deliveries_body_present CHECK (
(body_redacted_at IS NULL AND (text_body <> '' OR html_body <> ''))
OR (
body_redacted_at IS NOT NULL
AND text_body = '' AND html_body = ''
AND kind IN ('verification', 'password_reset')
AND status IN ('accepted', 'rejected', 'cancelled')
)
);
ALTER TABLE deliveries DROP CONSTRAINT deliveries_cancel_reason_matches_status;
ALTER TABLE deliveries ADD CONSTRAINT deliveries_cancel_reason_matches_status CHECK (
(status = 'cancelled') = (cancel_reason IS NOT NULL)
AND (
cancel_reason IS NULL
OR cancel_reason IN (
'token_missing',
'token_purpose_mismatch',
'token_owner_mismatch',
'token_consumed',
'token_revoked',
'token_expired',
'account_not_current',
'recipient_mismatch',
'content_invalid',
'processing_failed'
)
)
);