Fix: webtest/app.py's set_cookie() calls escape_cookie_value()... - #278
Open
M001N wants to merge 1 commit into
Open
Fix: webtest/app.py's set_cookie() calls escape_cookie_value()...#278M001N wants to merge 1 commit into
M001N wants to merge 1 commit into
Conversation
TestApp.set_cookie() escapes the value with escape_cookie_value() (wraps it in quotes and substitutes special characters), but the cookies property returned cookie.value verbatim, so cookies.get(name) returned the raw quoted/escaped value instead of the original one. Add unescape_cookie_value() in webtest/compat.py, the inverse of escape_cookie_value(), and apply it in TestApp.cookies. Values that aren't wrapped in double quotes (e.g. cookies already unquoted by the cookiejar when parsing a real Set-Cookie header) are returned unchanged, so this doesn't affect cookies set via HTTP responses.
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.
Summary
Added unescape_cookie_value() in webtest/compat.py: it strips the surrounding double quotes only if both are present (returning the value unchanged otherwise, which makes it a safe no-op for cookies that arrived via extract_cookies and were never quoted by this library), then reverses each backslash-escape sequence (octal \NNN and quoted-pair \X) that escape_cookie_value's COOKIE_ESCAPE_CHAR_MAP could have produced. Applied it in TestApp.cookies (webtest/app.py) so
{cookie.name: unescape_cookie_value(cookie.value) for cookie in self.cookiejar}is returned instead of the raw cookie.value.Problem
Pylons/webtest issue reference: #171
Root Cause
webtest/app.py's set_cookie() calls escape_cookie_value() (webtest/compat.py) before storing the cookie, which unconditionally wraps the value in double quotes and substitutes special characters per COOKIE_ESCAPE_CHAR_MAP. The
cookiesproperty getter ({cookie.name: cookie.value for cookie in self.cookiejar}) returned cookie.value completely raw with no corresponding unescape step. Cookies extracted from a real HTTP response (via extract_cookies) are NOT affected: the stdlib http.cookiejar parser already strips surrounding quotes from Set-Cookie header values before storing them on the Cookie object, so cookie.value there is already the plain unquoted string (confirmed by the pre-existing test_preserves_cookies/test_secure_cookies tests, which assert app.cookies['foo'] == 'bar' for values sent as 'foo=bar;baz' over HTTP and pass both before and after this change).Testing
PASS - new regression test passes (plain value 'bar' and escaping-triggering value ';bar=baz'); full suite: 203 passed, 3 deselected (pre-existing lxml-dependency failures, unrelated to this change, per task feedback).
Related Issue
#171