From 916a850bd67245885e4b34a47769dc12153faa41 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Sun, 15 Nov 2026 09:21:01 -0500 Subject: [PATCH] fix: restrict local fixture paths and port publication --- docs/guide/connect-browser.md | 2 +- tests/Fixtures/server.php | 5 ++-- tests/Unit/FixtureRouterTest.php | 50 ++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/FixtureRouterTest.php diff --git a/docs/guide/connect-browser.md b/docs/guide/connect-browser.md index 8dbb186..423697d 100644 --- a/docs/guide/connect-browser.md +++ b/docs/guide/connect-browser.md @@ -105,7 +105,7 @@ An explicit `connect($endpoint, $options)` call uses the supplied endpoint and o For example, start a Playwright 1.64.1 server with its matching browser image: ```bash -docker run --rm --init --ipc=host -p 3000:3000 \ +docker run --rm --init --ipc=host -p 127.0.0.1:3000:3000 \ --workdir /home/pwuser --user pwuser \ mcr.microsoft.com/playwright:v1.64.1-noble \ /bin/sh -c "npx -y playwright@1.64.1 run-server --port 3000 --host 0.0.0.0" diff --git a/tests/Fixtures/server.php b/tests/Fixtures/server.php index 4330a5b..740c9b4 100644 --- a/tests/Fixtures/server.php +++ b/tests/Fixtures/server.php @@ -91,10 +91,11 @@ } // Construct file path -$file = __DIR__.'/html'.$uri; +$root = realpath(__DIR__.'/html'); +$file = realpath(__DIR__.'/html'.rawurldecode($uri)); // Serve static file if it exists -if (file_exists($file) && is_file($file)) { +if (is_string($root) && is_string($file) && str_starts_with($file, $root.DIRECTORY_SEPARATOR) && is_file($file)) { // Determine content type $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION)); $contentTypes = [ diff --git a/tests/Unit/FixtureRouterTest.php b/tests/Unit/FixtureRouterTest.php new file mode 100644 index 0000000..dbaa16a --- /dev/null +++ b/tests/Unit/FixtureRouterTest.php @@ -0,0 +1,50 @@ +mustRun(); + + $this->assertStringContainsString('404 Not Found', $process->getOutput()); + $this->assertStringNotContainsString('readfile($file)', $process->getOutput()); + } + + /** @return iterable */ + public static function pathsOutsideTheFixtureRoot(): iterable + { + yield 'raw parent' => ['/../server.php']; + yield 'encoded parent' => ['/%2e%2e/server.php']; + yield 'encoded separator' => ['/%2e%2e%2fserver.php']; + } + + public function testKnownStaticFilesStillLoad(): void + { + $process = new Process([PHP_BINARY, '-r', '$_SERVER["REQUEST_URI"] = "/index.html"; include $argv[1];', dirname(__DIR__).'/Fixtures/server.php']); + $process->mustRun(); + + $this->assertSame(file_get_contents(dirname(__DIR__).'/Fixtures/html/index.html'), $process->getOutput()); + } +}