Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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')")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" - {
Expand Down