Skip to content
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## Unreleased

## 3.0.4 (2020-11-6)
- Fix capturing of the request body in the `RequestIntegration` integration (#1139)

## 3.0.4 (2020-11-06)

- Fix stacktrace missing from payload for non-exception events (#1123)
- Fix capturing of the request body in the `RequestIntegration` integration when the stream is empty (#1119)
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.5",
"ocramius/package-versions": "^1.8",
"php-http/async-client-implementation": "^1.0",
Expand Down
50 changes: 39 additions & 11 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 @@ -36,6 +37,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 RequestFetcherInterface PSR-7 request fetcher
*/
Expand Down Expand Up @@ -155,16 +167,9 @@ static function (string $key) use ($keysToRemove): bool {
private function captureRequestBody(Options $options, ServerRequestInterface $request)
{
$maxRequestBodySize = $options->getMaxRequestBodySize();
$requestBodySize = (int) $request->getHeaderLine('Content-Length');

$requestBody = $request->getBody();
$requestBodySize = $request->getBody()->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)
) {
if (!$this->isRequestBodySizeWithinReadBounds($requestBodySize, $maxRequestBodySize)) {
return null;
}

Expand All @@ -178,15 +183,17 @@ private function captureRequestBody(Options $options, ServerRequestInterface $re
return $requestData;
}

$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 @@ -217,4 +224,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