Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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('<html><head></head><body></body></html>')
Expand Down Expand Up @@ -45,3 +58,53 @@ 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: '<p>page</p>',
mimeType: 'text/html',
filename: 'page.html',
workspaceId: 'ws-1',
fileId: 'file-1',
fileKey: 'page.html',
})
Comment thread
waleedlatif1 marked this conversation as resolved.
Resolved
)
})
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())
})
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,15 @@ const HtmlPreview = memo(function HtmlPreview({
// sandboxed (cookie-less) frame 401s every image.
const wrappedContent = useInlinedWorkspaceImages(builtContent)

const iframeRef = useRef<HTMLIFrameElement>(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/')) {
Expand Down Expand Up @@ -448,6 +453,7 @@ const HtmlPreview = memo(function HtmlPreview({
<div ref={containerRef} className='flex min-h-0 flex-1 overflow-hidden'>
{isRenderable && (
<iframe
ref={iframeRef}
key={resumeNonce}
srcDoc={wrappedContent}
/* No clipboard-write delegation: this frame also renders untrusted
Expand Down
Loading