Skip to content

Commit 7638eeb

Browse files
committed
Improve localhost binding prevention
1 parent e75f1d0 commit 7638eeb

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Config/Config.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Kiwicom\Loopbind\Config;
44

55
use JsonSerializable;
6+
use Kiwicom\Loopbind\Helpers\IPHelpers;
67
use function array_map;
78
use function filter_var;
89
use function is_array;
@@ -37,10 +38,10 @@ public function __construct(
3738
array_map(fn (string $host): bool => filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) === false ?
3839
throw new \Kiwicom\Loopbind\Exceptions\InvalidHostnameException("Value `{$host}` is not valid hostname.") : true, $hostname);
3940

40-
array_map(fn (string $hostname): bool => $hostname === 'localhost' ?
41+
array_map(fn (string $hostname): bool => IPHelpers::isForbiddenDomain($hostname) ?
4142
throw new \Kiwicom\Loopbind\Exceptions\InvalidHostnameException("Hostname `{$hostname}` is forbidden by this tool.") : true, $hostname);
4243
}
43-
if ($hostname === 'localhost') {
44+
if (is_string($hostname) && IPHelpers::isForbiddenDomain($hostname)) {
4445
throw new \Kiwicom\Loopbind\Exceptions\InvalidHostnameException("Hostname `{$hostname}` is forbidden by this tool.");
4546
}
4647

src/Helpers/IPHelpers.php

+27
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,31 @@ public static function findRandomFreeLocalIP(): string
3434
}
3535
throw new UnableToFindUnreferencedIPAddressException();
3636
}
37+
38+
/**
39+
* Returns true iff the given URL is a forbidden domain
40+
* @param string $url
41+
* @return bool
42+
*/
43+
public static function isForbiddenDomain(string $url): bool
44+
{
45+
$url = mb_strtolower($url);
46+
$forbidden = [
47+
'localhost',
48+
'localhost4',
49+
'localhost6',
50+
'localhost.localdomain',
51+
'localhost4.localdomain4',
52+
'localhost6.localdomain6',
53+
'ip6-localhost',
54+
'ip6-loopback',
55+
'ip6-localnet',
56+
'ip6-mcastprefix',
57+
'ip6-allnodes',
58+
'ip6-allrouters',
59+
'ip6-allhosts',
60+
'broadcasthost'
61+
];
62+
return in_array($url, $forbidden, true);
63+
}
3764
}

0 commit comments

Comments
 (0)