-
Notifications
You must be signed in to change notification settings - Fork 193
Cookies separator ',' #390
Comments
@muratg @Tratcher Especially 4.2.1 Syntax
|
This is an important issue for me and the fix is trivial. |
@jods4 Look at section 4.1.1 for the rest of the definition
Comma is explicitly disallowed in a cookie value. We recognize comma as a separator because it is the standard separator between multi-line HTTP headers if they get merged to a single line. People are always putting invalid junk in cookies... you're going to need to work around this by either preprocessing the headers to remove/modify the invalid values, or use a custom parser. |
@Tratcher Interesting. Bear with me as I'm no cookie expert: First, the paragraph you quote speaks for Although it is forbidden inside the value, the comma is still not a valid separator either, is it? Technically it doesn't change the fact that splitting on it is not correct behavior? In fact, no comma should appear at all in the header. From a logic point of view, it probably means that the presence of a comma implies undefined behavior? Now as you said there is junk in the outside world. If there is a comma in the header (trust me there is 😦), what should the server do? If you don't want to take action on this, can I ask you what's the best place to clean the cookies before ASP.NET parses them? PS: I think you should consider that this is bound to pop up again in the future. |
I read more of the RFC. 5.2 is especially interesting because it explains how to parse the
And indeed the algorithm is very permissive: basically it splits on 5.4 explains how to build the sent I think that "to interoperate" it would make sense for ASP.NET to implement the same algorithms. |
Comma is supported because servers like IIS take this:
And convert it to this:
Which is valid per the HTTP RFC regardless of the cookie RFC. We also have to interoperate with older clients that use the old cookie RFC. |
@Tratcher Nitpicking but no it is not valid per the Cookie RFC. It states that browsers must only send a single Any hint as to what the best place to "fix" it in application code is? This is a real problem that I have to work around anyway. PS: I am a bit disappointed that you choose to interoperate with buggy browsers (sending two cookies header or sending cookies separated by commas, both of which are invalid), which are few and rather standard-compliant nowadays; rather than interoperability with buggy websites that set invalid cookie values (which is legion, especially since JSON is a popular structured format). |
We interop with dozens of non-browser clients as well. It's a big wild web. JSON is explicitly invalid by both specs, there's no reason to try supporting it. Your best bet is to get the raw header (in midldeware or app code) and parse it yourself, and optionally write back sanitized values to the headers. |
OK. We are discussing several invalid (and mutually incompatible) options, so in the end it's your call which one is more worth interoperating with than the other.
I will try that. I have to write values back because the consumer of the cookies is the Cookie Auth Middleware, which I have no control upon. If I process the header early enough can I just set it back? Is there something to do to invalidate a potentially already parsed cookie collection, or should I (can I?) set the cookie collection myself? |
I had a similar situation where a root domain was setting invalid cookie values (whitespace specifically) that broke the aspnet 5 cookie parser and subsequently the cookie auth middleware. The hacky solution I ended up with was to specifically name the auth cookie and attach a custom cookie manager to the cookie auth middleware. When the auth middleware came looking for it's cookie value, the manager parsed the request cookie header directly with my own custom parsing that allowed for the misbehaved cookies. It was then trivial to pass the value back out to the auth middleware. |
Maybe we just need a better place to sanitize cookie values? |
Inline middleware is one of the better options. That or hooking into the IRequestCookiesFeature. |
Because of this line of code:
HttpAbstractions/src/Microsoft.Net.Http.Headers/CookieHeaderParser.cs
Line 94 in 641a7fb
ASP.NET 5 accepts both
;
and,
as cookie value separators.I know RFC 2965 said a server MUST accept
;
and SHOULD accept,
but newer RFC 6265 only speaks about;
. Specifically section 5.4 The Cookie Header, paragraph 4.2:I am opening this ticket because I have a real-world issue. My application is hosted on a sub-domain and because of that I receive third party cookies -- I know it's not the best situation but there's nothing I can do about it.
Some of those cookies contain JSON-like content:
Which breaks the ASP.NET 5 cookie parser, which in turn breaks ASP.NET Identity middleware and my requests are refused because 401 Unauthorized.
The text was updated successfully, but these errors were encountered: