Skip to content

[Cookie] HttpClient does not properly parse all Set-Cookie headers #20942

Closed

Description

@Nothing4You

Hello,

I'm currently trying to build a .Net Core application that automates some HTTP requests.
In the first request I receive several cookies which should be sent back to the server on the second request.
For demonstration purposes I have created a small PHP script that sends some Set-Cookie headers similar to the style I receive them from the actual application.
The source code of this script is https://gist.github.com/Nothing4You/6623cda16eb2c2c5b4d3d9106b95a6ce. You can find a live version at https://hosting.rep.pm/cookietest.php.
I have tested the script using a web browser (Firefox), curl (in a linux shell) aswell as using python requests, which all handle cookies and show me the expected output in the second request:

Cookie[test1]: test1value
Cookie[test2]: test=2&value=2
Cookie[test3]: $test-3
Cookie[test4]: test4

However, if I try to do the same thing using System.Net.Http.HttpClient it only sets the 2 out of 4 cookies.

According to Visual Studio I'm using SDK version 1.1.1.
HttpClient is the following version:

Assembly: System.Net.Http, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Assembly location: .nuget\packages\system.net.http\4.3.0\ref\netstandard1.3\System.Net.Http.dll

Example code: https://gist.github.com/Nothing4You/c04af6781f8520efb4921ef144733731

Activity

  1. davidsh commented on Apr 6, 2017

    @davidsh
    Contributor

    Does this repro on just Windows or Linux or both?

  2. Nothing4You commented on Apr 6, 2017

    @Nothing4You
    Author

    I have only tested on windows so far.

  3. Nothing4You commented on Apr 6, 2017

    @Nothing4You
    Author

    Same results on linux.

  4. davidsh commented on Apr 6, 2017

    @davidsh
    Contributor

    However, if I try to do the same thing using System.Net.Http.HttpClient it only sets the 2 out of 4 cookies.

    Please post the specific of which cookies are being "skipped"? I assume being "skipped" implies that they are not being placed into the HttpClientHandler.CookieContainer?

    It would be useful if you could post a detailed Fiddler trace of the request and response as seen by Fiddler and then post which cookies on the responses 'Set-Cookie' headers are being "skipped" by HttpClient.

  5. davidsh commented on Apr 6, 2017

    @davidsh
    Contributor
  6. Nothing4You commented on Apr 6, 2017

    @Nothing4You
    Author

    In the gist with the code you can see which headers are working and which are not.

  7. Nothing4You commented on Apr 6, 2017

    @Nothing4You
    Author
  8. stephentoub commented on Apr 6, 2017

    @stephentoub
    Member

    This repros on desktop as well. This might be a red herring, but the difference between the cookies that work and those that don't appears to be the domain. The ones that don't work set a domain of domain=hosting.rep.pm. The ones that do work either don't have a domain or set one as domain=.hosting.rep.pm.

  9. davidsh commented on Apr 6, 2017

    @davidsh
    Contributor

    Usually the rules for 'Set-Cookie' require the leading prefix of ".". I'll need to lookup the applicable RFCs and it also differs depnding on which cookie RFC is being interpreted.

  10. davidsh commented on Apr 6, 2017

    @davidsh
    Contributor

    Not sure if this is related to https://github.com/dotnet/corefx/issues/11795 but as you can see cookie parsing changes are risky to make due to app-compat.

  11. Nothing4You commented on Apr 6, 2017

    @Nothing4You
    Author

    I have seen that bug, however, in this case it seems like the exact opposite is happening as those without a dot prefix don't work while the one with dot prefix aswell as the unspecified one work.

  12. davidsh commented on Apr 6, 2017

    @davidsh
    Contributor

    If you look at the current cookie parsing code:
    https://github.com/dotnet/corefx/blob/master/src/System.Net.Primitives/src/System/Net/CookieContainer.cs#L41

    These comments here are relevant:

    A cookie with Domain=ajax.com will be rejected because the value for Domain does not begin with a dot.

    // According to RFC2109/RFC2965, the cookie will be rejected for matching if:
    // * The value for the Domain attribute contains no embedded dots or does not start with a dot.
    // * The value for the request-host does not domain-match the Domain attribute.
    // " The request-host is a FQDN (not IP address) and has the form HD, where D is the value of the Domain 
    //  attribute, and H is a string that contains one or more dots.
    //
    // Examples:
    // * A cookie from request-host y.x.foo.com for Domain=.foo.com would be rejected, because H is y.x 
    //  and contains a dot.
    // 
    // * A cookie from request-host x.foo.com for Domain=.foo.com would be accepted.
    //
    // * A cookie with Domain=.com or Domain=.com., will always be rejected, because there is no embedded dot.
    //
    // * A cookie with Domain=ajax.com will be rejected because the value for Domain does not begin with a dot.
  13. Nothing4You commented on Apr 6, 2017

    @Nothing4You
    Author

    RFC2965 doesn't seeem to be relevant for Set-Cookie.
    In RFC2109 I found the following:

    Domain=domain

     Optional.  The Domain attribute specifies the domain for which the
     cookie is valid.  An explicitly specified domain must always start
     with a dot.
    

    However, as already written in my original post, firefox, curl and python-requests all support this method just fine, so I believe there should be a way to use the appearently commonly accepted way with .NET aswell.

    According to RFC2109 is obsoleted by RFC2965, which in turn is obsoleted by RFC6265. RFC6265 is in the state PROPOSED STANDARD, which according to wikipedia usually means it should be fine to be used in production code (correct me if I'm wrong).

    This RFC has the relevant part:

    If the first character of the attribute-value string is %x2E ("."):

     Let cookie-domain be the attribute-value without the leading %x2E
     (".") character.
    

    Otherwise:

     Let cookie-domain be the entire attribute-value.
    

    Convert the cookie-domain to lower case.

    Append an attribute to the cookie-attribute-list with an attribute-
    name of Domain and an attribute-value of cookie-domain.

    This would just strip a leading dot and parse domains the same way it does now with a leading dot.

  14. karelz commented on Apr 18, 2017

    @karelz
    Member

    Triage: Same behavior as on Desktop, so not critical for 2.0. Moving to Future.

  15. changed the title [-]HttpClient does not properly parse all Set-Cookie headers[/-] [+][Cookie] HttpClient does not properly parse all Set-Cookie headers[/+] on May 8, 2018
  16. caesar-chen commented on May 11, 2018

    @caesar-chen
    Contributor

    Tracked in dotnet/corefx#29651.

  17. transferred this issue fromdotnet/corefxon Jan 31, 2020
  18. added this to the 3.0 milestone on Jan 31, 2020
  19. ghost locked as resolved and limited conversation to collaborators on Dec 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions