From 31493e9417bd14b812de5b65a62a9abec83496ae Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Fri, 2 Aug 2019 17:41:11 +0200 Subject: [PATCH 1/3] Fix fatal error for long bodies --- spec/Formatter/CurlCommandFormatterSpec.php | 18 ++++++++++++++++++ src/Formatter/CurlCommandFormatter.php | 7 ++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/spec/Formatter/CurlCommandFormatterSpec.php b/spec/Formatter/CurlCommandFormatterSpec.php index 5340b62..f68d7e2 100644 --- a/spec/Formatter/CurlCommandFormatterSpec.php +++ b/spec/Formatter/CurlCommandFormatterSpec.php @@ -108,4 +108,22 @@ function it_formats_requests_with_nonseekable_body(RequestInterface $request, Ur $this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' --request POST --data '[non-seekable stream omitted]'"); } + + function it_formats_requests_with_long_body(RequestInterface $request, UriInterface $uri, StreamInterface $body) + { + $request->getUri()->willReturn($uri); + $request->getBody()->willReturn($body); + + $body->__toString()->willReturn('a very long body'); + $body->getSize()->willReturn(2097153); + $body->isSeekable()->willReturn(true); + $body->rewind()->willReturn(true); + + $uri->withFragment('')->willReturn('http://foo.com/bar'); + $request->getMethod()->willReturn('POST'); + $request->getProtocolVersion()->willReturn('1.1'); + $request->getHeaders()->willReturn([]); + + $this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' --request POST --data '[too long stream omitted]'"); + } } diff --git a/src/Formatter/CurlCommandFormatter.php b/src/Formatter/CurlCommandFormatter.php index 8060250..acb1ae4 100644 --- a/src/Formatter/CurlCommandFormatter.php +++ b/src/Formatter/CurlCommandFormatter.php @@ -36,7 +36,12 @@ public function formatRequest(RequestInterface $request) $body = $request->getBody(); if ($body->getSize() > 0) { - if ($body->isSeekable()) { + // escapeshellarg argument max length + $argMaxLength = '\\' === DIRECTORY_SEPARATOR ? 8192 : 2097152; + + if ($body->getSize() > $argMaxLength) { + $data = '[too long stream omitted]'; + } elseif ($body->isSeekable()) { $data = $body->__toString(); $body->rewind(); if (preg_match('/[\x00-\x1F\x7F]/', $data)) { From bc3606d2855e614c37dce2f7045ac89b4e4aab61 Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Sat, 3 Aug 2019 14:15:49 +0200 Subject: [PATCH 2/3] Set body size limit to 8192 --- CHANGELOG.md | 4 ++++ src/Formatter/CurlCommandFormatter.php | 6 ++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f29d07..fde1022 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## Unreleased +### Fixed + +- Fixed Fatal error on `CurlCommandFormatter` when body is larger than `escapeshellarg` allowed length. + ## [1.7.2] - 2018-10-30 ### Fixed diff --git a/src/Formatter/CurlCommandFormatter.php b/src/Formatter/CurlCommandFormatter.php index acb1ae4..1f8009d 100644 --- a/src/Formatter/CurlCommandFormatter.php +++ b/src/Formatter/CurlCommandFormatter.php @@ -36,10 +36,8 @@ public function formatRequest(RequestInterface $request) $body = $request->getBody(); if ($body->getSize() > 0) { - // escapeshellarg argument max length - $argMaxLength = '\\' === DIRECTORY_SEPARATOR ? 8192 : 2097152; - - if ($body->getSize() > $argMaxLength) { + // escapeshellarg argument max length on Windows, but impractical to use either way + if ($body->getSize() > 8192) { $data = '[too long stream omitted]'; } elseif ($body->isSeekable()) { $data = $body->__toString(); From cdf28ce1e2945cd0dfafabf418365df3644a1dd7 Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Sat, 3 Aug 2019 16:56:58 +0000 Subject: [PATCH 3/3] Phrasing --- src/Formatter/CurlCommandFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Formatter/CurlCommandFormatter.php b/src/Formatter/CurlCommandFormatter.php index 1f8009d..2ddb149 100644 --- a/src/Formatter/CurlCommandFormatter.php +++ b/src/Formatter/CurlCommandFormatter.php @@ -36,7 +36,7 @@ public function formatRequest(RequestInterface $request) $body = $request->getBody(); if ($body->getSize() > 0) { - // escapeshellarg argument max length on Windows, but impractical to use either way + // escapeshellarg argument max length on Windows, but longer body in curl command would be impractical anyways if ($body->getSize() > 8192) { $data = '[too long stream omitted]'; } elseif ($body->isSeekable()) {