Skip to content

Commit 2c7a61e

Browse files
authored
Don't cache introspection failures (#18339)
1 parent 45420b1 commit 2c7a61e

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

changelog.d/18339.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Stop caching introspection failures when delegating auth to MAS.

synapse/api/auth/msc3861_delegated.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
from synapse.types import Requester, UserID, create_requester
5050
from synapse.util import json_decoder
5151
from synapse.util.caches.cached_call import RetryOnExceptionCachedCall
52-
from synapse.util.caches.response_cache import ResponseCache
52+
from synapse.util.caches.response_cache import ResponseCache, ResponseCacheContext
5353

5454
if TYPE_CHECKING:
5555
from synapse.rest.admin.experimental_features import ExperimentalFeature
@@ -279,7 +279,9 @@ async def _introspection_endpoint(self) -> str:
279279
metadata = await self._issuer_metadata.get()
280280
return metadata.get("introspection_endpoint")
281281

282-
async def _introspect_token(self, token: str) -> IntrospectionResult:
282+
async def _introspect_token(
283+
self, token: str, cache_context: ResponseCacheContext[str]
284+
) -> IntrospectionResult:
283285
"""
284286
Send a token to the introspection endpoint and returns the introspection response
285287
@@ -295,6 +297,8 @@ async def _introspect_token(self, token: str) -> IntrospectionResult:
295297
Returns:
296298
The introspection response
297299
"""
300+
# By default, we shouldn't cache the result unless we know it's valid
301+
cache_context.should_cache = False
298302
introspection_endpoint = await self._introspection_endpoint()
299303
raw_headers: Dict[str, str] = {
300304
"Content-Type": "application/x-www-form-urlencoded",
@@ -352,6 +356,8 @@ async def _introspect_token(self, token: str) -> IntrospectionResult:
352356
"The introspection endpoint returned an invalid JSON response."
353357
)
354358

359+
# We had a valid response, so we can cache it
360+
cache_context.should_cache = True
355361
return IntrospectionResult(
356362
IntrospectionToken(**resp), retrieved_at_ms=self._clock.time_msec()
357363
)
@@ -482,7 +488,7 @@ async def get_user_by_access_token(
482488

483489
try:
484490
introspection_result = await self._introspection_cache.wrap(
485-
token, self._introspect_token, token
491+
token, self._introspect_token, token, cache_context=True
486492
)
487493
except Exception:
488494
logger.exception("Failed to introspect token")

0 commit comments

Comments
 (0)