diff --git a/frontend/src/lib/components/Dev.svelte b/frontend/src/lib/components/Dev.svelte index d6185c00e2..6515807dde 100644 --- a/frontend/src/lib/components/Dev.svelte +++ b/frontend/src/lib/components/Dev.svelte @@ -30,6 +30,7 @@ import { onDestroy, onMount, setContext, untrack } from 'svelte' import DarkModeToggle from '$lib/components/sidebar/DarkModeToggle.svelte' import { page } from '$app/state' + import { isFromWindowEmbedder } from '$lib/utils/embedderMessage' import { getUserExt } from '$lib/user' import FlowPreviewButtons from './flows/header/FlowPreviewButtons.svelte' import FlowModuleSchemaMap from './flows/map/FlowModuleSchemaMap.svelte' @@ -271,8 +272,8 @@ modeInitialized = true } - const el = (event) => { - // sendUserToast(`Received message from parent ${event.data.type}`, true) + const el = (event: MessageEvent) => { + if (!isFromWindowEmbedder(event)) return if (event.data.type == 'runTest') { runTest() event.preventDefault() diff --git a/frontend/src/lib/components/copilot/chat/global/rawAppBundlerBridge.ts b/frontend/src/lib/components/copilot/chat/global/rawAppBundlerBridge.ts index 62dce8af5c..00a87e7657 100644 --- a/frontend/src/lib/components/copilot/chat/global/rawAppBundlerBridge.ts +++ b/frontend/src/lib/components/copilot/chat/global/rawAppBundlerBridge.ts @@ -1,5 +1,6 @@ import { WorkspaceService } from '$lib/gen' import { randomUUID } from '$lib/utils/uuid' +import { uiBuilderFiles } from '$lib/components/raw_apps/utils' export type RawAppBundle = { js: string @@ -91,7 +92,7 @@ export function bundleRawAppFiles({ postToBundler({ type: 'bundleRawApp', requestId, - files, + files: uiBuilderFiles(files), sharedUiFiles, bundlerType }) diff --git a/frontend/src/lib/components/raw_apps/RawAppEditor.svelte b/frontend/src/lib/components/raw_apps/RawAppEditor.svelte index d209f4da1e..0f70023b7b 100644 --- a/frontend/src/lib/components/raw_apps/RawAppEditor.svelte +++ b/frontend/src/lib/components/raw_apps/RawAppEditor.svelte @@ -36,7 +36,8 @@ rawAppStorageKey, rawAppCtx, readRawAppStorage, - applyRawAppStorageOp + applyRawAppStorageOp, + uiBuilderFiles } from './utils' import { runDomQueryOnHtml, type RawAppDomQuery, type RawAppDomRequester } from './rawAppDom' import InlinePrompt from '../copilot/chat/InlinePrompt.svelte' @@ -1078,9 +1079,7 @@ addEventListener('message', function (e) { iframeFiles = { ...newFiles } iframeFocusPending = undefined buildTracker.start() - const files = Object.fromEntries( - Object.entries(newFiles).filter(([path, _]) => !path.endsWith('/')) - ) + const files = uiBuilderFiles(newFiles) target.postMessage( { type: 'setFiles', @@ -1098,9 +1097,7 @@ addEventListener('message', function (e) { const focus = iframeFocusPending === pathToSelect iframeFocusPending = undefined buildTracker.start() - const files = Object.fromEntries( - Object.entries(newFiles).filter(([path, _]) => !path.endsWith('/')) - ) + const files = uiBuilderFiles(newFiles) target.postMessage( { type: 'setFilesAndSelect', diff --git a/frontend/src/lib/components/raw_apps/utils.test.ts b/frontend/src/lib/components/raw_apps/utils.test.ts index dd55ea666c..10fb706543 100644 --- a/frontend/src/lib/components/raw_apps/utils.test.ts +++ b/frontend/src/lib/components/raw_apps/utils.test.ts @@ -10,6 +10,7 @@ import { rawAppCtx, rawAppPreviewModules, stripRawAppDiffNoise, + uiBuilderFiles, type Runnable } from './utils' @@ -290,3 +291,20 @@ describe('canonicalRawAppDiffValue', () => { expect(b.summary).toBe('app EDITED') }) }) + +describe('uiBuilderFiles', () => { + it('keeps app sources and drops directory entries and node_modules files', () => { + expect( + uiBuilderFiles({ + '/index.tsx': 'a', + '/src/node_modules_helper.ts': 'b', + '/src/': '', + '/node_modules/tailwindcss/dist/lib.mjs': 'x', + 'node_modules/pkg/index.js': 'x', + './node_modules/pkg/index.js': 'x', + '/src/vendor/node_modules/pkg/index.js': 'x', + '\\node_modules\\pkg\\index.js': 'x' + }) + ).toEqual({ '/index.tsx': 'a', '/src/node_modules_helper.ts': 'b' }) + }) +}) diff --git a/frontend/src/lib/components/raw_apps/utils.ts b/frontend/src/lib/components/raw_apps/utils.ts index 25b64615eb..99e47041c6 100644 --- a/frontend/src/lib/components/raw_apps/utils.ts +++ b/frontend/src/lib/components/raw_apps/utils.ts @@ -448,6 +448,25 @@ function hiddenRunnableToTsType(runnable: Runnable) { } } +function isNodeModulesPath(path: string): boolean { + return path + .replace(/^(\.?\/)+/, '') + .split(/[/\\]/) + .includes('node_modules') +} + +/** + * The app's stored files as the UI Builder bundler is given them. Its packages come from its + * own install into `node_modules/`, so a stored file under any `node_modules` segment would + * be imported in place of the installed package: such keys, like directory entries, are not + * app sources and stay out. + */ +export function uiBuilderFiles(files: Record): Record { + return Object.fromEntries( + Object.entries(files).filter(([path]) => !path.endsWith('/') && !isNodeModulesPath(path)) + ) +} + /** Shown in the file tree, generated by `genWmillTs` — never a key of `files`. */ export const WMILL_TS_PATH = '/wmill.ts' diff --git a/frontend/src/lib/utils/embedderMessage.test.ts b/frontend/src/lib/utils/embedderMessage.test.ts new file mode 100644 index 0000000000..e71c857d25 --- /dev/null +++ b/frontend/src/lib/utils/embedderMessage.test.ts @@ -0,0 +1,42 @@ +import { describe, expect, it } from 'vitest' +import { isFromEmbedder } from './embedderMessage' + +const self = {} as Window +const parent = {} as Window +const other = {} as Window +const ownOrigin = 'https://wm.example' + +describe('isFromEmbedder', () => { + it('accepts a same-origin embedder', () => { + expect( + isFromEmbedder( + { source: parent, origin: ownOrigin }, + { self, parent, ownOrigin, hasUrlToken: false } + ) + ).toBe(true) + }) + + it('accepts a cross-origin embedder only when the page carries a url token', () => { + const event = { source: parent, origin: 'vscode-webview://abc' } + expect(isFromEmbedder(event, { self, parent, ownOrigin, hasUrlToken: true })).toBe(true) + expect(isFromEmbedder(event, { self, parent, ownOrigin, hasUrlToken: false })).toBe(false) + }) + + it('ignores messages from windows other than the embedder', () => { + expect( + isFromEmbedder( + { source: other, origin: 'https://elsewhere.example' }, + { self, parent, ownOrigin, hasUrlToken: true } + ) + ).toBe(false) + }) + + it('ignores everything when the page is not embedded', () => { + expect( + isFromEmbedder( + { source: self, origin: ownOrigin }, + { self, parent: self, ownOrigin, hasUrlToken: true } + ) + ).toBe(false) + }) +}) diff --git a/frontend/src/lib/utils/embedderMessage.ts b/frontend/src/lib/utils/embedderMessage.ts new file mode 100644 index 0000000000..5f2d7a3280 --- /dev/null +++ b/frontend/src/lib/utils/embedderMessage.ts @@ -0,0 +1,29 @@ +type EmbedderMessageContext = { + self: Window + parent: Window | null + ownOrigin: string + // The page was opened with a `wm_token`, so its API calls carry that token rather than the + // session cookie. + hasUrlToken: boolean +} + +// The dev pages are driven by whatever embeds them (the VS Code extension's webview). Only that +// embedder may drive them, and a cross-origin one only when it handed the page a token: without +// one, the page acts with the session cookie, which another site must never steer. +export function isFromEmbedder( + event: Pick, + ctx: EmbedderMessageContext +): boolean { + if (!ctx.parent || ctx.parent === ctx.self) return false + if (event.source !== ctx.parent) return false + return event.origin === ctx.ownOrigin || ctx.hasUrlToken +} + +export function isFromWindowEmbedder(event: MessageEvent): boolean { + return isFromEmbedder(event, { + self: window, + parent: window.parent, + ownOrigin: window.location.origin, + hasUrlToken: !!new URLSearchParams(window.location.search).get('wm_token') + }) +} diff --git a/frontend/src/routes/(styled)/flows/dev/+page.svelte b/frontend/src/routes/(styled)/flows/dev/+page.svelte index e9a1c5773f..8d4829de67 100644 --- a/frontend/src/routes/(styled)/flows/dev/+page.svelte +++ b/frontend/src/routes/(styled)/flows/dev/+page.svelte @@ -20,6 +20,7 @@ import { deepEqual } from 'fast-equals' import { findModuleInFlow } from '$lib/components/flows/flowDiff' import { page } from '$app/state' + import { isFromWindowEmbedder } from '$lib/utils/embedderMessage' import { userStore, workspaceStore } from '$lib/stores' import { getUserExt } from '$lib/user' import DarkModeToggle from '$lib/components/sidebar/DarkModeToggle.svelte' @@ -140,7 +141,8 @@ connectWs() } - const el = (event) => { + const el = (event: MessageEvent) => { + if (!isFromWindowEmbedder(event)) return if (event.data.type == 'runTest') { runTest() } else if (event.data.type == 'replaceScript') {