Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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(
Expand All @@ -74,15 +75,15 @@ 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,
user_id=user_id,
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand All @@ -72,15 +73,15 @@ 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,
user_id=user_id,
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(
Expand Down
12 changes: 12 additions & 0 deletions slack_sdk/oauth/installation_store/internals.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import copy
from datetime import datetime, timezone
from typing import Type, TypeVar, Union

Expand Down Expand Up @@ -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
39 changes: 38 additions & 1 deletion tests/slack_sdk/oauth/installation_store/test_simple_cache.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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",
Expand All @@ -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,
)


Expand Down Expand Up @@ -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):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 praise: Replaying Bolt's user_token = None on both the miss and the hit, plus the scope list and custom value edits, pins every way the cache could leak.

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"])
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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",
Expand All @@ -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,
)


Expand Down Expand Up @@ -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"])
Loading