Repository navigation
Description
I tested client application and found bug related to cookies. Here i provide test server in python. Basically, first GET request sets cookie on client, second POST request followed by redirect updates that cookie.
Example work as intended in python requests, curl, System.Net.Http.WinHttpHandler 4.3.0.
Expected Response:
-- Done Set
-- Done Index: Set-Cookie: p=not_overwritten
-- Done Index: Set-Cookie: p=overwritten_on_post_with_redirect
When i test it with .Net Core 2.0 on Windows i get this:
-- Done Set
-- Done Index: No cookie!
-- Done Index: No cookie!
When i test it with .Net Core 2.0 on Debian 8 i get this:
-- Done Set
-- Done Index: No cookie!
-- Done Index: Set-Cookie: p=overwritten_on_post_with_redirect
If i modify my server code to include Path in cookies, everything starts to work as intended. This bug could be related to https://github.com/dotnet/corefx/issues/18013
Here is test server : https://gist.github.com/ism/b5ecb70cb9f947435ac863ef5c27c7f0
Here is python requests test code: https://gist.github.com/ism/1bf4a0003c797d264193db559c8f20ac
Here is .net core 2.0 code (Windows & Debian) : https://gist.github.com/ism/b9cce55d399883c0a689cf9ec37de44d
Here is WinHttpHandler test code: https://gist.github.com/ism/3580fe5c0c2fccbc0c3e6856814c6626
Example raw headers output given by server on first GET request:
HTTP/1.1 200 OK
Date: Sun, 29 Apr 2017 14:26:31 GMT
Server: CherryPy/10.2.1
Content-Length: 11
Content-Type: text/html;charset=utf-8
Set-Cookie: p=not_overwritten; httponly
dotnet --info
.NET Command Line Tools (2.0.0-preview2-005905)
Product Information:
Version: 2.0.0-preview2-005905
Commit SHA-1 hash: 2cc5612fec
Runtime Environment:
OS Name: Windows
OS Version: 6.1.7601
OS Platform: Windows
RID: win7-x64
Base Path: C:\Program Files\dotnet\sdk\2.0.0-preview2-005905
Microsoft .NET Core Shared Framework Host
Version : 2.0.0-preview2-002093-00
Build : 1a19504
Activity
davidsh commentedon Apr 30, 2017
What is the behavior if you use .NET Framework?
And which platform do you feel is not correct?
ism commentedon Apr 30, 2017
This cookie was found on production website. Browsers( i tested chrome & firefox) handle it correctly - they set cookie, then update cookie after redirect and website keeps working. I tested it against "old" HttpWebRequest\HttpWebResponse way using .Net 4.6.1 and it doesn't send any cookie with following request, while adding them into CookieContainer collection. The reason is the way how CookieContainer parses cookie without path. Lets say our url is 'http://localhost:8080/cookie/set' , when it gets new cookie without path it will set cookie.Path to '/cookie/set' while curl will set it to '/cookie/' and chrome will set it to '/cookie'. Manually fixing path to '/cookie' and re-adding it to CookieContainer will make it work, cookie is added to next request to '/cookie/index'.
So using HttpWebRequest\HttpWebResponse
when i GET url 'http://localhost:8080/cookie/set' (it sets cookie)
if i GET same url again then previously set cookie will be included in request because its path is set to '/cookie/set'
if i GET different url lets say 'http://localhost:8080/cookie/index' cookie is not included, paths do not match
I didn't test default HttpClientHandler, as it uses same CookieContainer.
There is also redirect bug in HttpWebRequest\HttpWebResponse where cookie is not updated on redirect, but this is out of the scope of current topic.
I expect it to work as in browser, all those guys can't be wrong (firefox, chrome, python requests, curl and WinHttpHandler) :)
ism commentedon May 1, 2017
I looked at the code and here is what i found.
According to https://github.com/dotnet/corefx/blob/master/src/System.Net.Primitives/src/System/Net/Cookie.cs by default all Version 0 cookies set as CookieVariant.Plain in VerifySetDefaults
Later in same procedure we handle empty path (_pathImplicit) based on CookieVariant
So cookie-path is set to uri-path. This behavior comes from old netscape doc referenced in CookieContainer.cs
It says
It doesn't say what document path is and how to get one.
Latest RFC6265 that obsoletes RFC2965 and RFC2109 explains what we have to do as follows:
So, if Path is missing we use default-path
In short it's Uri.AbsolutePath to rightmost '/', not including '/' OR '/' if empty
Path can't be empty as in Rfc2109.
I tried following code in VerifySetDefaults in Cookie.cs:
It passes my tests.
How different software products handles it?
I paste some code for reference here
Python requests uses http.cookiejar
Chromium from https://chromium.googlesource.com/chromium/src/net/+/master/cookies/canonical_cookie.cc#77
Your feedback is very welcome!
davidsh commentedon May 1, 2017
Thx for the added information.
We are aware of RFC6265. However, the Cookie parsing code in both .NET Core and .NET Framework have not yet adapted to handling this RFC revision. We are concerned about keeping app-compat behavior with previous versions of .NET Core and .NET Framework. It might be possible to incorporate these added rules of RFC6265 and still maintain previous RFC parsing behaviors but that work has not happened yet.
We have similar cookie parsing issues such #18597 which could also benefit by handling RFC6265.
karelz commentedon May 1, 2017
Do we want to merge both into an umbrella RFC6265 issue?
davidsh commentedon May 1, 2017
This is a feature-level issue and not a bug. Recommend this be target post 2.0.
ism commentedon May 1, 2017
There is still inconsistent behavior of my test example in Windows vs Debian 8. I am not sure what to do about it.
karelz commentedon May 1, 2017
Absolutely. The question is if we close this issue as dupe of the other, or if we want to keep both opened (i.e. they are different). Or if we want to create new umbrella feature issue and then close this one (and maybe the old one as well) as dupes.
karelz commentedon May 1, 2017
@davidsh is the Windows vs. Linux inconsistency mentioned https://github.com/dotnet/corefx/issues/19166#issuecomment-298384232 also due to missing RFC? If yes, how?
Or do we need more data/investigation?
davidsh commentedon May 1, 2017
@stephentoub would have to comment on Windows vs. Linux difference. I'm not sure how this happens because I thought both implementations rely on the same .NET cookie parsing of the 'Set-Cookie' HTTP response headers as well as the 'Cookie' HTTP request headers used.
stephentoub commentedon May 1, 2017
The CurlHandler implementation just passes the Set-Cookie header string to the CookieContainer:
https://github.com/dotnet/corefx/blob/b5e830365796613f5d076970653cc939d8726a94/src/System.Net.Http/src/System/Net/Http/Unix/CurlHandler.cs#L578
and stores the header from the CookieContainer into subsequent requests:
https://github.com/dotnet/corefx/blob/a999d1094734b1ba7248d3e92e576f9fe4567f5b/src/System.Net.Http/src/System/Net/Http/Unix/CurlHandler.EasyRequest.cs#L676-L679
appbureauet commentedon May 11, 2017
I think this is the same bug I am experiencing when using Refit in a Xamarin app.
When I run agains a localhost server the default
HttpClientHandlerworks great.But when I point it to a deployed version of the same server I get 401 unauthorized responses because the
CookieContainerdoesn't save the cookies for the domain I am contacting.I can work around this by creating a
HttpClientHandlermyself with aCookieContainerthat has a dummy cookie for the domain. Then everything works great.[-].net core 2.0 HttpClient cookie handling bug [/-][+][Cookie] .net core 2.0 HttpClient cookie handling bug [/+]caesar-chen commentedon May 11, 2018
Tracked in dotnet/corefx#29651.
Workaround for cookies with empty path https://github.com/dotnet/core…
Tests to validate #19166
Workaround for cookies with empty path https://github.com/dotnet/core…
Tests to validate #19166
Adapt RFC 6265 in path handling for cookies (#39250)
Adapt RFC 6265 in path handling for cookies (dotnet#39250)