File tree 4 files changed +43
-1
lines changed
4 files changed +43
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
🛠️️ CLI tool to easily bind localhost network interface with additional IP and ensure matching record in /etc/hosts.
4
4
5
+ > [ !WARNING]
6
+ > ** This tool should only be used in local environments and should not be used in production.**
7
+ >
8
+ > ** It is crucial to use this tool only on projects (configs) under your control as it can seriously compromise your /etc/hosts file.**
9
+
5
10
This is a helper tool to be installed via ` composer global ` to machines where the localhost deployment process of
6
11
Docker composition (via ` docker-compose.yaml ` ) is prepared in a way that it binds the ports on IP from localhost subnet block and multiple of such
7
12
compositions should be allowed to run in parallel (differentiated by the IP).
Original file line number Diff line number Diff line change 3
3
namespace Kiwicom \Loopbind \Config ;
4
4
5
5
use JsonSerializable ;
6
+ use Kiwicom \Loopbind \Helpers \IPHelpers ;
6
7
use function array_map ;
7
8
use function filter_var ;
8
9
use function is_array ;
@@ -36,8 +37,11 @@ public function __construct(
36
37
if (is_array ($ hostname )) {
37
38
array_map (fn (string $ host ): bool => filter_var ($ host , FILTER_VALIDATE_DOMAIN , FILTER_FLAG_HOSTNAME ) === false ?
38
39
throw new \Kiwicom \Loopbind \Exceptions \InvalidHostnameException ("Value ` {$ host }` is not valid hostname. " ) : true , $ hostname );
40
+
41
+ array_map (fn (string $ hostname ): bool => IPHelpers::isForbiddenDomain ($ hostname ) ?
42
+ throw new \Kiwicom \Loopbind \Exceptions \InvalidHostnameException ("Hostname ` {$ hostname }` is forbidden by this tool. " ) : true , $ hostname );
39
43
}
40
- if ($ hostname === ' localhost ' ) {
44
+ if (is_string ( $ hostname) && IPHelpers:: isForbiddenDomain ( $ hostname ) ) {
41
45
throw new \Kiwicom \Loopbind \Exceptions \InvalidHostnameException ("Hostname ` {$ hostname }` is forbidden by this tool. " );
42
46
}
43
47
Original file line number Diff line number Diff line change @@ -34,4 +34,31 @@ public static function findRandomFreeLocalIP(): string
34
34
}
35
35
throw new UnableToFindUnreferencedIPAddressException ();
36
36
}
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
+ }
37
64
}
Original file line number Diff line number Diff line change @@ -44,4 +44,10 @@ public function testInvalidHostname2(): void
44
44
$ this ->expectException (\Kiwicom \Loopbind \Exceptions \InvalidHostnameException::class);
45
45
new Config ('127.0.0.12 ' , 'č.test ' );
46
46
}
47
+
48
+ public function testInvalidHostname3 (): void
49
+ {
50
+ $ this ->expectException (\Kiwicom \Loopbind \Exceptions \InvalidHostnameException::class);
51
+ new Config ('127.0.0.12 ' , ['localhost ' ]);
52
+ }
47
53
}
You can’t perform that action at this time.
0 commit comments