From 521a5b613eff5f927a705bfaed50bd8fe03136d5 Mon Sep 17 00:00:00 2001 From: William Bergamin Date: Tue, 17 Nov 2026 12:21:22 -0500 Subject: [PATCH] fix(oauth): evict every cached installation for a workspace on save CacheableInstallationStore.save() only evicted the exact enterprise-team-user key, so the default no-user lookup (cached under enterprise-team-) kept serving the old bot token after a reinstall or token rotation. Underlying stores also merge the latest bot token into per-user lookups, so other users' cached entries went stale too. Evict the whole workspace prefix, as delete_installation() already does. Same fix in the async store. Co-Authored-By: Claude --- .../async_cacheable_installation_store.py | 7 ++-- .../cacheable_installation_store.py | 7 ++-- .../installation_store/test_simple_cache.py | 35 ++++++++++++++++++ .../installation_store/test_simple_cache.py | 36 +++++++++++++++++++ 4 files changed, 79 insertions(+), 6 deletions(-) diff --git a/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py b/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py index 4edc638ef..aa441fc1b 100644 --- a/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py +++ b/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py @@ -31,9 +31,10 @@ async def async_save(self, installation: Installation): key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}" if key in self.cached_bots: self.cached_bots.pop(key) - key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-{installation.user_id or ''}" - if key in self.cached_installations: - self.cached_installations.pop(key) + key_prefix = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-" + for key in list(self.cached_installations.keys()): + if key.startswith(key_prefix): + self.cached_installations.pop(key) return await self.underlying.async_save(installation) async def async_save_bot(self, bot: Bot): diff --git a/slack_sdk/oauth/installation_store/cacheable_installation_store.py b/slack_sdk/oauth/installation_store/cacheable_installation_store.py index 2455779dd..57e1c4938 100644 --- a/slack_sdk/oauth/installation_store/cacheable_installation_store.py +++ b/slack_sdk/oauth/installation_store/cacheable_installation_store.py @@ -29,9 +29,10 @@ def save(self, installation: Installation): key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}" if key in self.cached_bots: self.cached_bots.pop(key) - key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-{installation.user_id or ''}" - if key in self.cached_installations: - self.cached_installations.pop(key) + key_prefix = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-" + for key in list(self.cached_installations.keys()): + if key.startswith(key_prefix): + self.cached_installations.pop(key) return self.underlying.save(installation) diff --git a/tests/slack_sdk/oauth/installation_store/test_simple_cache.py b/tests/slack_sdk/oauth/installation_store/test_simple_cache.py index 2af633430..712c59947 100644 --- a/tests/slack_sdk/oauth/installation_store/test_simple_cache.py +++ b/tests/slack_sdk/oauth/installation_store/test_simple_cache.py @@ -1,13 +1,28 @@ import os +import tempfile import unittest from slack_sdk.oauth.installation_store import Installation from slack_sdk.oauth.installation_store.cacheable_installation_store import ( CacheableInstallationStore, ) +from slack_sdk.oauth.installation_store.file import FileInstallationStore from slack_sdk.oauth.installation_store.sqlite3 import SQLite3InstallationStore +def build_installation(*, user_id: str, bot_token: str) -> Installation: + return Installation( + app_id="A111", + enterprise_id="E111", + team_id="T111", + user_id=user_id, + bot_id="B111", + bot_token=bot_token, + bot_scopes=["chat:write"], + bot_user_id="UB111", + ) + + class TestCacheable(unittest.TestCase): def test_save_and_find(self): sqlite3_store = SQLite3InstallationStore(database="logs/cacheable.db", client_id="111.222") @@ -135,3 +150,23 @@ def test_save_and_find_token_rotation(self): self.assertIsNone(i) bot = store.find_bot(enterprise_id="E111", team_id="T222") self.assertIsNone(bot) + + def test_save_evicts_stale_installations(self): + base_dir = tempfile.TemporaryDirectory() + self.addCleanup(base_dir.cleanup) + file_store = FileInstallationStore(base_dir=base_dir.name, client_id="111.222") + store = CacheableInstallationStore(file_store) + + store.save(build_installation(user_id="U111", bot_token="xoxb-initial")) + store.find_installation(enterprise_id="E111", team_id="T111") + store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111") + + store.save(build_installation(user_id="U111", bot_token="xoxb-refreshed")) + i = store.find_installation(enterprise_id="E111", team_id="T111") + self.assertEqual(i.bot_token, "xoxb-refreshed") + i = store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111") + self.assertEqual(i.bot_token, "xoxb-refreshed") + + store.save(build_installation(user_id="U222", bot_token="xoxb-u222")) + i = store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111") + self.assertEqual(i.bot_token, "xoxb-u222") diff --git a/tests/slack_sdk_async/oauth/installation_store/test_simple_cache.py b/tests/slack_sdk_async/oauth/installation_store/test_simple_cache.py index c1f43f4e4..fc95e7bc6 100644 --- a/tests/slack_sdk_async/oauth/installation_store/test_simple_cache.py +++ b/tests/slack_sdk_async/oauth/installation_store/test_simple_cache.py @@ -1,14 +1,29 @@ import os +import tempfile import unittest from slack_sdk.oauth.installation_store import Installation from slack_sdk.oauth.installation_store.async_cacheable_installation_store import ( AsyncCacheableInstallationStore, ) +from slack_sdk.oauth.installation_store.file import FileInstallationStore from slack_sdk.oauth.installation_store.sqlite3 import SQLite3InstallationStore from tests.helpers import async_test +def build_installation(*, user_id: str, bot_token: str) -> Installation: + return Installation( + app_id="A111", + enterprise_id="E111", + team_id="T111", + user_id=user_id, + bot_id="B111", + bot_token=bot_token, + bot_scopes=["chat:write"], + bot_user_id="UB111", + ) + + class TestCacheable(unittest.TestCase): @async_test async def test_save_and_find(self): @@ -86,3 +101,24 @@ async def test_save_and_find_token_rotation(self): self.assertIsNone(bot) bot = await store.async_find_bot(enterprise_id="E111", team_id="T111") self.assertIsNotNone(bot) + + @async_test + async def test_save_evicts_stale_installations(self): + base_dir = tempfile.TemporaryDirectory() + self.addCleanup(base_dir.cleanup) + file_store = FileInstallationStore(base_dir=base_dir.name, client_id="111.222") + store = AsyncCacheableInstallationStore(file_store) + + await store.async_save(build_installation(user_id="U111", bot_token="xoxb-initial")) + await store.async_find_installation(enterprise_id="E111", team_id="T111") + await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111") + + await store.async_save(build_installation(user_id="U111", bot_token="xoxb-refreshed")) + i = await store.async_find_installation(enterprise_id="E111", team_id="T111") + self.assertEqual(i.bot_token, "xoxb-refreshed") + i = await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111") + self.assertEqual(i.bot_token, "xoxb-refreshed") + + await store.async_save(build_installation(user_id="U222", bot_token="xoxb-u222")) + i = await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111") + self.assertEqual(i.bot_token, "xoxb-u222")