3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-26 12:43:09 +01:00

Rewrite is_server_name() to fail on hostnames with - and _

This commit is contained in:
James Lu 2019-09-10 19:31:57 -07:00
parent 462fa91622
commit 083dc6a58f
2 changed files with 7 additions and 16 deletions

View File

@ -774,16 +774,16 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore):
""" """
return str(obj).startswith('#') return str(obj).startswith('#')
@staticmethod
def _isASCII(s):
"""Returns whether the given string only contains non-whitespace ASCII characters."""
chars = string.ascii_letters + string.digits + string.punctuation
return all(char in chars for char in str(s))
# Modified from https://stackoverflow.com/a/106223 (RFC 1123):
# - Allow hostnames that end in '.'
# - Require at least one '.' in the hostname
_HOSTNAME_RE = re.compile(r'^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+'
r'([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])*$')
@classmethod @classmethod
def is_server_name(cls, s): def is_server_name(cls, text):
"""Returns whether the string given is a valid server name.""" """Returns whether the string given is a valid server name."""
return cls._isASCII(s) and '.' in s and not s.startswith('.') return bool(cls._HOSTNAME_RE.match(text))
_HOSTMASK_RE = re.compile(r'^\S+!\S+@\S+$') _HOSTMASK_RE = re.compile(r'^\S+!\S+@\S+$')
@classmethod @classmethod

View File

@ -178,15 +178,6 @@ class BaseProtocolTest(unittest.TestCase):
assertF('&channel') # we don't support these yet assertF('&channel') # we don't support these yet
assertF('lorem ipsum') assertF('lorem ipsum')
def test_is_ascii(self):
assertT = lambda inp: self.assertTrue(self.p._isASCII(inp))
assertF = lambda inp: self.assertFalse(self.p._isASCII(inp))
assertT('iotgjw@sy9!4py645ujg990rEYghiwaK0r4{SEFIre')
assertT('touche')
assertF('touché')
assertF('测试1')
def test_is_server_name(self): def test_is_server_name(self):
self.assertTrue(self.p.is_server_name('test.local')) self.assertTrue(self.p.is_server_name('test.local'))
self.assertTrue(self.p.is_server_name('IRC.example.com')) self.assertTrue(self.p.is_server_name('IRC.example.com'))