fix(security): namespace command-palette recent searches per (user, tenant)
Recent searches were persisted under a single global localStorage
key (weknora_cmdk_recent), so:
- Multiple users sharing the same browser saw each other's
queries.
- Switching tenants inside the same session also leaked queries
across tenants.
Keys are now scoped as 'weknora_cmdk_recent:<userId>:<tenantId>'
with 'anon' / 'none' fallbacks for the pre-login window. A watch
on (user.id, effectiveTenantId) reloads from the right namespace
on login / logout / tenant switch. The auth store is resolved
lazily inside actions / watcher (not at setup-time) to dodge any
pinia init-order surprises — same pattern stores/menu.ts already
uses.
Old global key entries become orphans (~100 bytes); no migration
since that data is the leak we're fixing.
This commit is contained in:
@@ -1,9 +1,20 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { ref } from 'vue'
|
import { ref, watch } from 'vue'
|
||||||
|
import { useAuthStore } from './auth'
|
||||||
|
|
||||||
const RECENT_KEY = 'weknora_cmdk_recent'
|
const RECENT_KEY_PREFIX = 'weknora_cmdk_recent'
|
||||||
const RECENT_LIMIT = 4
|
const RECENT_LIMIT = 4
|
||||||
|
|
||||||
|
// recentKey scopes "recent searches" to the active (user, tenant) pair.
|
||||||
|
// The previous global key leaked queries between users sharing a browser
|
||||||
|
// and across tenant switches inside the same account. Falling back to
|
||||||
|
// "anon" keeps the palette functional before login finishes hydrating.
|
||||||
|
function recentKey(userId: string | null | undefined, tenantId: number | string | null | undefined): string {
|
||||||
|
const u = userId ? String(userId) : 'anon'
|
||||||
|
const t = tenantId !== null && tenantId !== undefined && tenantId !== '' ? String(tenantId) : 'none'
|
||||||
|
return `${RECENT_KEY_PREFIX}:${u}:${t}`
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pinia store for the global command palette (⌘K / Ctrl+K).
|
* Pinia store for the global command palette (⌘K / Ctrl+K).
|
||||||
* The palette itself is rendered once in platform/index.vue.
|
* The palette itself is rendered once in platform/index.vue.
|
||||||
@@ -15,9 +26,16 @@ export const useCommandPaletteStore = defineStore('commandPalette', () => {
|
|||||||
const initialQuery = ref('')
|
const initialQuery = ref('')
|
||||||
const recentQueries = ref<string[]>([])
|
const recentQueries = ref<string[]>([])
|
||||||
|
|
||||||
|
// Lazily resolve the auth store inside actions / watchers — at store
|
||||||
|
// setup time pinia may not have finished registering peer stores yet.
|
||||||
|
const currentRecentKey = (): string => {
|
||||||
|
const auth = useAuthStore()
|
||||||
|
return recentKey(auth.user?.id, auth.effectiveTenantId)
|
||||||
|
}
|
||||||
|
|
||||||
const loadRecent = () => {
|
const loadRecent = () => {
|
||||||
try {
|
try {
|
||||||
const raw = localStorage.getItem(RECENT_KEY)
|
const raw = localStorage.getItem(currentRecentKey())
|
||||||
recentQueries.value = raw ? JSON.parse(raw) : []
|
recentQueries.value = raw ? JSON.parse(raw) : []
|
||||||
} catch {
|
} catch {
|
||||||
recentQueries.value = []
|
recentQueries.value = []
|
||||||
@@ -42,7 +60,7 @@ export const useCommandPaletteStore = defineStore('commandPalette', () => {
|
|||||||
...recentQueries.value.filter(x => x !== trimmed),
|
...recentQueries.value.filter(x => x !== trimmed),
|
||||||
].slice(0, RECENT_LIMIT)
|
].slice(0, RECENT_LIMIT)
|
||||||
try {
|
try {
|
||||||
localStorage.setItem(RECENT_KEY, JSON.stringify(recentQueries.value))
|
localStorage.setItem(currentRecentKey(), JSON.stringify(recentQueries.value))
|
||||||
} catch {
|
} catch {
|
||||||
/* ignore quota errors */
|
/* ignore quota errors */
|
||||||
}
|
}
|
||||||
@@ -51,14 +69,23 @@ export const useCommandPaletteStore = defineStore('commandPalette', () => {
|
|||||||
const clearRecent = () => {
|
const clearRecent = () => {
|
||||||
recentQueries.value = []
|
recentQueries.value = []
|
||||||
try {
|
try {
|
||||||
localStorage.removeItem(RECENT_KEY)
|
localStorage.removeItem(currentRecentKey())
|
||||||
} catch {
|
} catch {
|
||||||
/* ignore */
|
/* ignore */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load recent queries immediately on store creation.
|
// Reload recent queries whenever the active user or tenant changes
|
||||||
loadRecent()
|
// (login, logout, tenant switch). Each (user, tenant) pair has its own
|
||||||
|
// localStorage namespace so queries don't bleed across identities.
|
||||||
|
watch(
|
||||||
|
() => {
|
||||||
|
const auth = useAuthStore()
|
||||||
|
return [auth.user?.id, auth.effectiveTenantId] as const
|
||||||
|
},
|
||||||
|
() => loadRecent(),
|
||||||
|
{ immediate: true },
|
||||||
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
open,
|
open,
|
||||||
|
|||||||
Reference in New Issue
Block a user