Serious CookieContainer Error?

Am I missing something or is this a bug in CookieContainer?

I add 3 cookies to the container, and then I call the GetCookieHeader function for 2 URLs:

CookieContainer cc = new CookieContainer(); cc.Add(new Cookie("Cookie1", "1", "/a", "test.com")); cc.Add(new Cookie("Cookie2", "2", "/a/0/", "test.com")); cc.Add(new Cookie("Cookie3", "3", "/a/1/", "test.com")); var result1 = cc.GetCookieHeader(new Uri("http://test.com/a/1/list")); Assert.AreEqual("Cookie3=3; Cookie1=1", result1); var result2 = cc.GetCookieHeader(new Uri("http://test.com/a/0/list")); Assert.AreEqual("Cookie2=2; Cookie1=1", result2); 

The problem is the last statement that throws an exception, because the returned header is only "Cookie2 = 2". I see no reason why Cookie1 cookie is missing here. According to RFC6265, it should return two cookies similar to the first statement above, right?

A few notes:

  • Cookies are in the container, so this is not an addition to this problem, but the GetHeader function.

  • This behavior remains unchanged when adding 4, 5, etc. cookie: only the path corresponding to the last cookie added will contain the cookie for the base path!

  • The behavior changes when you delete all "a" in the paths and use only "/", "/ 0 /" and "/ 1 /" as paths for 3 cookies and " http://test.com/1/list " and " http://test.com/0/list " in approval URLs). All statements then succeed - I would expect the same behavior with "a"!

PS: Let me add the appropriate part from the specification:

The request path path matches the specified cookie path if at least one of the following conditions is true:

- The cookie path and the request path are identical.

- The cookie path is the prefix of the request path, and the last character of the cookie path is% x2F ("/").

- The cookie path is the prefix of the request path, and the first character of the request path that is not included in the cookie path is the% x2F ("/") character.

So, for me, this is clearly a mistake ...?

+5
source share
1 answer

I believe I found the problem. In the System.Net.CookieContainer class in the function InternalGetCookies (Uri) around lines 700-730 there is an iteration along the paths. After the first path is found, these cookies are added, and the iteration is then broken and additional values ​​for "/" are added!

In other words: you only get cookies from the first matching path, as well as from "/". For me, this is a clear misconduct and therefore a mistake - or I do not understand the RFC I mentioned above.

+2
source

Source: https://habr.com/ru/post/1207162/


All Articles