From 76bcdeb1c13fc85000cef87f050e5c24338d0367 Mon Sep 17 00:00:00 2001 From: "He-Pin(kerr)" Date: Wed, 18 Nov 2026 05:22:21 +0800 Subject: [PATCH] fix: accept zero-offset cookie expiry dates Motivation: Legacy cookie expiry dates may specify UTC as -0000. The existing cookie date rules do not recognize this suffix. Modification: Extend only the cookie date rules to accept -0000. Try those rules before the shared HTTP-date rule so a supported asctime prefix does not leave the suffix unconsumed. Add positive format, canonical rendering and subsequent Path attribute coverage, and document the scoped receiver compatibility. Result: Supported cookie expiry formats retain their UTC value and following attributes. Shared HTTP date rules and existing cookie validation remain unchanged. This is not a full RFC 6265 token-parser implementation. Tests: - Five positive zero-offset cases fail against exact baseline rules. - All 14 selected positive cases pass against the final rules. - Scala 2.13.18 production compile with deprecation/import warnings fatal. - Focused MiMa 1.2.1 comparison; cached support classes, not a full build. - Scalafmt 3.11.5 and git diff --check pass. - Full sbt, Scala 3, Paradox and aggregate MiMa not run in this offline check. References: None - RFC 6265 sections 5.1.1 and 5.2.1; independent implementation informed by Netty commit edc4842309ad0491b7f2088c8116318b3d680db8. --- docs/src/main/paradox/client-side/request-level.md | 4 +++- .../pekko/http/impl/model/parser/CommonRules.scala | 10 ++++++---- .../http/impl/model/parser/HttpHeaderSpec.scala | 13 +++++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/docs/src/main/paradox/client-side/request-level.md b/docs/src/main/paradox/client-side/request-level.md index 80a05ede5c..5e8a4cc555 100644 --- a/docs/src/main/paradox/client-side/request-level.md +++ b/docs/src/main/paradox/client-side/request-level.md @@ -95,7 +95,9 @@ to direct the request to. ## Collecting headers from a server response -Sometimes we would like to get only headers of specific type which are sent from a server. In order to collect headers in a type safe way Apache Pekko HTTP API provides a type for each HTTP header. Here is an example for getting all cookies set by a server (`Set-Cookie` header): +Sometimes we would like to get only headers of specific type which are sent from a server. In order to collect headers in a type safe way Apache Pekko HTTP API provides a type for each HTTP header. Cookie expiry dates in the supported legacy date formats also accept the `-0000` UTC suffix and are rendered with `GMT`. This does not change parsing of the HTTP `Date` or `Expires` headers. + +Here is an example for getting all cookies set by a server (`Set-Cookie` header): Scala : @@snip [HttpClientExampleSpec.scala](/docs/src/test/scala/docs/http/scaladsl/HttpClientCollectingHeaders.scala) diff --git a/http-core/src/main/scala/org/apache/pekko/http/impl/model/parser/CommonRules.scala b/http-core/src/main/scala/org/apache/pekko/http/impl/model/parser/CommonRules.scala index 64129fe605..5713dabd9a 100644 --- a/http-core/src/main/scala/org/apache/pekko/http/impl/model/parser/CommonRules.scala +++ b/http-core/src/main/scala/org/apache/pekko/http/impl/model/parser/CommonRules.scala @@ -300,23 +300,25 @@ private[parser] trait CommonRules extends StringBuilding { this: Parser => ignoreCase("expires=") ~ OWS ~ `cookie-expires-date` ~> { (c: HttpCookie, dt: DateTime) => c.withExpires(dt) } } - // Cookie receivers accept one or two digits per time component (RFC 6265, section 5.1.1). + // Cookie receivers accept one or two digits per time component and interpret dates as UTC (RFC 6265, section 5.1.1). // Keep this separate from HTTP-date: this only extends the existing date shapes, not the full token algorithm. def `cookie-expires-date`: Rule1[DateTime] = rule { - (`HTTP-date` | `cookie-IMF-fixdate` | `cookie-asctime-date` | + (`cookie-IMF-fixdate` | `cookie-asctime-date` | `HTTP-date` | zeroOrMore(ANY) ~ push(DateTime.MinValue)) ~ OWS } def `cookie-time-of-day` = rule { (digit2 | digit) ~ ':' ~ (digit2 | digit) ~ ':' ~ (digit2 | digit) } def `cookie-IMF-fixdate` = rule { - (`day-name-l` | `day-name`) ~ ", " ~ (date1 | date2) ~ ' ' ~ `cookie-time-of-day` ~ ' ' ~ ("GMT" | "UTC") ~> { + (`day-name-l` | `day-name`) ~ ", " ~ + (date1 | date2) ~ ' ' ~ `cookie-time-of-day` ~ ' ' ~ + ("GMT" | "UTC" | "-0000") ~> { (wkday, day, month, year, hour, min, sec) => createDateTime(year, month, day, hour, min, sec, wkday) } } def `cookie-asctime-date` = rule { - `day-name` ~ ' ' ~ date3 ~ ' ' ~ `cookie-time-of-day` ~ ' ' ~ year ~> { + `day-name` ~ ' ' ~ date3 ~ ' ' ~ `cookie-time-of-day` ~ ' ' ~ year ~ optional(" -0000") ~> { (wkday, month, day, hour, min, sec, year) => createDateTime(year, month, day, hour, min, sec, wkday) } } diff --git a/http-core/src/test/scala/org/apache/pekko/http/impl/model/parser/HttpHeaderSpec.scala b/http-core/src/test/scala/org/apache/pekko/http/impl/model/parser/HttpHeaderSpec.scala index b827d46e69..d1f5898198 100644 --- a/http-core/src/test/scala/org/apache/pekko/http/impl/model/parser/HttpHeaderSpec.scala +++ b/http-core/src/test/scala/org/apache/pekko/http/impl/model/parser/HttpHeaderSpec.scala @@ -38,6 +38,19 @@ class HttpHeaderSpec extends AnyFreeSpec with Matchers { val `application/vnd.spray` = MediaType.applicationBinary("vnd.spray", MediaType.Compressible) val PROPFIND = HttpMethod.custom("PROPFIND") + for (date <- Seq( + "Sun, 06 Nov 1994 08:09:07 -0000", + "Sunday, 06-Nov-94 08:09:07 -0000", + "Sun Nov 06 08:09:07 1994 -0000", + "Sun, 06 Nov 1994 8:9:7 -0000", + "Sun Nov 06 8:9:7 1994 -0000")) { + s"cookie expiry zero-offset suffix $date" in { + s"Set-Cookie: sid=value; Expires=$date; Path=/" =!= + `Set-Cookie`(HttpCookie("sid", "value", expires = Some(DateTime(1994, 11, 6, 8, 9, 7)), path = Some("/"))) + .renderedTo("sid=value; Expires=Sun, 06 Nov 1994 08:09:07 GMT; Path=/") + } + } + for ((time, hour, minute, second) <- Seq( ("08:09:07", 8, 9, 7), ("8:9:7", 8, 9, 7), ("8:09:07", 8, 9, 7), ("08:9:07", 8, 9, 7), ("08:09:7", 8, 9, 7), ("0:0:0", 0, 0, 0), ("23:59:59", 23, 59, 59))) {