Scrapy, a fast high-level web crawling & scraping framework for Python.
Files and images pipelines send S3 requests over plaintext HTTP by default
Adrian Chaves5204544619b5
diff --git a/docs/news.rst b/docs/news.rstindex 26be66d5c..0a7d29e37 100644--- a/docs/news.rst+++ b/docs/news.rst@@ -85,6 +85,29 @@ Security bug fixes.. _x3px-j3qh-fp5w: https://github.com/scrapy/scrapy/security/advisories/GHSA-x3px-j3qh-fp5w+- :class:`~scrapy.pipelines.files.S3FilesStore` now uses HTTPS with the+ default Scrapy settings.++ In previous Scrapy versions, the default value of :setting:`AWS_USE_SSL`,+ ``None``, was incorrectly interpreted to mean "disable HTTPS", so+ :class:`~scrapy.pipelines.files.S3FilesStore` used plain HTTP when talking+ to S3 unless that setting was explicitly set to ``True`` or+ :setting:`AWS_ENDPOINT_URL` was set to a custom HTTPS URL.++ An attacker who could observe that traffic could read the contents, S3+ paths and metadata of the files uploaded by+ :class:`~scrapy.pipelines.files.FilesPipeline` and+ :class:`~scrapy.pipelines.images.ImagesPipeline`, the AWS access key ID+ (but not the secret access key) and, with temporary credentials, the+ session token (``X-Amz-Security-Token``). An attacker who could tamper with+ that traffic could make Scrapy falsely think that a file is up to date or+ was successfully stored. See the `fwm7-29m4-rrj9`_ security advisory for+ details.++ To restore the previous behavior, set :setting:`AWS_USE_SSL` to ``False``.++ .. _fwm7-29m4-rrj9: https://github.com/scrapy/scrapy/security/advisories/GHSA-fwm7-29m4-rrj9+Modified requirements~~~~~~~~~~~~~~~~~~~~~diff --git a/scrapy/pipelines/files.py b/scrapy/pipelines/files.pyindex 7c77b2471..8f4a76432 100644--- a/scrapy/pipelines/files.py+++ b/scrapy/pipelines/files.py@@ -231,7 +231,7 @@ class S3FilesStore:"aws_session_token": self.AWS_SESSION_TOKEN,"endpoint_url": self.AWS_ENDPOINT_URL,"region_name": self.AWS_REGION_NAME,- "use_ssl": self.AWS_USE_SSL,+ "use_ssl": True if self.AWS_USE_SSL is None else self.AWS_USE_SSL,"verify": self.AWS_VERIFY,"config": Config(**config_kwargs),}diff --git a/tests/test_pipeline_files.py b/tests/test_pipeline_files.pyindex 9d06b1e56..1d6e1188f 100644--- a/tests/test_pipeline_files.py+++ b/tests/test_pipeline_files.py@@ -20,6 +20,7 @@ from tempfile import mkdtempfrom typing import Anyfrom unittest import mockfrom unittest.mock import MagicMock+from urllib.parse import urlparseimport attrimport pytest@@ -1396,6 +1397,23 @@ class TestS3FilesStore:config: Any = store.s3_client.meta.configassert config.max_pool_connections == expected+ @pytest.mark.parametrize(+ ("settings", "expected"),+ [+ ({}, "https"),+ ({"AWS_USE_SSL": None}, "https"),+ ({"AWS_USE_SSL": True}, "https"),+ ({"AWS_USE_SSL": False}, "http"),+ ],+ )+ def test_use_ssl(self, settings: dict[str, Any], expected: str) -> None:+ crawler = get_crawler(+ settings_dict={"FILES_STORE": "s3://mybucket/prefix/", **settings}+ )+ store = build_from_crawler(FilesPipeline, crawler).store+ assert isinstance(store, S3FilesStore)+ assert urlparse(store.s3_client.meta.endpoint_url).scheme == expected+@pytest.mark.requires_aiobotocore@pytest.mark.only_asyncio@@ -1452,6 +1470,22 @@ class TestS3FilesStoreAsync:assert client.meta.config.max_pool_connections == 30await store.close()+ @pytest.mark.parametrize(+ ("config", "expected"),+ [+ ({}, "https"),+ ({"AWS_USE_SSL": None}, "https"),+ ({"AWS_USE_SSL": True}, "https"),+ ({"AWS_USE_SSL": False}, "http"),+ ],+ )+ @coroutine_test+ async def test_use_ssl(self, config: dict[str, Any], expected: str) -> None:+ store = S3FilesStore("s3://mybucket/prefix/", **config)+ client = await store._get_aio_client()+ assert urlparse(client.meta.endpoint_url).scheme == expected+ await store.close()+@coroutine_testasync def test_stat(self):bucket = "mybucket"