From 36e6fa09350a7489123623ebd552cf543591cb6b Mon Sep 17 00:00:00 2001 From: Phillip Look Date: Fri, 3 Jun 2022 12:29:57 +0200 Subject: [PATCH] [HttpClient][WebProfilerBundle] Catch errors when encoding body for curl command line --- .../DataCollector/HttpClientDataCollector.php | 6 ++++- .../HttpClientDataCollectorTest.php | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php b/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php index 292cdf3945bcf..9c92db2cc5582 100644 --- a/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php +++ b/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php @@ -194,7 +194,11 @@ private function getCurlCommand(array $trace): ?string $dataArg[] = '--data '.escapeshellarg(json_encode($json, \JSON_PRETTY_PRINT)); } elseif ($body = $trace['options']['body'] ?? null) { if (\is_string($body)) { - $dataArg[] = '--data '.escapeshellarg($body); + try { + $dataArg[] = '--data '.escapeshellarg($body); + } catch (\ValueError $e) { + return null; + } } elseif (\is_array($body)) { foreach ($body as $key => $value) { $dataArg[] = '--data '.escapeshellarg("$key=$value"); diff --git a/src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php b/src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php index ebe4c2c52569b..37bba8c79aac2 100755 --- a/src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php +++ b/src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php @@ -343,6 +343,28 @@ public function testItDoesNotGeneratesCurlCommandsForUnsupportedBodyType() self::assertNull($curlCommand); } + /** + * @requires extension openssl + */ + public function testItDoesNotGeneratesCurlCommandsForNotEncodableBody() + { + $sut = new HttpClientDataCollector(); + $sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([ + [ + 'method' => 'POST', + 'url' => 'http://localhost:8057/json', + 'options' => [ + 'body' => "\0", + ], + ], + ])); + $sut->collect(new Request(), new Response()); + $collectedData = $sut->getClients(); + self::assertCount(1, $collectedData['http_client']['traces']); + $curlCommand = $collectedData['http_client']['traces'][0]['curlCommand']; + self::assertNull($curlCommand); + } + private function httpClientThatHasTracedRequests($tracedRequests): TraceableHttpClient { $httpClient = new TraceableHttpClient(new NativeHttpClient());