Skip to content

http.cookies.BaseCookie.load() applies partial state before raising CookieError #154546

Description

@tonghuaroot

Bug description

BaseCookie.load() parses the cookie string in two phases: parse then apply. The comment says it rejects the string if syntactically invalid. However, _is_legal_key() validation runs during the apply phase (inside Morsel.set()), not the parse phase. Cookies before an illegal key are applied to the jar before CookieError is raised.

from http.cookies import SimpleCookie, CookieError

C = SimpleCookie()
try:
    C.load("a=1; b,c=2; d=3")
except CookieError:
    pass
print(len(C))  # 1, expected 0

Related to gh-84183 but distinct: that issue is about raise-vs-silent for different invalid inputs; this is about partial state when CookieError IS raised.

CPython versions tested on

3.13, 3.14, main

Operating systems tested on

macOS

Linked PRs

Activity

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