fix(plugin): clear brokered session state on logout

sessionStorage outlives a logout inside the same tab, so the keys stay readable
after the next user signs in. They are namespaced by user id, which stops the new
user's plugins from reading them, but the data is still sitting in the tab — the
same reason logout already drops the appearance snapshot, the SW caches and the
user-scoped offline DB. Purge them there too.

Three things around it, from reading the handlers next to the new ones:

- The inactive-plugin purge ran before the store was updated, so a sessionStorage
  failure cost us the plugin list we had just fetched. Commit first, purge after.
- The session handlers answered without checking requestId, unlike confirm and
  geolocation — an answer the frame can never match is worth skipping.
- The mock host threw plain Errors while the real bridge rejects with .code set,
  and checked the key before the trip scope where the host does the reverse, so a
  plugin's error handling could pass its tests and still break in the app.
This commit is contained in:
Maurice
2026-07-26 11:39:00 +02:00
committed by Maurice
parent 2068133de2
commit c8369bd14b
6 changed files with 87 additions and 12 deletions
+19 -1
View File
@@ -23,6 +23,20 @@ function clearInactivePluginSessions(activePluginIds: Set<string>) {
keysToRemove.forEach((storageKey) => sessionStorage.removeItem(storageKey))
}
/**
* Drops every plugin's session state, whatever user or trip it belonged to.
* Logout calls this so the next user on a shared browser starts clean — the
* same reason the appearance snapshot and the user-scoped offline DB go.
*/
export function clearAllPluginSessions() {
const keysToRemove: string[] = []
for (let i = 0; i < sessionStorage.length; i += 1) {
const storageKey = sessionStorage.key(i)
if (storageKey?.startsWith(PLUGIN_SESSION_NAMESPACE)) keysToRemove.push(storageKey)
}
keysToRemove.forEach((storageKey) => sessionStorage.removeItem(storageKey))
}
/**
* Active plugins the client renders (#plugins, M3). Page plugins become nav
* entries + a full-page iframe route; widget plugins mount on the dashboard.
@@ -70,8 +84,12 @@ export const usePluginStore = create<PluginState>((set, get) => ({
try {
const data = await pluginsApi.active()
const plugins = (data.plugins as ActivePlugin[]) || []
clearInactivePluginSessions(new Set(plugins.map((plugin) => plugin.id)))
set({ plugins, loaded: true })
// After the state is committed: a sessionStorage failure (Safari private
// mode, quota) must not cost us the plugin list we just fetched.
try {
clearInactivePluginSessions(new Set(plugins.map((plugin) => plugin.id)))
} catch { /* leaving stale plugin state behind beats losing the nav entries */ }
} catch {
set({ loaded: true })
}