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

Refactor cookie handling in CookiesMiddleware #5946

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion scrapy/http/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@
IPV4_RE = re.compile(r"\.\d+$", re.ASCII)


class CustomDefaultCookiePolicy(DefaultCookiePolicy):
def set_ok(self, cookie, request):
# always set cookies
return True


class CookieJar:
def __init__(self, policy=None, check_expired_frequency=10000):
self.policy = policy or DefaultCookiePolicy()
self.policy = policy or CustomDefaultCookiePolicy()
self.jar = _CookieJar(self.policy)
self.jar._cookies_lock = _DummyLock()
self.check_expired_frequency = check_expired_frequency
Expand Down
27 changes: 27 additions & 0 deletions tests/test_downloadermiddleware_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,3 +732,30 @@ def test_server_set_cookie_domain_public_period(self):
"co.uk",
cookies=True,
)

def test_off_domain_jar_storage(self):
request1 = Request(
"https://a.example",
cookies=[
{
"name": "foo",
"value": "bar",
"domain": "b.example",
},
],
)
assert self.mw.process_request(request1, self.spider) is None
self.assertNotIn(b"Cookie", request1.headers)

request2 = Request("https://b.example/")
assert self.mw.process_request(request2, self.spider) is None
self.assertEqual(request2.headers.get(b"Cookie"), b"foo=bar")

def test_process_response_unrelated_domain(self):
request = Request("http://a.example")
request.headers["Cookie"] = "foo=bar; domain=b.example"
response = Response("http://c.example")
response.headers["Set-Cookie"] = "asd=fgh; domain=d.example"
self.mw.process_response(request, response, spider=None)
jar = self.mw.jars[None]
assert not jar._cookies.get("c.example", {}).get("/", {}).get("asd")
Copy link
Member

@Gallaecio Gallaecio Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with the new approach is, I think, that it also applies to server-side cookies.

I think this test is not right, and fixing it as follows:

Suggested change
response.headers["Set-Cookie"] = "asd=fgh; domain=d.example"
self.mw.process_response(request, response, spider=None)
jar = self.mw.jars[None]
assert not jar._cookies.get("c.example", {}).get("/", {}).get("asd")
response.headers["Set-Cookie"] = "asd=fgh; domain=c.example"
self.mw.process_response(request, response, spider=None)
jar = self.mw.jars[None]
assert not jar._cookies.get("c.example", {}).get("/", {}).get("asd") # I wonder if we can just assert that c.example is not in the jar to simplify the test.

Will probably make it fail with the new approach.

It would not be very clean, but I wonder if it would be possible to change the policy right before setting user-defined cookies (and only user-defined cookies, not Set-Cookie headers from servers), and restore it right after.

If not that way, we need to think of something else.