Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CookieJar - return 'best-match' and not LIFO #7577

Merged
merged 7 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/7577.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix filter_cookies return 'best-mach' (instead of lifo) cookie -- by :user:`marq24`.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Martin Richard
Mathias Fröjdman
Mathieu Dugré
Matt VanEseltine
Matthias Marquardt
Matthieu Hauglustaine
Matthieu Rigal
Matvey Tingaev
Expand Down
21 changes: 17 additions & 4 deletions aiohttp/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,23 @@ def filter_cookies(
if is_not_secure and cookie["secure"]:
continue

# It's critical we use the Morsel so the coded_value
# (based on cookie version) is preserved
mrsl_val = cast("Morsel[str]", cookie.get(cookie.key, Morsel()))
mrsl_val.set(cookie.key, cookie.value, cookie.coded_value)
# we need to check, if the found cookie is a better match then the
# previously found
if name in filtered and len(cookie["path"]) < len(filtered[name]["path"]):
continue

filtered[name] = cookie

# It's critical we use the Morsel so the coded_value
# (based on cookie version) is preserved
for name in filtered:
filtered_cookie = filtered[name]
mrsl_val = cast(
"Morsel[str]", filtered_cookie.get(filtered_cookie.key, Morsel())
)
mrsl_val.set(
filtered_cookie.key, filtered_cookie.value, filtered_cookie.coded_value
)
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
filtered[name] = mrsl_val

return filtered
Expand Down
30 changes: 30 additions & 0 deletions tests/test_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,36 @@ async def make_jar():
self.assertEqual(len(jar_filtered), 1)
self.assertEqual(jar_filtered["path-cookie"].value, "one")

def test_path_filter_diff_folder_same_name_return_best_match_independent_from_put_order(
self,
) -> None:
async def make_jar():
return CookieJar(unsafe=True)

jar = self.loop.run_until_complete(make_jar())
jar.update_cookies(
SimpleCookie("path-cookie=one; Domain=pathtest.com; Path=/one; ")
)
jar.update_cookies(
SimpleCookie("path-cookie=zero; Domain=pathtest.com; Path=/; ")
)
jar.update_cookies(
SimpleCookie("path-cookie=two; Domain=pathtest.com; Path=/second; ")
)
self.assertEqual(len(jar), 3)

jar_filtered = jar.filter_cookies(URL("http://pathtest.com/"))
self.assertEqual(len(jar_filtered), 1)
self.assertEqual(jar_filtered["path-cookie"].value, "zero")

jar_filtered = jar.filter_cookies(URL("http://pathtest.com/second"))
self.assertEqual(len(jar_filtered), 1)
self.assertEqual(jar_filtered["path-cookie"].value, "two")

jar_filtered = jar.filter_cookies(URL("http://pathtest.com/one"))
self.assertEqual(len(jar_filtered), 1)
self.assertEqual(jar_filtered["path-cookie"].value, "one")


async def test_dummy_cookie_jar() -> None:
cookie = SimpleCookie("foo=bar; Domain=example.com;")
Expand Down