Skip to content

Commit a513864

Browse files
committed
fix: avoid using startswith for SSL configuration
It would wrongly match hosts whose domain name starts with 127.0.0.1.
1 parent aafdb50 commit a513864

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

wlc/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
DEVEL_URL = "https://github.com/WeblateOrg/wlc"
2929
API_URL = "http://127.0.0.1:8000/api/"
3030
USER_AGENT = f"wlc/{__version__}"
31-
LOCALHOST_NETLOC = "127.0.0.1"
31+
LOCALHOST_ADDRESSES = {"127.0.0.1", "localhost", "::1", "[::1]"}
32+
3233
TIMESTAMPS = {"last_change"}
3334

3435

@@ -195,7 +196,7 @@ def invoke_request(self, method, path, data=None, files=None, params=None):
195196
headers = {"user-agent": USER_AGENT, "Accept": "application/json"}
196197
if self.key:
197198
headers["Authorization"] = f"Token {self.key}"
198-
verify_ssl = self._should_verify_ssl(path)
199+
verify_ssl = self.should_verify_ssl(path)
199200
kwargs = {
200201
"headers": headers,
201202
"verify": verify_ssl,
@@ -371,11 +372,10 @@ def create_language(self, code, name, direction="ltr", plural=None):
371372
return self.post("languages/", **data)
372373

373374
@staticmethod
374-
def _should_verify_ssl(path):
375+
def should_verify_ssl(path: str) -> bool:
375376
"""Checks if it should verify ssl certificates."""
376377
url = urlparse(path)
377-
is_localhost = url.netloc.startswith(LOCALHOST_NETLOC)
378-
return url.scheme == "https" and (not is_localhost)
378+
return url.hostname not in LOCALHOST_ADDRESSES
379379

380380

381381
class LazyObject(dict):

wlc/test_wlc.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ def test_create_component_local_files(self) -> None:
300300
filemask="po/*.po",
301301
)
302302

303+
def test_should_verify_ssl(self) -> None:
304+
self.assertEqual(Weblate.should_verify_ssl("http://localhost/api/"), False)
305+
self.assertEqual(Weblate.should_verify_ssl("invalid/api/"), True)
306+
self.assertEqual(
307+
Weblate.should_verify_ssl("https://localhost.example.com/api/"), True
308+
)
309+
self.assertEqual(Weblate.should_verify_ssl("http://example.com/api/"), True)
310+
303311

304312
class ObjectTestBaseClass(APITest):
305313
"""Base class for objects testing."""

0 commit comments

Comments
 (0)