From 1a2b9480fe4bc1933778a1bb054bb24e86d0f45c Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sun, 22 Nov 2026 11:56:53 -0800 Subject: [PATCH 1/2] fix(files): bind sim-page navigation messages to the preview frame --- .../html-preview-navigation.test.ts | 62 ++++++++++++++++++- .../components/file-viewer/preview-panel.tsx | 6 ++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/html-preview-navigation.test.ts b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/html-preview-navigation.test.ts index 4baccccb9bb..22d1db7adac 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/html-preview-navigation.test.ts +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/html-preview-navigation.test.ts @@ -1,6 +1,19 @@ /** @vitest-environment jsdom */ + +import { act, createElement } from 'react' +import { nextNavigationMock, nextNavigationMockFns } from '@sim/testing/mocks/next-navigation.mock' +import { createRoot } from 'react-dom/client' import { expect, it, vi } from 'vitest' -import { buildHtmlPreviewDocument } from '@/app/workspace/[workspaceId]/files/components/file-viewer/preview-panel' +import { + buildHtmlPreviewDocument, + PreviewPanel, +} from '@/app/workspace/[workspaceId]/files/components/file-viewer/preview-panel' + +vi.mock('next/navigation', () => nextNavigationMock) +vi.mock('next-themes', () => ({ useTheme: () => ({ resolvedTheme: 'light' }) })) +vi.mock('@/hooks/queries/workspace-files', () => ({ + useWorkspaceFileBinary: () => ({ data: undefined }), +})) it('keeps fragment navigation local, decodes targets and preserves external handoff', () => { const html = buildHtmlPreviewDocument('') @@ -45,3 +58,50 @@ it('keeps fragment navigation local, decodes targets and preserves external hand expect(event.defaultPrevented).toBe(true) expect(handoff).toHaveBeenCalledWith({ __simPageNav: 'https://example.com/help' }, '*') }) + +it('honors navigation handoffs only from its own preview frame', async () => { + vi.stubGlobal( + 'ResizeObserver', + class { + observe() {} + disconnect() {} + } + ) + vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockReturnValue( + new DOMRect(0, 0, 800, 600) + ) + const openTab = vi.spyOn(window, 'open').mockReturnValue(null) + const container = document.createElement('div') + document.body.append(container) + const root = createRoot(container) + await act(async () => { + root.render( + createElement(PreviewPanel, { + content: '

page

', + mimeType: 'text/html', + filename: 'page.html', + }) + ) + }) + const frameWindow = container.querySelector('iframe')?.contentWindow + if (!frameWindow) throw new Error('preview frame did not mount') + const routerPush = nextNavigationMockFns.router.push + + const post = (href: string, source: MessageEventSource | null) => + window.dispatchEvent(new MessageEvent('message', { data: { __simPageNav: href }, source })) + + const foreignWindow = document.createElement('iframe') + document.body.append(foreignWindow) + post('https://attacker.example/login', foreignWindow.contentWindow) + post('https://attacker.example/login', null) + post('/workspace/victim/files', window) + expect(openTab).not.toHaveBeenCalled() + expect(routerPush).not.toHaveBeenCalled() + + post('https://example.com/help', frameWindow) + post('/workspace/ws-1/files', frameWindow) + expect(openTab).toHaveBeenCalledWith('https://example.com/help', '_blank', 'noopener,noreferrer') + expect(routerPush).toHaveBeenCalledWith('/workspace/ws-1/files') + + await act(async () => root.unmount()) +}) diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/preview-panel.tsx b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/preview-panel.tsx index dd87419b3ac..31160a27921 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/preview-panel.tsx +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/preview-panel.tsx @@ -365,10 +365,15 @@ const HtmlPreview = memo(function HtmlPreview({ // sandboxed (cookie-less) frame 401s every image. const wrappedContent = useInlinedWorkspaceImages(builtContent) + const iframeRef = useRef(null) + // Receives sim-resource link clicks bridged out of the sandboxed page and // routes them in the app. Only workspace-internal paths are honored. useEffect(() => { const onMessage = (event: MessageEvent) => { + // The sandboxed frame's origin is opaque ('null'), so bind to its window: + // any other window holding a handle to this tab could post the same shape. + if (event.source !== iframeRef.current?.contentWindow) return const href = (event.data as { __simPageNav?: unknown } | null)?.__simPageNav if (typeof href !== 'string') return if (href.startsWith('/workspace/')) { @@ -448,6 +453,7 @@ const HtmlPreview = memo(function HtmlPreview({
{isRenderable && (