From ce75a380293142c75f880fdc453013ae317e3402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Thu, 21 Mar 2024 14:13:37 +0100 Subject: [PATCH] Request handler: Remove everything after # from the URL Closes https://github.com/WordPress/wordpress-playground/issues/1120 Fixes a bug in the URL parsing where `#foo` is passed to PHP when requesting `/index.php#foo`. ## Testing instructions Confirm the tests passed --- .../node/src/test/php-request-handler.spec.ts | 11 +++++++++++ .../php-wasm/universal/src/lib/php-request-handler.ts | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/php-wasm/node/src/test/php-request-handler.spec.ts b/packages/php-wasm/node/src/test/php-request-handler.spec.ts index a44501791e..279dcc1d51 100644 --- a/packages/php-wasm/node/src/test/php-request-handler.spec.ts +++ b/packages/php-wasm/node/src/test/php-request-handler.spec.ts @@ -177,6 +177,17 @@ describe.each(SupportedPHPVersions)( expect(response.text).toEqual('true'); }); + /** + * @see https://github.com/WordPress/wordpress-playground/issues/1120 + */ + it('Should not propagate the # part of the URL to PHP', async () => { + php.writeFile('/index.php', ` { php.writeFile( '/index.php', diff --git a/packages/php-wasm/universal/src/lib/php-request-handler.ts b/packages/php-wasm/universal/src/lib/php-request-handler.ts index 2d74a73626..ebaa2dead5 100644 --- a/packages/php-wasm/universal/src/lib/php-request-handler.ts +++ b/packages/php-wasm/universal/src/lib/php-request-handler.ts @@ -119,7 +119,8 @@ export class PHPRequestHandler implements RequestHandler { request.url.startsWith('http://') || request.url.startsWith('https://'); const requestedUrl = new URL( - request.url, + // Remove the hash part of the URL as it's meant for the server. + request.url.split('#')[0], isAbsolute ? undefined : DEFAULT_BASE_URL );