Skip to content

urllib2 does not correctly handle multiple www-authenticate headers in an HTTP response #57532

Closed

Description

@dfischer
mannequin
BPO 13323
Nosy @loewis, @orsenthil, @akheron, @vadmium
Files
  • urllib2_basicauth.patch: fixes issues with multiple www-authenticate headers
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/orsenthil'
    closed_at = None
    created_at = <Date 2011-11-02.16:06:05.582>
    labels = []
    title = 'urllib2 does not correctly handle multiple\twww-authenticate headers in an HTTP response'
    updated_at = <Date 2015-02-10.06:39:27.357>
    user = 'https://bugs.python.org/dfischer'

    bugs.python.org fields:

    activity = <Date 2015-02-10.06:39:27.357>
    actor = 'martin.panter'
    assignee = 'orsenthil'
    closed = False
    closed_date = None
    closer = None
    components = []
    creation = <Date 2011-11-02.16:06:05.582>
    creator = 'dfischer'
    dependencies = []
    files = ['23619']
    hgrepos = []
    issue_num = 13323
    keywords = ['patch']
    message_count = 10.0
    messages = ['146842', '146866', '146867', '146870', '146875', '146981', '147193', '196783', '196784', '235670']
    nosy_count = 7.0
    nosy_names = ['loewis', 'orsenthil', 'dfischer', 'petri.lehtinen', 'martin.panter', 'Dmitry.Beransky', 'Sigmund.Augdal']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue13323'
    versions = ['Python 2.7']

    Activity

    1. dfischer commented on Nov 2, 2011

      dfischermannequin
      MannequinAuthor

      I ran into an application that responded with two www-authenticate challenges to an HTTP request. First, it sends a standard Basic authentication challenge and then it also returns a www-authenticate header referencing another scheme. This looks legal to me according to rfc2617.

      However, the regex in AbstractBasicAuthHandler that parses the www-authenticate header only grabs one of the challenges (the last one) and therefore urllib2 will not respond to the challenge with credentials.

    2. self-assigned this
      on Nov 2, 2011
    3. loewis commented on Nov 2, 2011

      loewismannequin
      Mannequin

      I ran into an application that responded with two www-authenticate
      challenges to an HTTP request. First, it sends a standard Basic
      authentication challenge and then it also returns a www-authenticate
      header referencing another scheme.

      What exactly do you mean by "then"? In HTTP, there is only a single
      response to a single request, so in what way does the server send
      two responses to a single request?

    4. changed the title [-]urllib2 does not correctly handle multiple www-authenticate headers in an HTTP response[/-] [+]urllib2 does not correctly handle multiple www-authenticate headers in an HTTP response[/+] on Nov 2, 2011
    5. dfischer commented on Nov 2, 2011

      dfischermannequin
      MannequinAuthor

      Perhaps I should have chosen my words more carefully. There are two www-authenticate headers the single HTTP response.

    6. loewis commented on Nov 2, 2011

      loewismannequin
      Mannequin

      So what do you propose to do? RFC 2617 specifies

      The user agent MUST choose to use one of the challenges with the
      strongest auth-scheme it understands and request credentials from the
      user based upon that challenge.

    7. dfischer commented on Nov 2, 2011

      dfischermannequin
      MannequinAuthor

      I think the key words in the RFC are "strongest auth-scheme it understands". I think in an ideal world, the urllib2 opener (given its handlers) would see that it doesn't understand the second auth-scheme and then see if it understands the first one. However, the term "strongest" implies an ordering among the auth schemes.

    8. loewis commented on Nov 3, 2011

      loewismannequin
      Mannequin

      Would you like to provide a patch?

    9. dfischer commented on Nov 6, 2011

      dfischermannequin
      MannequinAuthor

      With this patch, the AbstractBasicAuthHandler (and its subclasses) performs a regex search on the amalgamated "www-authenticate" header for basic authentication.

      This fixes the case where you have an HTTP response of the form:

      HTTP/1.1 401 Authorization Required
      Server: HTTPd/1.0
      WWW-Authenticate: Basic realm="Secure Area"
      WWW-Authenticate: SomethingElse realm="Alternative"
      ...

      In the above case, AbstractBasicAuthHandler will attempt basic auth against the "Secure Area" realm.

    10. SigmundAugdal commented on Sep 2, 2013

      SigmundAugdalmannequin
      Mannequin

      Can someone please apply this patch or provide a reason why it should not be applied?

    11. orsenthil commented on Sep 2, 2013

      @orsenthil
      Member

      Sigmund: Sorry for the delay. I shall act on this.

    12. vadmium commented on Feb 10, 2015

      @vadmium
      Member

      bpo-15310 appears to have a more thorough patch

    13. transferred this issue fromon Apr 10, 2022
    14. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Nov 26, 2023
    15. moved this from Todo to In Progress in Urllib issueson Oct 28, 2026
    16. orsenthil commented on Oct 28, 2026

      @orsenthil
      Member

      This is handled by

      AbstractBasicAuthHandler - #18284 - Completed in 2020

      AbstractDigestAuthHandler - #160828 - Tracked in #59515

    17. moved this from In Progress to Done in Urllib issueson Oct 28, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions