micronaut-core
micronaut-core copied to clipboard
Cookie Parsing Failure
Expected Behavior
Parsing multiple cookies on a single Cookie HTTP Header should see all the cookies.
Actual Behaviour
Cookies are not parsed correctly due to two bugs.
-
Even when the
NettyLaxServerCookieDecoderis present, it's not used because of the presence of theMETA-INF/services/io.micronaut.http.cookie.ServerCookieDecoderwithin the micronaut-http library that points toio.micronaut.http.cookie.DefaultServerCookieDecoder. This service files should not be present, since all the loading code already has this class as a fallback, so having them present can cause problems since the loading order is indeterminate. -
While the
NettyLaxServerCookieDecoderdoes decode cookies properly, the fallback,DefaultServerCookieDecoderdoes not. It relies on thejava.net.HttpCookie.parsemethods to perform parsing. However, these methods are based on parsing theSet-Cookieheader, not theCookieheader. The format of these headers are quite different.Set-CookieisSet-Cookie: <cookie-name>=<cookie-value>; Domain=<domain-value>; Secure; HttpOnly, whileCookieisCookie: <cookie-name-1>=<cookie-value-1>; <cookie-name-2>=<cookie-value-2>; <cookie-name-3>=<cookie-value-3>. This means that if you receive multiple cookies in aCookieheader, theHttpCookiecode treats it as one cookie with multiple parameters, instead of multiple cookies.
Steps To Reproduce
No response
Environment Information
- JDK 17
Example Application
No response
Version
4.3.3
@diamondq thanks for submitting an issue. Do you have an example which reproduces cookies not being parsed correctly?
It is really difficult from the interface names (ClientCookieEncoder, ServerCookieDecoder, ServerCookieEncoder) to tell which interface encodes/decodes which header.
- ClientCookieEncoder encodes the Cookie header
- ServerCookieEncoder encodes the Set-Cookie header
- ServerCookieDecoder decodes the Cookie header
I can see two issues:
- CookieElement/CookiesElement in the netty access log handler mixes the two headers arbitrarily and uses ServerCookieDecoder for both
- DefaultServerCookieDecoder parses Set-Cookie syntax instead of Cookie syntax.
imo the docs need to be a bit more clear on this, and we need to fix DefaultServerCookieDecoder. I don't see a Cookie header decoder in the jdk though so not sure how.
Also we might want to fix the access log handler.
Following this, it seems that core > 4.3.13 removed the failing cookie decoder which solves the issue. Otherwise adding this service will fix the issue for those in lower versions