Skip to content

Parse asctime dates and RFC 850 years 70-99 in DateUtils - #952

Open
shinseongsu wants to merge 1 commit into
apache:masterfrom
shinseongsu:dateutils-asctime-two-digit-year
Open

shinseongsu wants to merge 1 commit into
apache:masterfrom
shinseongsu:dateutils-asctime-two-digit-year

Conversation

@shinseongsu

Copy link
Copy Markdown
Contributor

I found this while comparing date parsing across HTTP libraries for a Netty PR (netty/netty#18429). Two of the three HTTP-date formats don't parse correctly in DateUtils.

asctime never parses. FORMATTER_ASCTIME has no zone, so Instant.from always fails and parseStandardDate("Thu Feb 22 17:20:18 2024") returns null. This is the input from HTTPCLIENT-2319. That issue only asked about the exception, and the fix made it return null, so the value still can't be parsed. The formatter also rejects the space-padded day that asctime uses (Nov 6). RFC 9110 section 5.6.7 says a recipient MUST accept all three formats, and that asctime values are in UTC.

RFC 850 years 70-99 fail. yy reads the year as 2000-2099, so Thursday, 01-Jan-70 00:00:00 GMT becomes 2070-01-01. That day is a Wednesday, so the weekday check fails and the value is rejected. A date in 1970 is a common way to delete a cookie. With a two-digit year it is rejected, and the default classic client (strict cookie spec, BasicExpiresHandler(DateUtils.STANDARD_PATTERNS)) keeps the cookie. With this change, 70-99 is read as 1970-1999 and 00-69 as 2000-2069. This is the RFC 6265 section 5.1.1 rule, and LaxExpiresHandler already uses it.

End-to-end, with the default classic client against a raw socket server (master 28844736b):

Server sends master this PR
Set-Cookie: session=deleted; Expires=Thursday, 01-Jan-70 00:00:00 GMT cookie kept, WARN Invalid 'expires' attribute deleted
Set-Cookie: session=deleted; Expires=Friday, 31-Dec-99 23:59:59 GMT cookie kept deleted
Set-Cookie: id=1; Expires=Sat Nov 6 08:49:37 2094 cookie dropped stored, 2094-11-06T08:49:37Z
503 + Retry-After: <asctime, now+4s> retried after 1008 ms (date ignored) retried after 3988 ms

The same parser is used for Date/Expires in httpclient5-cache. There, an asctime value was treated as missing.

Changes:

  • FORMATTER_ASCTIME parses in UTC and accepts a space-padded day.
    • Formatting now writes Nov 6 instead of Nov 6, which is the asctime layout.
    • Formatting a non-UTC ZonedDateTime directly with this formatter now prints the UTC time instead of the local time, and formatting an Instant works instead of throwing. DateUtils.formatDate already converted to GMT, so its output is unchanged apart from the padding.
  • FORMATTER_RFC1036 uses base year 1970 for two-digit years.
  • I left PATTERN_ASCTIME, PATTERN_RFC1036 and the deprecated parseDate(String, String[]) alone. A pattern string can't express a base year, so that path keeps the old behavior.

One thing to check: RFC 9110 uses a sliding window for two-digit years (more than 50 years in the future means the past). Today that window would read 70 as 2070. I used the fixed RFC 6265 rule because the same formatters parse cookie Expires, and it matches LaxExpiresHandler. I can change it to the sliding window if you prefer.

A question, not in this PR: the formatters use ResolverStyle.SMART, so a day that doesn't exist is moved to the last day of the month. Sun, 31 Feb 2094 08:49:37 GMT is stored as 2094-02-28, and Fri, 31 Apr 2094 as 2094-04-30. With a different weekday (Thu, 31 Apr 2094), the whole cookie is dropped instead. RFC 6265 section 5.1.1 says to fail when the date doesn't exist. This is the same issue as the Netty PR. Should DateUtils reject these? Rejecting them would change behavior, so I left it out of this PR.

Tested: the new TestDateUtils tests fail on master and pass with this change. mvn -pl httpclient5,httpclient5-cache verify passes (1162 and 745 tests).

FORMATTER_ASCTIME has no zone, so Instant.from always failed and
parseStandardDate returned null for every asctime value. It also did not
accept a space-padded day of month ("Nov  6"). Read asctime values as UTC,
as RFC 9110 section 5.6.7 requires, and accept the padded day.

FORMATTER_RFC1036 read a two-digit year as 2000-2099, so
"Thursday, 01-Jan-70 00:00:00 GMT" became 2070, the weekday did not match
and the value was rejected. A cookie with that Expires was not deleted by
the default (strict) cookie spec. Read 70-99 as 1970-1999 and 00-69 as
2000-2069, as RFC 6265 section 5.1.1 requires and LaxExpiresHandler
already does.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant