From b2afb6bb486b42664ba2a81ab470602ea8da0227 Mon Sep 17 00:00:00 2001 From: Dennis Riehle Date: Mon, 12 Oct 2020 12:01:33 +0200 Subject: [PATCH] fixed detection of binary strings (#132) --- spec/Formatter/CurlCommandFormatterSpec.php | 18 +++++++++++++++ .../FullHttpMessageFormatterSpec.php | 22 +++++++++++++++++++ src/Formatter/CurlCommandFormatter.php | 3 ++- src/Formatter/FullHttpMessageFormatter.php | 3 ++- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/spec/Formatter/CurlCommandFormatterSpec.php b/spec/Formatter/CurlCommandFormatterSpec.php index f68d7e2..f847a6b 100644 --- a/spec/Formatter/CurlCommandFormatterSpec.php +++ b/spec/Formatter/CurlCommandFormatterSpec.php @@ -91,6 +91,24 @@ function it_formats_requests_with_null_bytes(RequestInterface $request, UriInter $this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' --request POST --data '[binary stream omitted]'"); } + function it_formats_requests_with_line_break(RequestInterface $request, UriInterface $uri, StreamInterface $body) + { + $request->getUri()->willReturn($uri); + $request->getBody()->willReturn($body); + + $body->__toString()->willReturn("foo\nbar"); + $body->getSize()->willReturn(1); + $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 'foo\nbar'"); + } + function it_formats_requests_with_nonseekable_body(RequestInterface $request, UriInterface $uri, StreamInterface $body) { $request->getUri()->willReturn($uri); diff --git a/spec/Formatter/FullHttpMessageFormatterSpec.php b/spec/Formatter/FullHttpMessageFormatterSpec.php index af45c7f..5d2e6bf 100644 --- a/spec/Formatter/FullHttpMessageFormatterSpec.php +++ b/spec/Formatter/FullHttpMessageFormatterSpec.php @@ -245,6 +245,28 @@ function it_omits_body_with_null_bytes(RequestInterface $request, StreamInterfac GET /foo HTTP/1.1 [binary stream omitted] +STR; + $this->formatRequest($request)->shouldReturn($expectedMessage); + } + + function it_omits_body_with_line_break(RequestInterface $request, StreamInterface $stream) + { + $this->beConstructedWith(7); + + $stream->isSeekable()->willReturn(true); + $stream->rewind()->shouldBeCalled(); + $stream->__toString()->willReturn("foo\nbar"); + $request->getBody()->willReturn($stream); + $request->getMethod()->willReturn('GET'); + $request->getRequestTarget()->willReturn('/foo'); + $request->getProtocolVersion()->willReturn('1.1'); + $request->getHeaders()->willReturn([]); + + $expectedMessage = <<formatRequest($request)->shouldReturn($expectedMessage); } diff --git a/src/Formatter/CurlCommandFormatter.php b/src/Formatter/CurlCommandFormatter.php index 6554fc1..78b1d55 100644 --- a/src/Formatter/CurlCommandFormatter.php +++ b/src/Formatter/CurlCommandFormatter.php @@ -42,7 +42,8 @@ public function formatRequest(RequestInterface $request) } elseif ($body->isSeekable()) { $data = $body->__toString(); $body->rewind(); - if (preg_match('/[\x00-\x1F\x7F]/', $data)) { + // all non-printable ASCII characters and except for \t, \r, \n + if (preg_match('/([\x00-\x09\x0C\x0E-\x1F\x7F])/', $data)) { $data = '[binary stream omitted]'; } } else { diff --git a/src/Formatter/FullHttpMessageFormatter.php b/src/Formatter/FullHttpMessageFormatter.php index d252d64..64ce3ce 100644 --- a/src/Formatter/FullHttpMessageFormatter.php +++ b/src/Formatter/FullHttpMessageFormatter.php @@ -86,7 +86,8 @@ private function addBody(MessageInterface $request, $message) $data = $stream->__toString(); $stream->rewind(); - if (preg_match('/[\x00-\x1F\x7F]/', $data)) { + // all non-printable ASCII characters and except for \t, \r, \n + if (preg_match('/([\x00-\x09\x0C\x0E-\x1F\x7F])/', $data)) { return $message.'[binary stream omitted]'; }