Skip to content

fix: reject empty cookie names - #1677

Open
pjfanning wants to merge 1 commit into
apache:mainfrom
pjfanning:reject-empty-cookie-name
Open

pjfanning wants to merge 1 commit into
apache:mainfrom
pjfanning:reject-empty-cookie-name

Conversation

@pjfanning

Copy link
Copy Markdown
Member

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 (cookie-name is token), 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"). This is a behaviour change for any caller that was creating such cookies.

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

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 apache#1676 - found while reviewing that PR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants