Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix regression introduced in #1119 that prevents capturing of the request body #1143

Merged
merged 1 commit into from
Nov 27, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Fix regression introduced in #1129 (#1143)
- Fix capturing of the request body in the `RequestIntegration` integration when the stream is empty (#1129)

### 2.5.0 (2020-09-14)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"ext-json": "*",
"ext-mbstring": "*",
"guzzlehttp/promises": "^1.3",
"guzzlehttp/psr7": "^1.6",
"guzzlehttp/psr7": "^1.7",
"jean85/pretty-package-versions": "^1.2",
"php-http/async-client-implementation": "^1.0",
"php-http/client-common": "^1.5|^2.0",
Expand Down
63 changes: 46 additions & 17 deletions src/Integration/RequestIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sentry\Integration;

use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;
use Sentry\Event;
Expand Down Expand Up @@ -35,6 +36,17 @@ final class RequestIntegration implements IntegrationInterface
*/
private const REQUEST_BODY_MEDIUM_MAX_CONTENT_LENGTH = 10 ** 4;

/**
* This constant is a map of maximum allowed sizes for each value of the
* `max_request_body_size` option.
*/
private const MAX_REQUEST_BODY_SIZE_OPTION_TO_MAX_LENGTH_MAP = [
'none' => 0,
'small' => self::REQUEST_BODY_SMALL_MAX_CONTENT_LENGTH,
'medium' => self::REQUEST_BODY_MEDIUM_MAX_CONTENT_LENGTH,
'always' => -1,
];

/**
* @var Options|null The client options
*/
Expand Down Expand Up @@ -176,45 +188,41 @@ static function (string $key) use ($keysToRemove): bool {
* the parsing fails then the raw data is returned. If there are submitted
* fields or files, all of their information are parsed and returned.
*
* @param Options $options The options of the client
* @param ServerRequestInterface $serverRequest The server request
* @param Options $options The options of the client
* @param ServerRequestInterface $request The server request
*
* @return mixed
*/
private function captureRequestBody(Options $options, ServerRequestInterface $serverRequest)
private function captureRequestBody(Options $options, ServerRequestInterface $request)
{
$maxRequestBodySize = $options->getMaxRequestBodySize();
$requestBody = $serverRequest->getBody();
$requestBodySize = $requestBody->getSize();

if (
!$requestBodySize ||
'none' === $maxRequestBodySize ||
('small' === $maxRequestBodySize && $requestBodySize > self::REQUEST_BODY_SMALL_MAX_CONTENT_LENGTH) ||
('medium' === $maxRequestBodySize && $requestBodySize > self::REQUEST_BODY_MEDIUM_MAX_CONTENT_LENGTH)
) {
$requestBodySize = (int) $request->getHeaderLine('Content-Length');

if (!$this->isRequestBodySizeWithinReadBounds($requestBodySize, $maxRequestBodySize)) {
return null;
}

$requestData = $serverRequest->getParsedBody();
$requestData = $request->getParsedBody();
$requestData = array_merge(
$this->parseUploadedFiles($serverRequest->getUploadedFiles()),
$this->parseUploadedFiles($request->getUploadedFiles()),
\is_array($requestData) ? $requestData : []
);

if (!empty($requestData)) {
return $requestData;
}

if ('application/json' === $serverRequest->getHeaderLine('Content-Type')) {
$requestBody = Utils::copyToString($request->getBody(), self::MAX_REQUEST_BODY_SIZE_OPTION_TO_MAX_LENGTH_MAP[$maxRequestBodySize]);

if ('application/json' === $request->getHeaderLine('Content-Type')) {
try {
return JSON::decode($requestBody->getContents());
return JSON::decode($requestBody);
} catch (JsonException $exception) {
// Fallback to returning the raw data from the request body
}
}

return $requestBody->getContents();
return $requestBody;
}

/**
Expand Down Expand Up @@ -245,4 +253,25 @@ private function parseUploadedFiles(array $uploadedFiles): array

return $result;
}

private function isRequestBodySizeWithinReadBounds(int $requestBodySize, string $maxRequestBodySize): bool
{
if ($requestBodySize <= 0) {
return false;
}

if ('none' === $maxRequestBodySize) {
return false;
}

if ('small' === $maxRequestBodySize && $requestBodySize > self::REQUEST_BODY_SMALL_MAX_CONTENT_LENGTH) {
return false;
}

if ('medium' === $maxRequestBodySize && $requestBodySize > self::REQUEST_BODY_MEDIUM_MAX_CONTENT_LENGTH) {
return false;
}

return true;
}
}
Loading