From 2ad6cc1b516dcffd8863d391e0f6d10552cc0ac7 Mon Sep 17 00:00:00 2001 From: Salad Dais Date: Fri, 17 Dec 2021 00:18:51 +0000 Subject: [PATCH] Better handle broken 'LLSD' responses --- hippolyzer/lib/proxy/message_logger.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/hippolyzer/lib/proxy/message_logger.py b/hippolyzer/lib/proxy/message_logger.py index 13aa763..df2253f 100644 --- a/hippolyzer/lib/proxy/message_logger.py +++ b/hippolyzer/lib/proxy/message_logger.py @@ -396,6 +396,14 @@ class AbstractMessageLogEntry(abc.ABC): xmlified = re.sub(rb" ", b"", xmlified) return xmlified.decode("utf8", errors="replace") + @staticmethod + def _format_xml(content): + beautified = minidom.parseString(content).toprettyxml(indent=" ") + # kill blank lines. will break cdata sections. meh. + beautified = re.sub(r'\n\s*\n', '\n', beautified, flags=re.MULTILINE) + return re.sub(r'<([\w]+)>\s*', r'<\1>', + beautified, flags=re.MULTILINE) + class HTTPMessageLogEntry(AbstractMessageLogEntry): __slots__ = ["flow"] @@ -484,13 +492,17 @@ class HTTPMessageLogEntry(AbstractMessageLogEntry): if not beautified: content_type = self._guess_content_type(message) if content_type.startswith("application/llsd"): - beautified = self._format_llsd(llsd.parse(message.content)) + try: + beautified = self._format_llsd(llsd.parse(message.content)) + except llsd.LLSDParseError: + # Sometimes LL sends plain XML with a Content-Type of application/llsd+xml. + # Try to detect that case and work around it + if content_type == "application/llsd+xml" and message.content.startswith(b'<'): + beautified = self._format_xml(message.content) + else: + raise elif any(content_type.startswith(x) for x in ("application/xml", "text/xml")): - beautified = minidom.parseString(message.content).toprettyxml(indent=" ") - # kill blank lines. will break cdata sections. meh. - beautified = re.sub(r'\n\s*\n', '\n', beautified, flags=re.MULTILINE) - beautified = re.sub(r'<([\w]+)>\s*', r'<\1>', - beautified, flags=re.MULTILINE) + beautified = self._format_xml(message.content) except: LOG.exception("Failed to beautify message")