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 04ed8a6f7d9e5160bd266125b9c9a76aa3e5736f
Author: Benoit TELLIER <[email protected]>
AuthorDate: Tue Nov 10 11:55:07 2026 +0100

    [FIX] DateTimeFieldLenientImpl: a zone after FWS or without space is 
honoured
    
    Each zone section of RFC_5322 began with a single space literal, and the
    parse ignored where it stopped. A zone after two spaces or a tab (valid
    RFC 5322 FWS: "09:55:06  -0600"), an obs-zone with no space before it
    ("1:2EDT", allowed by RFC 5322 4.3), or an offset read only in part (the
    "+11" of "+1178") was dropped with the rest of the value, and the time was
    read as GMT or with a partial offset: any sender could have the stored
    sent date shifted by hours. This is what the fix of #24 left open.
    
    Runs of white space now read as one space before parsing, the space
    before an obs-zone is optional, and a value whose numeric zone is left
    unread, in whole or in part, gives no date rather than a shifted one.
    Other trailing text, such as a comment, is still tolerated.
---
 .../mime4j/field/DateTimeFieldLenientImpl.java     | 57 ++++++++++++++++++++--
 .../mime4j/field/LenientDateTimeFieldTest.java     | 31 ++++++++++++
 2 files changed, 85 insertions(+), 3 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 14be08e0..fe8b4c79 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
@@ -84,12 +84,17 @@ public class DateTimeFieldLenientImpl extends AbstractField 
implements DateTimeF
             .appendLiteral(' ')
             .appendOffsetId()
         .optionalEnd()
+        // RFC 5322 4.3: obs-zone has no leading FWS of its own ("09:55:06EDT")
         .optionalStart()
-            .appendLiteral(' ')
+            .optionalStart()
+                .appendLiteral(' ')
+            .optionalEnd()
             .appendText(OFFSET_SECONDS, obsoleteZones())
         .optionalEnd()
         .optionalStart()
-            .appendLiteral(' ')
+            .optionalStart()
+                .appendLiteral(' ')
+            .optionalEnd()
             .appendText(OFFSET_SECONDS, obsoleteDaylightZones())
         .optionalEnd()
         .toFormatter()
@@ -182,7 +187,15 @@ public class DateTimeFieldLenientImpl extends 
AbstractField implements DateTimeF
             return null;
         }
         try {
-            return 
Date.from(Instant.from(RFC_5322.parse(normalizeShortYear(text), new 
ParsePosition(0))));
+            // RFC 5322 FWS: any run of white space reads as one space
+            String normalized = normalizeShortYear(collapseWhiteSpace(text));
+            ParsePosition position = new ParsePosition(0);
+            Instant instant = Instant.from(RFC_5322.parse(normalized, 
position));
+            if (zoneLeftUnread(normalized, position.getIndex())) {
+                // Reading it as GMT, or with the part of the offset read, 
would shift the date
+                return null;
+            }
+            return Date.from(instant);
         } catch (Exception e) {
             // Ignore: invalid date
             return null;
@@ -331,6 +344,44 @@ public class DateTimeFieldLenientImpl extends 
AbstractField implements DateTimeF
         return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
     }
 
+    /**
+     * 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>).
+     */
+    private static boolean zoneLeftUnread(final String text, final int end) {
+        int p = end;
+        if (p < text.length() && text.charAt(p) == ' ') {
+            p++;
+        }
+        if (p >= text.length()) {
+            return false;
+        }
+        char c = text.charAt(p);
+        return c == '+' || c == '-' || (c >= '0' && c <= '9');
+    }
+
+    private static String collapseWhiteSpace(final String text) {
+        StringBuilder sb = null;
+        boolean previousSpace = false;
+        for (int i = 0; i < text.length(); i++) {
+            char c = text.charAt(i);
+            boolean space = c == ' ' || c == '\t' || c == '\r' || c == '\n';
+            if (space && (previousSpace || c != ' ')) {
+                if (sb == null) {
+                    sb = new StringBuilder(text.length());
+                    sb.append(text, 0, i);
+                }
+                if (!previousSpace) {
+                    sb.append(' ');
+                }
+            } else if (sb != null) {
+                sb.append(c);
+            }
+            previousSpace = space;
+        }
+        return sb == null ? text : sb.toString();
+    }
+
     /**
      * The lenient formatter reads a 1 or 3 digit year literally: map it as 
RFC 5322 section 4.3
      * and the strict parser do (1 digit: 2000 + year, 3 digits: 1900 + year).
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 4822e89b..b738c400 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
@@ -137,4 +137,35 @@ public class LenientDateTimeFieldTest {
         Assert.assertEquals(Instant.parse("2899-10-04T10:00:00Z"), 
parse("Date: 4 Oct 999 10:00:00 +0000").getDate().toInstant());
         Assert.assertEquals(Instant.parse("2001-10-04T10:00:00Z"), 
parse("Date: 4 Oct 2001 10:00:00 +0000").getDate().toInstant());
     }
+
+    @Test
+    public void zoneAfterFoldingWhiteSpaceShouldBeHonoured() throws Exception {
+        Assert.assertEquals(Instant.parse("1997-11-21T15:55:06Z"),
+            parse("Date: Fri, 21 Nov 1997 09:55:06  
-0600").getDate().toInstant());
+        Assert.assertEquals(Instant.parse("1997-11-21T14:55:06Z"),
+            parse("Date: Fri, 21 Nov 1997 09:55:06  
EST").getDate().toInstant());
+        Assert.assertEquals(Instant.parse("1997-11-21T15:55:06Z"),
+            parse("Date: Fri, 21 Nov 1997 
09:55:06\t-0600").getDate().toInstant());
+    }
+
+    @Test
+    public void obsoleteZoneWithoutSpaceShouldBeHonoured() throws Exception {
+        // RFC 5322 4.3: obs-zone has no leading FWS, obs-second is [CFWS] 
2DIGIT [CFWS]
+        Assert.assertEquals(Instant.parse("1997-11-21T13:55:06Z"),
+            parse("Date: Fri, 21 Nov 1997 09:55:06EDT").getDate().toInstant());
+        Assert.assertEquals(Instant.parse("2000-10-04T05:02:00Z"),
+            parse("Date: 4 Oct 0 1:2EDT").getDate().toInstant());
+    }
+
+    @Test
+    public void partlyReadOffsetShouldNotGiveADate() throws Exception {
+        Assert.assertNull(parse("Date: 32 Feb 3416118 48:84:32 
+1178").getDate());
+        Assert.assertNull(parse("Date: 21 Nov 1997 09:55:06 
+11789").getDate());
+    }
+
+    @Test
+    public void trailingTextAfterTheZoneShouldStillBeTolerated() throws 
Exception {
+        Assert.assertEquals(Instant.parse("1997-11-21T15:55:06Z"),
+            parse("Date: Fri, 21 Nov 1997 09:55:06 -0600 
(CST)").getDate().toInstant());
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to