Skip to content

[HttpFoundation] Check IPv6 is valid before comparing it #48050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2022
Merged
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
9 changes: 9 additions & 0 deletions src/Symfony/Component/HttpFoundation/IpUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ public static function checkIp6($requestIp, $ip)
throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
}

// Check to see if we were given a IP4 $requestIp or $ip by mistake
if (str_contains($requestIp, '.') || str_contains($ip, '.')) {
return self::$checkedIps[$cacheKey] = false;
}

if (!filter_var($requestIp, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
return self::$checkedIps[$cacheKey] = false;
}

if (str_contains($ip, '/')) {
[$address, $netmask] = explode('/', $ip, 2);

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/IpUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function getIpv6Data()
[false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'],
[false, '', '::1'],
[false, null, '::1'],
[false, '127.0.0.1', '::1'],
[false, '0.0.0.0/8', '::1'],
[false, '::1', '127.0.0.1'],
[false, '::1', '0.0.0.0/8'],
];
}

Expand Down