Skip to content

Digest authentication requires Cookie header for auth to succeed  #124

Closed

Description

@MorganConrad

Everything works as expected from a web browser. As it should.

But I can't get my Java client code to work. I'm 99% sure I'm generating the correct MD5s, but could somebody please walk me through the steps? Must be something I don't understand there. Here's what I'm doing now

  1. create a HttpURLConnection to http://httpbin.org/digest-auth/auth/user/passwd
  2. connect, try to read the response back
  3. get an Exception (as expected, the 401 response)
  4. disconnect
  5. get the ""WWW-Authenticate" header that was returned, with nonce, etc...
  6. create a new HttpURLConnection to http://httpbin.org/digest-auth/auth/user/passwd
  7. Create the response, put it into "Authorizaton" header
  8. connect(), try to read the response
  9. This always fails with another 401.

p.s. If it helps I'd be happy to try this at some planned time, to a planned user/password, and let one of you view the log files on the httpbin side to try to see what is happening.

Activity

  1. Tryneus commented on May 7, 2014

    @Tryneus

    I ran into this myself, and looked into this with @christopherhesse's help. It appears this was introduced in commit 4870f70, which added the requirement that the request include a Cookie: header line, with any value. I have no idea why this is done, I'm pretty sure the standard doesn't require that a client handle cookies in order to perform digest authentication.

  2. MorganConrad commented on May 27, 2014

    @MorganConrad
    Author

    @Tryneus Thanks - that did the trick! IMO, should be fixed or at least noted in the docs.

  3. kevin1024 commented on Jun 21, 2014

    @kevin1024
    Contributor

    ping @sigmavirus24, what was the reason behind requiring a cookie header for digest auth in 4870f70? The RFC doesn't seem to mention anything about requiring a cookie. Is there a corresponding test in requests that expects this behavior?

  4. sigmavirus24 commented on Jun 22, 2014

    @sigmavirus24
    Contributor

    @kevin1024 yes. There's nothing requiring it in the RFC but there are servers that expect this behaviour and we needed this behaviour for requests. The RFC allows this behaviour and as such every user-agent should be prepared to handle it.

  5. kevin1024 commented on Jun 22, 2014

    @kevin1024
    Contributor

    OK I found the corresponding tests in requests. It appears that Requests used to have a bug with processing set-cookie headers on 401 responses.

    The RFC allows this behaviour and as such every user-agent should be prepared to handle it.

    Agreed. However, there is no way to use httpbin to test a a basic auth endpoint that doesn't require a cookie. Also, I don't think that the behavior of requiring a cookie to be set in order for the digest auth to succeed is actually tested or required by requests.

    How about this:

    1. Remove the requirement for a cookie to be set in order for the auth to succeed
    2. Add a way to make httpbin return extra headers from the auth request with a querystring parameter.
      This would work in the same way as the /response-headers endpoint.

    Now you can rewrite the 2 requests tests like this:

        def test_DIGEST_AUTH_RETURNS_COOKIE(self):
            url = httpbin('digest-auth', 'auth', 'user', 'pass')
            auth = HTTPDigestAuth('user', 'pass')
            s = requests.Session()
            r = s.get(url, params={'Set-Cookie':'fake=fake_value'})
            assert r.cookies['fake'] == 'fake_value'
    
            r = s.get(url, auth=auth)
            assert r.status_code == 200
    
        def test_DIGEST_AUTH_SETS_SESSION_COOKIES(self):
            url = httpbin('digest-auth', 'auth', 'user', 'pass')
            auth = HTTPDigestAuth('user', 'pass')
            s = requests.Session()
            s.get(url, auth=auth, params={'Set-Cookie':'fake=fake_value'})
            assert s.cookies['fake'] == 'fake_value'

    What do you think @sigmavirus24? I would like to come up with a solution that will still work for requests, but be general enough that httpbin can be used for testing other libraries too.

  6. changed the title [-]Can't get Digest Authorization to work[/-] [+]Digest authentication requires Cookie header for auth to succeed [/+] on Jun 22, 2014
  7. sigmavirus24 commented on Jun 23, 2014

    @sigmavirus24
    Contributor

    but be general enough that httpbin can be used for testing other libraries too

    What other libraries use HTTPBin for testing? Specifically what other libraries use HTTPBin to test this and would not consider a failure (on their part to properly catch the cookies) useful information?

  8. kevin1024 commented on Jun 23, 2014

    @kevin1024
    Contributor

    Specifically what other libraries use HTTPBin to test this and would not consider a failure (on their part to properly catch the cookies) useful information?

    Well, perhaps it would be good to have a test that tests digest auth separately from digest auth + cookie handling. It's generally good practice to keep your tests isolated.

  9. sigmavirus24 commented on Jun 23, 2014

    @sigmavirus24
    Contributor

    I would agree with you except that this is not atypical behaviour for a server using digest authentication. Really, for parity, this should be implemented for basic auth, but most clients send that pre-emptively. I for one am also beginning work on a new user-agent in a different language and will be using this endpoint to ensure that user-agent works correctly.

  10. larkost commented on Dec 23, 2014

    @larkost

    I think it is worth stating protocol here, as I found it hard to follow without refreshing my memory (more details on Wikipedia):

    1. Client opens with a request without any headers (or cookies)
    2. Server responds with a 401 code including a 'WWW-Authenticate' header.
    3. Client re-requests with an "Authorization" header formatted using information from the 'WWW-Authenticate' header it got.
    4. Server validates this information, and returns the page as normal.

    The problem we are talking about in this bug is that if during step 2 the server also sends back a cookie that in step 3 the client sends that cookie back to the server. The current code is being a bit crude in that it only checks if there is any cookie at all in step 3, rather than checking for the cookie it sends specifically.

    While this might not be in the RFC for digest authentication specifically, it is part of the underlying http protocol, and any property functioning client should respect it. I will be sending in a pull request that both sharpens this requirement (checking the cookie explicitly) and allows a specific bypass cookie so that clients that don't want to support collecting cookies during authentication can bypass this.

  11. sigmavirus24 commented on Dec 23, 2014

    @sigmavirus24
    Contributor

    @larkost that sounds like a good way forward. I think the arguments here and on other issues are leading me to think that @johnsheehan and @Runscope/httpbin need to sit down on IRC or a G+ hangout and discuss the scope/purpose of HTTPBin and put together a clearly written document for users and contributors alike. Perhaps, I should start another issue to plan this (or we can just do an email thread to schedule it).

  12. larkost commented on Dec 23, 2014

    @larkost

    For those who want to see this in more clarity, you can run httpbin locally with the following command:
    python -m httpbin.core

    Then use curl to demonstrate that it fails without cookies:
    curl --digest 'http://localhost:5000/digest-auth/auth/a/b' --user a:b

    and that it succeeds with cookies:
    curl --digest 'http://localhost:5000/digest-auth/auth/a/b' --user a:b --cookie-jar /tmp/cookietemp

    If you want to see more details, add the 'i flag to get both headers from the server.

  13. larkost commented on Dec 23, 2014

    @larkost

    I have (re) posted my pull request that should handle most of the points raised in this issue. I did take a slight turn from what I posted before as the opt-out for cookie checking is no longer a cookie, but rather a new route (/digest-auth-nocookie) that uses the same method as /digest-auth but skips the cookie part. The PR also fixes a few minor issues with /digest-auth.

    On @sigmavirus24's call for a pow-wow: I don't see much of a call for it, but if you need a data point, we at RethinkDB are using it to check the correctness of our r.http term against a http server. Having lots of endpoints to deal with all of the cases we need (and erring when we make a mistake) is invaluable.

  14. sigmavirus24 commented on Dec 24, 2014

    @sigmavirus24
    Contributor

    @larkost the meeting is more of a high-level discussion than anything else.

  15. kevin1024 commented on Dec 24, 2014

    @kevin1024
    Contributor

    IMO the main issue is that the fact that the endpoint requires cookie support is kind of suprising and undocumented. Someone might spend a lot of time digging into their digest auth code trying to debug it, when in fact the issue lies elsewhere (the cookie). Their digest auth code is fine, but the missing cookie is breaking the response.

    So maybe more of a documentation problem than anything?

  16. larkost commented on Dec 26, 2014

    @larkost

    The version in my pull request separates out the error for the cookie problem, marking it as a 403 error (vs. a 401 for digest auth issues) and returns a message explaining this in the response body. So in this it is a bit better at self-documenting.

  17. MorganConrad commented on Dec 28, 2014

    @MorganConrad
    Author

    I agree with Kevin, it's more of a documentation thing. I was one of those
    "someones" who spent a lot of time trying to debug perfectly fine digest
    code. I think the change to a 403 with a useful message will help a lot.

    On Tue, Dec 23, 2014 at 7:31 PM, Kevin McCarthy [email protected]
    wrote:

    IMO the main issue is that the fact that the endpoint requires cookie
    support is kind of suprising and undocumented. Someone might spend a lot of
    time digging into their digest auth code trying to debug it, when in fact
    the issue lies elsewhere (the cookie). Their digest auth code is fine, but
    the missing cookie is breaking the response.

    So maybe more of a documentation problem than anything?

    —
    Reply to this email directly or view it on GitHub
    #124 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions