From 1a2ac87f466d33d847d2dc755c17cf0ec78a9a3a Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Wed, 22 Feb 2017 20:54:48 +0100 Subject: [PATCH 1/3] use precis_i18n instead of stringprepare when available to sanitize JIDs --- gajim/common/helpers.py | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/gajim/common/helpers.py b/gajim/common/helpers.py index fb5e076ff..a270ffa32 100644 --- a/gajim/common/helpers.py +++ b/gajim/common/helpers.py @@ -53,6 +53,12 @@ import nbxmpp from common.i18n import Q_ from common.i18n import ngettext +try: + import precis_i18n.codec + HAS_PRECIS_I18N = True +except ImportError: + HAS_PRECIS_I18N = False + HAS_SOUND = True if sys.platform == 'win32': try: @@ -244,8 +250,11 @@ def parse_resource(resource): """ if resource: try: - from nbxmpp.stringprepare import resourceprep - return resourceprep.prepare(resource) + if HAS_PRECIS_I18N: + return resource.encode('Nickname').decode('utf-8') + else: + from nbxmpp.stringprepare import resourceprep + return resourceprep.prepare(resource) except UnicodeError: raise InvalidFormat('Invalid character in resource.') @@ -274,8 +283,8 @@ def prep(user, server, resource): if not ip_address: if server is not None: - if len(server) < 1 or len(server) > 1023: - raise InvalidFormat(_('Server must be between 1 and 1023 chars')) + if len(server) < 1 or len(server.encode('utf-8')) > 1023: + raise InvalidFormat(_('Server must be between 1 and 1023 bytes')) try: from nbxmpp.stringprepare import nameprep server = nameprep.prepare(server) @@ -285,22 +294,28 @@ def prep(user, server, resource): raise InvalidFormat(_('Server address required.')) if user is not None: - if len(user) < 1 or len(user) > 1023: - raise InvalidFormat(_('Username must be between 1 and 1023 chars')) + if len(user) < 1 or len(user.encode('utf-8')) > 1023: + raise InvalidFormat(_('Username must be between 1 and 1023 bytes')) try: - from nbxmpp.stringprepare import nodeprep - user = nodeprep.prepare(user) + if HAS_PRECIS_I18N: + user = user.encode('UsernameCaseMapped').decode('utf-8') + else: + from nbxmpp.stringprepare import nodeprep + user = nodeprep.prepare(user) except UnicodeError: raise InvalidFormat(_('Invalid character in username.')) else: user = None if resource is not None: - if len(resource) < 1 or len(resource) > 1023: - raise InvalidFormat(_('Resource must be between 1 and 1023 chars')) + if len(resource) < 1 or len(resource.encode('utf-8')) > 1023: + raise InvalidFormat(_('Resource must be between 1 and 1023 bytes')) try: - from nbxmpp.stringprepare import resourceprep - resource = resourceprep.prepare(resource) + if HAS_PRECIS_I18N: + resource = resource.encode('OpaqueString').decode('utf-8') + else: + from nbxmpp.stringprepare import resourceprep + resource = resourceprep.prepare(resource) except UnicodeError: raise InvalidFormat(_('Invalid character in resource.')) else: From b6189a60cf5c1cd6d70fde2e9f2178b1407e03d5 Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Thu, 10 Aug 2017 14:53:20 +0200 Subject: [PATCH 2/3] use idna python module to check domainpart when available --- gajim/common/helpers.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/gajim/common/helpers.py b/gajim/common/helpers.py index a270ffa32..7e133c2bf 100644 --- a/gajim/common/helpers.py +++ b/gajim/common/helpers.py @@ -59,6 +59,12 @@ try: except ImportError: HAS_PRECIS_I18N = False +try: + import idna + HAS_IDNA = True +except ImportError: + HAS_IDNA = False + HAS_SOUND = True if sys.platform == 'win32': try: @@ -283,11 +289,16 @@ def prep(user, server, resource): if not ip_address: if server is not None: + if server.endswith('.'): # RFC7622, 3.2 + server = server[:-1] if len(server) < 1 or len(server.encode('utf-8')) > 1023: raise InvalidFormat(_('Server must be between 1 and 1023 bytes')) try: - from nbxmpp.stringprepare import nameprep - server = nameprep.prepare(server) + if HAS_IDNA: + server = idna.encode(server).decode('utf-8') + else: + from nbxmpp.stringprepare import nameprep + server = nameprep.prepare(server) except UnicodeError: raise InvalidFormat(_('Invalid character in hostname.')) else: From d468a1d9305121dd878c41187f3ffc7886d12ee2 Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Thu, 10 Aug 2017 15:04:19 +0200 Subject: [PATCH 3/3] update optional dependencies --- README | 1 + 1 file changed, 1 insertion(+) diff --git a/README b/README index c80cc214a..ad18b3503 100644 --- a/README +++ b/README @@ -37,6 +37,7 @@
  • gir1.2-farstream-0.2, gir1.2-gstreamer-1.0 and gir1.2-gst-plugins-base-1.0 for audio and video calls
  • gir1.2-gupnpigd-1.0 for better NAT traversing
  • gir1.2-networkmanager-1.0 for network lose detection
  • +
  • python3-idna and python3-precis-i18n for correctly parsing JIDs
  • Some distributions also split too much python standard library. I know SUSE does. In such distros you also need python-xml the xml lib that *comes* with python and not pyxml or whatever.