when parsing omemo messages ensure we only find one element

This commit is contained in:
Daniel Gultsch 2019-09-12 12:43:11 +02:00
parent a7c47a33fa
commit e395da18bf
3 changed files with 15 additions and 2 deletions

View file

@ -135,7 +135,7 @@ public class XmppAxolotlMessage {
break; 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) { if (payloadElement != null) {
ciphertext = Base64.decode(payloadElement.getContent().trim(), Base64.DEFAULT); ciphertext = Base64.decode(payloadElement.getContent().trim(), Base64.DEFAULT);
} }

View file

@ -338,7 +338,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
final URL xP1S3url = xP1S3 == null ? null : P1S3UrlStreamHandler.of(xP1S3); final URL xP1S3url = xP1S3 == null ? null : P1S3UrlStreamHandler.of(xP1S3);
final String oobUrl = oob != null ? oob.findChildContent("url") : null; final String oobUrl = oob != null ? oob.findChildContent("url") : null;
final String replacementId = replaceElement == null ? null : replaceElement.getAttribute("id"); 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; int status;
final Jid counterpart; final Jid counterpart;
final Jid to = packet.getTo(); final Jid to = packet.getTo();

View file

@ -80,6 +80,19 @@ public class Element {
return null; return null;
} }
public Element findChildEnsureSingle(String name, String xmlns) {
final List<Element> 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) { public String findChildContent(String name, String xmlns) {
Element element = findChild(name,xmlns); Element element = findChild(name,xmlns);
return element == null ? null : element.getContent(); return element == null ? null : element.getContent();