Skip to content

Commit 1d1686c

Browse files
authored
Capture invalid IP address exceptions (#931)
1 parent eaee154 commit 1d1686c

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ parameters:
332332

333333
-
334334
message: "#^Access to undefined constant Symfony\\\\Component\\\\HttpKernel\\\\HttpKernelInterface\\:\\:MASTER_REQUEST\\.$#"
335-
count: 6
335+
count: 7
336336
path: tests/EventListener/RequestListenerTest.php
337337

338338
-

src/EventListener/RequestListener.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ public function handleKernelRequestEvent(RequestEvent $event): void
5656
$user = $scope->getUser() ?? new UserDataBag();
5757

5858
if (null === $user->getIpAddress()) {
59-
$user->setIpAddress($event->getRequest()->getClientIp());
59+
try {
60+
$user->setIpAddress($event->getRequest()->getClientIp());
61+
} catch (\InvalidArgumentException $e) {
62+
// If the IP is in an invalid format, we ignore it
63+
}
6064
}
6165

6266
$scope->setUser($user);

tests/EventListener/RequestListenerTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ public function handleKernelRequestEventDataProvider(): \Generator
121121
new UserDataBag('foo_user', null, '::1'),
122122
new UserDataBag('foo_user', null, '::1'),
123123
];
124+
125+
yield 'remote address empty' => [
126+
new RequestEvent(
127+
$this->createMock(HttpKernelInterface::class),
128+
new Request([], [], [], [], [], ['REMOTE_ADDR' => '']),
129+
\defined(HttpKernelInterface::class . '::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST
130+
),
131+
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
132+
new UserDataBag(),
133+
new UserDataBag(),
134+
];
124135
}
125136

126137
/**

0 commit comments

Comments
 (0)