From 6b02cfc6b3ceafa416553479d1ac9aa60b38018d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Broquet?= Date: Fri, 9 Jan 2026 14:55:02 +0100 Subject: [PATCH] Fix #413. Ignore trailing bytes where a property cannot fit --- msgparser/pom.xml | 6 +++ .../java/com/auxilii/msgparser/MsgParser.java | 6 +-- .../com/auxilii/msgparser/MsgParserTest.java | 37 +++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 msgparser/src/test/java/com/auxilii/msgparser/MsgParserTest.java diff --git a/msgparser/pom.xml b/msgparser/pom.xml index 55e9a396..a8efc696 100644 --- a/msgparser/pom.xml +++ b/msgparser/pom.xml @@ -36,5 +36,11 @@ 6.0.2 test + + com.google.jimfs + jimfs + 1.3.1 + test + diff --git a/msgparser/src/main/java/com/auxilii/msgparser/MsgParser.java b/msgparser/src/main/java/com/auxilii/msgparser/MsgParser.java index 6ada3e39..c6e97fe0 100644 --- a/msgparser/src/main/java/com/auxilii/msgparser/MsgParser.java +++ b/msgparser/src/main/java/com/auxilii/msgparser/MsgParser.java @@ -69,7 +69,7 @@ private static Message parseMsg(DirectoryEntry dir) throws IOException { DirectoryEntry entry = (DirectoryEntry) dir.getEntry(String.format("__attach_version1.0_#%08X", index)); msg.addAttachment(parseAttachment(entry)); } - while (propertyStream.available() > 0) { + while (propertyStream.available() >= 8) { msg.setProperty(new Property(propertyStream, dir)); } return msg; @@ -113,7 +113,7 @@ private static Attachment parseAttachment(DirectoryEntry dir) throws IOException if (dir.hasEntry(Ptyp.SUBSTORAGE_PREFIX + "3701000D")) { return parseEmbeddedMessage(dir); } - return ParseFileAttachment(dir); + return parseFileAttachment(dir); } private static MsgAttachment parseEmbeddedMessage(DirectoryEntry dir) throws IOException { @@ -122,7 +122,7 @@ private static MsgAttachment parseEmbeddedMessage(DirectoryEntry dir) throws IOE return new MsgAttachment(parseMsg(entry)); } - private static FileAttachment ParseFileAttachment(DirectoryEntry dir) throws IOException { + private static FileAttachment parseFileAttachment(DirectoryEntry dir) throws IOException { DocumentEntry propertyEntry = (DocumentEntry) dir.getEntry(PROPERTIES_ENTRY); try (DocumentInputStream propertyStream = new DocumentInputStream(propertyEntry)) { propertyStream.skip(8); diff --git a/msgparser/src/test/java/com/auxilii/msgparser/MsgParserTest.java b/msgparser/src/test/java/com/auxilii/msgparser/MsgParserTest.java new file mode 100644 index 00000000..fce54b67 --- /dev/null +++ b/msgparser/src/test/java/com/auxilii/msgparser/MsgParserTest.java @@ -0,0 +1,37 @@ +package com.auxilii.msgparser; + +import static org.junit.jupiter.api.Assertions.*; + +import com.google.common.jimfs.Jimfs; +import java.io.*; +import java.nio.file.*; +import org.apache.poi.poifs.filesystem.*; +import org.junit.jupiter.api.Test; + +class MsgParserTest { + @Test + void should_ignore_trailing_bytes() throws IOException { + try (FileSystem fileSystem = Jimfs.newFileSystem()) { + Path messagePath = fileSystem.getPath("message.msg"); + writeMessageWithTrailingBytes(messagePath); + MsgParser parser = new MsgParser(messagePath); + assertDoesNotThrow(parser::parseMsg); + } + } + + private static void writeMessageWithTrailingBytes(Path target) throws IOException { + try (POIFSFileSystem fs = new POIFSFileSystem()) { + byte[] content = { + 0,0,0,0,0,0,0,0, // reserved + 0,0,0,0, // nextRecipientId + 0,0,0,0, // nextAttachmentId + 0,0,0,0, // recipientCount + 0,0,0,0, // attachmentCount + 0,0,0,0,0,0,0,0, // reserved for top level message + 1,2,3,4 // trailing garbage + }; + fs.getRoot().createDocument("__properties_version1.0", new ByteArrayInputStream(content)); + fs.writeFilesystem(Files.newOutputStream(target)); + } + } +}