Skip to content

Commit dc394e1

Browse files
authored
Merge pull request #201 from getsentry/respect-send-pii
Add PII info only if option is enabled
2 parents e152ca8 + d83c7c4 commit dc394e1

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ update-submodules:
99
git submodule update
1010

1111
cs:
12-
vendor/bin/php-cs-fixer fix --config-file=.php_cs --verbose --diff
12+
vendor/bin/php-cs-fixer fix --verbose
1313

1414
cs-dry-run:
15-
vendor/bin/php-cs-fixer fix --config-file=.php_cs --verbose --diff --dry-run
15+
vendor/bin/php-cs-fixer fix --verbose --dry-run
16+
17+
phpstan:
18+
vendor/bin/phpstan analyze
1619

1720
test:
1821
vendor/bin/phpunit
1922

23+
pre-commit-check: cs phpstan test
24+
2025
setup-git:
2126
git config branch.autosetuprebase always

src/EventListener/RequestListener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public function onKernelRequest(GetResponseEvent $event): void
5454
return;
5555
}
5656

57+
$currentClient = Hub::getCurrent()->getClient();
58+
if (null === $currentClient || ! $currentClient->getOptions()->shouldSendDefaultPii()) {
59+
return;
60+
}
61+
5762
$token = null;
5863

5964
if ($this->tokenStorage instanceof TokenStorageInterface) {

test/EventListener/RequestListenerTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use PHPUnit\Framework\TestCase;
66
use Prophecy\Argument;
7+
use Sentry\ClientInterface;
8+
use Sentry\Options;
79
use Sentry\SentryBundle\EventListener\RequestListener;
810
use Sentry\State\Hub;
911
use Sentry\State\HubInterface;
@@ -21,13 +23,24 @@ class RequestListenerTest extends TestCase
2123
{
2224
private $currentScope;
2325
private $currentHub;
26+
private $options;
2427

2528
protected function setUp()
2629
{
2730
parent::setUp();
2831

2932
$this->currentScope = $scope = new Scope();
3033
$this->currentHub = $this->prophesize(HubInterface::class);
34+
35+
$client = $this->prophesize(ClientInterface::class);
36+
$this->options = new Options();
37+
38+
$this->currentHub->getClient()
39+
->willReturn($client->reveal());
40+
$client->getOptions()
41+
->willReturn($this->options);
42+
$this->options->setSendDefaultPii(true);
43+
3144
$this->currentHub->configureScope(Argument::type('callable'))
3245
->shouldBeCalled()
3346
->will(function ($arguments) use ($scope): void {
@@ -96,6 +109,56 @@ public function userDataProvider(): \Generator
96109
yield [new ToStringUser('john-doe')];
97110
}
98111

112+
public function testOnKernelRequestUserDataIsNotSetIfSendPiiIsDisabled(): void
113+
{
114+
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
115+
$authorizationChecker = $this->prophesize(AuthorizationCheckerInterface::class);
116+
$event = $this->prophesize(GetResponseEvent::class);
117+
118+
$event->isMasterRequest()
119+
->willReturn(true);
120+
121+
$this->options->setSendDefaultPii(false);
122+
123+
$this->currentHub->configureScope(Argument::type('callable'))
124+
->shouldNotBeCalled();
125+
126+
$listener = new RequestListener(
127+
$this->currentHub->reveal(),
128+
$tokenStorage->reveal(),
129+
$authorizationChecker->reveal()
130+
);
131+
132+
$listener->onKernelRequest($event->reveal());
133+
134+
$this->assertEquals([], $this->currentScope->getUser());
135+
}
136+
137+
public function testOnKernelRequestUserDataIsNotSetIfNoClientIsPresent(): void
138+
{
139+
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
140+
$authorizationChecker = $this->prophesize(AuthorizationCheckerInterface::class);
141+
$event = $this->prophesize(GetResponseEvent::class);
142+
143+
$event->isMasterRequest()
144+
->willReturn(true);
145+
146+
$this->currentHub->getClient()
147+
->willReturn(null);
148+
$this->currentHub->configureScope(Argument::type('callable'))
149+
->shouldNotBeCalled();
150+
151+
$listener = new RequestListener(
152+
$this->currentHub->reveal(),
153+
$tokenStorage->reveal(),
154+
$authorizationChecker->reveal()
155+
);
156+
157+
$listener->onKernelRequest($event->reveal());
158+
159+
$this->assertEquals([], $this->currentScope->getUser());
160+
}
161+
99162
public function testOnKernelRequestUsernameIsNotSetIfTokenStorageIsAbsent(): void
100163
{
101164
$authorizationChecker = $this->prophesize(AuthorizationCheckerInterface::class);

0 commit comments

Comments
 (0)