From fdfb97f505ceb91583909885bc5652fadcf7b120 Mon Sep 17 00:00:00 2001 From: William Bergamin Date: Mon, 30 Nov 2026 10:39:24 -0500 Subject: [PATCH 1/2] fix(oauth): evict cached installations for a workspace on save_bot save_bot() and async_save_bot() evicted only the cached bot, so cached per-user installations kept serving the old bot token after a bot-only token rotation. Evict the workspace's installation prefix, the same way save() does since #2051. Fixes #2089 Co-Authored-By: Claude --- .../async_cacheable_installation_store.py | 4 ++++ .../cacheable_installation_store.py | 4 ++++ .../oauth/installation_store/test_simple_cache.py | 13 +++++++++++++ .../oauth/installation_store/test_simple_cache.py | 14 ++++++++++++++ 4 files changed, 35 insertions(+) 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 aa441fc1b..5f37b6cf1 100644 --- a/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py +++ b/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py @@ -42,6 +42,10 @@ async def async_save_bot(self, bot: Bot): key = f"{bot.enterprise_id or ''}-{bot.team_id or ''}" if key in self.cached_bots: self.cached_bots.pop(key) + key_prefix = f"{bot.enterprise_id or ''}-{bot.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_bot(bot) async def async_find_bot( diff --git a/slack_sdk/oauth/installation_store/cacheable_installation_store.py b/slack_sdk/oauth/installation_store/cacheable_installation_store.py index 57e1c4938..7c82e6558 100644 --- a/slack_sdk/oauth/installation_store/cacheable_installation_store.py +++ b/slack_sdk/oauth/installation_store/cacheable_installation_store.py @@ -41,6 +41,10 @@ def save_bot(self, bot: Bot): key = f"{bot.enterprise_id or ''}-{bot.team_id or ''}" if key in self.cached_bots: self.cached_bots.pop(key) + key_prefix = f"{bot.enterprise_id or ''}-{bot.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_bot(bot) def find_bot( 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 712c59947..54d3db35d 100644 --- a/tests/slack_sdk/oauth/installation_store/test_simple_cache.py +++ b/tests/slack_sdk/oauth/installation_store/test_simple_cache.py @@ -170,3 +170,16 @@ def test_save_evicts_stale_installations(self): 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") + + def test_save_bot_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", user_id="U111") + + store.save_bot(build_installation(user_id="U111", bot_token="xoxb-refreshed").to_bot()) + i = store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111") + self.assertEqual(i.bot_token, "xoxb-refreshed") 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 fc95e7bc6..bae055fc6 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 @@ -122,3 +122,17 @@ async def test_save_evicts_stale_installations(self): 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") + + @async_test + async def test_save_bot_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", user_id="U111") + + await store.async_save_bot(build_installation(user_id="U111", bot_token="xoxb-refreshed").to_bot()) + i = await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111") + self.assertEqual(i.bot_token, "xoxb-refreshed") From 03eff62a3472aefc38801fe8868a5070ec40cc0d Mon Sep 17 00:00:00 2001 From: William Bergamin Date: Mon, 30 Nov 2026 11:18:33 -0500 Subject: [PATCH 2/2] refactor(oauth): share team cache eviction between save and save_bot save() and save_bot() ran the same eviction block in both cacheable stores. They now call one _evict_team_cache helper, so a future key fix lands once. The save_bot tests also cache a second team, T1111, and assert it survives. T1111 is used over T222 because its key starts with "E111-T111", so the assertion fails if the prefix loses its trailing "-". Co-Authored-By: Claude --- .../async_cacheable_installation_store.py | 23 +++++++------------ .../cacheable_installation_store.py | 22 ++++++------------ .../installation_store/test_simple_cache.py | 7 ++++-- .../installation_store/test_simple_cache.py | 7 ++++-- 4 files changed, 25 insertions(+), 34 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 5f37b6cf1..fb7a60dfb 100644 --- a/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py +++ b/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py @@ -26,26 +26,19 @@ def __init__(self, installation_store: AsyncInstallationStore): def logger(self) -> Logger: return self.underlying.logger - async def async_save(self, installation: Installation): - # Invalidate cache data for update operations - key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}" - if key in self.cached_bots: - self.cached_bots.pop(key) - key_prefix = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-" + def _evict_team_cache(self, enterprise_id: Optional[str], team_id: Optional[str]) -> None: + team_key = f"{enterprise_id or ''}-{team_id or ''}" + self.cached_bots.pop(team_key, None) for key in list(self.cached_installations.keys()): - if key.startswith(key_prefix): + if key.startswith(f"{team_key}-"): self.cached_installations.pop(key) + + async def async_save(self, installation: Installation): + self._evict_team_cache(installation.enterprise_id, installation.team_id) return await self.underlying.async_save(installation) async def async_save_bot(self, bot: Bot): - # Invalidate cache data for update operations - key = f"{bot.enterprise_id or ''}-{bot.team_id or ''}" - if key in self.cached_bots: - self.cached_bots.pop(key) - key_prefix = f"{bot.enterprise_id or ''}-{bot.team_id or ''}-" - for key in list(self.cached_installations.keys()): - if key.startswith(key_prefix): - self.cached_installations.pop(key) + self._evict_team_cache(bot.enterprise_id, bot.team_id) return await self.underlying.async_save_bot(bot) async def async_find_bot( diff --git a/slack_sdk/oauth/installation_store/cacheable_installation_store.py b/slack_sdk/oauth/installation_store/cacheable_installation_store.py index 7c82e6558..9cc64ca36 100644 --- a/slack_sdk/oauth/installation_store/cacheable_installation_store.py +++ b/slack_sdk/oauth/installation_store/cacheable_installation_store.py @@ -24,27 +24,19 @@ def __init__(self, installation_store: InstallationStore): def logger(self) -> Logger: return self.underlying.logger - def save(self, installation: Installation): - # Invalidate cache data for update operations - key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}" - if key in self.cached_bots: - self.cached_bots.pop(key) - key_prefix = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-" + def _evict_team_cache(self, enterprise_id: Optional[str], team_id: Optional[str]) -> None: + team_key = f"{enterprise_id or ''}-{team_id or ''}" + self.cached_bots.pop(team_key, None) for key in list(self.cached_installations.keys()): - if key.startswith(key_prefix): + if key.startswith(f"{team_key}-"): self.cached_installations.pop(key) + def save(self, installation: Installation): + self._evict_team_cache(installation.enterprise_id, installation.team_id) return self.underlying.save(installation) def save_bot(self, bot: Bot): - # Invalidate cache data for update operations - key = f"{bot.enterprise_id or ''}-{bot.team_id or ''}" - if key in self.cached_bots: - self.cached_bots.pop(key) - key_prefix = f"{bot.enterprise_id or ''}-{bot.team_id or ''}-" - for key in list(self.cached_installations.keys()): - if key.startswith(key_prefix): - self.cached_installations.pop(key) + self._evict_team_cache(bot.enterprise_id, bot.team_id) return self.underlying.save_bot(bot) def find_bot( 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 54d3db35d..626f8a941 100644 --- a/tests/slack_sdk/oauth/installation_store/test_simple_cache.py +++ b/tests/slack_sdk/oauth/installation_store/test_simple_cache.py @@ -10,11 +10,11 @@ from slack_sdk.oauth.installation_store.sqlite3 import SQLite3InstallationStore -def build_installation(*, user_id: str, bot_token: str) -> Installation: +def build_installation(*, user_id: str, bot_token: str, team_id: str = "T111") -> Installation: return Installation( app_id="A111", enterprise_id="E111", - team_id="T111", + team_id=team_id, user_id=user_id, bot_id="B111", bot_token=bot_token, @@ -179,7 +179,10 @@ def test_save_bot_evicts_stale_installations(self): store.save(build_installation(user_id="U111", bot_token="xoxb-initial")) store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111") + store.save(build_installation(user_id="U111", bot_token="xoxb-other-team", team_id="T1111")) + store.find_installation(enterprise_id="E111", team_id="T1111", user_id="U111") store.save_bot(build_installation(user_id="U111", bot_token="xoxb-refreshed").to_bot()) i = store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111") self.assertEqual(i.bot_token, "xoxb-refreshed") + self.assertIn("E111-T1111-U111", store.cached_installations) 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 bae055fc6..9fa462ec1 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 @@ -11,11 +11,11 @@ from tests.helpers import async_test -def build_installation(*, user_id: str, bot_token: str) -> Installation: +def build_installation(*, user_id: str, bot_token: str, team_id: str = "T111") -> Installation: return Installation( app_id="A111", enterprise_id="E111", - team_id="T111", + team_id=team_id, user_id=user_id, bot_id="B111", bot_token=bot_token, @@ -132,7 +132,10 @@ async def test_save_bot_evicts_stale_installations(self): await store.async_save(build_installation(user_id="U111", bot_token="xoxb-initial")) 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-other-team", team_id="T1111")) + await store.async_find_installation(enterprise_id="E111", team_id="T1111", user_id="U111") await store.async_save_bot(build_installation(user_id="U111", bot_token="xoxb-refreshed").to_bot()) i = await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111") self.assertEqual(i.bot_token, "xoxb-refreshed") + self.assertIn("E111-T1111-U111", store.cached_installations)