Skip to content

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

Raise error when space is in URL (#3198)

ngken099559a3a3f73e38
diff --git a/catalog/dags/common/urls.py b/catalog/dags/common/urls.py
index efaad85f0..b14990bb5 100644
--- a/catalog/dags/common/urls.py
+++ b/catalog/dags/common/urls.py
@@ -17,6 +17,10 @@ from requests.exceptions import RequestException
logger = logging.getLogger(__name__)
+class SpaceInUrlError(Exception):
+ pass
+
+
def validate_url_string(url_string, strip_slash: bool = True):
"""
Determine whether the given `url_string` is a valid URL with an https scheme.
@@ -33,11 +37,15 @@ def validate_url_string(url_string, strip_slash: bool = True):
strip_slash: Flag (bool) decides whether to strip slashes in URL or not,
Default value is `True`
"""
+
logger.debug(f"Validating_url {url_string}")
if not type(url_string) == str or not url_string:
return
- else:
- upgraded_url = _add_best_scheme(url_string, strip_slash)
+
+ if url_string and " " in url_string:
+ raise SpaceInUrlError(f"space is present in URL: {url_string}")
+
+ upgraded_url = _add_best_scheme(url_string, strip_slash)
parse_result = urlparse(upgraded_url)
tld = tldextract.extract(upgraded_url)
diff --git a/catalog/tests/dags/common/test_urls.py b/catalog/tests/dags/common/test_urls.py
index c64f013b4..bf5a9ade6 100644
--- a/catalog/tests/dags/common/test_urls.py
+++ b/catalog/tests/dags/common/test_urls.py
@@ -188,3 +188,9 @@ def test_add_url_scheme_leaves_nonprefix_scheme():
actual_url = urls.add_url_scheme(url_stub, scheme="https")
expect_url = "https://hreativecommons.org/?referer=https://abc.com"
assert actual_url == expect_url
+
+
+def test_validate_url_string_contain_space():
+ with pytest.raises(urls.SpaceInUrlError):
+ url_string = "https://wordpress.org/photos/photo/526283 9486/"
+ urls.validate_url_string(url_string)