From 5148156bf51e920a8c86e73cee994fe41ba5f42e Mon Sep 17 00:00:00 2001 From: lolo101 Date: Sun, 19 Nov 2023 09:58:07 +0100 Subject: [PATCH] Ignore invalid headers when converting to msg->mbox/eml. Fix #231 --- .../factory/mbox/MBoxWriterViaJavaMail.java | 8 +++++--- .../factory/mbox/MBoxWriterViaJavaMailTest.java | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/MSGViewer/src/main/java/net/sourceforge/MSGViewer/factory/mbox/MBoxWriterViaJavaMail.java b/MSGViewer/src/main/java/net/sourceforge/MSGViewer/factory/mbox/MBoxWriterViaJavaMail.java index 5bd90fc..24c47a8 100644 --- a/MSGViewer/src/main/java/net/sourceforge/MSGViewer/factory/mbox/MBoxWriterViaJavaMail.java +++ b/MSGViewer/src/main/java/net/sourceforge/MSGViewer/factory/mbox/MBoxWriterViaJavaMail.java @@ -159,9 +159,11 @@ private static void addHeaders(Message msg, Part jmsg) throws MessagingException while (!lines.isEmpty()) { String headerLine = lines.remove(); int separatorIndex = headerLine.indexOf(':'); - String name = headerLine.substring(0, separatorIndex); - String value = accumulateValue(lines, headerLine.substring(separatorIndex + 1)); - jmsg.addHeader(name, value); + if (separatorIndex > 0) { + String name = headerLine.substring(0, separatorIndex); + String value = accumulateValue(lines, headerLine.substring(separatorIndex + 1)); + jmsg.addHeader(name, value); + } } } diff --git a/MSGViewer/src/test/java/net/sourceforge/MSGViewer/factory/mbox/MBoxWriterViaJavaMailTest.java b/MSGViewer/src/test/java/net/sourceforge/MSGViewer/factory/mbox/MBoxWriterViaJavaMailTest.java index a79eca2..3403ff9 100644 --- a/MSGViewer/src/test/java/net/sourceforge/MSGViewer/factory/mbox/MBoxWriterViaJavaMailTest.java +++ b/MSGViewer/src/test/java/net/sourceforge/MSGViewer/factory/mbox/MBoxWriterViaJavaMailTest.java @@ -15,6 +15,7 @@ import java.nio.file.Path; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; class MBoxWriterViaJavaMailTest { @Test @@ -92,6 +93,21 @@ void testIssue178() throws Exception { } } + @Test + void testIssue231() throws Exception { + ModuleLauncher.BaseConfigureLogging(); + + try (FileSystem fileSystem = Jimfs.newFileSystem()) { + Path testOut = fileSystem.getPath("test_out.eml"); + try (OutputStream outputStream = Files.newOutputStream(testOut)) { + Message msg = new Message(); + msg.setHeaders("key without value\n"); + MBoxWriterViaJavaMail writer = givenWriter(); + assertThatCode(() -> writer.write(msg, outputStream)).doesNotThrowAnyException(); + } + } + } + private static Message givenMessage(String name) throws Exception { URI uri = MBoxWriterViaJavaMailTest.class.getResource(name).toURI(); return new MessageParser(Path.of(uri)).parseMessage();