From aee01020c429c059c2cbf329072b8accfc70e117 Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Mon, 1 Jul 2024 13:20:10 -0700 Subject: [PATCH 1/2] Support Modern Remote Chrome MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Starting in version 66, Chrome DevTools requires the host header to be “localhost” or an IP address. Work around this by re-running the request without a host header if we get the security error. --- library/Pdfexport/HeadlessChrome.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/library/Pdfexport/HeadlessChrome.php b/library/Pdfexport/HeadlessChrome.php index 0c72fac..41c6fbe 100644 --- a/library/Pdfexport/HeadlessChrome.php +++ b/library/Pdfexport/HeadlessChrome.php @@ -755,7 +755,20 @@ public function getVersion() protected function jsonVersion($host, $port) { $client = new \GuzzleHttp\Client(); - $response = $client->request('GET', sprintf('http://%s:%s/json/version', $host, $port)); + + try { + $response = $client->request('GET', sprintf('http://%s:%s/json/version', $host, $port)); + } catch (\GuzzleHttp\Exception\ServerException $e) { + // Check if we've run into the host header security change, and re-run the request with no host header. + // ref: https://issues.chromium.org/issues/40090537 + if (strstr($e->getMessage(), 'Host header is specified and is not an IP address or localhost.')) { + $response = $client->request( + 'GET', sprintf('http://%s:%s/json/version', $host, $port), + ['headers' => ['Host' => null]]); + } else { + throw $e; + } + } if ($response->getStatusCode() !== 200) { return false; From 0a738a5b6f6be21288002dc26099d058fae295c2 Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Tue, 26 Nov 2024 11:02:37 -0800 Subject: [PATCH 2/2] Formatting Fix for PHPCS Adjusted formatting of code to pass PHPCS checks. --- library/Pdfexport/HeadlessChrome.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/Pdfexport/HeadlessChrome.php b/library/Pdfexport/HeadlessChrome.php index 41c6fbe..23ebe85 100644 --- a/library/Pdfexport/HeadlessChrome.php +++ b/library/Pdfexport/HeadlessChrome.php @@ -763,8 +763,10 @@ protected function jsonVersion($host, $port) // ref: https://issues.chromium.org/issues/40090537 if (strstr($e->getMessage(), 'Host header is specified and is not an IP address or localhost.')) { $response = $client->request( - 'GET', sprintf('http://%s:%s/json/version', $host, $port), - ['headers' => ['Host' => null]]); + 'GET', + sprintf('http://%s:%s/json/version', $host, $port), + ['headers' => ['Host' => null]] + ); } else { throw $e; }