From 96bca146c079e6778cad412b5c13bb5853404a83 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Wed, 13 Sep 2017 13:54:54 +0200 Subject: [PATCH] Disregard protocol parameter of multipart/encrypted and /signed parts with missing bodies --- .../MessageCryptoStructureDetector.java | 12 ++++-- .../MessageCryptoStructureDetectorTest.java | 41 ++++++++++++++++--- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/crypto/MessageCryptoStructureDetector.java b/k9mail/src/main/java/com/fsck/k9/crypto/MessageCryptoStructureDetector.java index 219778f27..974811957 100644 --- a/k9mail/src/main/java/com/fsck/k9/crypto/MessageCryptoStructureDetector.java +++ b/k9mail/src/main/java/com/fsck/k9/crypto/MessageCryptoStructureDetector.java @@ -227,8 +227,10 @@ public class MessageCryptoStructureDetector { } String protocolParameter = MimeUtility.getHeaderParameter(part.getContentType(), PROTOCOL_PARAMETER); - BodyPart signatureBodyPart = mimeMultipart.getBodyPart(1); - return isSameMimeType(protocolParameter, signatureBodyPart.getMimeType()); + + boolean dataUnavailable = protocolParameter == null && mimeMultipart.getBodyPart(0).getBody() == null; + boolean protocolMatches = isSameMimeType(protocolParameter, mimeMultipart.getBodyPart(1).getMimeType()); + return dataUnavailable || protocolMatches; } private static boolean isPartMultipartEncrypted(Part part) { @@ -244,8 +246,10 @@ public class MessageCryptoStructureDetector { } String protocolParameter = MimeUtility.getHeaderParameter(part.getContentType(), PROTOCOL_PARAMETER); - BodyPart signatureBodyPart = mimeMultipart.getBodyPart(0); - return isSameMimeType(protocolParameter, signatureBodyPart.getMimeType()); + + boolean dataUnavailable = protocolParameter == null && mimeMultipart.getBodyPart(1).getBody() == null; + boolean protocolMatches = isSameMimeType(protocolParameter, mimeMultipart.getBodyPart(0).getMimeType()); + return dataUnavailable || protocolMatches; } public static boolean isMultipartEncryptedOpenPgpProtocol(Part part) { diff --git a/k9mail/src/test/java/com/fsck/k9/crypto/MessageCryptoStructureDetectorTest.java b/k9mail/src/test/java/com/fsck/k9/crypto/MessageCryptoStructureDetectorTest.java index 352d7bed3..077226597 100644 --- a/k9mail/src/test/java/com/fsck/k9/crypto/MessageCryptoStructureDetectorTest.java +++ b/k9mail/src/test/java/com/fsck/k9/crypto/MessageCryptoStructureDetectorTest.java @@ -182,7 +182,7 @@ public class MessageCryptoStructureDetectorTest { Message message = messageFromBody( multipart("encrypted", "application/not-pgp-encrypted", bodypart("application/pgp-encrypted"), - bodypart("application/octet-stream") + bodypart("application/octet-stream", "content") ) ); @@ -192,7 +192,7 @@ public class MessageCryptoStructureDetectorTest { } @Test - public void findEncrypted__withEmptyProtocol__shouldReturnEmpty() throws Exception { + public void findEncrypted__withBadProtocolAndNoBody__shouldReturnRoot() throws Exception { Message message = messageFromBody( multipart("encrypted", null, bodypart("application/pgp-encrypted"), @@ -202,6 +202,21 @@ public class MessageCryptoStructureDetectorTest { List encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(message); + assertEquals(1, encryptedParts.size()); + assertSame(message, encryptedParts.get(0)); + } + + @Test + public void findEncrypted__withEmptyProtocol__shouldReturnEmpty() throws Exception { + Message message = messageFromBody( + multipart("encrypted", null, + bodypart("application/pgp-encrypted"), + bodypart("application/octet-stream", "content") + ) + ); + + List encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(message); + assertTrue(encryptedParts.isEmpty()); } @@ -324,10 +339,26 @@ public class MessageCryptoStructureDetectorTest { } @Test - public void findSigned__withBadProtocol__shouldReturnRoot() throws Exception { + public void findSigned__withNoProtocolAndNoBody__shouldReturnRoot() throws Exception { + Message message = messageFromBody( + multipart("signed", null, + bodypart("text/plain"), + bodypart("application/pgp-signature") + ) + ); + + List signedParts = MessageCryptoStructureDetector + .findMultipartSignedParts(message, messageCryptoAnnotations); + + assertEquals(1, signedParts.size()); + assertSame(message, signedParts.get(0)); + } + + @Test + public void findSigned__withBadProtocol__shouldReturnNothing() throws Exception { Message message = messageFromBody( multipart("signed", "application/not-pgp-signature", - bodypart("text/plain"), + bodypart("text/plain", "content"), bodypart("application/pgp-signature") ) ); @@ -342,7 +373,7 @@ public class MessageCryptoStructureDetectorTest { public void findSigned__withEmptyProtocol__shouldReturnRoot() throws Exception { Message message = messageFromBody( multipart("signed", null, - bodypart("text/plain"), + bodypart("text/plain", "content"), bodypart("application/pgp-signature") ) );