8000 bug #46570 [HttpClient][WebProfilerBundle] Catch errors when encoding… · symfony/symfony@846bc96 · GitHub
[go: up one dir, main page]

Skip to content

Commit 846bc96

Browse files
bug #46570 [HttpClient][WebProfilerBundle] Catch errors when encoding body for c… (Phillip Look)
This PR was merged into the 6.1 branch. Discussion ---------- [HttpClient][WebProfilerBundle] Catch errors when encoding body for c… | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | In Symfony 6.1 a [button to copy a request as a cURL command](#43931) was introduced for the profiler. But if I post a binary file containing null characters using the curl-http-client the `HttpClientDataCollector` throws an error. ``` Warning: Uncaught ValueError: escapeshellarg(): Argument #1 ($arg) must not contain any null bytes ``` My solution is to catch the `ValueError` in this situation and to return `null` as the resulting curl command. Returning `null` seems to be the standard handling for unexpectad values in this data collector. Commits ------- 36e6fa0 [HttpClient][WebProfilerBundle] Catch errors when encoding body for curl command line
2 parents 72ab165 + 36e6fa0 commit 846bc96

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,11 @@ private function getCurlCommand(array $trace): ?string
197197
$dataArg[] = '--data '.escapeshellarg(json_encode($json, \JSON_PRETTY_PRINT));
198198
} elseif ($body = $trace['options']['body'] ?? null) {
199199
if (\is_string($body)) {
200-
$dataArg[] = '--data '.escapeshellarg($body);
200+
try {
201+
$dataArg[] = '--data '.escapeshellarg($body);
202+
} catch (\ValueError $e) {
203+
return null;
204+
}
201205
} elseif (\is_array($body)) {
202206
$body = explode('&', self::normalizeBody($body));
203207
foreach ($body as $value) {

src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,28 @@ public function testItDoesNotGeneratesCurlCommandsForUnsupportedBodyType()
381381
self::assertNull($curlCommand);
382382
}
383383

384+
/**
385+
* @requires extension openssl
386+
*/
387+
public function testItDoesNotGeneratesCurlCommandsForNotEncodableBody()
388+
{
389+
$sut = new HttpClientDataCollector();
390+
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
391+
[
392+
'method' => 'POST',
393+
'url' => 'http://localhost:8057/json',
394+
'options' => [
395+
'body' => "\0",
396+
],
397+
],
398+
]));
399+
$sut->collect(new Request(), new Response());
400+
$collectedData = $sut->getClients();
401+
self::assertCount(1, $collectedData['http_client']['traces']);
402+
$curlCommand = $collectedData['http_client']['traces'][0]['curlCommand'];
403+
self::assertNull($curlCommand);
404+
}
405+
384406
private function httpClientThatHasTracedRequests($tracedRequests): TraceableHttpClient
385407
{
386408
$httpClient = new TraceableHttpClient(new NativeHttpClient());

0 commit comments

Comments
 (0)
0