Skip to content

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

Improve license URL validation (#1028)

Olga Bulat4c0ceb0e58b1
diff --git a/openverse_catalog/dags/common/licenses/licenses.py b/openverse_catalog/dags/common/licenses/licenses.py
index cfe34c571..6edda387c 100644
--- a/openverse_catalog/dags/common/licenses/licenses.py
+++ b/openverse_catalog/dags/common/licenses/licenses.py
@@ -15,6 +15,8 @@ logger = logging.getLogger(__name__)
LICENSE_PATH_MAP = constants.get_license_path_map()
REVERSE_LICENSE_PATH_MAP = constants.get_reverse_license_path_map()
+CC_BASE_URL = "https://creativecommons.org/"
+LICENSE_URLS = {f"{CC_BASE_URL}{l}/" for l in LICENSE_PATH_MAP.keys()} # noqa: E741
class InvalidLicenseURLException(Exception):
@@ -143,14 +145,17 @@ def _get_valid_cc_url(license_url) -> str | None:
This function enforces:
- string type
- https scheme
- - parses into a urllib.parse.ParseResult with
- netloc=creativecommons.org
+ - trailing slash
+
+ If the resulting URL is in the `LICENSE_URLS` set, we return it.
+ Otherwise, we parse URL into a urllib.parse.ParseResult, ensuring
+ that its netloc=creativecommons.org
After that, we rewrite the URL to whatever we get redirected to when
we make a request using it.
If all of these validations and the rewriting succeed, we return the
- rewritten URL. Otherwise, we return None
+ rewritten URL. Otherwise, we return None.
"""
logger.debug(f"Checking license URL {license_url}")
if type(license_url) != str:
@@ -158,6 +163,11 @@ def _get_valid_cc_url(license_url) -> str | None:
return
https_url = urls.add_url_scheme(license_url.lower(), "https")
+ if not https_url.endswith("/"):
+ https_url += "/"
+ if https_url in LICENSE_URLS:
+ return https_url
+
parsed_url = urlparse(https_url)
if parsed_url.netloc != "creativecommons.org":
diff --git a/tests/dags/common/licenses/test_licenses.py b/tests/dags/common/licenses/test_licenses.py
index da3e7b331..7af605517 100644
--- a/tests/dags/common/licenses/test_licenses.py
+++ b/tests/dags/common/licenses/test_licenses.py
@@ -1,4 +1,5 @@
import logging
+from unittest.mock import patch
import common.urls
import pytest
@@ -150,13 +151,15 @@ def test_get_license_info_from_url_with_license_url_path_mismatch(
assert all([i is None for i in license_info])
-def test_get_license_info_from_url_with_good_license_url(mock_cc_url_validator):
+def test_get_license_info_from_url_with_good_license_url():
expected_license, expected_version = "cc0", "1.0"
license_url = "https://creativecommons.org/publicdomain/zero/1.0/"
path_map = {"publicdomain/zero/1.0": ("cc0", "1.0")}
- actual_license_info = licenses._get_license_info_from_url(
- license_url, path_map=path_map
- )
+
+ with patch.object(licenses.urls, "rewrite_redirected_url") as mock_rewriter:
+ actual_license_info = licenses._get_license_info_from_url(
+ license_url, path_map=path_map
+ )
expected_license_info = (
expected_license,
expected_version,
@@ -164,6 +167,17 @@ def test_get_license_info_from_url_with_good_license_url(mock_cc_url_validator):
license_url,
)
assert actual_license_info == expected_license_info
+ assert mock_rewriter.call_count == 0
+
+
+def test_get_valid_cc_url_adds_a_trailing_slash():
+ license_url = "https://creativecommons.org/licenses/by-nc-nd/2.0"
+
+ with patch.object(licenses.urls, "rewrite_redirected_url") as mock_rewriter:
+ actual_license_url = licenses._get_valid_cc_url(license_url)
+
+ assert mock_rewriter.call_count == 0
+ assert actual_license_url == f"{license_url}/"
def test_get_license_info_from_license_pair_nones_when_missing_license(mock_rewriter):
diff --git a/tests/dags/providers/provider_api_scripts/test_jamendo.py b/tests/dags/providers/provider_api_scripts/test_jamendo.py
index 14eb69510..c2686b322 100644
--- a/tests/dags/providers/provider_api_scripts/test_jamendo.py
+++ b/tests/dags/providers/provider_api_scripts/test_jamendo.py
@@ -80,7 +80,7 @@ def test_get_record_data():
"license_info": LicenseInfo(
license="by-nc",
version="2.0",
- url="https://creativecommons.org/licenses/by-nc/2.0",
+ url="https://creativecommons.org/licenses/by-nc/2.0/",
raw_url="http://creativecommons.org/licenses/by-nc/2.0/",
),
"meta_data": {