Scrapy, a fast high-level web crawling & scraping framework for Python.
Per-request http_user and http_pass credentials leaked on cross-domain redirects
Adrian Chaves08f41768f5f1
diff --git a/docs/news.rst b/docs/news.rstindex 4e6358695..f82c6b80c 100644--- a/docs/news.rst+++ b/docs/news.rst@@ -55,6 +55,23 @@ Security bug fixes.. _j72p-v7rm-cwm3: https://github.com/scrapy/scrapy/security/advisories/GHSA-j72p-v7rm-cwm3+- When :reqmeta:`http_auth_domain` is not set, the credentials from+ :reqmeta:`http_user` and :reqmeta:`http_pass` are now only sent to the+ origin (scheme, host and port) of the request where they were first used.++ In previous Scrapy versions, they were sent to any URL, so after a+ cross-origin redirect,+ :class:`~scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware` added+ them again to the ``Authorization`` header that+ :class:`~scrapy.downloadermiddlewares.redirect.RedirectMiddleware` had+ removed, sending them to the redirect target. A website could redirect+ Scrapy to a host under its control to steal those credentials. See the+ `466x-p45j-m4fx`_ security advisory for details.++ .. _466x-p45j-m4fx: https://github.com/scrapy/scrapy/security/advisories/GHSA-466x-p45j-m4fx++ (:gh:`8329`)+Modified requirements~~~~~~~~~~~~~~~~~~~~~diff --git a/scrapy/downloadermiddlewares/httpauth.py b/scrapy/downloadermiddlewares/httpauth.pyindex a48fcbfef..0dbe72f02 100644--- a/scrapy/downloadermiddlewares/httpauth.py+++ b/scrapy/downloadermiddlewares/httpauth.py@@ -94,10 +94,7 @@ class HttpAuthMiddleware:def process_request(self, request: Request, spider: Spider | None = None) -> Request | Response | None:- if (- b"Authorization" in request.headers- or urlparse_cached(request).scheme not in _DEFAULT_PORTS- ):+ if urlparse_cached(request).scheme not in _DEFAULT_PORTS:return None# Per-request meta overridesusr = request.meta.get("http_user", "")@@ -110,11 +107,13 @@ class HttpAuthMiddleware:else _setdefault_auth_origin(request) == _origin(request))if allowed:- request.headers[b"Authorization"] = basic_auth_header(usr, pwd)+ request.headers.setdefault(+ b"Authorization", basic_auth_header(usr, pwd)+ )return None# Middleware-level authif self._auth and (not self._domain or url_is_from_any_domain(request, [self._domain])):- request.headers[b"Authorization"] = self._auth+ request.headers.setdefault(b"Authorization", self._auth)return Nonediff --git a/tests/test_downloadermiddleware_httpauth.py b/tests/test_downloadermiddleware_httpauth.pyindex 8719f2c87..af2fed9cd 100644--- a/tests/test_downloadermiddleware_httpauth.py+++ b/tests/test_downloadermiddleware_httpauth.py@@ -277,13 +277,14 @@ def test_main(config: dict[str, Any], expected: bytes | type[Exception] | None)),],)[email protected]("headers", [{}, {"Authorization": "a"}])def test_origin_setdefault(- meta: dict[str, Any], url: str, output_value: str | None+ meta: dict[str, Any], url: str, output_value: str | None, headers: dict[str, str]) -> None:"""When request.meta is used for authorization, an _auth_origin meta key isdefined on the request if not defined already."""mw = build_from_crawler(HttpAuthMiddleware, get_crawler())- request = Request(url, meta=meta)+ request = Request(url, headers=headers, meta=meta)assert mw.process_request(request) is Noneif output_value is None:assert "_auth_origin" not in request.meta