Check for XML parsing errors and disconnect in that case.

This commit is contained in:
Emmanuel Gil Peyrot 2016-12-29 18:59:09 +01:00
parent df4012e66d
commit 6034df0a78

View File

@ -19,7 +19,7 @@ import ssl
import weakref
import uuid
import xml.etree.ElementTree
import xml.etree.ElementTree as ET
from slixmpp.xmlstream.asyncio import asyncio
from slixmpp.xmlstream import tostring, highlight
@ -339,7 +339,7 @@ class XMLStream(asyncio.BaseProtocol):
"""
self.xml_depth = 0
self.xml_root = None
self.parser = xml.etree.ElementTree.XMLPullParser(("start", "end"))
self.parser = ET.XMLPullParser(("start", "end"))
def connection_made(self, transport):
"""Called when the TCP connection has been established with the server
@ -359,6 +359,7 @@ class XMLStream(asyncio.BaseProtocol):
the stream is opened, etc).
"""
self.parser.feed(data)
try:
for event, xml in self.parser.read_events():
if event == 'start':
if self.xml_depth == 0:
@ -387,6 +388,17 @@ class XMLStream(asyncio.BaseProtocol):
# Keep the root element empty of children to
# save on memory use.
self.xml_root.clear()
except ET.ParseError:
log.error('Parse error: %r', data)
# Due to cyclic dependencies, this cant be imported at the module
# level.
from slixmpp.stanza.stream_error import StreamError
error = StreamError()
error['condition'] = 'not-well-formed'
error['text'] = 'Server sent: %r' % data
self.send(error)
self.disconnect()
def is_connected(self):
return self.transport is not None