Skip to content

Commit 706ba1a

Browse files
committed
MB-35505 Validate hostname
Change-Id: Id8755033baab4e7542c3abd144f1e9f7b4dd6b71 Reviewed-on: http://review.couchbase.org/113136 Reviewed-by: Carlos Gonzalez <carlos.gonzalez@couchbase.com> Tested-by: Build Bot <build@couchbase.com>
1 parent 7561cb1 commit 706ba1a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

cbmgr.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import getpass
44
import inspect
5+
import ipaddress
56
import json
67
import os
78
import platform
@@ -244,7 +245,16 @@ def __call__(self, parser, namespace, values, option_string=None):
244245
parsed = urllib.parse.urlparse("http://" + values)
245246

246247
if parsed.path != "" or parsed.params != "" or parsed.query != "" or parsed.fragment != "":
247-
raise ArgumentError(self, "%s is not an accepted hostname" % values)
248+
raise ArgumentError(self, f"{values} is not an accepted hostname")
249+
if not parsed.hostname:
250+
raise ArgumentError(self, f"{values} is not an accepted hostname")
251+
hostname_regex = re.compile("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$");
252+
if not hostname_regex.match(parsed.hostname):
253+
try:
254+
ipaddress.ip_address(parsed.hostname)
255+
except ValueError:
256+
raise ArgumentError(self, f"{values} is not an accepted hostname")
257+
248258

249259
scheme = parsed.scheme
250260
port = None

0 commit comments

Comments
 (0)