Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop): r1-l4 canonicalize shared Sandbox UI routes

Josuéa97aac3501ed
diff --git a/desktop-app/src/__tests__/sandboxUiDeepLinks.test.ts b/desktop-app/src/__tests__/sandboxUiDeepLinks.test.ts
index a50972a22..5a5656463 100644
--- a/desktop-app/src/__tests__/sandboxUiDeepLinks.test.ts
+++ b/desktop-app/src/__tests__/sandboxUiDeepLinks.test.ts
@@ -122,6 +122,19 @@ describe('sandbox UI deep links', () => {
).toBe('/tasks/task-42')
})
+ it('extracts canonical logical routes from browser-encoded app URLs', () => {
+ expect(
+ extractSandboxUiViewRoute({
+ currentUrl:
+ 'https://rpc.example/api/v1/sandbox-ui/sandbox-recipes/' +
+ 'agentic-task-board/view/caf%C3%A9%20menu/literal%25percent',
+ rpcProxyOrigin: 'https://rpc.example/',
+ recipeNs: 'sandbox-recipes',
+ recipeName: 'agentic-task-board',
+ })
+ ).toBe('/café menu/literal%percent')
+ })
+
it('accepts the deep-link host case-insensitively', () => {
expect(parseSandboxUiDeepLink('evenfire://APP/sandbox-recipes/agentic-task-board')).toEqual({
appRef: 'sandbox-recipes/agentic-task-board',
diff --git a/desktop-app/src/__tests__/sandboxUiDriverLifecycle.test.ts b/desktop-app/src/__tests__/sandboxUiDriverLifecycle.test.ts
index ebe209d79..97f2ea101 100644
--- a/desktop-app/src/__tests__/sandboxUiDriverLifecycle.test.ts
+++ b/desktop-app/src/__tests__/sandboxUiDriverLifecycle.test.ts
@@ -33,6 +33,10 @@ const electronMocks = vi.hoisted(() => {
return this
}
+ emit(event: string, ...args: unknown[]): void {
+ this.listeners.get(event)?.forEach(handler => handler(...args))
+ }
+
getURL(): string {
return this.currentUrl
}
@@ -268,6 +272,35 @@ describe('mountSandboxUiView lifecycle cleanup', () => {
expect(parentWindow.listenerCount('closed')).toBe(0)
})
+ it('encodes the canonical client route before handing it to the mounted app', async () => {
+ const parentWindow = new FakeParentWindow()
+
+ await mountSandboxUiView(
+ mountArgs({
+ parentWindow,
+ routePath: '/café menu/literal%percent',
+ })
+ )
+ const view = electronMocks.views[0]
+ expect(view).toBeDefined()
+ view!.webContents.executeJavaScript.mockResolvedValue(true)
+ view!.webContents.emit(
+ 'did-navigate',
+ {},
+ 'https://rpc.example/api/v1/sandbox-ui/sandbox-recipes/task-board/view/',
+ 200
+ )
+ view!.webContents.emit('did-finish-load')
+
+ await vi.waitFor(() => {
+ expect(view!.webContents.executeJavaScript).toHaveBeenCalledOnce()
+ })
+ expect(view!.webContents.executeJavaScript.mock.calls[0]?.[0]).toContain(
+ 'https://rpc.example/api/v1/sandbox-ui/sandbox-recipes/task-board/view/' +
+ 'caf%C3%A9%20menu/literal%25percent'
+ )
+ })
+
it('rejects a refresh cookie scoped to a different recipe', async () => {
const parentWindow = new FakeParentWindow()
await mountSandboxUiView(mountArgs({ parentWindow }))
diff --git a/desktop-app/src/sandboxUiDriver.ts b/desktop-app/src/sandboxUiDriver.ts
index 729501806..41feb7e5e 100644
--- a/desktop-app/src/sandboxUiDriver.ts
+++ b/desktop-app/src/sandboxUiDriver.ts
@@ -420,9 +420,10 @@ export async function mountSandboxUiView(args: MountSandboxUiArgs): Promise<void
`${proxyOriginUrl}/api/v1/sandbox-ui/` +
`${encodeURIComponent(recipeNs)}/${encodeURIComponent(recipeName)}/view${viewPath}`
if (clientRoutePath && clientRoutePath !== viewPath) {
+ const encodedClientRoutePath = encodeSandboxUiRoutePath(clientRoutePath)
const clientRouteUrl =
`${proxyOriginUrl}/api/v1/sandbox-ui/` +
- `${encodeURIComponent(recipeNs)}/${encodeURIComponent(recipeName)}/view${clientRoutePath}`
+ `${encodeURIComponent(recipeNs)}/${encodeURIComponent(recipeName)}/view${encodedClientRoutePath}`
let lastNavigation = { url: '', status: 0 }
let handoffFinished = false
let handoffTimeout: NodeJS.Timeout | null = null
@@ -540,6 +541,13 @@ function pathHasDotSegment(pathname: string): boolean {
return pathname.split('/').some(segment => DOT_SEGMENT_PATTERN.test(segment))
}
+function encodeSandboxUiRoutePath(routePath: string): string {
+ return routePath
+ .split('/')
+ .map((segment, index) => (index === 0 ? '' : encodeURIComponent(segment)))
+ .join('/')
+}
+
function sharedPathForDefaultPath(defaultPath: string): string {
const firstQueryIndex = defaultPath.indexOf('?')
const firstHashIndex = defaultPath.indexOf('#')
diff --git a/packages/desktop-app-links/index.cjs b/packages/desktop-app-links/index.cjs
index 51d9ac97a..ee5345904 100644
--- a/packages/desktop-app-links/index.cjs
+++ b/packages/desktop-app-links/index.cjs
@@ -54,6 +54,23 @@ function isUnsafeRouteSegment(rawSegment) {
return RECURSIVE_ROUTE_ESCAPE_PATTERN.test(segment)
}
+function canonicalizeRouteSegment(rawSegment) {
+ let segment = rawSegment
+ for (let depth = 0; depth <= MAX_ROUTE_SEGMENT_DECODE_DEPTH; depth += 1) {
+ if (isUnsafeRouteSegment(segment)) return null
+ if (!PERCENT_ESCAPE_PATTERN.test(segment)) return segment
+ let decoded
+ try {
+ decoded = decodeURIComponent(segment)
+ } catch {
+ return null
+ }
+ if (decoded === segment) return segment
+ segment = decoded
+ }
+ return PERCENT_ESCAPE_PATTERN.test(segment) ? null : segment
+}
+
function normalizeSandboxUiRoute(rawPath) {
const routePath = String(rawPath || '').trim()
if (!routePath) return undefined
@@ -68,8 +85,15 @@ function normalizeSandboxUiRoute(rawPath) {
) {
return null
}
- if (routePath.split('/').some(isUnsafeRouteSegment)) return null
- return routePath
+ const canonicalSegments = []
+ for (const segment of routePath.split('/')) {
+ const canonicalSegment = canonicalizeRouteSegment(segment)
+ if (canonicalSegment === null) return null
+ canonicalSegments.push(canonicalSegment)
+ }
+ const canonicalPath = canonicalSegments.join('/')
+ if (canonicalPath.length > MAX_APP_ROUTE_LENGTH) return null
+ return canonicalPath
}
function validateParts(parts) {
diff --git a/packages/desktop-app-links/index.test.cjs b/packages/desktop-app-links/index.test.cjs
index e1bfcdf16..aa87b3b25 100644
--- a/packages/desktop-app-links/index.test.cjs
+++ b/packages/desktop-app-links/index.test.cjs
@@ -48,6 +48,42 @@ test('web and desktop links use the same canonical route contract', () => {
})
})
+test('route normalization canonicalizes safe percent-encoded paths once', () => {
+ for (const [input, expected] of [
+ ['/café', '/café'],
+ ['/caf%C3%A9', '/café'],
+ ['/space here', '/space here'],
+ ['/space%20here', '/space here'],
+ ['/literal%percent', '/literal%percent'],
+ ['/literal%25percent', '/literal%percent'],
+ ['/already%2520encoded', '/already encoded'],
+ ]) {
+ assert.equal(links.normalizeSandboxUiRoute(input), expected)
+ }
+
+ const unicode = links.parseSandboxUiDeepLink(
+ links.buildSandboxUiDeepLink({
+ recipeNs: 'sandbox-recipes',
+ recipeName: 'task-board',
+ path: '/caf%C3%A9',
+ })
+ )
+ const decoded = links.parseSandboxUiDeepLink(
+ links.buildSandboxUiDeepLink({
+ recipeNs: 'sandbox-recipes',
+ recipeName: 'task-board',
+ path: '/café',
+ })
+ )
+
+ assert.deepEqual(unicode, {
+ appRef: 'sandbox-recipes/task-board',
+ path: '/café',
+ })
+ assert.deepEqual(decoded, unicode)
+ assert.equal(links.sandboxUiDeepLinkTargetsEqual(unicode, decoded), true)
+})
+
test('shared app links carry only client-side pathnames', () => {
for (const pathValue of [
'/tasks?authorization=secret',