Skip to content

http.cookies.SimpleCookie.load() fails to consistently handle malformed cookies #127195

Open

Description

@moonsikpark

Bug report

Bug description:

There are several issues with http.cookies.SimpleCookie.load() that deviate from current browser behavior:

  1. Malformed cookies are not processed at all

Consider the cookie a=b;c=d\x09d;e=f. The e value contains \x09, which is not allowed per RFC 6265, Section 4.1.1.

When this is sent to a browser (Chrome 130), the browser processes all valid cookies and filters out invalid ones:

HTTP/1.1 200 OK
Content-Type: text/html
Set-Cookie: a=b;
Set-Cookie: c=d	d;
Set-Cookie: e=f

Resulting behavior:

> document.cookie
< 'a=b; e=f'

However, http.cookies.SimpleCookie.load() ignores the entire cookie string:

>>> from http import cookies
>>> C = cookies.SimpleCookie()
>>> C.load("a=b;c=d\x09d;e=f")
>>> C.output()
''
  1. Malformed cookies are inconsistently processed

Consider the cookie a=b;c={"d":"e"};f=g. The c value is invalid per RFC 6265, Section 4.1.1.

Browsers process this cookie without an issue:

HTTP/1.1 200 OK
Content-Type: text/html
Set-Cookie: a=b;
Set-Cookie: c={"d":"e"};
Set-Cookie: f=g

Resulting behavior:

> document.cookie
< 'a=b; c={"d":"e"}; f=g'

However, http.cookies.SimpleCookie.load() processes only the valid portion before the malformed cookie and stops entirely:

>>> from http import cookies
>>> C = cookies.SimpleCookie()
>>> C.load('a=b; c={"d":"e"}; f=g')
>>> C.output()
'Set-Cookie: a=b'

It seems we should ensure consistent handling by (a) processing all valid cookies and discarding only invalid ones, or
(b) rejecting the entire cookie string if any invalid cookie is present.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 23, 2024
  2. moonsikpark commented on Nov 24, 2024

    @moonsikpark
    ContributorAuthor

    After further investigation, it appears the module exhibits inconsistent behavior when encountering unexpected inputs and it is likely due to its regex-based parsing, which assumes very clean input.

    cpython/Lib/http/cookies.py

    Lines 420 to 437 in a4d4c1e

    _CookiePattern = re.compile(r"""
    \s* # Optional whitespace at start of cookie
    (?P<key> # Start of group 'key'
    [""" + _LegalKeyChars + r"""]+? # Any word of at least one letter
    ) # End of group 'key'
    ( # Optional group: there may not be a value.
    \s*=\s* # Equal Sign
    (?P<val> # Start of group 'val'
    "(?:[^\\"]|\\.)*" # Any doublequoted string
    | # or
    \w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
    | # or
    [""" + _LegalValueChars + r"""]* # Any word or empty string
    ) # End of group 'val'
    )? # End of optional value group
    \s* # Any number of spaces.
    (\s+|;|$) # Ending either at space, semicolon, or EOS.
    """, re.ASCII | re.VERBOSE) # re.ASCII may be removed if safe.

    If the maintainers are in agreement, I’d like to propose a patch to improve cookie parsing by following these steps:

    1. Split the cookie into parts.
    2. For each part:
      (a) Validate the key. If invalid, issue a warning and skip the part.
      (b) Validate the value. If invalid, issue a warning and skip the part.
    3. Store only valid parts in the cookie jar.

    Additionally, I’d like to gather opinions on whether we should allow invalid—but commonly occurring—characters in cookie values, such as those used in JSON. While the RFC advises against accepting these characters, they are widely accepted by major browsers and have become a common practice among web developers.

    @picnixz, it seems you triaged my issue. If possible, could you notify the appropriate core maintainers?

  3. picnixz commented on Nov 27, 2024

    @picnixz
    Member
  4. vadmium commented on Dec 12, 2024

    @vadmium
    Member

    The first case with the tab character looks the same as Issue #75637. For the second case with double quotes, there are a few related reports:

  5. nburns commented on Aug 6, 2025

    @nburns
    Contributor

    Was wondering if someone would be willing to take a look at my issue/PR (and provide feedback) related to a similar issue with the cookies parser:

    Browsers tolerate json values well, and for example Zoom sets cookies with json in them:
    #92936

    so it would be nice if the cookie parser could handle them (webob for example is able to do so): #113663

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

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions