Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Set the thumbnail proxy's response headers explicitly (#5808)

Konstantin Obenlandcb1f659e49cc
diff --git a/api/api/utils/image_proxy/__init__.py b/api/api/utils/image_proxy/__init__.py
index d2b32075c..1f1516100 100644
--- a/api/api/utils/image_proxy/__init__.py
+++ b/api/api/utils/image_proxy/__init__.py
@@ -1,3 +1,4 @@
+import re
from functools import wraps
from typing import Literal
from urllib.parse import urlparse
@@ -33,6 +34,17 @@ HEADERS = {
PHOTON_TYPES = {"gif", "jpg", "jpeg", "png", "webp"}
ORIGINAL_TYPES = {"svg"}
+# Upstream bytes and headers come from third parties, so the proxy picks its own
+# response headers: the body is always declared an image and rendered passively.
+IMAGE_MEDIA_TYPE = re.compile(r"image/[a-z0-9][a-z0-9.+-]*")
+RESPONSE_HEADERS = {
+ "Content-Security-Policy": (
+ "default-src 'none'; font-src data:; img-src data:; "
+ "style-src 'unsafe-inline'; sandbox"
+ ),
+ "X-Content-Type-Options": "nosniff",
+}
+
PHOTON = "photon"
ORIGINAL = "original"
THUMBNAIL_STRATEGY = Literal["photon_proxy", "original"]
@@ -88,6 +100,18 @@ def get_request_params_for_extension(
)
+def _get_image_media_type(upstream_response: aiohttp.ClientResponse) -> str | None:
+ """
+ Return the upstream response's media type, without parameters.
+
+ Return ``None`` unless it is an ``image/*`` type.
+ """
+
+ content_type = upstream_response.headers.get("Content-Type", "")
+ media_type = content_type.split(";")[0].strip().lower()
+ return media_type if IMAGE_MEDIA_TYPE.fullmatch(media_type) else None
+
+
@sync_to_async
def _tally_response(
tallies_conn,
@@ -267,7 +291,15 @@ async def get(
upstream_response.raise_for_status()
status_code = upstream_response.status
- content_type = upstream_response.headers.get("Content-Type")
+ content_type = _get_image_media_type(upstream_response)
+ if content_type is None:
+ logger.warning(
+ "thumbnail_upstream_not_image",
+ url=upstream_url,
+ provider=media_info.media_provider,
+ content_type=upstream_response.headers.get("Content-Type"),
+ )
+ raise UpstreamThumbnailException("Upstream response is not an image.")
content = await upstream_response.content.read()
@@ -275,6 +307,7 @@ async def get(
content,
status=status_code,
content_type=content_type,
+ headers=RESPONSE_HEADERS,
)
except Exception as exc:
exception_name = f"{exc.__class__.__module__}.{exc.__class__.__name__}"
diff --git a/api/test/unit/utils/test_image_proxy.py b/api/test/unit/utils/test_image_proxy.py
index 04c26a040..45efedb56 100644
--- a/api/test/unit/utils/test_image_proxy.py
+++ b/api/test/unit/utils/test_image_proxy.py
@@ -98,6 +98,7 @@ def test_get_successful_no_auth_key_default_args(mock_image_data):
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -114,6 +115,7 @@ def test_get_successful_original_svg_no_auth_key_default_args(mock_image_data):
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/svg+xml")
.body(SVG_BODY)
)
@@ -127,6 +129,98 @@ def test_get_successful_original_svg_no_auth_key_default_args(mock_image_data):
assert res.status_code == 200
+TEST_SVG_MEDIA_INFO = replace(
+ TEST_MEDIA_INFO, image_url=TEST_IMAGE_URL.replace(".jpg", ".svg")
+)
+
+upstream_strategy_params = pytest.mark.parametrize(
+ "media_info, upstream_url",
+ [
+ pytest.param(TEST_MEDIA_INFO, PHOTON_URL_FOR_TEST_IMAGE, id="photon"),
+ pytest.param(TEST_SVG_MEDIA_INFO, TEST_SVG_MEDIA_INFO.image_url, id="original"),
+ ],
+)
+
+
+@upstream_strategy_params
+ "upstream_content_type, expected_content_type",
+ [
+ ("image/jpeg", "image/jpeg"),
+ ("image/svg+xml", "image/svg+xml"),
+ ("Image/SVG+XML; charset=utf-8", "image/svg+xml"),
+ ],
+)
+def test_get_declares_bare_image_media_type(
+ media_info, upstream_url, upstream_content_type, expected_content_type
+):
+ (
+ pook.get(upstream_url)
+ .reply(200)
+ .header("Content-Type", upstream_content_type)
+ .body(MOCK_BODY)
+ )
+
+ res = photon_get(media_info)
+
+ assert res.headers["Content-Type"] == expected_content_type
+
+
+@upstream_strategy_params
+ "upstream_content_type",
+ [
+ None,
+ "image/",
+ "text/html",
+ "text/xml; charset=utf-8",
+ "application/xhtml+xml",
+ "application/octet-stream",
+ ],
+)
+def test_get_requires_image_media_type_from_upstream(
+ media_info, upstream_url, upstream_content_type
+):
+ response = pook.get(upstream_url).reply(200).body(MOCK_BODY)
+ if upstream_content_type is not None:
+ response.header("Content-Type", upstream_content_type)
+
+ with (
+ capture_logs() as cap_logs,
+ pytest.raises(UpstreamThumbnailException, match="not an image"),
+ ):
+ photon_get(media_info)
+
+ assert {
+ "event": "thumbnail_upstream_not_image",
+ "log_level": "warning",
+ "url": upstream_url,
+ "provider": media_info.media_provider,
+ "content_type": upstream_content_type,
+ } in cap_logs
+
+
+@upstream_strategy_params
+def test_get_restricts_response_to_passive_rendering(media_info, upstream_url):
+ (
+ pook.get(upstream_url)
+ .reply(200)
+ .header("Content-Type", "image/svg+xml")
+ .body(SVG_BODY)
+ )
+
+ res = photon_get(media_info)
+
+ assert res.headers["Content-Security-Policy"] == (
+ "default-src 'none'; font-src data:; img-src data:; "
+ "style-src 'unsafe-inline'; sandbox"
+ )
+ assert res.headers["X-Content-Type-Options"] == "nosniff"
+
+
@pytest.mark.pook
def test_get_successful_with_auth_key_default_args(mock_image_data, auth_key):
(
@@ -141,6 +235,7 @@ def test_get_successful_with_auth_key_default_args(mock_image_data, auth_key):
.header("Accept", "image/*")
.header("X-Photon-Authentication", auth_key)
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -162,6 +257,7 @@ def test_get_successful_no_auth_key_not_compressed(mock_image_data):
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -183,6 +279,7 @@ def test_get_successful_no_auth_key_full_size(mock_image_data):
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -199,6 +296,7 @@ def test_get_successful_no_auth_key_full_size_not_compressed(mock_image_data):
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -224,6 +322,7 @@ def test_get_successful_no_auth_key_png_only(mock_image_data):
.header("User-Agent", UA_HEADER)
.header("Accept", "image/png")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -248,6 +347,7 @@ def test_get_successful_forward_query_params(mock_image_data):
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -292,6 +392,7 @@ def test_get_successful_records_response_code(
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -487,7 +588,12 @@ def test_caches_failures_with_successes_extending_tolerance(
photon_get(TEST_MEDIA_INFO)
def _make_successful_request():
- (pook.get(PHOTON_URL_FOR_TEST_IMAGE).reply(200).body(MOCK_BODY))
+ (
+ pook.get(PHOTON_URL_FOR_TEST_IMAGE)
+ .reply(200)
+ .header("Content-Type", "image/jpeg")
+ .body(MOCK_BODY)
+ )
res = photon_get(TEST_MEDIA_INFO)
assert res.content == MOCK_BODY.encode()
@@ -530,6 +636,7 @@ def test_get_successful_https_image_url_sends_ssl_parameter(mock_image_data):
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -674,6 +781,7 @@ def test_wikimedia_thumbnail_default_params_small_image():
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -709,6 +817,7 @@ def test_wikimedia_thumbnail_default_params_large_image():
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -743,6 +852,7 @@ def test_wikimedia_thumbnail_default_params_unknown_width():
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -776,6 +886,7 @@ def test_wikimedia_thumbnail_full_size_params():
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -815,6 +926,7 @@ def test_wikimedia_thumbnail_uncompressed_params():
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -849,6 +961,7 @@ def test_wikimedia_thumbnail_uncompressed_full_size_params():
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
@@ -885,6 +998,7 @@ def test_wikimedia_thumbnail_unknown_url_format():
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
+ .header("Content-Type", "image/jpeg")
.body(MOCK_BODY)
)
diff --git a/api/test/unit/views/test_image_views.py b/api/test/unit/views/test_image_views.py
index b3f63e7dc..bbe04c68f 100644
--- a/api/test/unit/views/test_image_views.py
+++ b/api/test/unit/views/test_image_views.py
@@ -53,6 +53,7 @@ def test_thumbnail_uses_upstream_thumb_for_smk(
pook.get(settings.PHOTON_ENDPOINT[:-1])
.path(expected_thumb_url.replace("http://", "/"))
.response(200)
+ .header("Content-Type", "image/jpeg")
).mock
response = api_client.get(f"/v1/images/{image.identifier}/thumb/")