Skip to content

Fix regression that send PII even when the send_default_pii option is off #425

New issue

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

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

Already on GitHub? Sign in to your account

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

## Unreleased

- Added missing `capture-soft-fails` config schema option (#417)
- Add missing `capture-soft-fails` option to the XSD schema for the XML config (#417)
- Fix regression that send PII even when the `send_default_pii` option is off (#425)

## 4.0.0 (2021-01-19)

Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ parameters:

-
message: "#^Instantiated class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseEvent not found\\.$#"
count: 6
count: 8
path: tests/EventListener/RequestListenerTest.php

-
Expand Down
6 changes: 6 additions & 0 deletions src/EventListener/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public function handleKernelRequestEvent(RequestListenerRequestEvent $event): vo
return;
}

$client = $this->hub->getClient();

if (null === $client || !$client->getOptions()->shouldSendDefaultPii()) {
return;
}

$token = null;
$userData = UserDataBag::createFromUserIpAddress($event->getRequest()->getClientIp());

Expand Down
78 changes: 75 additions & 3 deletions tests/EventListener/RequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\Options;
use Sentry\SentryBundle\EventListener\RequestListener;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
Expand Down Expand Up @@ -53,15 +55,19 @@ protected function setUp(): void
*
* @param GetResponseEvent|RequestEvent $requestEvent
*/
public function testHandleKernelRequestEvent($requestEvent, ?TokenInterface $token, UserDataBag $expectedUser): void
public function testHandleKernelRequestEvent($requestEvent, ?ClientInterface $client, ?TokenInterface $token, ?UserDataBag $expectedUser): void
{
$scope = new Scope();

$this->tokenStorage->expects($this->once())
$this->hub->expects($this->any())
->method('getClient')
->willReturn($client);

$this->tokenStorage->expects($this->any())
->method('getToken')
->willReturn($token);

$this->hub->expects($this->once())
$this->hub->expects($this->any())
->method('configureScope')
->willReturnCallback(static function (callable $callback) use ($scope): void {
$callback($scope);
Expand All @@ -83,12 +89,35 @@ public function handleKernelRequestEventForSymfonyVersionLowerThan43DataProvider
return;
}

yield 'event.requestType != MASTER_REQUEST' => [
new GetResponseEvent(
$this->createMock(HttpKernelInterface::class),
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::SUB_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
null,
null,
];

yield 'options.send_default_pii = FALSE' => [
new GetResponseEvent(
$this->createMock(HttpKernelInterface::class),
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => false])),
null,
null,
];

yield 'token IS NULL' => [
new GetResponseEvent(
$this->createMock(HttpKernelInterface::class),
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
null,
UserDataBag::createFromUserIpAddress('127.0.0.1'),
];
Expand All @@ -99,6 +128,7 @@ public function handleKernelRequestEventForSymfonyVersionLowerThan43DataProvider
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
new class() extends AbstractToken {
public function __construct()
{
Expand All @@ -121,6 +151,7 @@ public function getCredentials()
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
new class() extends AbstractToken {
public function __construct()
{
Expand All @@ -143,6 +174,7 @@ public function getCredentials()
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
new class() extends AbstractToken {
public function __construct()
{
Expand All @@ -166,6 +198,7 @@ public function getCredentials()
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
new class() extends AbstractToken {
public function __construct()
{
Expand Down Expand Up @@ -213,6 +246,7 @@ public function getCredentials()
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
new class() extends AbstractToken {
public function __construct()
{
Expand Down Expand Up @@ -245,12 +279,35 @@ public function handleKernelRequestEventForSymfonyVersionAtLeast43DataProvider()
return;
}

yield 'event.requestType != MASTER_REQUEST' => [
new RequestEvent(
$this->createMock(HttpKernelInterface::class),
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::SUB_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
null,
null,
];

yield 'options.send_default_pii = FALSE' => [
new RequestEvent(
$this->createMock(HttpKernelInterface::class),
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => false])),
null,
null,
];

yield 'token IS NULL' => [
new RequestEvent(
$this->createMock(HttpKernelInterface::class),
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
null,
UserDataBag::createFromUserIpAddress('127.0.0.1'),
];
Expand All @@ -261,6 +318,7 @@ public function handleKernelRequestEventForSymfonyVersionAtLeast43DataProvider()
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
new class() extends AbstractToken {
public function __construct()
{
Expand All @@ -283,6 +341,7 @@ public function getCredentials()
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
new class() extends AbstractToken {
public function __construct()
{
Expand All @@ -305,6 +364,7 @@ public function getCredentials()
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
new class() extends AbstractToken {
public function __construct()
{
Expand All @@ -328,6 +388,7 @@ public function getCredentials()
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
new class() extends AbstractToken {
public function __construct()
{
Expand Down Expand Up @@ -375,6 +436,7 @@ public function getCredentials()
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
HttpKernelInterface::MASTER_REQUEST
),
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
new class() extends AbstractToken {
public function __construct()
{
Expand Down Expand Up @@ -511,4 +573,14 @@ static function () {
],
];
}

private function getMockedClientWithOptions(Options $options): ClientInterface
{
$client = $this->createMock(ClientInterface::class);
$client->expects($this->any())
->method('getOptions')
->willReturn($options);

return $client;
}
}