Skip to content

[HttpClient] Prevent "Fatal error" in data collector #46700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,15 @@ private function getCurlCommand(array $trace): ?string
$dataArg = [];

if ($json = $trace['options']['json'] ?? null) {
$dataArg[] = '--data '.escapeshellarg(self::jsonEncode($json));
if (!$this->argMaxLengthIsSafe($payload = self::jsonEncode($json))) {
return null;
}
$dataArg[] = '--data '.escapeshellarg($payload);
} elseif ($body = $trace['options']['body'] ?? null) {
if (\is_string($body)) {
if (!$this->argMaxLengthIsSafe($body)) {
return null;
}
try {
$dataArg[] = '--data '.escapeshellarg($body);
} catch (\ValueError) {
Expand All @@ -204,7 +210,10 @@ private function getCurlCommand(array $trace): ?string
} elseif (\is_array($body)) {
$body = explode('&', self::normalizeBody($body));
foreach ($body as $value) {
$dataArg[] = '--data '.escapeshellarg(urldecode($value));
if (!$this->argMaxLengthIsSafe($payload = urldecode($value))) {
return null;
}
$dataArg[] = '--data '.escapeshellarg($payload);
}
} else {
return null;
Expand Down Expand Up @@ -240,4 +249,14 @@ private function getCurlCommand(array $trace): ?string

return implode(" \\\n ", $command);
}

/**
* Let's be defensive : we authorize only size of 8kio on Windows for escapeshellarg() argument to avoid a fatal error
*
* @see https://github.com/php/php-src/blob/9458f5f2c8a8e3d6c65cc181747a5a75654b7c6e/ext/standard/exec.c#L397
*/
private function argMaxLengthIsSafe(string $payload): bool
{
return \strlen($payload) < ('\\' === \DIRECTORY_SEPARATOR ? 8100 : 256000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,28 @@ public function testItDoesNotGeneratesCurlCommandsForNotEncodableBody()
self::assertNull($curlCommand);
}

/**
* @requires extension openssl
*/
public function testItDoesNotGeneratesCurlCommandsForTooBigData()
{
$sut = new HttpClientDataCollector();
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
[
'method' => 'POST',
'url' => 'http://localhost:8057/json',
'options' => [
'body' => str_repeat('1', 257000),
],
],
]));
$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());
Expand Down