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 6700011eec18b29aefd466980f4069894a65ab05
Author: Benoit TELLIER <[email protected]>
AuthorDate: Tue Nov 10 11:59:18 2026 +0100

    [FIX] DateTimeFieldLenientImpl: an out-of-range offset is not read as 
seconds
    
    The obs-zone sections added by the fix of #24 are text fields of a lenient
    formatter, and a lenient text field that matches no name falls back to a
    number. So once appendOffset had rejected an offset such as "+9900" or
    "+6357", the obs-zone section read it as 9900 or 6357 seconds, and the
    date was shifted by hours where the strict parser reads 99 hours: any
    sender could make the two parsers disagree on the sent date.
    
    The obs-zone sections are now parsed strictly and match only a zone name;
    an offset nobody can read then gives no date. An offset without its sign
    right after the time ("22:23:35.882 0000", MIME4J-219), which only this
    fallback read, is now read explicitly as positive.
---
 .../mime4j/field/DateTimeFieldLenientImpl.java     | 24 ++++++++++++++++++++--
 .../mime4j/field/LenientDateTimeFieldTest.java     | 18 ++++++++++++++++
 2 files changed, 40 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 fe8b4c79..36daec1f 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
@@ -41,6 +41,8 @@ import java.util.Date;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.james.mime4j.codec.DecodeMonitor;
 import org.apache.james.mime4j.dom.FieldParser;
@@ -84,7 +86,10 @@ 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")
+        // RFC 5322 4.3: obs-zone has no leading FWS of its own 
("09:55:06EDT").
+        // Parsed strictly: a lenient text field falls back to a number, which 
would read an
+        // offset rejected above ("+9900") as a number of seconds
+        .parseStrict()
         .optionalStart()
             .optionalStart()
                 .appendLiteral(' ')
@@ -97,6 +102,7 @@ public class DateTimeFieldLenientImpl extends AbstractField 
implements DateTimeF
             .optionalEnd()
             .appendText(OFFSET_SECONDS, obsoleteDaylightZones())
         .optionalEnd()
+        .parseLenient()
         .toFormatter()
         .withZone(ZoneId.of("GMT"))
         .withResolverStyle(ResolverStyle.LENIENT)
@@ -188,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 = normalizeShortYear(collapseWhiteSpace(text));
+            String normalized = 
signUnsignedOffset(normalizeShortYear(collapseWhiteSpace(text)));
             ParsePosition position = new ParsePosition(0);
             Instant instant = Instant.from(RFC_5322.parse(normalized, 
position));
             if (zoneLeftUnread(normalized, position.getIndex())) {
@@ -360,6 +366,20 @@ public class DateTimeFieldLenientImpl extends 
AbstractField implements DateTimeF
         return c == '+' || c == '-' || (c >= '0' && c <= '9');
     }
 
+    /**
+     * 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.
+     */
+    private static final Pattern UNSIGNED_OFFSET = 
Pattern.compile("(\\d:\\d+(?:\\.\\d+)?) (\\d{4})(?=$|[ (])");
+
+    private static String signUnsignedOffset(final String text) {
+        Matcher matcher = UNSIGNED_OFFSET.matcher(text);
+        if (!matcher.find()) {
+            return text;
+        }
+        return text.substring(0, matcher.start(2)) + '+' + 
text.substring(matcher.start(2));
+    }
+
     private static String collapseWhiteSpace(final String text) {
         StringBuilder sb = null;
         boolean previousSpace = false;
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 b738c400..d61b5cf3 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
@@ -168,4 +168,22 @@ public class LenientDateTimeFieldTest {
         Assert.assertEquals(Instant.parse("1997-11-21T15:55:06Z"),
             parse("Date: Fri, 21 Nov 1997 09:55:06 -0600 
(CST)").getDate().toInstant());
     }
+
+    @Test
+    public void outOfRangeNumericOffsetShouldNotBeReadAsSeconds() throws 
Exception {
+        Date date = parse("Date: 21 Nov 1997 09:55:06 +9900").getDate();
+        // 9900 seconds = +02:45
+        Assert.assertNotEquals(Instant.parse("1997-11-21T07:10:06Z"), date == 
null ? null : date.toInstant());
+        Assert.assertNull(date);
+        Assert.assertNull(parse("Date: 1 Jan 2020 25:61:61 +9999").getDate());
+        Assert.assertNull(parse("Date: 32 Jan 0 00:54 +6357").getDate());
+    }
+
+    @Test
+    public void obsoleteZonesShouldStillBeHonoured() throws Exception {
+        Assert.assertEquals(Instant.parse("1997-11-21T16:55:06Z"),
+            parse("Date: 21 Nov 1997 09:55:06 PDT").getDate().toInstant());
+        Assert.assertEquals(Instant.parse("1997-11-21T14:55:06Z"),
+            parse("Date: 21 Nov 1997 09:55:06 est").getDate().toInstant());
+    }
 }


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

Reply via email to