Skip to content

Commit 10ff70e

Browse files
authored
prevent dwonload of unpausible response bodies (#50)
1 parent b5e5084 commit 10ff70e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

app/RemoteSite/Connection.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use GuzzleHttp\Exception\RequestException;
1616
use GuzzleHttp\Psr7\Request;
1717
use GuzzleHttp\Psr7\Response;
18+
use GuzzleHttp\Psr7\Utils;
1819
use Illuminate\Support\Facades\App;
1920
use Psr\Http\Message\RequestInterface;
2021

@@ -121,12 +122,33 @@ protected function performHttpRequest(
121122
/** @var Client $httpClient */
122123
$httpClient = App::make(Client::class);
123124

125+
// Send a streamed response to be able to validate the size
126+
$options['stream'] = true;
127+
$options['progress'] = function (
128+
$downloadTotal,
129+
$downloadedBytes
130+
) use ($request) {
131+
if ($downloadedBytes > 1024000) {
132+
throw new \RuntimeException("Unplausible response size while fetching from " . $request->getUri());
133+
}
134+
};
135+
124136
/** @var Response $response */
125137
$response = $httpClient->send(
126138
$request,
127139
$options
128140
);
129141

142+
// Convert the streamed response into a "normal" one
143+
$buffer = '';
144+
145+
while (!$response->getBody()->eof()) {
146+
$buffer .= $response->getBody()->read(8192);
147+
}
148+
149+
// Overwrite streamed body
150+
$response = $response->withBody(Utils::streamFor($buffer));
151+
130152
// Validate response
131153
if (!json_validate((string) $response->getBody())) {
132154
throw new RequestException(

0 commit comments

Comments
 (0)