From d0e2e5ebba6b3648f54818d1f8de718abf715824 Mon Sep 17 00:00:00 2001 From: sarayourfriend <24264157+sarayourfriend@users.noreply.github.com> Date: Thu, 12 Jan 2023 14:21:56 +1100 Subject: [PATCH] Forward SSL status of upstream image URL to Photon (#1080) --- api/catalog/api/utils/photon.py | 6 ++++++ api/test/unit/utils/photon_test.py | 28 +++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/api/catalog/api/utils/photon.py b/api/catalog/api/utils/photon.py index 2a1056174..82d437251 100644 --- a/api/catalog/api/utils/photon.py +++ b/api/catalog/api/utils/photon.py @@ -52,6 +52,12 @@ def get( # request URL. params["q"] = parsed_image_url.query + if parsed_image_url.scheme == "https": + # Photon defaults to HTTP without this parameter + # which will cause some providers to fail (if they + # do not serve over HTTP and do not have a redirect) + params["ssl"] = "true" + # Photon excludes the protocol so we need to reconstruct the url + port + path # to send as the "path" of the Photon request domain = parsed_image_url.netloc diff --git a/api/test/unit/utils/photon_test.py b/api/test/unit/utils/photon_test.py index 0531646fe..5ca0582fd 100644 --- a/api/test/unit/utils/photon_test.py +++ b/api/test/unit/utils/photon_test.py @@ -11,7 +11,7 @@ PHOTON_URL_FOR_TEST_IMAGE = f"{settings.PHOTON_ENDPOINT}subdomain.example.com/path_part1/part2/image_dot_jpg.jpg" -TEST_IMAGE_URL = PHOTON_URL_FOR_TEST_IMAGE.replace(settings.PHOTON_ENDPOINT, "https://") +TEST_IMAGE_URL = PHOTON_URL_FOR_TEST_IMAGE.replace(settings.PHOTON_ENDPOINT, "http://") UA_HEADER = HEADERS["User-Agent"] @@ -261,3 +261,29 @@ def test_get_generic_exception(capture_exception, setup_requests_get_exception): photon_get(TEST_IMAGE_URL) capture_exception.assert_called_once_with(exc) + + +@pook.on +def test_get_successful_https_image_url_sends_ssl_parameter(mock_image_data): + https_url = TEST_IMAGE_URL.replace("http://", "https://") + mock_get: pook.Mock = ( + pook.get(PHOTON_URL_FOR_TEST_IMAGE) + .params( + { + "w": settings.THUMBNAIL_WIDTH_PX, + "quality": settings.THUMBNAIL_QUALITY, + "ssl": "true", + } + ) + .header("User-Agent", UA_HEADER) + .header("Accept", "image/*") + .reply(200) + .body(MOCK_BODY) + .mock + ) + + res = photon_get(https_url) + + assert res.content == MOCK_BODY.encode() + assert res.status_code == 200 + assert mock_get.matched