From 1ba14961d39e4e75a1c1593a3e4f727ae70bc899 Mon Sep 17 00:00:00 2001 From: William Bergamin Date: Tue, 1 Dec 2026 11:01:56 -0500 Subject: [PATCH 1/2] fix(oauth): return copies from the cacheable installation stores The cacheable stores handed out the cached Bot/Installation itself, so a caller that changed the result changed the cache. Bolt's authorize clears user_token on the installation when the requester isn't the installer, which wiped the installer's user token for every later lookup. Both stores now cache a copy on a miss and return a copy on a hit. The copy is shallow with fresh list and dict attributes, so scope lists and custom_values are independent too. Co-Authored-By: Claude --- .../async_cacheable_installation_store.py | 22 +++++++--- .../cacheable_installation_store.py | 22 +++++++--- .../installation_store/test_simple_cache.py | 39 +++++++++++++++++- .../installation_store/test_simple_cache.py | 41 ++++++++++++++++++- 4 files changed, 112 insertions(+), 12 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 fb7a60dfb..679eba1ee 100644 --- a/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py +++ b/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py @@ -1,5 +1,6 @@ +from copy import copy from logging import Logger -from typing import Optional, Dict +from typing import Optional, Dict, TypeVar from slack_sdk.oauth.installation_store import Bot, Installation from slack_sdk.oauth.installation_store.async_installation_store import ( @@ -7,6 +8,9 @@ ) +Model = TypeVar("Model", Bot, Installation) + + class AsyncCacheableInstallationStore(AsyncInstallationStore): underlying: AsyncInstallationStore cached_bots: Dict[str, Bot] @@ -26,6 +30,14 @@ def __init__(self, installation_store: AsyncInstallationStore): def logger(self) -> Logger: return self.underlying.logger + @staticmethod + def _copy(value: Model) -> Model: + duplicate = copy(value) + for name, field in vars(value).items(): + if isinstance(field, (list, dict)): + setattr(duplicate, name, copy(field)) + return duplicate + 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) @@ -52,14 +64,14 @@ async def async_find_bot( team_id = "" key = f"{enterprise_id or ''}-{team_id or ''}" if key in self.cached_bots: - return self.cached_bots[key] + return self._copy(self.cached_bots[key]) bot = await self.underlying.async_find_bot( enterprise_id=enterprise_id, team_id=team_id, is_enterprise_install=is_enterprise_install, ) if bot: - self.cached_bots[key] = bot + self.cached_bots[key] = self._copy(bot) return bot async def async_find_installation( @@ -74,7 +86,7 @@ async def async_find_installation( team_id = "" key = f"{enterprise_id or ''}-{team_id or ''}-{user_id or ''}" if key in self.cached_installations: - return self.cached_installations[key] + return self._copy(self.cached_installations[key]) installation = await self.underlying.async_find_installation( enterprise_id=enterprise_id, team_id=team_id, @@ -82,7 +94,7 @@ async def async_find_installation( is_enterprise_install=is_enterprise_install, ) if installation: - self.cached_installations[key] = installation + self.cached_installations[key] = self._copy(installation) return installation async def async_delete_bot( diff --git a/slack_sdk/oauth/installation_store/cacheable_installation_store.py b/slack_sdk/oauth/installation_store/cacheable_installation_store.py index 9cc64ca36..a9ede4599 100644 --- a/slack_sdk/oauth/installation_store/cacheable_installation_store.py +++ b/slack_sdk/oauth/installation_store/cacheable_installation_store.py @@ -1,10 +1,14 @@ +from copy import copy from logging import Logger -from typing import Optional, Dict +from typing import Optional, Dict, TypeVar from slack_sdk.oauth import InstallationStore from slack_sdk.oauth.installation_store import Bot, Installation +Model = TypeVar("Model", Bot, Installation) + + class CacheableInstallationStore(InstallationStore): underlying: InstallationStore cached_bots: Dict[str, Bot] @@ -24,6 +28,14 @@ def __init__(self, installation_store: InstallationStore): def logger(self) -> Logger: return self.underlying.logger + @staticmethod + def _copy(value: Model) -> Model: + duplicate = copy(value) + for name, field in vars(value).items(): + if isinstance(field, (list, dict)): + setattr(duplicate, name, copy(field)) + return duplicate + 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) @@ -50,14 +62,14 @@ def find_bot( team_id = "" key = f"{enterprise_id or ''}-{team_id or ''}" if key in self.cached_bots: - return self.cached_bots[key] + return self._copy(self.cached_bots[key]) bot = self.underlying.find_bot( enterprise_id=enterprise_id, team_id=team_id, is_enterprise_install=is_enterprise_install, ) if bot: - self.cached_bots[key] = bot + self.cached_bots[key] = self._copy(bot) return bot def find_installation( @@ -72,7 +84,7 @@ def find_installation( team_id = "" key = f"{enterprise_id or ''}-{team_id or ''}-{user_id or ''}" if key in self.cached_installations: - return self.cached_installations[key] + return self._copy(self.cached_installations[key]) installation = self.underlying.find_installation( enterprise_id=enterprise_id, team_id=team_id, @@ -80,7 +92,7 @@ def find_installation( is_enterprise_install=is_enterprise_install, ) if installation: - self.cached_installations[key] = installation + self.cached_installations[key] = self._copy(installation) return installation def delete_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 626f8a941..793e7fa7a 100644 --- a/tests/slack_sdk/oauth/installation_store/test_simple_cache.py +++ b/tests/slack_sdk/oauth/installation_store/test_simple_cache.py @@ -1,6 +1,7 @@ import os import tempfile import unittest +from typing import Optional from slack_sdk.oauth.installation_store import Installation from slack_sdk.oauth.installation_store.cacheable_installation_store import ( @@ -10,7 +11,9 @@ from slack_sdk.oauth.installation_store.sqlite3 import SQLite3InstallationStore -def build_installation(*, user_id: str, bot_token: str, team_id: str = "T111") -> Installation: +def build_installation( + *, user_id: str, bot_token: str, team_id: str = "T111", user_token: Optional[str] = None +) -> Installation: return Installation( app_id="A111", enterprise_id="E111", @@ -20,6 +23,7 @@ def build_installation(*, user_id: str, bot_token: str, team_id: str = "T111") - bot_token=bot_token, bot_scopes=["chat:write"], bot_user_id="UB111", + user_token=user_token, ) @@ -186,3 +190,36 @@ def test_save_bot_evicts_stale_installations(self): 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) + + def test_find_installation_returns_a_copy(self): + base_dir = tempfile.TemporaryDirectory() + self.addCleanup(base_dir.cleanup) + store = CacheableInstallationStore(FileInstallationStore(base_dir=base_dir.name, client_id="111.222")) + store.save(build_installation(user_id="U111", bot_token="xoxb-initial", user_token="xoxp-initial")) + + first = store.find_installation(enterprise_id="E111", team_id="T111") + first.user_token = None + second = store.find_installation(enterprise_id="E111", team_id="T111") + second.user_scopes = None + second.bot_scopes.append("channels:read") + second.set_custom_value("k", "changed") + + third = store.find_installation(enterprise_id="E111", team_id="T111") + self.assertEqual(third.user_token, "xoxp-initial") + self.assertEqual(third.bot_scopes, ["chat:write"]) + self.assertEqual(third.custom_values, {}) + + def test_find_bot_returns_a_copy(self): + base_dir = tempfile.TemporaryDirectory() + self.addCleanup(base_dir.cleanup) + store = CacheableInstallationStore(FileInstallationStore(base_dir=base_dir.name, client_id="111.222")) + store.save(build_installation(user_id="U111", bot_token="xoxb-initial")) + + first = store.find_bot(enterprise_id="E111", team_id="T111") + first.bot_token = None + second = store.find_bot(enterprise_id="E111", team_id="T111") + second.bot_scopes.append("channels:read") + + third = store.find_bot(enterprise_id="E111", team_id="T111") + self.assertEqual(third.bot_token, "xoxb-initial") + self.assertEqual(third.bot_scopes, ["chat:write"]) 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 9fa462ec1..a389f24d4 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,6 +1,7 @@ import os import tempfile import unittest +from typing import Optional from slack_sdk.oauth.installation_store import Installation from slack_sdk.oauth.installation_store.async_cacheable_installation_store import ( @@ -11,7 +12,9 @@ from tests.helpers import async_test -def build_installation(*, user_id: str, bot_token: str, team_id: str = "T111") -> Installation: +def build_installation( + *, user_id: str, bot_token: str, team_id: str = "T111", user_token: Optional[str] = None +) -> Installation: return Installation( app_id="A111", enterprise_id="E111", @@ -21,6 +24,7 @@ def build_installation(*, user_id: str, bot_token: str, team_id: str = "T111") - bot_token=bot_token, bot_scopes=["chat:write"], bot_user_id="UB111", + user_token=user_token, ) @@ -139,3 +143,38 @@ async def test_save_bot_evicts_stale_installations(self): 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) + + @async_test + async def test_find_installation_returns_a_copy(self): + base_dir = tempfile.TemporaryDirectory() + self.addCleanup(base_dir.cleanup) + store = AsyncCacheableInstallationStore(FileInstallationStore(base_dir=base_dir.name, client_id="111.222")) + await store.async_save(build_installation(user_id="U111", bot_token="xoxb-initial", user_token="xoxp-initial")) + + first = await store.async_find_installation(enterprise_id="E111", team_id="T111") + first.user_token = None + second = await store.async_find_installation(enterprise_id="E111", team_id="T111") + second.user_scopes = None + second.bot_scopes.append("channels:read") + second.set_custom_value("k", "changed") + + third = await store.async_find_installation(enterprise_id="E111", team_id="T111") + self.assertEqual(third.user_token, "xoxp-initial") + self.assertEqual(third.bot_scopes, ["chat:write"]) + self.assertEqual(third.custom_values, {}) + + @async_test + async def test_find_bot_returns_a_copy(self): + base_dir = tempfile.TemporaryDirectory() + self.addCleanup(base_dir.cleanup) + store = AsyncCacheableInstallationStore(FileInstallationStore(base_dir=base_dir.name, client_id="111.222")) + await store.async_save(build_installation(user_id="U111", bot_token="xoxb-initial")) + + first = await store.async_find_bot(enterprise_id="E111", team_id="T111") + first.bot_token = None + second = await store.async_find_bot(enterprise_id="E111", team_id="T111") + second.bot_scopes.append("channels:read") + + third = await store.async_find_bot(enterprise_id="E111", team_id="T111") + self.assertEqual(third.bot_token, "xoxb-initial") + self.assertEqual(third.bot_scopes, ["chat:write"]) From bd357308e2a4bbb7276671c7172d4800cfd81fdd Mon Sep 17 00:00:00 2001 From: William Bergamin Date: Tue, 1 Dec 2026 11:47:43 -0500 Subject: [PATCH 2/2] refactor(oauth): share the cache copy helper from internals Both cacheable stores defined the same _copy staticmethod and a public Model TypeVar. They now import one private _copy_model from installation_store/internals.py. The TypeVar is unconstrained because internals.py cannot import the models without an import cycle. Co-Authored-By: Claude --- .../async_cacheable_installation_store.py | 23 +++++-------------- .../cacheable_installation_store.py | 23 +++++-------------- .../oauth/installation_store/internals.py | 12 ++++++++++ 3 files changed, 24 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 679eba1ee..4de221313 100644 --- a/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py +++ b/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py @@ -1,16 +1,13 @@ -from copy import copy from logging import Logger -from typing import Optional, Dict, TypeVar +from typing import Optional, Dict from slack_sdk.oauth.installation_store import Bot, Installation +from slack_sdk.oauth.installation_store.internals import _copy_model from slack_sdk.oauth.installation_store.async_installation_store import ( AsyncInstallationStore, ) -Model = TypeVar("Model", Bot, Installation) - - class AsyncCacheableInstallationStore(AsyncInstallationStore): underlying: AsyncInstallationStore cached_bots: Dict[str, Bot] @@ -30,14 +27,6 @@ def __init__(self, installation_store: AsyncInstallationStore): def logger(self) -> Logger: return self.underlying.logger - @staticmethod - def _copy(value: Model) -> Model: - duplicate = copy(value) - for name, field in vars(value).items(): - if isinstance(field, (list, dict)): - setattr(duplicate, name, copy(field)) - return duplicate - 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) @@ -64,14 +53,14 @@ async def async_find_bot( team_id = "" key = f"{enterprise_id or ''}-{team_id or ''}" if key in self.cached_bots: - return self._copy(self.cached_bots[key]) + return _copy_model(self.cached_bots[key]) bot = await self.underlying.async_find_bot( enterprise_id=enterprise_id, team_id=team_id, is_enterprise_install=is_enterprise_install, ) if bot: - self.cached_bots[key] = self._copy(bot) + self.cached_bots[key] = _copy_model(bot) return bot async def async_find_installation( @@ -86,7 +75,7 @@ async def async_find_installation( team_id = "" key = f"{enterprise_id or ''}-{team_id or ''}-{user_id or ''}" if key in self.cached_installations: - return self._copy(self.cached_installations[key]) + return _copy_model(self.cached_installations[key]) installation = await self.underlying.async_find_installation( enterprise_id=enterprise_id, team_id=team_id, @@ -94,7 +83,7 @@ async def async_find_installation( is_enterprise_install=is_enterprise_install, ) if installation: - self.cached_installations[key] = self._copy(installation) + self.cached_installations[key] = _copy_model(installation) return installation async def async_delete_bot( diff --git a/slack_sdk/oauth/installation_store/cacheable_installation_store.py b/slack_sdk/oauth/installation_store/cacheable_installation_store.py index a9ede4599..adea8b8ac 100644 --- a/slack_sdk/oauth/installation_store/cacheable_installation_store.py +++ b/slack_sdk/oauth/installation_store/cacheable_installation_store.py @@ -1,12 +1,9 @@ -from copy import copy from logging import Logger -from typing import Optional, Dict, TypeVar +from typing import Optional, Dict from slack_sdk.oauth import InstallationStore from slack_sdk.oauth.installation_store import Bot, Installation - - -Model = TypeVar("Model", Bot, Installation) +from slack_sdk.oauth.installation_store.internals import _copy_model class CacheableInstallationStore(InstallationStore): @@ -28,14 +25,6 @@ def __init__(self, installation_store: InstallationStore): def logger(self) -> Logger: return self.underlying.logger - @staticmethod - def _copy(value: Model) -> Model: - duplicate = copy(value) - for name, field in vars(value).items(): - if isinstance(field, (list, dict)): - setattr(duplicate, name, copy(field)) - return duplicate - 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) @@ -62,14 +51,14 @@ def find_bot( team_id = "" key = f"{enterprise_id or ''}-{team_id or ''}" if key in self.cached_bots: - return self._copy(self.cached_bots[key]) + return _copy_model(self.cached_bots[key]) bot = self.underlying.find_bot( enterprise_id=enterprise_id, team_id=team_id, is_enterprise_install=is_enterprise_install, ) if bot: - self.cached_bots[key] = self._copy(bot) + self.cached_bots[key] = _copy_model(bot) return bot def find_installation( @@ -84,7 +73,7 @@ def find_installation( team_id = "" key = f"{enterprise_id or ''}-{team_id or ''}-{user_id or ''}" if key in self.cached_installations: - return self._copy(self.cached_installations[key]) + return _copy_model(self.cached_installations[key]) installation = self.underlying.find_installation( enterprise_id=enterprise_id, team_id=team_id, @@ -92,7 +81,7 @@ def find_installation( is_enterprise_install=is_enterprise_install, ) if installation: - self.cached_installations[key] = self._copy(installation) + self.cached_installations[key] = _copy_model(installation) return installation def delete_bot( diff --git a/slack_sdk/oauth/installation_store/internals.py b/slack_sdk/oauth/installation_store/internals.py index 9fc467f2a..93a7bd92b 100644 --- a/slack_sdk/oauth/installation_store/internals.py +++ b/slack_sdk/oauth/installation_store/internals.py @@ -1,3 +1,4 @@ +from copy import copy from datetime import datetime, timezone from typing import Type, TypeVar, Union @@ -40,3 +41,14 @@ def _timestamp_to_type(ts: Union[TimestampType, datetime, str], target_type: Typ raise ValueError(f"Unsupported data format for timestamp {ts}") return result + + +_Model = TypeVar("_Model") + + +def _copy_model(model: _Model) -> _Model: + duplicate = copy(model) + for name, field in vars(model).items(): + if isinstance(field, (list, dict)): + setattr(duplicate, name, copy(field)) + return duplicate