Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Fix cookie lookup for dotless hosts and IP addresses (#7900)

Adrian63d5ce6272a7
diff --git a/scrapy/http/cookies.py b/scrapy/http/cookies.py
index 8edeae01c..555d930e6 100644
--- a/scrapy/http/cookies.py
+++ b/scrapy/http/cookies.py
@@ -54,9 +54,9 @@ class CookieJar:
if not IPV4_RE.search(req_host):
hosts = potential_domain_matches(req_host)
if "." not in req_host:
- hosts.append(req_host + ".local")
+ hosts += potential_domain_matches(req_host + ".local")
else:
- hosts = [req_host]
+ hosts = [req_host, "." + req_host]
cookies = []
for host in hosts:
diff --git a/tests/test_downloadermiddleware_cookies.py b/tests/test_downloadermiddleware_cookies.py
index 8d999d952..e4b66fe10 100644
--- a/tests/test_downloadermiddleware_cookies.py
+++ b/tests/test_downloadermiddleware_cookies.py
@@ -345,6 +345,30 @@ class TestCookiesMiddleware:
assert "Cookie" in request.headers
assert request.headers["Cookie"] == b"currencyCookie=USD"
+ @pytest.mark.parametrize(
+ ("url", "domain"),
+ [
+ ("http://example-host/", "example-host.local"),
+ ("http://127.0.0.1/", "127.0.0.1"),
+ pytest.param(
+ "http://example-host/",
+ "example-host",
+ marks=pytest.mark.xfail(
+ reason=(
+ "http.cookiejar accepts a dotless domain for a dotless "
+ "host but never returns the resulting cookie"
+ )
+ ),
+ ),
+ ],
+ )
+ def test_explicit_local_domain(self, url: str, domain: str) -> None:
+ request = Request(
+ url, cookies=[{"name": "currencyCookie", "value": "USD", "domain": domain}]
+ )
+ assert self.mw.process_request(request) is None
+ assert request.headers.get("Cookie") == b"currencyCookie=USD"
+
@pytest.mark.xfail(reason="Cookie header is not currently being processed")
def test_keep_cookie_from_default_request_headers_middleware(self):
DEFAULT_REQUEST_HEADERS = {"Cookie": "default=value; asdf=qwerty"}