Skip to content

Fix: webtest/app.py's set_cookie() calls escape_cookie_value()... - #278

Open
M001N wants to merge 1 commit into
Pylons:mainfrom
M001N:oss-engine/15109ab2-09700cd2
Open

Fix: webtest/app.py's set_cookie() calls escape_cookie_value()...#278
M001N wants to merge 1 commit into
Pylons:mainfrom
M001N:oss-engine/15109ab2-09700cd2

Conversation

@M001N

@M001N M001N commented Aug 17, 2026

Copy link
Copy Markdown

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 cookies property 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

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant