From fe843f7bc55ae363ad56b3a8d92b498f82f75636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Wed, 28 Oct 2026 16:48:09 +0100 Subject: [PATCH] [Website] Reject OPFS root and sites-directory mounts --- ...yground-worker-endpoint-blueprints.spec.ts | 62 +++++++++++ .../lib/playground-worker-endpoint.spec.ts | 104 ++++++++++++++---- .../src/lib/playground-worker-endpoint.ts | 17 ++- 3 files changed, 160 insertions(+), 23 deletions(-) diff --git a/packages/playground/remote/src/lib/playground-worker-endpoint-blueprints.spec.ts b/packages/playground/remote/src/lib/playground-worker-endpoint-blueprints.spec.ts index bb0ecafa4ed..442ae9adf01 100644 --- a/packages/playground/remote/src/lib/playground-worker-endpoint-blueprints.spec.ts +++ b/packages/playground/remote/src/lib/playground-worker-endpoint-blueprints.spec.ts @@ -23,6 +23,68 @@ describe('PlaygroundWorkerEndpointBlueprints', () => { vi.unstubAllGlobals(); }); + it.each([ + ['do-not-attempt-installing', '/'], + ['do-not-attempt-installing', '/sites'], + ['install-from-existing-files-if-needed', '/'], + ['install-from-existing-files-if-needed', '/sites'], + ])( + 'rejects OPFS root mounts during %s boot from %s', + async (wordpressInstallMode, path) => { + const php = createFakePhp(); + const bootWordPress = vi.fn(async (_requestHandler, options) => { + await options.hooks.beforeWordPressFiles(php); + }); + let endpoint: + | { boot(options: Record): Promise } + | undefined; + vi.doMock('@wp-playground/wordpress', () => ({ bootWordPress })); + vi.doMock('@php-wasm/web', () => ({ + certificateToPEM: vi.fn(), + createDirectoryHandleMountHandler: vi.fn(), + exposeAPI: vi.fn((api) => { + endpoint = api; + return [vi.fn(), vi.fn()]; + }), + loadWebRuntime: vi.fn(), + })); + await import('./playground-worker-endpoint-blueprints'); + if (!endpoint) { + throw new Error('Expected exposeAPI to receive an endpoint'); + } + vi.spyOn(endpoint as any, 'computeSiteUrl').mockReturnValue( + 'http://playground.test' + ); + vi.spyOn(endpoint as any, 'createRequestHandler').mockResolvedValue( + { + getPrimaryPhp: vi.fn(async () => php), + } + ); + const getDirectory = vi.fn(); + vi.stubGlobal('navigator', { storage: { getDirectory } }); + + await expect( + endpoint.boot({ + scope: 'test', + mounts: [ + { + device: { type: 'opfs', path }, + mountpoint: '/wordpress', + initialSyncDirection: 'opfs-to-memfs', + }, + ], + phpVersion: '8.3', + wordpressInstallMode, + withNetworking: false, + }) + ).rejects.toThrow( + 'Cannot mount the OPFS root or the /sites directory.' + ); + expect(getDirectory).not.toHaveBeenCalled(); + }, + 10000 + ); + it.each([false, true])( 'runs mounted-site boot setup (migration failure: %s)', async (migrationFails) => { diff --git a/packages/playground/remote/src/lib/playground-worker-endpoint.spec.ts b/packages/playground/remote/src/lib/playground-worker-endpoint.spec.ts index f8d49dc4b52..9a36bf000f4 100644 --- a/packages/playground/remote/src/lib/playground-worker-endpoint.spec.ts +++ b/packages/playground/remote/src/lib/playground-worker-endpoint.spec.ts @@ -5,6 +5,7 @@ import { } from '@php-wasm/universal'; import type { MountHandler } from '@php-wasm/universal'; import type { SyncProgressCallback } from '@php-wasm/web'; +import type { MountDevice } from '@wp-playground/storage'; import { Semaphore } from '@php-wasm/util'; describe('PlaygroundWorkerEndpoint OPFS flushing', () => { @@ -12,26 +13,88 @@ describe('PlaygroundWorkerEndpoint OPFS flushing', () => { vi.unstubAllGlobals(); }); - it('registers OPFS mounts created through mountOpfs', async () => { - const endpoint = await createEndpoint({}); - const php = createFakePhp(); - endpoint.__internal_getPHP = () => php; + it.each(['', '/', '////', 'sites', '/sites', '/sites/', '//sites//'])( + 'rejects the OPFS source root %j before accessing storage', + async (path) => { + const endpoint = await createEndpoint({}); + const php = createFakePhp(); + endpoint.__internal_getPHP = () => php; + const handle = createEmptyDirectoryHandle(); + handle.getDirectoryHandle = vi.fn(async () => handle); + const getDirectory = vi.fn(async () => handle); + vi.stubGlobal('navigator', { storage: { getDirectory } }); + + await expect( + endpoint.mountOpfs({ + device: { type: 'opfs', path }, + mountpoint: '/saved', + initialSyncDirection: 'opfs-to-memfs', + }) + ).rejects.toThrow( + 'Cannot mount the OPFS root or the /sites directory.' + ); - await endpoint.mountOpfs({ - device: { - type: 'local-fs', - handle: createEmptyDirectoryHandle(), - }, - mountpoint: '/wordpress', - }); + expect(getDirectory).not.toHaveBeenCalled(); + expect(php.mount).not.toHaveBeenCalled(); + expect(await endpoint.hasOpfsMount('/saved')).toBe(false); + } + ); - expect(await endpoint.hasOpfsMount('/wordpress')).toBe(true); - expect(endpoint.opfsMounts['/wordpress']).toBeDefined(); - expect(php.mount).toHaveBeenCalledWith( - '/wordpress', - expect.any(Function) - ); - }, 15_000); + it.each(['opfs-to-memfs', 'memfs-to-opfs'] as const)( + 'mounts site and custom OPFS subdirectories with %s sync', + async (initialSyncDirection) => { + for (const path of [ + '/sites/site-test', + 'sites//site-test/', + 'port-libs/example.test/my-site', + '/sites-backup', + '/sites/%2e%2e', + ]) { + const endpoint = await createEndpoint({}); + const php = createFakePhp(); + endpoint.__internal_getPHP = () => php; + const handle = createEmptyDirectoryHandle(); + handle.getDirectoryHandle = vi.fn(async () => handle); + vi.stubGlobal('navigator', { + storage: { getDirectory: vi.fn(async () => handle) }, + }); + + await endpoint.mountOpfs({ + device: { type: 'opfs', path }, + mountpoint: '/wordpress', + initialSyncDirection, + }); + + expect(await endpoint.hasOpfsMount('/wordpress')).toBe(true); + await endpoint.unmountOpfs('/wordpress'); + } + } + ); + + it.each(['/wordpress', '/'])( + 'registers local-folder mounts at %s', + async (mountpoint) => { + const endpoint = await createEndpoint({}); + const php = createFakePhp(); + endpoint.__internal_getPHP = () => php; + + await endpoint.mountOpfs({ + device: { + type: 'local-fs', + handle: createEmptyDirectoryHandle(), + }, + mountpoint, + }); + + expect(await endpoint.hasOpfsMount(mountpoint)).toBe(true); + expect(endpoint.opfsMounts[mountpoint]).toBeDefined(); + expect(php.mount).toHaveBeenCalledWith( + mountpoint, + expect.any(Function) + ); + }, + 15_000 + ); it.each(['copy', 'progress'])( 'allows another save after initial %s fails', @@ -387,10 +450,7 @@ async function createEndpoint( hasOpfsMount(mountpoint: string): Promise; mountOpfs( options: { - device: { - type: 'local-fs'; - handle: FileSystemDirectoryHandle; - }; + device: MountDevice; mountpoint: string; initialSyncDirection?: 'opfs-to-memfs' | 'memfs-to-opfs'; }, diff --git a/packages/playground/remote/src/lib/playground-worker-endpoint.ts b/packages/playground/remote/src/lib/playground-worker-endpoint.ts index 3df928a6b03..2ce4cd199e1 100644 --- a/packages/playground/remote/src/lib/playground-worker-endpoint.ts +++ b/packages/playground/remote/src/lib/playground-worker-endpoint.ts @@ -2,7 +2,12 @@ import type { FilesystemOperation } from '@php-wasm/fs-journal'; import { journalFSEvents, replayFSJournal } from '@php-wasm/fs-journal'; import type { EmscriptenDownloadMonitor } from '@php-wasm/progress'; import { setURLScope } from '@php-wasm/scopes'; -import { formatBytes, joinPaths, sendmailSpawnHandler } from '@php-wasm/util'; +import { + ensureAbsolutePath, + formatBytes, + joinPaths, + sendmailSpawnHandler, +} from '@php-wasm/util'; import type { PHPSendmailSpawnedEvent } from '@php-wasm/util'; import PostalMime from 'postal-mime'; import type { Email } from 'postal-mime'; @@ -657,6 +662,16 @@ export abstract class PlaygroundWorkerEndpoint extends PHPWorker { options: MountDescriptor, onProgress?: SyncProgressCallback ) { + // Block bulk access to saved sites through both startup mounts and mountOpfs(). + // The site manager still needs direct directory access to list saved sites. + if (options.device.type === 'opfs') { + const path = ensureAbsolutePath(options.device.path); + if (path === '/' || path === '/sites') { + throw new Error( + 'Cannot mount the OPFS root or the /sites directory.' + ); + } + } if ( hasOwnProperty(this.opfsMounts, options.mountpoint) || hasOwnProperty(this.unmounts, options.mountpoint)