Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cookielib chokes on non-integer cookie version, should ignore it instead #48174

Closed
DenNukem mannequin opened this issue Sep 21, 2008 · 8 comments
Closed

cookielib chokes on non-integer cookie version, should ignore it instead #48174

DenNukem mannequin opened this issue Sep 21, 2008 · 8 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@DenNukem
Copy link
Mannequin

DenNukem mannequin commented Sep 21, 2008

BPO 3924
Nosy @loewis, @birkenfeld
Files
  • issue3924.patch
  • 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 = None
    closed_at = <Date 2010-05-22.11:33:38.200>
    created_at = <Date 2008-09-21.18:30:24.167>
    labels = ['type-bug', 'library']
    title = 'cookielib chokes on non-integer cookie version, should ignore it instead'
    updated_at = <Date 2010-05-22.11:33:38.198>
    user = 'https://bugs.python.org/DenNukem'

    bugs.python.org fields:

    activity = <Date 2010-05-22.11:33:38.198>
    actor = 'georg.brandl'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-05-22.11:33:38.200>
    closer = 'georg.brandl'
    components = ['Library (Lib)']
    creation = <Date 2008-09-21.18:30:24.167>
    creator = 'DenNukem'
    dependencies = []
    files = ['11888']
    hgrepos = []
    issue_num = 3924
    keywords = ['patch']
    message_count = 8.0
    messages = ['73518', '74825', '75213', '75214', '77493', '83013', '85790', '106298']
    nosy_count = 6.0
    nosy_names = ['loewis', 'georg.brandl', 'jjlee', 'DenNukem', 'henriko', 'andysk']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue3924'
    versions = ['Python 2.6', 'Python 3.0', 'Python 3.1', 'Python 2.7']

    @DenNukem
    Copy link
    Mannequin Author

    DenNukem mannequin commented Sep 21, 2008

    PROBLEM:

    Some sites (e.g. https://itunesconnect.apple.com) sends cookies where
    version is "1" instead of 1. Cookielib chokes on it so none of the
    cookies work after that.

    PROBLEM CODE:
    def _cookie_from_cookie_tuple(self, tup, request):
    ...
    name, value, standard, rest = tup
    ...
    version = standard.get("version", None)
    if version is not None: version = int(version) << CRASH HERE!!!

    WORKAROUND:

    use my own cookie jar, e.g.:

    class MyCookieJar(CookieJar):
        def _cookie_from_cookie_tuple(self, tup, request):
            name, value, standard, rest = tup
            standard["version"]= None
            CookieJar._cookie_from_cookie_tuple(self, tup, request)

    REAL FIX:
    do not assume that version is int, keep it as string if it does not
    parse as int:

    CRASH STACK:

    /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/cookielib.py:1577:
    UserWarning: cookielib bug!
    Traceback (most recent call last):
      File
    "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/cookielib.py",
    line 1575, in make_cookies
        parse_ns_headers(ns_hdrs), request)
      File
    "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/cookielib.py",
    line 1532, in _cookies_from_attrs_set
        cookie = self._cookie_from_cookie_tuple(tup, request)
      File
    "/Users/denis/Documents/svn2/tson/main/sales/src/download_sales.py",
    line 28, in _cookie_from_cookie_tuple
        CookieJar._cookie_from_cookie_tuple(self, tup, request)
      File
    "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/cookielib.py",
    line 1451, in _cookie_from_cookie_tuple
        if version is not None: version = int(version)
    ValueError: invalid literal for int() with base 10: '"1"'
      _warn_unhandled_exception()

    @DenNukem DenNukem mannequin added the type-crash A hard crash of the interpreter, possibly with a core dump label Sep 21, 2008
    @jjlee
    Copy link
    Mannequin

    jjlee mannequin commented Oct 15, 2008

    The sensible fix for this is to strip the quotes off, defaulting to
    version 0 on failure to parse the version cookie-attribute. It's not
    necessary to retain the original version string.

    By the way, what you posted warning rather than a strictly unhandled
    exception or "crash" -- it's a bug, but won't cause the program to stop.
    And by "none of the cookies work after that", you mean that no cookies
    in headers containing the quoted version cookie-attribute are accepted
    by the cookiejar.

    FWIW, this bug only affects RFC 2109 cookies, not RFC 2965 cookies.

    @jjlee
    Copy link
    Mannequin

    jjlee mannequin commented Oct 25, 2008

    Patch with tests attached. The patch is slightly different to my first
    suggestion: in the patch, invalid version values cause the cookie to be
    ignored (but double quotes around valid versions are fine).

    @jjlee
    Copy link
    Mannequin

    jjlee mannequin commented Oct 25, 2008

    The bug is present on trunk and on the py3k branch, so I've selected
    versions "Python 2.7" and "Python 3.0"

    This is a straightforward bug, so I selected 2.5.3 and 2.6 also, to
    indicate this is a candidate for backport.

    @jjlee jjlee mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Oct 25, 2008
    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Dec 10, 2008

    As the patch hasn't been applied to the trunk yet, I'm rejecting it for
    2.5.3.

    @henriko
    Copy link
    Mannequin

    henriko mannequin commented Mar 2, 2009

    The cookiejar workaround in the first comment did not work for me. The
    cookies didn't stick in it. I guess version needs to be set.. this
    worked for me:

    class ForgivingCookieJar(cookielib.CookieJar):
        def _cookie_from_cookie_tuple(self, tup, request):
            name, value, standard, rest = tup
            version = standard.get("version", None)
            if version is not None:
                # Some servers add " around the version number, this module
    expects a pure int.
                standard["version"] = version.strip('"')
            return cookielib.CookieJar._cookie_from_cookie_tuple(self, tup,
    request)

    @andysk
    Copy link
    Mannequin

    andysk mannequin commented Apr 9, 2009

    Thank you Henrik. The workaround in the first comment caused some
    cookies to be handled incorrectly due to ignoring version on all
    cookies, but your workaround is nice.

    It seems that the patch jjlee supplied should really be applied,
    however, to save others from having this problem.

    @birkenfeld
    Copy link
    Member

    Thanks for the patch! Applied in r81465 f. Merged to 2.x in r81467, will merge to 3k later.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant