Skip to content

Commit

Permalink
Merge pull request #117 from buggregator/hotfix/sentry-client
Browse files Browse the repository at this point in the history
Fixes problem with parsing request payload with gzip content-type
  • Loading branch information
butschster authored Dec 9, 2023
2 parents 9530d47 + 1b9e870 commit 3c4f3cc
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/modules/Sentry/Application/PayloadParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(
public function parse(ServerRequestInterface $request): array
{
$isV4 = $request->getHeaderLine('Content-Type') === 'application/x-sentry-envelope' ||
\str_contains($request->getHeaderLine('X-Sentry-Auth'), 'sentry.php/4');
\str_contains($request->getHeaderLine('X-Sentry-Auth'), 'sentry_client=sentry.php');

if ($isV4) {
if ($request->getHeaderLine('Content-Encoding') === 'gzip') {
Expand Down
2 changes: 2 additions & 0 deletions app/modules/Sentry/Interfaces/Http/Handler/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function handle(ServerRequestInterface $request, \Closure $next): Respons

$payloads = $this->payloadParser->parse($request);

dump($payloads);

match (true) {
\str_ends_with($url, '/envelope') => $this->handleEnvelope($payloads),
\str_ends_with($url, '/store') => $this->handleEvent($payloads),
Expand Down
2 changes: 1 addition & 1 deletion app/src/Application/Bootloader/RoutesBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace App\Application\Bootloader;

use App\Application\HTTP\Middleware\JsonPayloadMiddleware;
use App\Interfaces\Http\EventHandlerAction;
use Spiral\Bootloader\Http\RoutesBootloader as BaseRoutesBootloader;
use Spiral\Filter\ValidationHandlerMiddleware;
use Spiral\Http\Middleware\ErrorHandlerMiddleware;
use Spiral\Http\Middleware\JsonPayloadMiddleware;
use Spiral\Router\Bootloader\AnnotatedRoutesBootloader;
use Spiral\Router\GroupRegistry;
use Spiral\Router\Loader\Configurator\RoutingConfigurator;
Expand Down
52 changes: 52 additions & 0 deletions app/src/Application/HTTP/Middleware/JsonPayloadMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace App\Application\HTTP\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Spiral\Bootloader\Http\JsonPayloadConfig;

final class JsonPayloadMiddleware implements MiddlewareInterface
{
public function __construct(
private readonly JsonPayloadConfig $config,
) {
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($this->isJsonPayload($request)) {
$body = (string)$request->getBody();
if ($body !== '') {
$parsedBody = \json_decode($body, true);
if (\json_last_error() === 0) {
$request = $request->withParsedBody($parsedBody);
}
}
}

return $handler->handle($request);
}

private function isJsonPayload(ServerRequestInterface $request): bool
{
if ($request->getHeaderLine('Content-Encoding') === 'gzip') {
return false;
}

$contentType = $request->getHeaderLine('Content-Type');

foreach ($this->config->getContentTypes() as $allowedType) {
if (\stripos($contentType, $allowedType) === 0) {
return true;
}
}

return false;
}
}

32 changes: 32 additions & 0 deletions tests/Feature/Interfaces/Http/Sentry/SentryV4ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,38 @@ public function testSendGzipped(): void
$this->assertNotEmpty($data['data']['timestamp']);


return true;
});
}

public function testSendGzippedSpiral(): void
{
$this->http
->postJson(
uri: '/api/1/envelope/',
data: Stream::create(\gzcompress(self::JSON, -1, \ZLIB_ENCODING_GZIP)),
headers: [
'Content-Encoding' => 'gzip',
'X-Buggregator-Event' => 'sentry',
'Content-Type' => 'application/json',
'X-Sentry-Auth' => 'Sentry sentry_version=7, sentry_client=sentry.php.spiral/3.1.2, sentry_key=sentry',
],
)->assertOk();

$this->broadcastig->assertPushed('events', function (array $data) {
$this->assertSame('event.received', $data['event']);
$this->assertSame('sentry', $data['data']['type']);

$this->assertSame(1701455435.634665, $data['data']['payload']['timestamp']);
$this->assertSame('php', $data['data']['payload']['platform']);
$this->assertSame('sentry.php', $data['data']['payload']['sdk']['name']);
$this->assertSame('4.0.1', $data['data']['payload']['sdk']['version']);
$this->assertSame('Test', $data['data']['payload']['server_name']);
$this->assertSame('production', $data['data']['payload']['environment']);

$this->assertNotEmpty($data['data']['uuid']);
$this->assertNotEmpty($data['data']['timestamp']);

return true;
});
}
Expand Down

0 comments on commit 3c4f3cc

Please sign in to comment.