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 1c24ea3d36dd8e97648f02856595e5101c57843c Author: Benoit TELLIER <[email protected]> AuthorDate: Sat Nov 21 07:09:05 2026 +0100 [FIX] Date parsers: a numeric zone is a sign and 4 digits RFC 5322 section 3.3: zone = (FWS ( "+" / "-" ) 4DIGIT) / obs-zone. Both parsers accepted other digit counts, and read them differently: - the lenient parser parsed the offset leniently, which reads "+11" as +11:00 and "+111900" as +11:19:00; - the strict parser took any number of digits as HHMM, so "+11" was 11 minutes and "+111900" 1119 hours. The lenient parser now parses the zone strictly: a short or long offset is left unread, and zoneLeftUnread rejects it, as well as a numeric zone read with other than 4 digits, which the offset parser of some JDKs does even in strict mode. Its second offset section reads "Z" only, instead of any offset id: parsed strictly, it would have accepted "+11:00" in place of the first section, which the strict parser rejects. The strict parser throws a ParseException for a zone that is not 4 digits, or whose minutes are over 59, as the lenient parser already did. Fixes #144 --- .../mime4j/field/DateTimeFieldLenientImpl.java | 23 +++++++++++++++++----- .../james/mime4j/field/datetime/DateTimeParser.jj | 10 +++++++++- .../james/mime4j/field/DateTimeFieldImplTest.java | 11 +++++++++++ .../mime4j/field/LenientDateTimeFieldTest.java | 15 ++++++++++++++ 4 files changed, 53 insertions(+), 6 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 ff1a3c91..6ca03670 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 @@ -78,18 +78,21 @@ public class DateTimeFieldLenientImpl extends AbstractField implements DateTimeF .appendLiteral('.') .appendValue(MILLI_OF_SECOND, 3) .optionalEnd() + // The zone is parsed strictly. RFC 5322 3.3: a numeric zone is a sign and 4 digits, while + // a lenient offset reads "+11" as +11:00 and "+111900" as +11:19:00, and an offset id + // "+11:00"; left unread, they are rejected by zoneLeftUnread + .parseStrict() .optionalStart() .appendLiteral(' ') .appendOffset("+HHMM", "GMT") .optionalEnd() .optionalStart() .appendLiteral(' ') - .appendOffsetId() + .appendOffset("+HHMM", "Z") .optionalEnd() // 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() + // A lenient text field falls back to a number, which would read an offset rejected + // above ("+9900") as a number of seconds .optionalStart() .optionalStart() .appendLiteral(' ') @@ -352,7 +355,8 @@ 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>), and + * zone that was not read at all or only in part (<code>+11</code> of <code>+1178</code>) or not + * as 4 digits, 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. */ @@ -360,6 +364,15 @@ public class DateTimeFieldLenientImpl extends AbstractField implements DateTimeF if (end < text.length() && (text.charAt(end) == ':' || text.charAt(end) == '.')) { return true; } + // A numeric zone read with other than 4 digits: the offset parser of some JDKs reads + // optional seconds even in strict mode ("+111900") + int digits = end; + while (digits > 0 && Character.isDigit(text.charAt(digits - 1))) { + digits--; + } + if (digits > 0 && (text.charAt(digits - 1) == '+' || text.charAt(digits - 1) == '-') && end - digits != 4) { + return true; + } int p = end; if (p < text.length() && text.charAt(p) == ' ') { p++; diff --git a/dom/src/main/javacc/org/apache/james/mime4j/field/datetime/DateTimeParser.jj b/dom/src/main/javacc/org/apache/james/mime4j/field/datetime/DateTimeParser.jj index 0c6ee9b5..23b1fb5e 100644 --- a/dom/src/main/javacc/org/apache/james/mime4j/field/datetime/DateTimeParser.jj +++ b/dom/src/main/javacc/org/apache/james/mime4j/field/datetime/DateTimeParser.jj @@ -298,7 +298,15 @@ int second() : int zone() : { Token t, u; int z; } { -( t=< OFFSETDIR: ["+", "-"] > u=<DIGITS> { z=parseDigits(u)*(t.image.equals("-") ? -1 : 1); } +( t=< OFFSETDIR: ["+", "-"] > u=<DIGITS> + { + // RFC 5322 3.3: 4 digits, HHMM. "+11" is not 11 minutes, nor "+111900" 1119 hours + if (u.image.length() != 4 || parseDigits(u) % 100 > 59) + { + throw new ParseException("Invalid zone: " + t.image + u.image); + } + z=parseDigits(u)*(t.image.equals("-") ? -1 : 1); + } | z=obs_zone() ) { return z; } diff --git a/dom/src/test/java/org/apache/james/mime4j/field/DateTimeFieldImplTest.java b/dom/src/test/java/org/apache/james/mime4j/field/DateTimeFieldImplTest.java index ff0bac45..fb0bbf7e 100644 --- a/dom/src/test/java/org/apache/james/mime4j/field/DateTimeFieldImplTest.java +++ b/dom/src/test/java/org/apache/james/mime4j/field/DateTimeFieldImplTest.java @@ -82,6 +82,17 @@ public class DateTimeFieldImplTest { return DateTimeFieldImpl.PARSER.parse(rawField, null); } + @Test + public void strictParserShouldRejectNumericZoneOfOtherThanFourDigits() throws Exception { + for (String value : new String[] {"1 Jan 2020 21:23:23 +111900", "1 Jan 2002 02:03 +11", "1 Jan 2002 02:03 +1160"}) { + DateTimeField field = parse("Date: " + value); + assertNull(value, field.getDate()); + assertNotNull(value, field.getParseException()); + } + assertEquals(1577874203000L, parse("Date: 1 Jan 2020 21:23:23 +1100").getDate().getTime()); + assertEquals(1577874203000L - 59 * 60000L, parse("Date: 1 Jan 2020 21:23:23 +1159").getDate().getTime()); + } + @Test public void parseShouldFailWhenYearIsTooLong() throws Exception { DateTimeField field = parse("Date: 1 Jan 99999999999 00:00 +0000"); 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 7bfd966c..bb7e0490 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 @@ -216,6 +216,21 @@ public class LenientDateTimeFieldTest { // 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()); } + @Test + public void numericZoneOfOtherThanFourDigitsShouldBeRejected() throws Exception { + String[] values = {"1 Jan 2002 02:03 +11", "1 JAn\t2 2:3 +11", "1 Jan 2020 21:23:23 +111900", + "1 Jan 2020 21:23:23 -011", "1 Jan 2020 21:23:23 +11:00", "1 Jan 2020 21:23:23 +11:00:30", "1 Jan 2020 21:23:23 +01:00 (x)", + "1 Jan 2020 21:23:23 +1178"}; + for (String value : values) { + Assert.assertNull(value, parse("Date: " + value).getDate()); + Date strict = DateTimeFieldImpl.PARSER.parse( + RawFieldParser.DEFAULT.parseField(ContentUtil.encode("Date: " + value)), null).getDate(); + Assert.assertNull(value, strict); + } + Assert.assertEquals(1577874203000L, parse("Date: 1 Jan 2020 21:23:23 +1100").getDate().getTime()); + Assert.assertEquals(1577913803000L, parse("Date: 1 Jan 2020 21:23:23 Z").getDate().getTime()); + } + static Date dateOf(final String body) { return DateTimeFieldLenientImpl.PARSER.parse(new RawField("Date", body), null).getDate(); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
