Skip to content

Commit 8b943da

Browse files
cleptricclaude
andauthored
fix(UserDataBag): Handle bracketed IPv6 addresses in setIpAddress (#2007)
Co-authored-by: Claude <[email protected]>
1 parent 1904b90 commit 8b943da

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

src/UserDataBag.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,23 @@ public function getIpAddress(): ?string
220220
*/
221221
public function setIpAddress(?string $ipAddress): self
222222
{
223-
if ($ipAddress !== null && filter_var($ipAddress, \FILTER_VALIDATE_IP) === false) {
224-
throw new \InvalidArgumentException(\sprintf('The "%s" value is not a valid IP address.', $ipAddress));
223+
if ($ipAddress !== null) {
224+
// Strip brackets from IPv6 addresses (e.g. [::1] -> ::1)
225+
if (strpos($ipAddress, '[') === 0 && substr($ipAddress, -1) === ']') {
226+
$ipAddress = substr($ipAddress, 1, -1);
227+
}
228+
229+
if (filter_var($ipAddress, \FILTER_VALIDATE_IP) === false) {
230+
$client = SentrySdk::getCurrentHub()->getClient();
231+
232+
if ($client !== null) {
233+
$client->getOptions()->getLoggerOrNullLogger()->debug(
234+
\sprintf('The "%s" value is not a valid IP address.', $ipAddress)
235+
);
236+
}
237+
238+
return $this;
239+
}
225240
}
226241

227242
$this->ipAddress = $ipAddress;

tests/UserDataBagTest.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,29 +162,46 @@ public static function unexpectedValueForIdFieldDataProvider(): iterable
162162
];
163163
}
164164

165-
public function testConstructorThrowsIfIpAddressArgumentIsInvalid(): void
165+
/**
166+
* @dataProvider bracketedIpv6AddressDataProvider
167+
*/
168+
public function testSetIpAddressStripsBracketsFromIpv6(string $input, string $expected): void
166169
{
167-
$this->expectException(\InvalidArgumentException::class);
168-
$this->expectExceptionMessage('The "foo" value is not a valid IP address.');
170+
$userDataBag = new UserDataBag();
171+
$userDataBag->setIpAddress($input);
169172

170-
new UserDataBag(null, null, 'foo');
173+
$this->assertSame($expected, $userDataBag->getIpAddress());
174+
}
175+
176+
public static function bracketedIpv6AddressDataProvider(): iterable
177+
{
178+
yield 'IPv6 loopback with brackets' => ['[::1]', '::1'];
179+
yield 'IPv6 full address with brackets' => ['[2001:db8::1]', '2001:db8::1'];
180+
yield 'IPv6 loopback without brackets' => ['::1', '::1'];
181+
yield 'IPv4 address' => ['127.0.0.1', '127.0.0.1'];
171182
}
172183

173-
public function testSetIpAddressThrowsIfArgumentIsInvalid(): void
184+
public function testConstructorDoesNotSetInvalidIpAddress(): void
174185
{
175-
$this->expectException(\InvalidArgumentException::class);
176-
$this->expectExceptionMessage('The "foo" value is not a valid IP address.');
186+
$userDataBag = new UserDataBag(null, null, 'foo');
177187

188+
$this->assertNull($userDataBag->getIpAddress());
189+
}
190+
191+
public function testSetIpAddressDoesNotSetInvalidIpAddress(): void
192+
{
178193
$userDataBag = new UserDataBag();
194+
$userDataBag->setIpAddress('127.0.0.1');
179195
$userDataBag->setIpAddress('foo');
196+
197+
$this->assertSame('127.0.0.1', $userDataBag->getIpAddress());
180198
}
181199

182-
public function testCreateFromIpAddressThrowsIfArgumentIsInvalid(): void
200+
public function testCreateFromIpAddressDoesNotSetInvalidIpAddress(): void
183201
{
184-
$this->expectException(\InvalidArgumentException::class);
185-
$this->expectExceptionMessage('The "foo" value is not a valid IP address.');
202+
$userDataBag = UserDataBag::createFromUserIpAddress('foo');
186203

187-
UserDataBag::createFromUserIpAddress('foo');
204+
$this->assertNull($userDataBag->getIpAddress());
188205
}
189206

190207
public function testMerge(): void

0 commit comments

Comments
 (0)