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" - {