Repository navigation
fix(oauth): return copies from the cacheable installation stores - #2091
Conversation
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 <svc-devxp-claude@slack-corp.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2091 +/- ##
==========================================
- Coverage 84.97% 84.95% -0.03%
==========================================
Files 118 118
Lines 14232 14287 +55
==========================================
+ Hits 12094 12137 +43
- Misses 2138 2150 +12 ☔ View full report in Codecov by Harness. |
mwbrooks
left a comment
There was a problem hiding this comment.
✅ Thanks @WilliamBergamin, copying on both the miss and the hit means Bolt clearing user_token for a non-installer can't wipe it for everyone else, and the tests that fail on main prove it.
| duplicate = copy(value) | ||
| for name, field in vars(value).items(): | ||
| if isinstance(field, (list, dict)): | ||
| setattr(duplicate, name, copy(field)) |
There was a problem hiding this comment.
suggestion (non-blocking): A nested value inside custom_values (a dict of settings) is still shared with the cache, since this copies one level deep. Could we deepcopy just custom_values? It is usually small and that would close the last gap, here and in the async store.
zimeg
left a comment
There was a problem hiding this comment.
🐍 @WilliamBergamin LGTM! Copying on both the miss and the hit keeps one caller's changes out of every later lookup. Thanks for tracking this down 🔐
WilliamBergamin
left a comment
There was a problem hiding this comment.
🧪 Deep review pass: the miss and hit tests pin exactly the user_token wipe Bolt's authorize caused, and they fail on main.
Left a few comments, nothing blocking 💯
| from slack_sdk.oauth.installation_store import Bot, Installation | ||
|
|
||
|
|
||
| Model = TypeVar("Model", Bot, Installation) |
There was a problem hiding this comment.
🏷️ suggestion (non-blocking): Could this be _Model? Without the underscore it's an importable name, and dropping it later would count as removing public API.
| Model = TypeVar("Model", Bot, Installation) | |
| _Model = TypeVar("_Model", Bot, Installation) |
| ) | ||
|
|
||
|
|
||
| Model = TypeVar("Model", Bot, Installation) |
There was a problem hiding this comment.
🏷️ suggestion (non-blocking): Same here.
| Model = TypeVar("Model", Bot, Installation) | |
| _Model = TypeVar("_Model", Bot, Installation) |
| return self.underlying.logger | ||
|
|
||
| @staticmethod | ||
| def _copy(value: Model) -> Model: |
There was a problem hiding this comment.
🪓 suggestion (non-blocking): _copy and Model now live in both stores. Could they live once in installation_store/internals.py and be imported by both?
| duplicate = copy(value) | ||
| for name, field in vars(value).items(): | ||
| if isinstance(field, (list, dict)): | ||
| setattr(duplicate, name, copy(field)) |
There was a problem hiding this comment.
🤔 question (non-blocking): A nested value in custom_values (like custom_values["k"]["x"]) is still shared with the cache. Is that worth deepcopy, or is shallow enough since a custom value might not be copyable?
There was a problem hiding this comment.
Good point, a deepcopy that raises on an uncopyable custom value would break the lookup itself, which is worse than a shared nested value. Shallow works for me, so let's drop my suggestion 👍
| self.assertEqual(i.bot_token, "xoxb-refreshed") | ||
| self.assertIn("E111-T1111-U111", store.cached_installations) | ||
|
|
||
| def test_find_installation_returns_a_copy(self): |
There was a problem hiding this comment.
💯 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.
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 <svc-devxp-claude@slack-corp.com>
Summary
This pull request makes the cacheable installation stores (sync and async) return a copy of each cached bot or installation, so a caller that changes the result no longer changes the cache.
find_bot()andfind_installation()returned the cached object itself. Bolt'sauthorize.pysetsuser_token = Noneon the installation when the requester isn't the installer, which wiped the installer's user token from the cache for every later lookup._copy_model()ininstallation_store/internals.py.custom_valuescome back as their own copies, at about a quarter ofdeepcopy's cost. A nested value insidecustom_valuesis still shared: custom values are app-defined and may not be copyable, and a deep copy would run on every cached lookup.Testing
The new sync and async tests fail on
mainand pass here. Each one changes the result of a cache miss and a cache hit (an attribute, a scope list, a custom value), then checks that the next lookup is unchanged.To check by hand, wrap a
FileInstallationStoreinCacheableInstallationStoreand save an installation with a user token. Callfind_installation(), setuser_token = Noneon the result, then callfind_installation()again: the user token is still there.Category
/docs(Documents)/tutorial(PythOnBoardingBot tutorial)tests/integration_tests(Automated tests for this library)Requirements
python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.shafter making the changes.🤖 Generated with Claude Code