From 6a6dbfa16555b2916db49db351bf276c4409159b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Wed, 21 Oct 2026 13:39:36 +0200 Subject: [PATCH 1/2] [Website] Keep database tools in the app browser partition --- .../remote/src/lib/wordpress-popup.ts | 14 ++++++ packages/playground/remote/vite.config.ts | 1 + .../playground/remote/wordpress-popup.html | 28 ++++++++++++ .../tools/src/phpmyadmin/config.inc.php | 5 +++ .../website/playwright/e2e/website-ui.spec.ts | 45 ++++++++++++------- .../site-database-panel/adminer-button.tsx | 5 ++- .../site-database-panel/phpmyadmin-button.tsx | 7 ++- .../website/src/lib/wordpress-popup.spec.ts | 30 +++++++++++++ .../website/src/lib/wordpress-popup.ts | 14 ++++++ 9 files changed, 132 insertions(+), 17 deletions(-) create mode 100644 packages/playground/remote/src/lib/wordpress-popup.ts create mode 100644 packages/playground/remote/wordpress-popup.html create mode 100644 packages/playground/website/src/lib/wordpress-popup.spec.ts create mode 100644 packages/playground/website/src/lib/wordpress-popup.ts diff --git a/packages/playground/remote/src/lib/wordpress-popup.ts b/packages/playground/remote/src/lib/wordpress-popup.ts new file mode 100644 index 00000000000..cbfde99539c --- /dev/null +++ b/packages/playground/remote/src/lib/wordpress-popup.ts @@ -0,0 +1,14 @@ +import { getURLScope } from '@php-wasm/scopes'; +import { wordPressSiteUrl } from './config'; + +/** Keep popup WordPress in the app's service-worker partition without exposing app documents. */ +function bootWordPressPopup() { + const target = new URL(location.hash.substring(1), wordPressSiteUrl); + if (target.origin !== wordPressSiteUrl || !getURLScope(target)) { + throw new Error('The popup URL must name a scoped WordPress page.'); + } + const frame = document.querySelector('#wp') as HTMLIFrameElement; + frame.src = target.href; +} + +bootWordPressPopup(); diff --git a/packages/playground/remote/vite.config.ts b/packages/playground/remote/vite.config.ts index acea589ba49..b0055a7f49a 100644 --- a/packages/playground/remote/vite.config.ts +++ b/packages/playground/remote/vite.config.ts @@ -195,6 +195,7 @@ export default defineConfig(({ mode }) => { rollupOptions: { input: { wordpress: path('/remote.html'), + 'wordpress-popup': path('/wordpress-popup.html'), }, output: { assetFileNames: (chunkInfo) => { diff --git a/packages/playground/remote/wordpress-popup.html b/packages/playground/remote/wordpress-popup.html new file mode 100644 index 00000000000..546a666d93b --- /dev/null +++ b/packages/playground/remote/wordpress-popup.html @@ -0,0 +1,28 @@ + + + + WordPress Playground + + + + + + + diff --git a/packages/playground/tools/src/phpmyadmin/config.inc.php b/packages/playground/tools/src/phpmyadmin/config.inc.php index 60888b99aa3..268c0e23dfc 100644 --- a/packages/playground/tools/src/phpmyadmin/config.inc.php +++ b/packages/playground/tools/src/phpmyadmin/config.inc.php @@ -16,6 +16,11 @@ $cfg['ShowCreateDb'] = false; $cfg['ShowChgPassword'] = false; +// Split-origin popups keep phpMyAdmin in a WordPress iframe. Its frame-busting +// script must not try to replace the app-origin wrapper; the iframe's sandbox +// and the browser's origin checks still prevent access to app documents/storage. +$cfg['AllowThirdPartyFraming'] = defined('PLAYGROUND_PARENT_ORIGIN'); + // Cookie authentication secret. $cfg['blowfish_secret'] = 'r/g+J#&)L2&p!z5gUS)d(vEU#KAynq#g'; diff --git a/packages/playground/website/playwright/e2e/website-ui.spec.ts b/packages/playground/website/playwright/e2e/website-ui.spec.ts index 868ffbf6c43..145d6e72ea9 100644 --- a/packages/playground/website/playwright/e2e/website-ui.spec.ts +++ b/packages/playground/website/playwright/e2e/website-ui.spec.ts @@ -1,6 +1,6 @@ import { test, expect } from '../playground-fixtures.ts'; import type { Blueprint } from '@wp-playground/blueprints'; -import type { Page } from '@playwright/test'; +import type { Frame, Page } from '@playwright/test'; import { getDirectoryNameForSlug } from '../../src/lib/state/opfs/opfs-site-path'; // We can't import the SupportedPHPVersions versions directly from the remote package @@ -1224,8 +1224,8 @@ echo $wp_env['db']['path']; await adminerButton.click(); // Verify Adminer opened in new tab - const newPage = await pagePromise; - await newPage.waitForLoadState(); + const popup = await pagePromise; + const newPage = await getWordPressPopupDocument(popup); expect(newPage.url()).toContain('/adminer/'); await expect(newPage.locator('body')).toContainText('Adminer'); await expect(newPage.locator('body')).toContainText('wp_posts'); @@ -1271,7 +1271,10 @@ echo $wp_env['db']['path']; ).toContainText('Updated post content.'); // Go to SQL tab and execute "SHOW TABLES" - await newPage.getByRole('link', { name: 'SQL command' }).click(); + await newPage + .locator('#menu') + .getByRole('link', { name: 'SQL command' }) + .click(); await newPage.waitForLoadState(); const sqlTextarea = newPage.locator('textarea[name="query"]'); await sqlTextarea.fill('SHOW TABLES', { force: true }); @@ -1279,7 +1282,7 @@ echo $wp_env['db']['path']; await newPage.waitForLoadState(); await expect(newPage.locator('body')).toContainText('wp_posts'); - await newPage.close(); + await popup.close(); }); test('should load and open phpMyAdmin', async ({ website }) => { @@ -1296,8 +1299,8 @@ echo $wp_env['db']['path']; await phpMyAdminButton.click(); // Verify phpMyAdmin opened in new tab - const newPage = await pagePromise; - await newPage.waitForLoadState(); + const popup = await pagePromise; + const newPage = await getWordPressPopupDocument(popup); expect(newPage.url()).toContain('/phpmyadmin'); await expect(newPage.locator('body')).toContainText('phpMyAdmin'); await expect(newPage.locator('body')).toContainText('wp_posts'); @@ -1361,12 +1364,12 @@ echo $wp_env['db']['path']; .click(); await newPage.waitForLoadState(); await newPage.locator('.CodeMirror.cm-s-default').click(); - await newPage.keyboard.type('SHOW TABLES'); + await popup.keyboard.type('SHOW TABLES'); await newPage.getByRole('button', { name: 'Go' }).click(); await newPage.waitForLoadState(); await expect(newPage.locator('body')).toContainText('wp_posts'); - await newPage.close(); + await popup.close(); }); /* @@ -1379,8 +1382,8 @@ echo $wp_env['db']['path']; await website.page .getByRole('button', { name: 'Open Adminer' }) .click(); - const adminerPage = await adminerPagePromise; - await adminerPage.waitForLoadState(); + const adminerPopup = await adminerPagePromise; + const adminerPage = await getWordPressPopupDocument(adminerPopup); await expect(adminerPage.locator('body')).toContainText('Adminer'); await expect(adminerPage.locator('body')).toContainText('wp_posts'); @@ -1388,18 +1391,30 @@ echo $wp_env['db']['path']; await website.page .getByRole('button', { name: 'Open phpMyAdmin' }) .click(); - const phpMyAdminPage = await phpMyAdminPagePromise; - await phpMyAdminPage.waitForLoadState(); + const phpMyAdminPopup = await phpMyAdminPagePromise; + const phpMyAdminPage = await getWordPressPopupDocument(phpMyAdminPopup); await expect(phpMyAdminPage.locator('body')).toContainText( 'phpMyAdmin' ); await expect(phpMyAdminPage.locator('body')).toContainText('wp_posts'); - await adminerPage.close(); - await phpMyAdminPage.close(); + await adminerPopup.close(); + await phpMyAdminPopup.close(); }); }); +/** Follow the isolated WordPress iframe when the database tool needs an app-origin popup. */ +async function getWordPressPopupDocument(popup: Page): Promise { + await popup.waitForLoadState(); + const frame = popup.frame({ name: 'wordpress-popup' }); + if (!frame) { + return popup; + } + // WebKit can finish loading the wrapper before its iframe leaves about:blank. + await frame.waitForURL((url) => url.pathname.startsWith('/scope:')); + return frame; +} + // Test browser-saved Playgrounds by default and explicit temporary opt-outs. // The `@storage` tag routes this group to the one-worker CI storage lane. test.describe('Default Playground storage', { tag: '@storage' }, () => { diff --git a/packages/playground/website/src/components/site-manager/site-database-panel/adminer-button.tsx b/packages/playground/website/src/components/site-manager/site-database-panel/adminer-button.tsx index 5c153201a03..ae031cf87e2 100644 --- a/packages/playground/website/src/components/site-manager/site-database-panel/adminer-button.tsx +++ b/packages/playground/website/src/components/site-manager/site-database-panel/adminer-button.tsx @@ -3,6 +3,7 @@ import { logger } from '@php-wasm/logger'; import { external } from '@wordpress/icons'; import { useEffect, useState } from 'react'; import css from './style.module.css'; +import { getWordPressPopupUrl } from '../../../lib/wordpress-popup'; import { type PlaygroundClient, type StepDefinition, @@ -53,6 +54,7 @@ async function installAdminer(playground: PlaygroundClient) { await runBlueprintV1Steps(blueprint, playground as UniversalPHP); } +/** Install and open Adminer without moving WordPress into another storage partition. */ export function AdminerButton({ playground, }: { @@ -91,6 +93,7 @@ export function AdminerButton({ }; }, [playground]); + /** Open the installed tool through the matching WordPress browsing context. */ const handleOpenAdminer = async () => { if (!playground) { return; @@ -117,7 +120,7 @@ export function AdminerButton({ const playgroundUrl = await playground.absoluteUrl; if (playgroundUrl) { window.open( - `${playgroundUrl}/adminer/`, + getWordPressPopupUrl(`${playgroundUrl}/adminer/`), '_blank', 'noopener,noreferrer' ); diff --git a/packages/playground/website/src/components/site-manager/site-database-panel/phpmyadmin-button.tsx b/packages/playground/website/src/components/site-manager/site-database-panel/phpmyadmin-button.tsx index 1db78cd5146..d671086d7d9 100644 --- a/packages/playground/website/src/components/site-manager/site-database-panel/phpmyadmin-button.tsx +++ b/packages/playground/website/src/components/site-manager/site-database-panel/phpmyadmin-button.tsx @@ -3,6 +3,7 @@ import { logger } from '@php-wasm/logger'; import { Button, Icon, Flex, FlexItem } from '@wordpress/components'; import { external } from '@wordpress/icons'; import css from './style.module.css'; +import { getWordPressPopupUrl } from '../../../lib/wordpress-popup'; import { type PlaygroundClient, type UniversalPHP, @@ -27,6 +28,7 @@ async function installPhpMyAdmin(playground: PlaygroundClient) { await runBlueprintV1Steps(blueprint, playground as UniversalPHP); } +/** Install and open phpMyAdmin without moving WordPress into another storage partition. */ export function PhpMyAdminButton({ playground, }: { @@ -65,6 +67,7 @@ export function PhpMyAdminButton({ }; }, [playground]); + /** Open the installed tool through the matching WordPress browsing context. */ const handleOpenPhpMyAdmin = async () => { if (!playground) { return; @@ -91,7 +94,9 @@ export function PhpMyAdminButton({ const playgroundUrl = await playground.absoluteUrl; if (playgroundUrl) { window.open( - `${playgroundUrl}${PHPMYADMIN_URL_PATH}${PHPMYADMIN_ENTRY_PATH}`, + getWordPressPopupUrl( + `${playgroundUrl}${PHPMYADMIN_URL_PATH}${PHPMYADMIN_ENTRY_PATH}` + ), '_blank', 'noopener,noreferrer' ); diff --git a/packages/playground/website/src/lib/wordpress-popup.spec.ts b/packages/playground/website/src/lib/wordpress-popup.spec.ts new file mode 100644 index 00000000000..80a918a6189 --- /dev/null +++ b/packages/playground/website/src/lib/wordpress-popup.spec.ts @@ -0,0 +1,30 @@ +import { getWordPressPopupUrl } from './wordpress-popup'; + +vi.mock('virtual:website-config', () => ({ buildVersion: 'test' })); + +describe('WordPress popup URLs', () => { + beforeEach(() => { + vi.stubGlobal('window', { + location: { origin: 'http://playground.localhost:9500' }, + }); + }); + afterEach(() => vi.unstubAllGlobals()); + + it('keeps same-origin popup URLs unchanged', () => { + const url = + 'http://playground.localhost:9500/scope:site/adminer/?select=wp_posts'; + expect(getWordPressPopupUrl(url)).toBe(url); + }); + + it('preserves the scoped path, query and fragment inside the app-origin popup', () => { + const popup = new URL( + getWordPressPopupUrl( + 'http://wp.playground.localhost:9500/scope:site/adminer/?select=wp_posts#result' + ) + ); + expect(popup.origin).toBe('http://playground.localhost:9500'); + expect(popup.pathname).toBe('/wordpress-popup.html'); + expect(popup.search).toBe(''); + expect(popup.hash).toBe('#/scope:site/adminer/?select=wp_posts#result'); + }); +}); diff --git a/packages/playground/website/src/lib/wordpress-popup.ts b/packages/playground/website/src/lib/wordpress-popup.ts new file mode 100644 index 00000000000..ecacbc93923 --- /dev/null +++ b/packages/playground/website/src/lib/wordpress-popup.ts @@ -0,0 +1,14 @@ +import { getRemoteUrl } from './config'; + +/** Keep a split-origin popup in the same service-worker partition as the running site. */ +export function getWordPressPopupUrl(url: string): string { + const target = new URL(url); + const remote = getRemoteUrl(); + if (target.origin === remote.origin) { + return target.href; + } + remote.pathname = '/wordpress-popup.html'; + remote.search = ''; + remote.hash = target.pathname + target.search + target.hash; + return remote.href; +} From 8a9458f67014215005dbb51e79c27786294b8787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Wed, 21 Oct 2026 16:12:04 +0200 Subject: [PATCH 2/2] [Website] Explain how popup frames reuse the running service worker --- .../playground/remote/src/lib/wordpress-popup.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/playground/remote/src/lib/wordpress-popup.ts b/packages/playground/remote/src/lib/wordpress-popup.ts index cbfde99539c..2ad85164c16 100644 --- a/packages/playground/remote/src/lib/wordpress-popup.ts +++ b/packages/playground/remote/src/lib/wordpress-popup.ts @@ -1,7 +1,18 @@ import { getURLScope } from '@php-wasm/scopes'; import { wordPressSiteUrl } from './config'; -/** Keep popup WordPress in the app's service-worker partition without exposing app documents. */ +/** + * Opens a WordPress tool in a popup with the same frame layout as the main app: + * an app-origin top-level page containing a WordPress-origin iframe. + * + * Browsers can select service-worker registrations using both the WordPress + * origin and the top-level site. Opening WordPress directly can then miss the + * worker serving it inside the main app. This app-origin wrapper preserves + * that top-level site, so #wp can use the existing worker and PHP instance. + * + * The hash supplies the destination. Accept only scoped WordPress URLs so this + * wrapper cannot be used to load trusted app documents into the iframe. + */ function bootWordPressPopup() { const target = new URL(location.hash.substring(1), wordPressSiteUrl); if (target.origin !== wordPressSiteUrl || !getURLScope(target)) {