Skip to content

Commit 49dcaf7

Browse files
committed
Gracefully handle invalid cURL options
1 parent c7b5bc9 commit 49dcaf7

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/Browser.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use React\Stream\ThroughStream;
2020
use React\Http\Message\Uri;
2121
use Throwable;
22+
use ValueError;
2223
use function array_change_key_case;
2324
use function array_key_exists;
2425
use function count;
@@ -75,7 +76,6 @@ class Browser {
7576
//CURLOPT_HSTS_ENABLE => true, //PHP8.2
7677
];
7778

78-
7979
protected array $defaultHeaders = [
8080
'User-Agent' => 'EdgeTelemetricsBrowser/1',
8181
];
@@ -356,7 +356,7 @@ public function disableConnectionCaches() : void {
356356
private function initCurl() : CurlHandle {
357357
$curl = curl_init();
358358

359-
if ($curl === false || $curl === null) {
359+
if (!$curl) {
360360
throw new \RuntimeException('Unable to init curl');
361361
}
362362

@@ -369,7 +369,16 @@ private function initCurl() : CurlHandle {
369369
curl_setopt($curl, CURLOPT_SHARE, $this->curlShare);
370370
}
371371

372-
curl_setopt_array($curl, $options);
372+
try {
373+
error_clear_last();
374+
@curl_setopt_array($curl, $options);
375+
if (error_get_last()) {
376+
//Capture malformed options that result in conversion errors like "Array to string conversion"
377+
throw new \RuntimeException('One or more cURL options values were invalid');
378+
}
379+
} catch (ValueError $e) {
380+
throw new \RuntimeException('Invalid cURL options keys provided');
381+
}
373382

374383
return $curl;
375384
}

0 commit comments

Comments
 (0)