Skip to content
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: 7 additions & 2 deletions src/Config/HostsFile.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -103,10 +103,12 @@ public function getIpsForHost($name)
if ($parts && array_search($name, $parts) !== false) {
// remove IPv6 zone ID (`fe80::1%lo0` => `fe80:1`)
if (strpos($ip, ':') !== false && ($pos = strpos($ip, '%')) !== false) {
$ip= substr($ip, 0, $pos);
$ip= substr($ip, 0, $pos);
}

$ips[] = $ip;
if (@inet_pton($ip) !== false) {
$ips[] = $ip;
}
}
}

Expand All@@ -123,6 +125,9 @@ public function getHostsForIp($ip)
{
// check binary representation of IP to avoid string case and short notation
$ip = @inet_pton($ip);
if ($ip === false) {
return array();
}

$names = array();
foreach (preg_split('/\r?\n/', $this->contents) as $line) {
Expand Down
16 changes: 16 additions & 0 deletions tests/Config/HostsFileTest.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -41,6 +41,14 @@ public function testContainsSingleLocalhostEntry()
$this->assertEquals(array(), $hosts->getIpsForHost('example.com'));
}

public function testNonIpReturnsNothingForInvalidHosts()
{
$hosts = new HostsFile('a b');

$this->assertEquals(array(), $hosts->getIpsForHost('a'));
$this->assertEquals(array(), $hosts->getIpsForHost('b'));
}

public function testIgnoresIpv6ZoneId()
{
$hosts = new HostsFile('fe80::1%lo0 localhost');
Expand DownExpand Up@@ -108,6 +116,14 @@ public function testReverseNonIpReturnsNothing()
$this->assertEquals(array(), $hosts->getHostsForIp('127.0.0.1.1'));
}

public function testReverseNonIpReturnsNothingForInvalidHosts()
{
$hosts = new HostsFile('a b');

$this->assertEquals(array(), $hosts->getHostsForIp('a'));
$this->assertEquals(array(), $hosts->getHostsForIp('b'));
}

public function testReverseLookupReturnsLowerCaseHost()
{
$hosts = new HostsFile('127.0.0.1 LocalHost');
Expand Down