Skip to content

fix: OIDC/SSO Token + Media issues#548

Draft
Just-Insane wants to merge 4 commits intoSableClient:devfrom
Just-Insane:fix/media-error-handling
Draft

fix: OIDC/SSO Token + Media issues#548
Just-Insane wants to merge 4 commits intoSableClient:devfrom
Just-Insane:fix/media-error-handling

Conversation

@Just-Insane
Copy link
Contributor

@Just-Insane Just-Insane commented Mar 26, 2026

Description

Three related bug fixes for media reliability.

1. Suppress unhandled promise rejections from useAsyncCallback

useAsyncCallback stored errors in AsyncStatus.Error state and then re-threw them. Fire-and-forget call sites (e.g. loadSrc() / loadThumbSrc()) had no .catch(), so every failure produced an "Uncaught (in promise)" console error — most visibly "Mismatched SHA-256 digest" from encrypted media decryption. The error is already surfaced via retry UI; the re-throw added nothing. Removed it and updated the test accordingly.

2. Wire OIDC token refresh through to the service worker

loginUtil.ts silently discarded refresh_token from the login response, so createClient was never given a tokenRefreshFunction. After an access token expired the SDK would stall and the service worker — which caches the token for authenticated media requests — would keep sending the old Bearer token, causing 401s on every media load.

  • loginUtil.ts: capture refresh_token / expires_in_ms into the session object
  • initMatrix.ts: when a refreshToken is present, pass tokenRefreshFunction to createClient; the function calls mx.refreshToken() and invokes an optional onTokenRefresh callback
  • ClientRoot.tsx: supply the callback; it writes the new tokens to sessionsAtom and calls pushSessionToSW so the SW picks them up immediately

No-op when the server doesn't return a refresh token.

3. Retry authenticated media requests on 401 in the service worker

There is a timing window between when the SDK resolves a token refresh (and updates its own in-flight requests) and when the resulting pushSessionToSW setSession postMessage is processed by the SW. Media requests that land in this window carry the stale Bearer token and receive 401. Because browsers retry failed image/video loads automatically, this produced repeated 401 bursts for the same media IDs.

Added fetchMediaWithRetry() in sw.ts: on a 401 response it re-checks the in-memory sessions map (and preloadedSession fallback) for a fresher token for the same origin. If one is found, the request is retried once. Applied consistently across all four code paths in the fetch event handler. Zero overhead for non-401 responses.

Fixes #

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings

AI disclosure:

  • Partially AI assisted (clarify which code was AI assisted and briefly explain what it does).
  • Fully AI generated (explain what all the generated code does in moderate detail).

Fix 1 removes throw e from useAsyncCallback's catch block and drops the now-unnecessary .catch(() => {}) in the test. Fix 2 reads refresh_token/expires_in_ms from the login response and stores them in the session; buildClient spreads { refreshToken, tokenRefreshFunction } into createClient options when a refresh token is present; the onTokenRefresh callback in ClientRoot updates sessionsAtom and calls pushSessionToSW with the new access token. Fix 3 adds fetchMediaWithRetry() to sw.ts — a thin wrapper around fetch() that on HTTP 401 re-checks the in-memory sessions map for a token that differs from the one just used, and if found retries the request once; covers the race window between SDK token refresh and SW setSession propagation.

useAsync re-threw errors after storing them in Error state. Any call site
that discards the returned Promise (e.g. fire-and-forget useEffect calls
like loadSrc() or loadThumbSrc()) produced an 'Uncaught (in promise)'
console error — notably 'Mismatched SHA-256 digest' from encrypted media
decryption failures.

The error is already fully handled: it is stored in AsyncStatus.Error state
and components surface it via a retry button. The re-throw added no value
and only caused noise / unhandled rejection warnings.

Update the test accordingly — the .catch(() => {}) guard was only needed to
silence the re-throw and is no longer necessary.
Stores refresh_token from the login response, passes it and a
tokenRefreshFunction to createClient so the SDK can auto-refresh
expired access tokens. The callback propagates new tokens to both
the sessionsAtom (localStorage) and the service worker via
pushSessionToSW, preventing 401 errors on authenticated media
after token expiry.
@Just-Insane Just-Insane force-pushed the fix/media-error-handling branch from 8ecb7cc to 715e9e4 Compare March 26, 2026 03:36
@Just-Insane Just-Insane marked this pull request as ready for review March 26, 2026 03:40
@Just-Insane Just-Insane requested review from 7w1 and hazre as code owners March 26, 2026 03:40
@Just-Insane Just-Insane changed the title fix: suppress unhandled promise rejections and wire OIDC token refresh to service worker fix: OIDC/SSO Token + Media issues Mar 26, 2026
@Just-Insane Just-Insane marked this pull request as draft March 26, 2026 13:17
There is a timing window between when the SDK refreshes its access
token (tokenRefreshFunction resolves and pushSessionToSW is called)
and when the resulting setSession postMessage is processed by the SW.
Media requests that land in this window carry the stale token and
receive 401. The browser then retries those image/video loads, hitting
the SW again with the same stale token — producing the repeated 401
bursts visible in the console.

fetchMediaWithRetry() resolves this by retrying once on 401: it
re-checks the in-memory sessions map (and preloadedSession fallback)
for a different access token. By the time the retry runs, setSession
will normally have been processed and the map will hold the new token.
Applied consistently across all four branches of the fetch handler.
@Just-Insane Just-Insane force-pushed the fix/media-error-handling branch from ae8c97d to c177218 Compare March 26, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant