Skip to content

Commit

Permalink
Allow bracket IPv6 address format inside IPAdress Normalizer
Browse files Browse the repository at this point in the history
When run with php's build-in server (for instance on localhost:8080), IP provided through $this->server['REMOTE_ADDR'] is [::1], which is not an acceptable format for \inet_pton. This removes the brackets if there's any.

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
tcitworld committed Jan 3, 2019
1 parent 4d8f2c2 commit c9b5887
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/private/Security/Normalizer/IpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ private function getIPv4Subnet(string $ip, int $maskBits = 32): string {
* @return string
*/
private function getIPv6Subnet(string $ip, int $maskBits = 48): string {
if ($ip[0] === '[' && $ip[-1] === ']') { // If IP is with brackets, for example [::1]
$ip = substr($ip, 1, strlen($ip) - 2);
}
$binary = \inet_pton($ip);
for ($i = 128; $i > $maskBits; $i -= 8) {
$j = \intdiv($i, 8) - 1;
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/Security/Normalizer/IpAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function subnetDataProvider() {
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
'2001:db8:85a3::8a2e:370:7334/128',
],
[
'[::1]',
'::1/128',
],
];
}

Expand Down

0 comments on commit c9b5887

Please sign in to comment.