From c508c7899ee369c53e4c829c0456d6bc036f3d28 Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 22 Jan 2026 11:11:29 +0100 Subject: [PATCH] Fix FK constraint to preserve backchannel logout chain Change compat_sessions.user_session_id FK from ON DELETE SET NULL to ON DELETE NO ACTION. This prevents deletion of user_sessions while compat_sessions still reference them, which is critical for backchannel logout propagation. When an upstream IdP sends a backchannel logout, MAS must trace through: upstream_oauth_authorization_sessions -> user_sessions -> compat_sessions If user_session_id links are SET NULL, logout propagation fails. Uses two-step migration (DROP+ADD NOT VALID, then VALIDATE) to minimize table locking during deployment. --- ...compat_sessions_user_session_no_action.sql | 19 +++++++++++++++++++ ..._compat_sessions_user_session_validate.sql | 9 +++++++++ 2 files changed, 28 insertions(+) create mode 100644 crates/storage-pg/migrations/20260122113523_compat_sessions_user_session_no_action.sql create mode 100644 crates/storage-pg/migrations/20260122114353_compat_sessions_user_session_validate.sql diff --git a/crates/storage-pg/migrations/20260122113523_compat_sessions_user_session_no_action.sql b/crates/storage-pg/migrations/20260122113523_compat_sessions_user_session_no_action.sql new file mode 100644 index 000000000..d86ea2291 --- /dev/null +++ b/crates/storage-pg/migrations/20260122113523_compat_sessions_user_session_no_action.sql @@ -0,0 +1,19 @@ +-- Copyright 2026 Element Creations Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial +-- Please see LICENSE files in the repository root for full details. + +-- Change compat_sessions.user_session_id FK from ON DELETE SET NULL to NO ACTION +-- This ensures user_sessions cannot be deleted while compat_sessions reference them, +-- which is required for backchannel logout propagation to work correctly. +-- +-- Uses NOT VALID to avoid scanning the entire table while holding a lock. +-- A separate migration will validate the constraint. + +ALTER TABLE compat_sessions + DROP CONSTRAINT compat_sessions_user_session_id_fkey, + ADD CONSTRAINT compat_sessions_user_session_id_fkey + FOREIGN KEY (user_session_id) + REFERENCES user_sessions (user_session_id) + ON DELETE NO ACTION + NOT VALID; diff --git a/crates/storage-pg/migrations/20260122114353_compat_sessions_user_session_validate.sql b/crates/storage-pg/migrations/20260122114353_compat_sessions_user_session_validate.sql new file mode 100644 index 000000000..4a5a7f23d --- /dev/null +++ b/crates/storage-pg/migrations/20260122114353_compat_sessions_user_session_validate.sql @@ -0,0 +1,9 @@ +-- Copyright 2026 Element Creations Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial +-- Please see LICENSE files in the repository root for full details. + +-- Validate the constraint added in the previous migration. +-- This scans the table but does not hold an exclusive lock. +ALTER TABLE compat_sessions + VALIDATE CONSTRAINT compat_sessions_user_session_id_fkey;