Skip to content
Open
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
19 changes: 17 additions & 2 deletions src/UserDataBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,23 @@ public function getIpAddress(): ?string
*/
public function setIpAddress(?string $ipAddress): self
{
if ($ipAddress !== null && filter_var($ipAddress, \FILTER_VALIDATE_IP) === false) {
throw new \InvalidArgumentException(\sprintf('The "%s" value is not a valid IP address.', $ipAddress));
if ($ipAddress !== null) {
// Strip brackets from IPv6 addresses (e.g. [::1] -> ::1)
if (strpos($ipAddress, '[') === 0 && substr($ipAddress, -1) === ']') {
$ipAddress = substr($ipAddress, 1, -1);
}

if (filter_var($ipAddress, \FILTER_VALIDATE_IP) === false) {
$client = SentrySdk::getCurrentHub()->getClient();

if ($client !== null) {
$client->getOptions()->getLoggerOrNullLogger()->debug(
\sprintf('The "%s" value is not a valid IP address.', $ipAddress)
);
}

return $this;
}
}

$this->ipAddress = $ipAddress;
Expand Down
39 changes: 28 additions & 11 deletions tests/UserDataBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,29 +162,46 @@ public static function unexpectedValueForIdFieldDataProvider(): iterable
];
}

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

new UserDataBag(null, null, 'foo');
$this->assertSame($expected, $userDataBag->getIpAddress());
}

public static function bracketedIpv6AddressDataProvider(): iterable
{
yield 'IPv6 loopback with brackets' => ['[::1]', '::1'];
yield 'IPv6 full address with brackets' => ['[2001:db8::1]', '2001:db8::1'];
yield 'IPv6 loopback without brackets' => ['::1', '::1'];
yield 'IPv4 address' => ['127.0.0.1', '127.0.0.1'];
}

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

$this->assertNull($userDataBag->getIpAddress());
}

public function testSetIpAddressDoesNotSetInvalidIpAddress(): void
{
$userDataBag = new UserDataBag();
$userDataBag->setIpAddress('127.0.0.1');
$userDataBag->setIpAddress('foo');

$this->assertSame('127.0.0.1', $userDataBag->getIpAddress());
}

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

UserDataBag::createFromUserIpAddress('foo');
$this->assertNull($userDataBag->getIpAddress());
}

public function testMerge(): void
Expand Down