Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): r4-b2 sanitize plugin-controlled app tab titles at the store border
Diego Zacariasf1bf6826585a
diff --git a/desktop-app/ui/src/constants/workspaceTabs.ts b/desktop-app/ui/src/constants/workspaceTabs.tsnew file mode 100644index 000000000..ef0f93a35--- /dev/null+++ b/desktop-app/ui/src/constants/workspaceTabs.ts@@ -0,0 +1,7 @@+/**+ * Max length of an app tab's live title (mini-spec 08 §2). The title is+ * controlled by the plugin (`document.title` of the embed), so it is bounded+ * before it reaches the host chrome; longer values are truncated to this length+ * with a trailing ellipsis by `sanitizeAppTabTitle`.+ */+export const MAX_TAB_TITLE_LEN = 64diff --git a/desktop-app/ui/src/lib/__tests__/sanitizeAppTabTitle.test.ts b/desktop-app/ui/src/lib/__tests__/sanitizeAppTabTitle.test.tsnew file mode 100644index 000000000..3e74e11be--- /dev/null+++ b/desktop-app/ui/src/lib/__tests__/sanitizeAppTabTitle.test.ts@@ -0,0 +1,105 @@+import { describe, expect, it } from 'vitest'+import fc from 'fast-check'+import { MAX_TAB_TITLE_LEN } from '../../constants/workspaceTabs'+import { sanitizeAppTabTitle } from '../sanitizeAppTabTitle'++const cp = (code: number) => String.fromCodePoint(code)++// One representative code point per stripped range. Asserted against the real+// sanitizer output (not a hand-copied regex), so a silent deletion of any range+// from the source denylist fails this test.+const STRIPPED_REPRESENTATIVES: ReadonlyArray<readonly [string, number]> = [+ ['C0 low (U+0000–U+0008)', 0x0001],+ ['C0 high (U+000E–U+001F)', 0x000e],+ ['DEL (U+007F)', 0x007f],+ ['C1 (U+0080–U+009F)', 0x0085],+ ['zero-width (U+200B–U+200D)', 0x200c],+ ['bidi override (U+202A–U+202E)', 0x202e],+ ['bidi isolate (U+2066–U+2069)', 0x2066],+ ['BOM / ZWNBSP (U+FEFF)', 0xfeff],+]++const hasLoneSurrogate = (s: string) =>+ /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/.test(s)++describe('sanitizeAppTabTitle (mini-spec 08 §2)', () => {+ it('passes a normal title through, only trimming and collapsing whitespace', () => {+ expect(sanitizeAppTabTitle(' Ticket 42 — Acme ')).toBe('Ticket 42 — Acme')+ expect(sanitizeAppTabTitle('Report \t v2')).toBe('Report v2')+ })++ it('returns empty for a control / whitespace-only input', () => {+ expect(sanitizeAppTabTitle('')).toBe('')+ expect(sanitizeAppTabTitle(' \t\n ')).toBe('')+ expect(sanitizeAppTabTitle(`${cp(0x202e)}${cp(0x200b)}${cp(0xfeff)}`)).toBe('')+ })++ it('removes one representative from every stripped range', () => {+ for (const [label, code] of STRIPPED_REPRESENTATIVES) {+ expect(sanitizeAppTabTitle(`a${cp(code)}b`), label).toBe('ab')+ }+ })++ it('keeps whitespace control characters, collapsing them instead of dropping', () => {+ // U+0009–U+000D are NOT in the denylist: they must survive removal and fold+ // to a single space, not vanish and glue the words together.+ expect(sanitizeAppTabTitle('a\tb\nc')).toBe('a b c')+ })++ it('truncates to MAX_TAB_TITLE_LEN with a trailing ellipsis', () => {+ const out = sanitizeAppTabTitle('x'.repeat(MAX_TAB_TITLE_LEN + 50))+ expect(out.length).toBe(MAX_TAB_TITLE_LEN)+ expect(out.endsWith('…')).toBe(true)+ })++ it('truncates on a code-point boundary, never splitting a surrogate pair', () => {+ // Each 😀 is one code point / two UTF-16 code units. A code-unit slice would+ // cut the last pair in half and leave a lone high surrogate before the '…'.+ const out = sanitizeAppTabTitle('😀'.repeat(MAX_TAB_TITLE_LEN + 5))+ expect(hasLoneSurrogate(out)).toBe(false)+ expect(Array.from(out).length).toBeLessThanOrEqual(MAX_TAB_TITLE_LEN)+ expect(out.endsWith('…')).toBe(true)+ })++ it('property: output stays within the code-point bound, is idempotent, and never leaks a lone surrogate', () => {+ // A char generator biased toward the dangerous ranges the sanitizer targets+ // (control, bidi, zero-width, whitespace) mixed with printable ASCII AND+ // astral code points, so the fuzz exercises removal + collapse + code-point+ // truncation together — including the surrogate-boundary case.+ const titleChar = fc+ .oneof(+ fc.integer({ min: 0x20, max: 0x7e }),+ fc.integer({ min: 0x00, max: 0x1f }),+ fc.integer({ min: 0x1f300, max: 0x1faff }), // astral (emoji planes)+ fc.constantFrom(+ 0x0007,+ 0x001b,+ 0x007f,+ 0x0085,+ 0x009f,+ 0x2028,+ 0x2029,+ 0x200b,+ 0x200d,+ 0x202a,+ 0x202e,+ 0x2066,+ 0x2069,+ 0xfeff,+ 0x09,+ 0x0a,+ 0x20+ )+ )+ .map(code => String.fromCodePoint(code))+ const titleArb = fc.array(titleChar, { maxLength: 200 }).map(chars => chars.join(''))+ fc.assert(+ fc.property(titleArb, raw => {+ const once = sanitizeAppTabTitle(raw)+ expect(Array.from(once).length).toBeLessThanOrEqual(MAX_TAB_TITLE_LEN)+ expect(hasLoneSurrogate(once)).toBe(false)+ expect(sanitizeAppTabTitle(once)).toBe(once)+ })+ )+ })+})diff --git a/desktop-app/ui/src/lib/__tests__/workspaceTabs.test.ts b/desktop-app/ui/src/lib/__tests__/workspaceTabs.test.tsindex 87e77c4d3..f480b64c6 100644--- a/desktop-app/ui/src/lib/__tests__/workspaceTabs.test.ts+++ b/desktop-app/ui/src/lib/__tests__/workspaceTabs.test.ts@@ -220,6 +220,45 @@ describe('workspaceTabs — setAppTabTitle (mini-spec 06 §2)', () => {expect(setAppTabTitle(state, 'files-1', 'X')).toBe(state)expect(state.tabs.find(t => t.id === 'chat-1')?.title).toBe('Chat')})++ // §2 (mini-spec 08, R4-B2): the plugin-controlled title is sanitized at this+ // entry border, so every render site inherits the cleaned value. These go+ // through the real channel `setAppTabTitle(state, tabId, raw)` (T1).+ it('strips control, bidi override, and zero-width characters from the title (§2, R4-B2)', () => {+ let state = createEmptyWorkspaceTabsState()+ state = openAppTab(state, { id: 'app-1', appRef: 'eventasks', title: 'App' })+ // A plugin document.title carrying a bidi override, a zero-width space, and a+ // C0 control character between visible runs.+ state = setAppTabTitle(state, 'app-1', 'Ticket42Acme')+ const stored = state.tabs.find(t => t.id === 'app-1')?.title ?? ''+ expect(stored).not.toMatch(/[]/)+ expect(stored).toBe('Ticket42Acme')+ })++ it('collapses whitespace runs (tabs/newlines) to single spaces (§2, R4-B2)', () => {+ let state = createEmptyWorkspaceTabsState()+ state = openAppTab(state, { id: 'app-1', appRef: 'eventasks', title: 'App' })+ state = setAppTabTitle(state, 'app-1', 'Ticket\t42\n\n Acme')+ expect(state.tabs.find(t => t.id === 'app-1')?.title).toBe('Ticket 42 Acme')+ })++ it('truncates an over-long reported title with a trailing ellipsis (§2, R4-B2)', () => {+ let state = createEmptyWorkspaceTabsState()+ state = openAppTab(state, { id: 'app-1', appRef: 'eventasks', title: 'App' })+ state = setAppTabTitle(state, 'app-1', 'A'.repeat(200))+ const stored = state.tabs.find(t => t.id === 'app-1')?.title ?? ''+ // MAX_TAB_TITLE_LEN is 64; the stored title never exceeds it and ends with …+ expect(stored.length).toBeLessThanOrEqual(64)+ expect(stored.endsWith('…')).toBe(true)+ })++ it('ignores a title that reduces to empty after sanitizing, keeping the previous (§2, R4-B2)', () => {+ let state = createEmptyWorkspaceTabsState()+ state = openAppTab(state, { id: 'app-1', appRef: 'eventasks', title: 'Doc' })+ // All-control/invisible input sanitizes to '' → no-op (same reference).+ expect(setAppTabTitle(state, 'app-1', '')).toBe(state)+ expect(state.tabs.find(t => t.id === 'app-1')?.title).toBe('Doc')+ })})describe('workspaceTabs — files multi-instance by path (mini-spec 06 §3)', () => {diff --git a/desktop-app/ui/src/lib/sanitizeAppTabTitle.ts b/desktop-app/ui/src/lib/sanitizeAppTabTitle.tsnew file mode 100644index 000000000..0438b81f5--- /dev/null+++ b/desktop-app/ui/src/lib/sanitizeAppTabTitle.ts@@ -0,0 +1,62 @@+import { MAX_TAB_TITLE_LEN } from '../constants/workspaceTabs'++/**+ * Sanitize a plugin-controlled app tab title before it reaches the host chrome+ * (mini-spec 08 §2). An app embed's `document.title` is attacker-influenceable,+ * so a raw title can carry control characters, bidi overrides, or invisible+ * code points that corrupt the tab strip layout, the visual order of labels, or+ * the accessible names of the tab and its close button, and can be arbitrarily+ * long. This is the single entry-border sanitizer; every render site inherits+ * the cleaned value from the store, so none re-sanitizes.+ *+ * The contract (pure, idempotent: `sanitize(sanitize(x)) === sanitize(x)`):+ * - remove non-whitespace C0/C1 control characters (whitespace controls such as+ * tab/newline survive removal so they can be collapsed, not dropped);+ * - remove bidi overrides/embeddings/isolates and zero-width / BOM code points;+ * - collapse any run of whitespace to a single space and trim;+ * - truncate to `MAX_TAB_TITLE_LEN` code points, replacing the tail with an+ * ellipsis so the result never exceeds the bound;+ * - an input that is empty after sanitizing yields `''`: the caller keeps the+ * previous title rather than blanking the tab.+ *+ * The denylist is declared as numeric code-point ranges and compiled with+ * `new RegExp`, NOT written as embedded literal bytes or `\u` escapes. A+ * security denylist built from invisible characters can be mutated with no+ * visible diff by an editor or formatter pass; numeric ranges keep every+ * stripped span legible and diff-safe in review, and the compiled regex still+ * matches the exact code points.+ *+ * Ranges: non-whitespace C0 (U+0000-U+0008, U+000E-U+001F), DEL + C1+ * (U+007F-U+009F), zero-width (U+200B-U+200D), bidi overrides/embeddings+ * (U+202A-U+202E), bidi isolates (U+2066-U+2069), and BOM/ZWNBSP (U+FEFF).+ * U+0009-U+000D (tab, LF, VT, FF, CR) are intentionally NOT removed here: they+ * are whitespace and collapse to a single space in the next step.+ */+const UNSAFE_RANGES: ReadonlyArray<readonly [number, number]> = [+ [0x0000, 0x0008],+ [0x000e, 0x001f],+ [0x007f, 0x009f],+ [0x200b, 0x200d],+ [0x202a, 0x202e],+ [0x2066, 0x2069],+ [0xfeff, 0xfeff],+]++const STRIP_UNSAFE_CHARS = new RegExp(+ `[${UNSAFE_RANGES.map(([lo, hi]) =>+ lo === hi ? String.fromCodePoint(lo) : `${String.fromCodePoint(lo)}-${String.fromCodePoint(hi)}`+ ).join('')}]`,+ 'gu'+)++export function sanitizeAppTabTitle(raw: string): string {+ const collapsed = raw.replace(STRIP_UNSAFE_CHARS, '').replace(/\s+/g, ' ').trim()+ // Count and slice by code point, not UTF-16 code unit, so truncation never+ // splits a surrogate pair and leaves a lone half dangling before the ellipsis.+ const points = Array.from(collapsed)+ if (points.length <= MAX_TAB_TITLE_LEN) return collapsed+ return `${points+ .slice(0, MAX_TAB_TITLE_LEN - 1)+ .join('')+ .trimEnd()}…`+}diff --git a/desktop-app/ui/src/lib/workspaceTabs.ts b/desktop-app/ui/src/lib/workspaceTabs.tsindex 1a80bf848..45ec7eb53 100644--- a/desktop-app/ui/src/lib/workspaceTabs.ts+++ b/desktop-app/ui/src/lib/workspaceTabs.ts@@ -1,3 +1,4 @@+import { sanitizeAppTabTitle } from './sanitizeAppTabTitle'import type {ActiveChat,AppTabPayload,@@ -194,23 +195,26 @@ export function setAppTabSavedRoutePath(* §2), so the strip names the tab after what the plugin currently shows. A* mirror of `setAppTabSavedRoutePath`: a no-op (same reference) when the tab is* missing, is not an app tab, or already holds this title, so a `setState`- * bails out. An empty / whitespace-only title is IGNORED (keeps the previous- * title) — a mid-navigation blank must not blank the tab label; precedence- * `document.title → app.label → 'App'` is resolved by the caller, not here.+ * bails out. The plugin-controlled title is sanitized at this entry border+ * (mini-spec 08 §2) so every render site inherits the cleaned value; a title+ * that is empty / whitespace-only OR reduces to empty after sanitizing is+ * IGNORED (keeps the previous title) — a mid-navigation blank must not blank the+ * tab label; precedence `document.title → app.label → 'App'` is resolved by the+ * caller, not here.*/export function setAppTabTitle(state: WorkspaceTabsState,tabId: string,title: string): WorkspaceTabsState {- const trimmed = title.trim()- if (!trimmed) return state+ const clean = sanitizeAppTabTitle(title)+ if (!clean) return stateconst target = state.tabs.find(tab => tab.id === tabId && tab.kind === 'app')if (!target) return state- if (target.title === trimmed) return state+ if (target.title === clean) return statereturn {...state,- tabs: state.tabs.map(tab => (tab.id === tabId ? { ...tab, title: trimmed } : tab)),+ tabs: state.tabs.map(tab => (tab.id === tabId ? { ...tab, title: clean } : tab)),}}