Cap persisted browser history payloads (#1822)
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import type { BrowserHistoryEntry, WorkspaceSessionState } from './types'
|
||||
import { redactKagiSessionToken } from './browser-url'
|
||||
|
||||
export const MAX_BROWSER_HISTORY_ENTRIES = 200
|
||||
|
||||
export function normalizeBrowserHistoryUrl(url: string): string {
|
||||
try {
|
||||
const parsed = new URL(redactKagiSessionToken(url))
|
||||
parsed.hostname = parsed.hostname.toLowerCase()
|
||||
parsed.protocol = parsed.protocol.toLowerCase()
|
||||
let normalized = parsed.toString()
|
||||
if (normalized.endsWith('/')) {
|
||||
normalized = normalized.slice(0, -1)
|
||||
}
|
||||
return normalized
|
||||
} catch {
|
||||
return redactKagiSessionToken(url).toLowerCase()
|
||||
}
|
||||
}
|
||||
|
||||
export function normalizeBrowserHistoryEntries(
|
||||
entries: readonly BrowserHistoryEntry[]
|
||||
): BrowserHistoryEntry[] {
|
||||
const seen = new Set<string>()
|
||||
const normalizedEntries: BrowserHistoryEntry[] = []
|
||||
for (const entry of entries) {
|
||||
const safeUrl = redactKagiSessionToken(entry.url)
|
||||
const key = normalizeBrowserHistoryUrl(safeUrl)
|
||||
if (seen.has(key)) {
|
||||
continue
|
||||
}
|
||||
seen.add(key)
|
||||
normalizedEntries.push({ ...entry, url: safeUrl, normalizedUrl: key })
|
||||
if (normalizedEntries.length >= MAX_BROWSER_HISTORY_ENTRIES) {
|
||||
break
|
||||
}
|
||||
}
|
||||
return normalizedEntries
|
||||
}
|
||||
|
||||
export function pruneWorkspaceSessionBrowserHistory(
|
||||
session: WorkspaceSessionState
|
||||
): WorkspaceSessionState {
|
||||
if (!session.browserUrlHistory) {
|
||||
return session
|
||||
}
|
||||
const browserUrlHistory = normalizeBrowserHistoryEntries(session.browserUrlHistory)
|
||||
if (
|
||||
browserUrlHistory.length === session.browserUrlHistory.length &&
|
||||
browserUrlHistory.every((entry, index) => entry === session.browserUrlHistory?.[index])
|
||||
) {
|
||||
return session
|
||||
}
|
||||
return { ...session, browserUrlHistory }
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { parseWorkspaceSession } from './workspace-session-schema'
|
||||
import { MAX_BROWSER_HISTORY_ENTRIES } from './workspace-session-browser-history'
|
||||
|
||||
describe('parseWorkspaceSession', () => {
|
||||
it('accepts a minimal valid session', () => {
|
||||
@@ -116,4 +117,27 @@ describe('parseWorkspaceSession', () => {
|
||||
expect(result.value.lastVisitedAtByWorktreeId).toEqual({ good: 1_700_000_000_000 })
|
||||
}
|
||||
})
|
||||
|
||||
it('caps oversized browser history while parsing legacy workspace sessions', () => {
|
||||
const result = parseWorkspaceSession({
|
||||
activeRepoId: null,
|
||||
activeWorktreeId: null,
|
||||
activeTabId: null,
|
||||
tabsByWorktree: {},
|
||||
terminalLayoutsByTabId: {},
|
||||
browserUrlHistory: Array.from({ length: 500 }, (_, index) => ({
|
||||
url: `https://example.com/${index}`,
|
||||
normalizedUrl: `https://example.com/${index}`,
|
||||
title: `Example ${index}`,
|
||||
lastVisitedAt: 1_700_000_000_000 - index,
|
||||
visitCount: 1
|
||||
}))
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
if (result.ok) {
|
||||
expect(result.value.browserUrlHistory).toHaveLength(MAX_BROWSER_HISTORY_ENTRIES)
|
||||
expect(result.value.browserUrlHistory?.at(-1)?.url).toBe('https://example.com/199')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -15,6 +15,7 @@ import type {
|
||||
TerminalPaneLayoutNode,
|
||||
WorkspaceSessionState
|
||||
} from './types'
|
||||
import { normalizeBrowserHistoryEntries } from './workspace-session-browser-history'
|
||||
|
||||
// ─── Terminal pane layout (recursive) ───────────────────────────────
|
||||
|
||||
@@ -182,6 +183,10 @@ const browserHistoryEntrySchema = z.object({
|
||||
visitCount: z.number()
|
||||
})
|
||||
|
||||
const browserHistoryEntriesSchema = z
|
||||
.array(browserHistoryEntrySchema)
|
||||
.transform((entries) => normalizeBrowserHistoryEntries(entries))
|
||||
|
||||
// ─── Workspace session ──────────────────────────────────────────────
|
||||
|
||||
export const workspaceSessionStateSchema: z.ZodType<WorkspaceSessionState> = z.object({
|
||||
@@ -197,7 +202,7 @@ export const workspaceSessionStateSchema: z.ZodType<WorkspaceSessionState> = z.o
|
||||
browserPagesByWorkspace: z.record(z.string(), z.array(browserPageSchema)).optional(),
|
||||
activeBrowserTabIdByWorktree: z.record(z.string(), z.string().nullable()).optional(),
|
||||
activeTabTypeByWorktree: z.record(z.string(), workspaceVisibleTabTypeSchema).optional(),
|
||||
browserUrlHistory: z.array(browserHistoryEntrySchema).optional(),
|
||||
browserUrlHistory: browserHistoryEntriesSchema.optional(),
|
||||
activeTabIdByWorktree: z.record(z.string(), z.string().nullable()).optional(),
|
||||
unifiedTabs: z.record(z.string(), z.array(tabSchema)).optional(),
|
||||
tabGroups: z.record(z.string(), z.array(tabGroupSchema)).optional(),
|
||||
|
||||
Reference in New Issue
Block a user