Yann Leboulanger pushed to branch master at gajim / gajim

Commits:
1a2ac87f by Yann Leboulanger at 2017-08-11T08:11:52+02:00
use precis_i18n instead of stringprepare when available to sanitize JIDs

- - - - -
b6189a60 by Yann Leboulanger at 2017-08-11T08:12:00+02:00
use idna python module to check domainpart when available

- - - - -
d468a1d9 by Yann Leboulanger at 2017-08-11T08:12:00+02:00
update optional dependencies

- - - - -
baa54f50 by Yann Leboulanger at 2017-08-12T14:55:08+02:00
Merge branch 'use_precis' into 'master'

Use precis instead of stringprep when available. Fixes #8566

See merge request !116
- - - - -


2 changed files:

- README
- gajim/common/helpers.py


Changes:

=====================================
README
=====================================
--- a/README
+++ b/README
@@ -37,6 +37,7 @@
 <li>gir1.2-farstream-0.2, gir1.2-gstreamer-1.0 and gir1.2-gst-plugins-base-1.0 
for audio and video calls</li>
 <li>gir1.2-gupnpigd-1.0 for better NAT traversing</li>
 <li>gir1.2-networkmanager-1.0 for network lose detection</li>
+<li>python3-idna and python3-precis-i18n for correctly parsing JIDs</li>
 </ul>
 
 <p>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.</p>


=====================================
gajim/common/helpers.py
=====================================
--- a/gajim/common/helpers.py
+++ b/gajim/common/helpers.py
@@ -53,6 +53,18 @@ 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
+
+try:
+    import idna
+    HAS_IDNA = True
+except ImportError:
+    HAS_IDNA = False
+
 HAS_SOUND = True
 if sys.platform == 'win32':
     try:
@@ -244,8 +256,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,33 +289,44 @@ 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 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:
             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:



View it on GitLab: 
https://dev.gajim.org/gajim/gajim/compare/af547a1827a1fd8e8b408912426e68367ce59579...baa54f50104119f94946f69c5f877b86149477a6

---
View it on GitLab: 
https://dev.gajim.org/gajim/gajim/compare/af547a1827a1fd8e8b408912426e68367ce59579...baa54f50104119f94946f69c5f877b86149477a6
You're receiving this email because of your account on dev.gajim.org.
_______________________________________________
Commits mailing list
Commits@gajim.org
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to