Repository navigation
Parse asctime dates and RFC 850 years 70-99 in DateUtils - #952
Open
shinseongsu wants to merge 1 commit into
Open
shinseongsu wants to merge 1 commit into
shinseongsu wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_ASCTIMEhas no zone, soInstant.fromalways fails andparseStandardDate("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.
yyreads the year as 2000-2099, soThursday, 01-Jan-70 00:00:00 GMTbecomes 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, andLaxExpiresHandleralready uses it.End-to-end, with the default classic client against a raw socket server (master
28844736b):Set-Cookie: session=deleted; Expires=Thursday, 01-Jan-70 00:00:00 GMTInvalid 'expires' attributeSet-Cookie: session=deleted; Expires=Friday, 31-Dec-99 23:59:59 GMTSet-Cookie: id=1; Expires=Sat Nov 6 08:49:37 2094503+Retry-After: <asctime, now+4s>The same parser is used for
Date/Expiresin httpclient5-cache. There, an asctime value was treated as missing.Changes:
FORMATTER_ASCTIMEparses in UTC and accepts a space-padded day.Nov 6instead ofNov 6, which is the asctime layout.ZonedDateTimedirectly with this formatter now prints the UTC time instead of the local time, and formatting anInstantworks instead of throwing.DateUtils.formatDatealready converted to GMT, so its output is unchanged apart from the padding.FORMATTER_RFC1036uses base year 1970 for two-digit years.PATTERN_ASCTIME,PATTERN_RFC1036and the deprecatedparseDate(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
70as 2070. I used the fixed RFC 6265 rule because the same formatters parse cookieExpires, and it matchesLaxExpiresHandler. 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 GMTis stored as 2094-02-28, andFri, 31 Apr 2094as 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. ShouldDateUtilsreject these? Rejecting them would change behavior, so I left it out of this PR.Tested: the new
TestDateUtilstests fail on master and pass with this change.mvn -pl httpclient5,httpclient5-cache verifypasses (1162 and 745 tests).