From c7f994349869d94ebf65016913048e2bcec074db Mon Sep 17 00:00:00 2001 From: arlo Date: Wed, 2 Dec 2026 17:45:48 +0800 Subject: [PATCH] fix(hub-ui): resolve dock iframe paths against the hub in cross-origin embeds --- .../hub-ui/src/client/state/context.test.ts | 28 +++++++++++++++++++ packages/hub-ui/src/client/state/context.ts | 19 +++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/packages/hub-ui/src/client/state/context.test.ts b/packages/hub-ui/src/client/state/context.test.ts index 6e5eb95b..d8d12b8e 100644 --- a/packages/hub-ui/src/client/state/context.test.ts +++ b/packages/hub-ui/src/client/state/context.test.ts @@ -602,6 +602,34 @@ describe('dock route persistence', () => { vi.unstubAllGlobals() }) + it('resolves a hub iframe against its serving origin and restores its saved route', async () => { + const { rpc, sharedStates, trust } = createStubRpc() + rpc.connection.metaBaseUrl = 'https://hub.example/tools/__connection.json' + const files = { + id: 'files', + type: 'iframe', + title: 'Files', + icon: 'ph:folder', + url: '/files/', + } as const + const route = 'https://hub.example/files/tree?sort=name#readme' + const session = ref({ open: true, selectedDockId: files.id, selectedDockRoute: route }) + const context = await createDocksContext('embedded', rpc, undefined, session) + trust() + sharedStates.get(HUB_EVENTS.sharedState.docks)!.push([ + files, + { ...files, id: 'report', url: 'reports/panel' }, + ]) + sharedStates.get(HUB_EVENTS.sharedState.dockRenderers)!.push({}) + await flushRestore() + + expect(context.docks.selected).toMatchObject({ url: 'https://hub.example/files/' }) + expect(context.panel.consumeBootRoute?.(files.id)).toBe(route) + expect(context.docks.entries.find(entry => entry.id === 'report')).toMatchObject({ url: 'reports/panel' }) + context.docks.register({ ...files, url: '/local/' }, true) + expect(context.docks.selected).toMatchObject({ url: '/local/' }) + }) + it('clears the old route before publishing a different dock selection', async () => { expect.assertions(2) diff --git a/packages/hub-ui/src/client/state/context.ts b/packages/hub-ui/src/client/state/context.ts index d9a31396..e3a1cae1 100644 --- a/packages/hub-ui/src/client/state/context.ts +++ b/packages/hub-ui/src/client/state/context.ts @@ -4,7 +4,7 @@ import type { SharedState } from 'devframe/utils/shared-state' import type { WhenContext } from 'devframe/utils/when' import type { Ref } from 'vue' import type { DevframeDocksUserSettings } from './dock-settings' -import { attachFrameNavClient, createDockRenderersContext } from '@devframes/hub/client' +import { attachFrameNavClient, createDockRenderersContext, resolveDockUrl } from '@devframes/hub/client' import { DEFAULT_STATE_USER_SETTINGS, DOCK_RENDERERS_STATE_KEY, HUB_EVENTS } from '@devframes/hub/constants' import { DEVFRAME_EVENTS } from 'devframe/constants' import { createEventEmitter } from 'devframe/utils/events' @@ -55,6 +55,9 @@ export async function createDocksContext( // `devframe:docks` docks by id, and never sync to shared state. Mirrors the // merge semantics of `@devframes/hub`'s own client host. const clientDocks = reactive(new Map()) + const connection = rpc.connection + const isCrossOriginEmbed = clientType === 'embedded' && connection?.metaBaseUrl + && new URL(connection.metaBaseUrl, globalThis.location?.href).origin !== globalThis.location?.origin const entries = computed(() => { const server = dockEntries.value let base: DevframeDockEntry[] @@ -78,7 +81,19 @@ export async function createDocksContext( // hub-ui owns the built-in Settings tab so it's always reachable without a // host registering `~settings`; a host that registered its own wins, so add // ours only when the merged list has none. - const localized = base.map(entry => localizeTitle(entry)) + const localized = base.map((entry) => { + const dock = localizeTitle(entry) + // Resolve /, ./ and ../ against the hub. Keep bare paths relative to the page. + if ( + isCrossOriginEmbed + && dock.type === 'iframe' + && !clientDocks.has(dock.id) + && /^\.{0,2}\//.test(dock.url) + ) { + dock.url = resolveDockUrl(dock.url, connection) + } + return dock + }) if (localized.some(entry => entry.id === BUILTIN_ENTRY_SETTINGS.id)) return localized return [