From 15709024ed5d173ef347bdb7ffb2aca705cd93a8 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 26 Nov 2026 22:53:07 +0100 Subject: [PATCH] fix: reject empty cookie names Motivation: RFC 6265 defines cookie-name as a token, which needs at least one character. The cookie model only checked that every character of the name was a token character, which is vacuously true for an empty name, so HttpCookie("", "dark") was accepted and rendered as `Set-Cookie: =dark`. The header parser already requires a non-empty name, so only programmatically created cookies were affected. Modification: Share the name check between HttpCookiePair.validate and validateRaw and require a non-empty name there. This covers HttpCookiePair, HttpCookie and Cookie in both the Scala and Java DSLs. Result: Creating a cookie or cookie pair with an empty name throws IllegalArgumentException("cookie name must not be empty"). Tests: - sbt "http-core / Test / testOnly org.apache.pekko.http.scaladsl.model.headers.HeaderSpec org.apache.pekko.http.javadsl.model.headers.HttpCookieSpec" - 40 passed; the 2 new tests fail without the fix - scalafmt --mode diff-ref=upstream/main - no changes - git diff --check - clean - Left to CI: full test suites, MiMa, docs build References: Refs #1676 - found while reviewing that PR --- .../http/scaladsl/model/headers/HttpCookie.scala | 11 +++++++++-- .../http/javadsl/model/headers/HttpCookieSpec.scala | 6 ++++++ .../http/scaladsl/model/headers/HeaderSpec.scala | 13 +++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/headers/HttpCookie.scala b/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/headers/HttpCookie.scala index f0245c3dbf..c4d6d5bc05 100644 --- a/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/headers/HttpCookie.scala +++ b/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/headers/HttpCookie.scala @@ -51,15 +51,22 @@ object HttpCookiePair { new HttpCookiePair(name, value) {} } - private[http] def validate(name: String, value: String): Unit = { + // cookie-name is a token (RFC 6265 section 4.1.1), so it needs at least one character + private def validateName(name: String): Unit = { import HttpCookie._ + require(name.nonEmpty, "cookie name must not be empty") require(nameChars.matchesAll(name), s"'${nameChars.firstMismatch(name).get}' not allowed in cookie name ('$name')") + } + + private[http] def validate(name: String, value: String): Unit = { + import HttpCookie._ + validateName(name) require(valueChars.matchesAll(value), s"'${valueChars.firstMismatch(value).get}' not allowed in cookie content ('$value')") } private[http] def validateRaw(name: String, value: String): Unit = { import HttpCookie._ - require(nameChars.matchesAll(name), s"'${nameChars.firstMismatch(name).get}' not allowed in cookie name ('$name')") + validateName(name) require(rawValueChars.matchesAll(value), s"'${rawValueChars.firstMismatch(value).get}' not allowed in cookie content ('$value')") } diff --git a/http-core/src/test/scala/org/apache/pekko/http/javadsl/model/headers/HttpCookieSpec.scala b/http-core/src/test/scala/org/apache/pekko/http/javadsl/model/headers/HttpCookieSpec.scala index 0c28962e0f..0b84ed4dda 100644 --- a/http-core/src/test/scala/org/apache/pekko/http/javadsl/model/headers/HttpCookieSpec.scala +++ b/http-core/src/test/scala/org/apache/pekko/http/javadsl/model/headers/HttpCookieSpec.scala @@ -29,6 +29,12 @@ class HttpCookieSpec extends AnyFreeSpec with Matchers { cookie.name() must be("cookieName") cookie.value() must be("cookieValue") } + "a non-empty name only" in { + val e = the[IllegalArgumentException] thrownBy HttpCookie.create("", "cookieValue") + e.getMessage must include("cookie name must not be empty") + an[IllegalArgumentException] must be thrownBy HttpCookiePair.create("", "cookieValue") + an[IllegalArgumentException] must be thrownBy HttpCookiePair.createRaw("", "cookieValue") + } "SameSite None by default" in { HttpCookie.create("cookieName", "cookieValue").getSameSite must be(Optional.empty) } diff --git a/http-core/src/test/scala/org/apache/pekko/http/scaladsl/model/headers/HeaderSpec.scala b/http-core/src/test/scala/org/apache/pekko/http/scaladsl/model/headers/HeaderSpec.scala index c013666281..202fdf4f5b 100644 --- a/http-core/src/test/scala/org/apache/pekko/http/scaladsl/model/headers/HeaderSpec.scala +++ b/http-core/src/test/scala/org/apache/pekko/http/scaladsl/model/headers/HeaderSpec.scala @@ -34,6 +34,19 @@ class HeaderSpec extends AnyFreeSpec with Matchers { an[IllegalArgumentException] should be thrownBy Cookie("theme", ";") an[IllegalArgumentException] should be thrownBy JavaCookie.create("theme", ";") } + + "reject an empty cookie name" in { + def rejected(create: => Any) = (the[IllegalArgumentException] thrownBy create).getMessage should + include("cookie name must not be empty") + + rejected(Cookie("", "dark")) + rejected(JavaCookie.create("", "dark")) + rejected(HttpCookiePair("", "dark")) + rejected(HttpCookiePair.raw("", "dark")) + rejected(HttpCookie("", "dark")) + // `copy` goes through the constructor too + rejected(HttpCookie("theme", "dark").withName("")) + } } "Sec-WebSocket-Accept should" - {