This is an automated email from the ASF dual-hosted git repository. chibenwa pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-mime4j.git
commit dd60f6708d82fd3b500a4c4263941f72f4d68d5a Author: Benoit TELLIER <[email protected]> AuthorDate: Fri Nov 13 13:58:06 2026 +0100 [FIX] DateTimeFieldLenientImpl: seconds after white space or a comment are read RFC 5322 obs-hour, obs-minute and obs-second allow CFWS around the colons of the time. On "09:55: 06 -0600" or "09:55:(x)06 -0600" the optional seconds section of RFC_5322 failed, every later section too, and the parse stopped at the second colon. zoneLeftUnread only rejects a rest starting with a sign or a digit, so ": 06 -0600" was ignored and the date read as 09:55:00 GMT, hours away from the instant of the strict parser: a wrong sent date for IMAP SENTBEFORE/SENTSINCE, JMAP sentAt and sort order. White space and comments around a colon between two digits are now removed before parsing, so the seconds and the zone are read as the strict parser reads them. A value whose parse still stops on a ':' or a '.' (a nested comment) gives no date rather than one without its seconds and zone. Fixes #131 --- .../mime4j/field/DateTimeFieldLenientImpl.java | 24 +++++++++++++++-- .../mime4j/field/LenientDateTimeFieldTest.java | 30 ++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/dom/src/main/java/org/apache/james/mime4j/field/DateTimeFieldLenientImpl.java b/dom/src/main/java/org/apache/james/mime4j/field/DateTimeFieldLenientImpl.java index 36daec1f..2a0255a8 100644 --- a/dom/src/main/java/org/apache/james/mime4j/field/DateTimeFieldLenientImpl.java +++ b/dom/src/main/java/org/apache/james/mime4j/field/DateTimeFieldLenientImpl.java @@ -194,7 +194,7 @@ public class DateTimeFieldLenientImpl extends AbstractField implements DateTimeF } try { // RFC 5322 FWS: any run of white space reads as one space - String normalized = signUnsignedOffset(normalizeShortYear(collapseWhiteSpace(text))); + String normalized = signUnsignedOffset(normalizeShortYear(removeCfwsAroundTimeColons(collapseWhiteSpace(text)))); ParsePosition position = new ParsePosition(0); Instant instant = Instant.from(RFC_5322.parse(normalized, position)); if (zoneLeftUnread(normalized, position.getIndex())) { @@ -352,9 +352,14 @@ public class DateTimeFieldLenientImpl extends AbstractField implements DateTimeF /** * Text left after the parse is tolerated (a comment, an unknown zone name), except a numeric - * zone that was not read at all or only in part (<code>+11</code> of <code>+1178</code>). + * zone that was not read at all or only in part (<code>+11</code> of <code>+1178</code>), and + * seconds or milliseconds that were not read (<code>:61</code> of <code>09:55:61</code>): the + * zone after them would be left unread too. */ private static boolean zoneLeftUnread(final String text, final int end) { + if (end < text.length() && (text.charAt(end) == ':' || text.charAt(end) == '.')) { + return true; + } int p = end; if (p < text.length() && text.charAt(p) == ' ') { p++; @@ -366,6 +371,21 @@ public class DateTimeFieldLenientImpl extends AbstractField implements DateTimeF return c == '+' || c == '-' || (c >= '0' && c <= '9'); } + /** + * RFC 5322 obs-hour, obs-minute and obs-second allow CFWS around the colons of the time + * (<code>09:55: 06</code>, <code>09:55:(c)06</code>), as the strict parser reads them. Applied + * to a text whose white space is collapsed. Nested comments are not removed: the value is then + * not read, see {@link #zoneLeftUnread}. + */ + private static final Pattern CFWS_AROUND_TIME_COLON = Pattern.compile("(\\d)(?: |\\([^()]*\\))*:(?: |\\([^()]*\\))*(?=\\d)"); + + private static String removeCfwsAroundTimeColons(final String text) { + if (text.indexOf('(') < 0 && text.indexOf(": ") < 0 && text.indexOf(" :") < 0) { + return text; + } + return CFWS_AROUND_TIME_COLON.matcher(text).replaceAll("$1:"); + } + /** * An offset without its sign right after the time ("22:23:35.882 0000", MIME4J-219) reads as * a positive one: the obs-zone sections do not read digits. diff --git a/dom/src/test/java/org/apache/james/mime4j/field/LenientDateTimeFieldTest.java b/dom/src/test/java/org/apache/james/mime4j/field/LenientDateTimeFieldTest.java index d61b5cf3..bc438a0a 100644 --- a/dom/src/test/java/org/apache/james/mime4j/field/LenientDateTimeFieldTest.java +++ b/dom/src/test/java/org/apache/james/mime4j/field/LenientDateTimeFieldTest.java @@ -186,4 +186,34 @@ public class LenientDateTimeFieldTest { Assert.assertEquals(Instant.parse("1997-11-21T14:55:06Z"), parse("Date: 21 Nov 1997 09:55:06 est").getDate().toInstant()); } + + @Test + public void secondsAfterFoldingWhiteSpaceAreRead() throws Exception { + // same as the strict DateTimeFieldImpl: 1997-11-21T15:55:06Z + Assert.assertEquals(880127706000L, parse("Date: Fri, 21 Nov 1997 09:55: 06 -0600").getDate().getTime()); + Assert.assertEquals(880127706000L, parse("Date: Fri, 21 Nov 1997 09 : 55 :\t06 -0600").getDate().getTime()); + } + + @Test + public void secondsAfterCommentAreRead() throws Exception { + Assert.assertEquals(880127706000L, parse("Date: Fri, 21 Nov 1997 09:55:(x)06 -0600").getDate().getTime()); + Assert.assertEquals(880127706000L, parse("Date: Fri, 21 Nov 1997 09:55: () (y) 06 -0600").getDate().getTime()); + } + + @Test + public void secondsAfterCfwsAgreeWithTheStrictParser() throws Exception { + String[] values = {"Fri, 21 Nov 1997 09:55: 06 -0600", "Fri, 21 Nov 1997 09:55:(x)06 -0600", + "1 Jan 2020 23:01:\t()\t()59 +0000", "Fri, 21 Nov 1997 09 :55:06 +0100"}; + for (String value : values) { + Date strict = DateTimeFieldImpl.PARSER.parse( + RawFieldParser.DEFAULT.parseField(ContentUtil.encode("Date: " + value)), null).getDate(); + Assert.assertEquals(value, strict, parse("Date: " + value).getDate()); + } + } + + @Test + public void unreadSecondsGiveNoDate() throws Exception { + // a nested comment is not removed: rather no date than one without its seconds and zone + Assert.assertNull(parse("Date: Fri, 21 Nov 1997 09:55:((x))06 -0600").getDate()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
