Skip to content

Commit 27550e3

Browse files
committed
feat: Validate URLs
urlparse from Python stdlib doesn’t fail on an invalid URL. parse_url from urllib3 used by requests does though. Invalid base URL or proxy URL raises thus an uncaught exception.
1 parent 38921a9 commit 27550e3

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

insights/client/connection.py

+33-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import print_function
55
from __future__ import absolute_import
66
import requests
7+
import urllib3
78
import socket
89
import os
910
import six
@@ -479,9 +480,15 @@ def _test_auth_config(self):
479480

480481
return not errors
481482

482-
def _dump_urls(self):
483-
base_parsed = urlparse(self.base_url)
484-
if base_parsed.hostname.endswith("stage.redhat.com"):
483+
def _test_url_config(self):
484+
try:
485+
base_parsed = urllib3.util.url.parse_url(self.base_url)
486+
except urllib3.exceptions.LocationParseError:
487+
base_parsed = None
488+
489+
if not base_parsed:
490+
hostname_desc = "invalid URL"
491+
elif base_parsed.hostname.endswith("stage.redhat.com"):
485492
hostname_desc = "Red Hat Insights (staging)"
486493
elif base_parsed.hostname.endswith("redhat.com"):
487494
if self.config.verbose:
@@ -499,14 +506,32 @@ def _dump_urls(self):
499506

500507
logger.info(" Ping URL: %s", self.ping_url)
501508

509+
invalid_proxies = {}
502510
if self.proxies:
503511
for proxy_type, proxy_url in self.proxies.items():
504-
logger.info(" %s proxy: %s", proxy_type.upper(), proxy_url)
512+
try:
513+
urllib3.util.url.parse_url(proxy_url)
514+
except urllib3.exceptions.LocationParseError:
515+
invalid_proxies[proxy_type] = proxy_url
516+
proxy_desc = " (invalid)" if proxy_type in invalid_proxies else ""
517+
logger.info(" %s proxy: %s%s", proxy_type.upper(), proxy_url, proxy_desc)
505518
else:
506519
logger.info(" Proxy: not set")
507520

508521
logger.info("")
509522

523+
if not base_parsed or invalid_proxies:
524+
if not base_parsed:
525+
logger.error("Invalid base URL: %s", self.base_url)
526+
527+
for proxy_type, proxy_url in invalid_proxies.items():
528+
logger.error("Invalid %s proxy: %s", proxy_type.upper(), proxy_url)
529+
530+
logger.error("")
531+
return False
532+
533+
return True
534+
510535
def _test_connection(self, url):
511536
parsed_url = urlparse(url)
512537
logger.error(" Could not resolve %s.", parsed_url.hostname)
@@ -534,11 +559,10 @@ def test_connection(self, rc=0):
534559
"""
535560
Test connection to Red Hat
536561
"""
537-
auth_config_ok = self._test_auth_config()
538-
if not auth_config_ok:
539-
return 1
540-
541-
self._dump_urls()
562+
for config_test in [self._test_auth_config, self._test_url_config]:
563+
config_ok = config_test()
564+
if not config_ok:
565+
return 1
542566

543567
logger.info("Running Connection Tests...")
544568
logger.info("")

0 commit comments

Comments
 (0)