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..4de221313 100644 --- a/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py +++ b/slack_sdk/oauth/installation_store/async_cacheable_installation_store.py @@ -2,6 +2,7 @@ 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, ) @@ -52,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.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] = bot + self.cached_bots[key] = _copy_model(bot) return bot async def async_find_installation( @@ -74,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.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, @@ -82,7 +83,7 @@ async def async_find_installation( is_enterprise_install=is_enterprise_install, ) if installation: - self.cached_installations[key] = 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 9cc64ca36..adea8b8ac 100644 --- a/slack_sdk/oauth/installation_store/cacheable_installation_store.py +++ b/slack_sdk/oauth/installation_store/cacheable_installation_store.py @@ -3,6 +3,7 @@ from slack_sdk.oauth import InstallationStore from slack_sdk.oauth.installation_store import Bot, Installation +from slack_sdk.oauth.installation_store.internals import _copy_model class CacheableInstallationStore(InstallationStore): @@ -50,14 +51,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 _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] = bot + self.cached_bots[key] = _copy_model(bot) return bot def find_installation( @@ -72,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.cached_installations[key] + return _copy_model(self.cached_installations[key]) installation = self.underlying.find_installation( enterprise_id=enterprise_id, team_id=team_id, @@ -80,7 +81,7 @@ def find_installation( is_enterprise_install=is_enterprise_install, ) if installation: - self.cached_installations[key] = 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 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"])