Skip to content

Commit 739b957

Browse files
authored
fix(files): bind sim-page navigation messages to the preview frame (#10315)
1 parent 507953c commit 739b957

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/html-preview-navigation.test.ts‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
/** @vitest-environment jsdom */
2+
3+
import { act, createElement } from 'react'
4+
import { nextNavigationMock, nextNavigationMockFns } from '@sim/testing/mocks/next-navigation.mock'
5+
import { createRoot } from 'react-dom/client'
26
import { expect, it, vi } from 'vitest'
3-
import { buildHtmlPreviewDocument } from '@/app/workspace/[workspaceId]/files/components/file-viewer/preview-panel'
7+
import {
8+
buildHtmlPreviewDocument,
9+
PreviewPanel,
10+
} from '@/app/workspace/[workspaceId]/files/components/file-viewer/preview-panel'
11+
12+
vi.mock('next/navigation', () => nextNavigationMock)
13+
vi.mock('next-themes', () => ({ useTheme: () => ({ resolvedTheme: 'light' }) }))
14+
vi.mock('@/hooks/queries/workspace-files', () => ({
15+
useWorkspaceFileBinary: () => ({ data: undefined }),
16+
}))
417

518
it('keeps fragment navigation local, decodes targets and preserves external handoff', () => {
619
const html = buildHtmlPreviewDocument('<html><head></head><body></body></html>')
@@ -45,3 +58,53 @@ it('keeps fragment navigation local, decodes targets and preserves external hand
4558
expect(event.defaultPrevented).toBe(true)
4659
expect(handoff).toHaveBeenCalledWith({ __simPageNav: 'https://example.com/help' }, '*')
4760
})
61+
62+
it('honors navigation handoffs only from its own preview frame', async () => {
63+
vi.stubGlobal(
64+
'ResizeObserver',
65+
class {
66+
observe() {}
67+
disconnect() {}
68+
}
69+
)
70+
vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockReturnValue(
71+
new DOMRect(0, 0, 800, 600)
72+
)
73+
const openTab = vi.spyOn(window, 'open').mockReturnValue(null)
74+
const container = document.createElement('div')
75+
document.body.append(container)
76+
const root = createRoot(container)
77+
await act(async () => {
78+
root.render(
79+
createElement(PreviewPanel, {
80+
content: '<p>page</p>',
81+
mimeType: 'text/html',
82+
filename: 'page.html',
83+
workspaceId: 'ws-1',
84+
fileId: 'file-1',
85+
fileKey: 'page.html',
86+
})
87+
)
88+
})
89+
const frameWindow = container.querySelector('iframe')?.contentWindow
90+
if (!frameWindow) throw new Error('preview frame did not mount')
91+
const routerPush = nextNavigationMockFns.router.push
92+
93+
const post = (href: string, source: MessageEventSource | null) =>
94+
window.dispatchEvent(new MessageEvent('message', { data: { __simPageNav: href }, source }))
95+
96+
const foreignWindow = document.createElement('iframe')
97+
document.body.append(foreignWindow)
98+
post('https://attacker.example/login', foreignWindow.contentWindow)
99+
post('https://attacker.example/login', null)
100+
post('/workspace/victim/files', window)
101+
expect(openTab).not.toHaveBeenCalled()
102+
expect(routerPush).not.toHaveBeenCalled()
103+
104+
post('https://example.com/help', frameWindow)
105+
post('/workspace/ws-1/files', frameWindow)
106+
expect(openTab).toHaveBeenCalledWith('https://example.com/help', '_blank', 'noopener,noreferrer')
107+
expect(routerPush).toHaveBeenCalledWith('/workspace/ws-1/files')
108+
109+
await act(async () => root.unmount())
110+
})

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/preview-panel.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,15 @@ const HtmlPreview = memo(function HtmlPreview({
365365
// sandboxed (cookie-less) frame 401s every image.
366366
const wrappedContent = useInlinedWorkspaceImages(builtContent)
367367

368+
const iframeRef = useRef<HTMLIFrameElement>(null)
369+
368370
// Receives sim-resource link clicks bridged out of the sandboxed page and
369371
// routes them in the app. Only workspace-internal paths are honored.
370372
useEffect(() => {
371373
const onMessage = (event: MessageEvent) => {
374+
// The sandboxed frame's origin is opaque ('null'), so bind to its window:
375+
// any other window holding a handle to this tab could post the same shape.
376+
if (event.source !== iframeRef.current?.contentWindow) return
372377
const href = (event.data as { __simPageNav?: unknown } | null)?.__simPageNav
373378
if (typeof href !== 'string') return
374379
if (href.startsWith('/workspace/')) {
@@ -448,6 +453,7 @@ const HtmlPreview = memo(function HtmlPreview({
448453
<div ref={containerRef} className='flex min-h-0 flex-1 overflow-hidden'>
449454
{isRenderable && (
450455
<iframe
456+
ref={iframeRef}
451457
key={resumeNonce}
452458
srcDoc={wrappedContent}
453459
/* No clipboard-write delegation: this frame also renders untrusted

0 commit comments

Comments
 (0)