Commit 3946f35b authored by Philipp Hörist's avatar Philipp Hörist

Remove old JID validation code

parent c7597a9e
Pipeline #3818 passed with stages
in 32 seconds
......@@ -22,10 +22,10 @@ from nbxmpp.protocol import NS_ROSTERNOTES
from nbxmpp.protocol import Iq
from nbxmpp.protocol import Node
from nbxmpp.protocol import isResultNode
from nbxmpp.protocol import JID
from nbxmpp.structs import AnnotationNote
from nbxmpp.structs import CommonResult
from nbxmpp.modules.date_and_time import parse_datetime
from nbxmpp.util import validate_jid
from nbxmpp.util import call_on_response
from nbxmpp.util import callback
from nbxmpp.util import raise_error
......@@ -66,10 +66,10 @@ class Annotations:
notes = []
for note in nodes:
try:
jid = validate_jid(note.getAttr('jid'))
except Exception:
log.warning('Invalid JID: %s, ignoring it',
note.getAttr('jid'))
jid = JID(note.getAttr('jid'))
except Exception as error:
log.warning('Invalid JID: %s, %s',
note.getAttr('jid'), error)
continue
cdate = note.getAttr('cdate')
......
......@@ -23,12 +23,12 @@ from nbxmpp.protocol import NS_PRIVATE
from nbxmpp.protocol import isResultNode
from nbxmpp.protocol import Node
from nbxmpp.protocol import Iq
from nbxmpp.protocol import JID
from nbxmpp.structs import StanzaHandler
from nbxmpp.structs import BookmarkData
from nbxmpp.const import BookmarkStoreType
from nbxmpp.util import from_xs_boolean
from nbxmpp.util import to_xs_boolean
from nbxmpp.util import validate_jid
from nbxmpp.util import call_on_response
from nbxmpp.util import callback
from nbxmpp.util import raise_error
......@@ -87,10 +87,10 @@ class Bookmarks:
autojoin = False
try:
jid = validate_jid(conf.getAttr('jid'))
except Exception:
log.warning('Invalid JID: %s, ignoring it',
conf.getAttr('jid'))
jid = JID(conf.getAttr('jid'))
except Exception as error:
log.warning('Invalid JID: %s, %s',
conf.getAttr('jid'), error)
continue
bookmark = BookmarkData(
......
......@@ -21,8 +21,8 @@
from nbxmpp.protocol import NS_DATA
from nbxmpp.protocol import NS_DATA_MEDIA
from nbxmpp.protocol import JID
from nbxmpp.simplexml import Node
from nbxmpp.util import validate_jid
# exceptions used in this module
......@@ -426,7 +426,7 @@ class JidSingleField(ListSingleField):
def is_valid(self):
if self.value:
try:
validate_jid(self.value)
JID(self.value)
return True, ''
except Exception as error:
return False, error
......@@ -481,7 +481,7 @@ class JidMultiField(ListMultiField):
if self.values:
for value in self.values:
try:
validate_jid(value)
JID(value)
except Exception as error:
return False, error
return True, ''
......
......@@ -32,7 +32,6 @@ from nbxmpp.protocol import Message
from nbxmpp.protocol import DataForm
from nbxmpp.protocol import DataField
from nbxmpp.protocol import isResultNode
from nbxmpp.protocol import InvalidJid
from nbxmpp.simplexml import Node
from nbxmpp.structs import StanzaHandler
from nbxmpp.const import InviteType
......@@ -48,7 +47,6 @@ from nbxmpp.structs import CommonResult
from nbxmpp.structs import MucConfigResult
from nbxmpp.structs import MucUserData
from nbxmpp.structs import MucDestroyed
from nbxmpp.util import validate_jid
from nbxmpp.util import call_on_response
from nbxmpp.util import callback
from nbxmpp.util import raise_error
......@@ -112,9 +110,9 @@ class MUC:
alternate = destroy.getAttr('jid')
if alternate is not None:
try:
alternate = JID(validate_jid(alternate))
except InvalidJid:
log.warning('Invalid alternate JID provided')
alternate = JID(alternate)
except Exception as error:
log.warning('Invalid alternate JID provided: %s', error)
log.warning(stanza)
alternate = None
properties.muc_destroyed = MucDestroyed(
......@@ -312,9 +310,10 @@ class MUC:
users_dict = {}
for item in items:
try:
jid = validate_jid(item.getAttr('jid'))
except InvalidJid as error:
log.exception(error)
jid = JID(item.getAttr('jid'))
except Exception as error:
log.warning('Invalid JID: %s, %s',
item.getAttr('jid'), error)
continue
users_dict[jid] = {}
......
......@@ -71,77 +71,6 @@ def get_properties_struct(name):
return Properties()
def validate_jid(jid_string):
jid = JID(jid_string)
return prep(jid.getNode() or None,
jid.getDomain() or None,
jid.getResource() or None)
def prep(user, server, resource):
"""
Perform stringprep on all JID fragments and return the full jid
"""
ip_address = False
try:
socket.inet_aton(server)
ip_address = True
except socket.error:
pass
if not ip_address and hasattr(socket, 'inet_pton'):
try:
socket.inet_pton(socket.AF_INET6, server.strip('[]'))
server = '[%s]' % server.strip('[]')
ip_address = True
except (socket.error, ValueError):
pass
if not ip_address:
if server is not None:
if server.endswith('.'): # RFC7622, 3.2
server = server[:-1]
if not server or len(server.encode('utf-8')) > 1023:
raise InvalidJid('Server must be between 1 and 1023 bytes')
try:
server = nameprep.prepare(server)
except UnicodeError:
raise InvalidJid('Invalid character in hostname')
else:
raise InvalidJid('Server address required')
if user is not None:
if not user or len(user.encode('utf-8')) > 1023:
raise InvalidJid('Username must be between 1 and 1023 bytes')
try:
user = user.encode('UsernameCaseMapped').decode('utf-8')
except UnicodeError:
raise InvalidJid('Invalid character in username')
else:
user = None
if resource is not None:
if not resource or len(resource.encode('utf-8')) > 1023:
raise InvalidJid('Resource must be between 1 and 1023 bytes')
try:
resource = resource.encode('OpaqueString').decode('utf-8')
except UnicodeError:
raise InvalidJid('Invalid character in resource')
else:
resource = None
if user:
if resource:
return '%s@%s/%s' % (user, server, resource)
return '%s@%s' % (user, server)
if resource:
return '%s/%s' % (server, resource)
return server
def call_on_response(cb):
def response_decorator(func):
@wraps(func)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment