-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Client: ClientSession will not send secure cookie to localhost on unsecure connection. #5571
Comments
We are running into the same issue during testing. Another solution would be to allow secure cookies over an insecure connection when the |
@DanielDewberry could you send a pull request with those failing tests? Let's incorporate them first and then talk about the possible fix. @Marco-Kaulea this may be a good solution, I need to think about it. We could talk about this in the PR with tests that would demonstrate how such interfaces could be used. I expect that we can follow TDD in this case and merge in failing (but agreed on) tests marked as xfail. And then, somebody could implement what those tests expect. |
@webknjaz Would you like the PR to a new or existing branch? |
Just a standalone PR with failing tests against master would be fine. We can then mark the tests as xfail per https://pganssle-talks.github.io/xfail-lightning/ and merge them (that would be followed by a backport PR to the 3.8 branch but you needn't worry about this right now). Once the tests are in, it'll be clearly documented what behavior is expected going forward as code. The next step would be to produce a separate PR making those tests pass by effectively changing the behavior. Of course, these changes could be a single PR but it's usually easier to agree upon and merge smaller chunks rather than a big set of changes because in big PRs there's usually some subset of patches that could be merged in right away but they would still be stuck in that PR until all other things are fixed which causes longer review/reiteration times. |
@webknjaz @Marco-Kaulea I have submitted a PR as requested. It is now:
Here is the revised test: #! /usr/bin/env python3
# -*- coding: utf-8 -*-
from http.cookies import SimpleCookie
from aiohttp.cookiejar import CookieJar
import pytest
from yarl import URL
async def test_secure_cookie_not_filtered_from_unsafe_cookiejar_when_given_unsecured_endpoint() -> None:
"""Secure SimpleCookie should not be filtered from unsafe CookieJar when given an unsecured endpoint.
There are times when sending a secure cookie to an unsecured endpoint is desireable. Such an
occasion is during testing. RFC 6265 section-4.1.2.5 states that this behaviour is a decision
based on the trust of a network by the user agent.
"""
endpoint = 'http://127.0.0.1/'
secure_cookie = SimpleCookie(
"cookie-key=cookie-value; HttpOnly; Path=/; Secure",
)
jar = CookieJar(unsafe=True)
# Confirm the jar is empty
assert len(jar) == 0
jar.update_cookies(
secure_cookie,
URL(endpoint),
)
# Confirm the jar contains the cookie
assert len(jar) == 1
filtered_cookies = jar.filter_cookies(request_url=endpoint)
# Confirm the filtered results contain the cookie
assert len(filtered_cookies) == 1 |
Based on that, it looks like you want some way to indicate you are on a safe/secure network, and when that value is set, the secure cookie would not get filtered out. |
Or rather, a way to indicate the destination is within the same secure network. |
@Dreamsorcerer The decision to send a secure cookie to the connected host should be made by the user agent, independent of which network they are on. The class CookieJar(AbstractCookieJar):
def __init__(
self, *,
unsafe: bool = False,
quote_cookie: bool = True
include_secure_cookie_over_unsecured_connection: bool = False, # new kwarg
) -> None:
self._include_secure_cookie_over_unsecured_connection = include_secure_cookie_over_unsecured_connection Then def filter_cookies(
self, request_url: URL = URL()
) -> Union["BaseCookie[str]", "SimpleCookie[str]"]:
# lines leading up to 253
if is_not_secure and \
cookie["secure"] and \
not self._include_secure_cookie_over_unsecured_connection: (I like verbose names but appreciate that's a long one). One suggestion was to add a |
IMO it would be more flexible to use a predicate instead of boolean flag. |
A ClientSession with an unsafe cookiejar will not send a secure cookie to a localhost endpoint, when that connection is over http (i,e. unsecure).
According to Mozilla documentation:
Note the (except on localhost) clause.
💡 To Reproduce
The following example sets up a test (
TestIntegration::test_cookie_is_sent_server
) which fails. Supplementary tests are included to demonstrate the setup operates as expected. Scroll to the end of the codeblock to see that particular test.Requirements:
💡 Expected behavior
Secure cookies should be sent to localhost/ 127.0.0.1 on unsecure connection (http)
📋 Your version of the Python
python 3.8.5
📋 Your version of the aiohttp/yarl/multidict distributions
📋 Additional context
This is a client issue.
Proposed Solution:
change this line of
aiohttp/cookiejar.py
from
to
or provide a way to influence the
is_not_secure
variable in order to proceed to sending the cookie, whether the connection is secure or not. The latter would allow for the ipv4 and ipv6 loopbacks to be affected without exceptional cases (as was the case in the former proposal), and would also provide a way for hosts in/etc/hosts
to be included in the unsecured requests.Further rationale:
This interpretation shows the RFC section that states:
I, the user agent, consider the sending and receipt of a request on a single machine, not across a network, to be secure. Particularly when performing unit testing.
The text was updated successfully, but these errors were encountered: