Skip to content

Commit

Permalink
Request handler: Remove everything after # from the URL (#1126)
Browse files Browse the repository at this point in the history
Closes #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
  • Loading branch information
adamziel authored Mar 21, 2024
1 parent e968680 commit fefd010
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/php-wasm/node/src/test/php-request-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 echo $_SERVER['REQUEST_URI'];`);
const response = await handler.request({
url: '/index.php#foo',
});
expect(response.text).toEqual('/index.php');
});

it('Should allow mixing data and files when `body` is a JavaScript object', async () => {
php.writeFile(
'/index.php',
Expand Down
3 changes: 2 additions & 1 deletion packages/php-wasm/universal/src/lib/php-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down

0 comments on commit fefd010

Please sign in to comment.