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
Expand Up @@ -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<string, unknown>): Promise<void> }
| 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) => {
Expand Down
104 changes: 82 additions & 22 deletions packages/playground/remote/src/lib/playground-worker-endpoint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,96 @@ 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', () => {
afterEach(() => {
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',
Expand Down Expand Up @@ -387,10 +450,7 @@ async function createEndpoint(
hasOpfsMount(mountpoint: string): Promise<boolean>;
mountOpfs(
options: {
device: {
type: 'local-fs';
handle: FileSystemDirectoryHandle;
};
device: MountDevice;
mountpoint: string;
initialSyncDirection?: 'opfs-to-memfs' | 'memfs-to-opfs';
},
Expand Down
17 changes: 16 additions & 1 deletion packages/playground/remote/src/lib/playground-worker-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)
Expand Down
Loading