mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-24 07:53:09 +00:00
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:
@@ -416,6 +416,9 @@ export default function PluginFrame({ pluginId, tripId = null, placeId = null, d
|
||||
break
|
||||
}
|
||||
case 'trek:session:clear': {
|
||||
// Same guard the other request/response handlers apply: without an id
|
||||
// the frame could never match our answer to its call.
|
||||
if (typeof msg.requestId !== 'string' || !msg.requestId) break
|
||||
const scope = msg.scope === 'trip' ? 'trip' : 'plugin'
|
||||
|
||||
// tripId required if we are on a trip-scoped key.
|
||||
@@ -437,6 +440,7 @@ export default function PluginFrame({ pluginId, tripId = null, placeId = null, d
|
||||
case 'trek:session:get':
|
||||
case 'trek:session:set':
|
||||
case 'trek:session:remove': {
|
||||
if (typeof msg.requestId !== 'string' || !msg.requestId) break
|
||||
const scope = msg.scope === 'trip' ? 'trip' : 'plugin'
|
||||
if (scope === 'trip' && tripId == null) {
|
||||
post({ type: 'trek:error', requestId: msg.requestId, code: 'NO_TRIP_CONTEXT', message: 'trip session storage requires a trip context' })
|
||||
@@ -552,7 +556,7 @@ export default function PluginFrame({ pluginId, tripId = null, placeId = null, d
|
||||
// The frame is going away (or re-bridging) — never leave a live GPS watch behind.
|
||||
if (geoWatchRef.current != null) { navigator.geolocation.clearWatch(geoWatchRef.current); geoWatchRef.current = null }
|
||||
}
|
||||
}, [pluginId, tripId, fill, navigate, postFrame, buildContext])
|
||||
}, [pluginId, tripId, fill, navigate, postFrame, buildContext, userId])
|
||||
|
||||
// Hosts swap pluginId in place (tab bar, /plugins/:id route) — the iframe below
|
||||
// is keyed so the document is a fresh first load, and the per-plugin bridge
|
||||
|
||||
@@ -10,6 +10,7 @@ import { setAuthed } from '../sync/authGate'
|
||||
import { unregisterSyncTriggers } from '../sync/syncTriggers'
|
||||
import { useSystemNoticeStore } from './systemNoticeStore.js'
|
||||
import { clearAppearanceSnapshot } from '../theme/applyAppearance'
|
||||
import { clearAllPluginSessions } from './pluginStore'
|
||||
|
||||
interface AuthResponse {
|
||||
user: User
|
||||
@@ -195,6 +196,9 @@ export const useAuthStore = create<AuthState>()(
|
||||
// Drop the per-device appearance snapshot so the next user on a shared
|
||||
// browser doesn't get a pre-paint flash of this user's theme.
|
||||
clearAppearanceSnapshot()
|
||||
// Same reason for the brokered plugin session state: it is keyed by user id,
|
||||
// but sessionStorage outlives a logout within the tab.
|
||||
clearAllPluginSessions()
|
||||
// 4. Tell server to clear the httpOnly cookie (best-effort).
|
||||
await fetch('/api/auth/logout', { method: 'POST', credentials: 'include' }).catch(() => {})
|
||||
// 5. Clear service worker caches containing sensitive data.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// FE-STORE-PLUGIN-001 to 004
|
||||
// FE-STORE-PLUGIN-001 to 006
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { server } from '../../tests/helpers/msw/server';
|
||||
import { usePluginStore } from './pluginStore';
|
||||
import { usePluginStore, clearAllPluginSessions } from './pluginStore';
|
||||
|
||||
const initial = usePluginStore.getState();
|
||||
|
||||
@@ -72,4 +72,31 @@ describe('pluginStore', () => {
|
||||
expect(sessionStorage.getItem('trek:plugin-session:7:disabled:trip:42:view')).toBeNull();
|
||||
expect(sessionStorage.getItem('trek_session')).toBe('app-session');
|
||||
});
|
||||
|
||||
it('FE-STORE-PLUGIN-005: clearAllPluginSessions drops every user and scope, but nothing else', () => {
|
||||
sessionStorage.setItem('trek:plugin-session:7:active:plugin:filters', '["flight"]');
|
||||
sessionStorage.setItem('trek:plugin-session:9:active:trip:42:view', '"table"');
|
||||
sessionStorage.setItem('trek_session', 'app-session');
|
||||
|
||||
clearAllPluginSessions();
|
||||
|
||||
expect(sessionStorage.getItem('trek:plugin-session:7:active:plugin:filters')).toBeNull();
|
||||
expect(sessionStorage.getItem('trek:plugin-session:9:active:trip:42:view')).toBeNull();
|
||||
expect(sessionStorage.getItem('trek_session')).toBe('app-session');
|
||||
});
|
||||
|
||||
it('FE-STORE-PLUGIN-006: a sessionStorage failure during the purge still keeps the loaded plugins', async () => {
|
||||
const key = vi.spyOn(Storage.prototype, 'key').mockImplementation(() => { throw new Error('storage disabled'); });
|
||||
server.use(
|
||||
http.get('/api/plugins', () =>
|
||||
HttpResponse.json({ plugins: [{ id: 'active', name: 'Active', type: 'widget', icon: null }] }),
|
||||
),
|
||||
);
|
||||
|
||||
await usePluginStore.getState().loadPlugins();
|
||||
|
||||
expect(usePluginStore.getState().plugins.map((p) => p.id)).toEqual(['active']);
|
||||
expect(usePluginStore.getState().loaded).toBe(true);
|
||||
key.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user