From e395da18bf07d73b4718c5e7a5a2bcf8e8737ae6 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 12 Sep 2019 12:43:11 +0200 Subject: [PATCH] when parsing omemo messages ensure we only find one element --- .../crypto/axolotl/XmppAxolotlMessage.java | 2 +- .../siacs/conversations/parser/MessageParser.java | 2 +- .../java/eu/siacs/conversations/xml/Element.java | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java b/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java index cd91adb82..853dc4bab 100644 --- a/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java +++ b/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java @@ -135,7 +135,7 @@ public class XmppAxolotlMessage { break; } } - Element payloadElement = axolotlMessage.findChild(PAYLOAD); //TODO make sure we only have _one_ paypload + final Element payloadElement = axolotlMessage.findChildEnsureSingle(PAYLOAD, AxolotlService.PEP_PREFIX); if (payloadElement != null) { ciphertext = Base64.decode(payloadElement.getContent().trim(), Base64.DEFAULT); } diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index 552f38ebb..738b8fffa 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java @@ -338,7 +338,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece final URL xP1S3url = xP1S3 == null ? null : P1S3UrlStreamHandler.of(xP1S3); final String oobUrl = oob != null ? oob.findChildContent("url") : null; final String replacementId = replaceElement == null ? null : replaceElement.getAttribute("id"); - final Element axolotlEncrypted = packet.findChild(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX); //TODO make sure we only have _one_ axolotl element! + final Element axolotlEncrypted = packet.findChildEnsureSingle(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX); int status; final Jid counterpart; final Jid to = packet.getTo(); diff --git a/src/main/java/eu/siacs/conversations/xml/Element.java b/src/main/java/eu/siacs/conversations/xml/Element.java index 9f27844df..2efd943de 100644 --- a/src/main/java/eu/siacs/conversations/xml/Element.java +++ b/src/main/java/eu/siacs/conversations/xml/Element.java @@ -80,6 +80,19 @@ public class Element { return null; } + public Element findChildEnsureSingle(String name, String xmlns) { + final List results = new ArrayList<>(); + for (Element child : this.children) { + if (name.equals(child.getName()) && xmlns.equals(child.getAttribute("xmlns"))) { + results.add(child); + } + } + if (results.size() == 1) { + return results.get(0); + } + return null; + } + public String findChildContent(String name, String xmlns) { Element element = findChild(name,xmlns); return element == null ? null : element.getContent();