gh-154546: Make http.cookies.BaseCookie.load() atomic on error#154674
Open
gianghungtien wants to merge 1 commit into
Open
gh-154546: Make http.cookies.BaseCookie.load() atomic on error#154674gianghungtien wants to merge 1 commit into
gianghungtien wants to merge 1 commit into
Conversation
BaseCookie.load() parses the whole cookie string before applying it, but the key and attribute legality checks run during the apply phase (inside Morsel.set() and Morsel.__setitem__()). An illegal key or unknown $-prefixed attribute part-way through the string therefore raised CookieError only after the earlier cookies had already been written to the jar, leaving it in a partially-updated state. Build the morsels in a temporary mapping and only commit them to the cookie jar once the entire string has been validated, so load() is now all-or-nothing. Existing morsels are copied before being updated, so a failing load() no longer mutates cookies that were already present.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BaseCookie.load()is documented as parse-then-apply — the code even comments that it "first parse[s] the whole cookie string and reject[s] it if it's syntactically invalid". However, the key and attribute legality checks actually run during the apply phase (insideMorsel.set()andMorsel.__setitem__()), not the parse phase. So when an illegal key or an unknown$-prefixed attribute appears part-way through the string,CookieErroris raised only after the earlier cookies have already been written into the jar, leaving it in a partially-updated state.This builds the morsels in a temporary mapping and only commits them to the jar once the whole string has validated, so
load()is now all-or-nothing. Existing morsels are copied before being updated, so a failingload()no longer mutates cookies that were already present in the jar. This also covers the sibling case of an unknown$-prefixed attribute (e.g."a=1; $foo=2"), which had the same partial-application problem.Regression tests and a
Misc/NEWS.dentry are included.Fixes #154546