Skip to content

Commit ff38765

Browse files
committed
Capture invalid IP address exceptions
1 parent eaee154 commit ff38765

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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)