|
1 | 1 | /** @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' |
2 | 6 | 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 | +})) |
4 | 17 |
|
5 | 18 | it('keeps fragment navigation local, decodes targets and preserves external handoff', () => { |
6 | 19 | const html = buildHtmlPreviewDocument('<html><head></head><body></body></html>') |
@@ -45,3 +58,53 @@ it('keeps fragment navigation local, decodes targets and preserves external hand |
45 | 58 | expect(event.defaultPrevented).toBe(true) |
46 | 59 | expect(handoff).toHaveBeenCalledWith({ __simPageNav: 'https://example.com/help' }, '*') |
47 | 60 | }) |
| 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 | +}) |
0 commit comments