From 382d49d8870c564289c4e493764f27d1911cf4bf Mon Sep 17 00:00:00 2001 From: Afonso Coutinho Date: Wed, 3 Jun 2026 00:35:16 +0100 Subject: [PATCH] fix: validate_caldav_url crashes with TypeError on a non-string URL (#1608) --- src/caldav_sync.py | 2 +- tests/test_caldav_url_nonstring.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/test_caldav_url_nonstring.py diff --git a/src/caldav_sync.py b/src/caldav_sync.py index 312cca8..a02112e 100644 --- a/src/caldav_sync.py +++ b/src/caldav_sync.py @@ -63,7 +63,7 @@ def _validate_caldav_ip(host: str) -> None: def validate_caldav_url(raw_url: str) -> str: """Validate and normalize a user-provided CalDAV URL before server-side use.""" - url = (raw_url or "").strip() + url = (raw_url if isinstance(raw_url, str) else "").strip() if not url: raise ValueError("CalDAV URL is required") parsed = urlparse(url) diff --git a/tests/test_caldav_url_nonstring.py b/tests/test_caldav_url_nonstring.py new file mode 100644 index 0000000..a9d8f3f --- /dev/null +++ b/tests/test_caldav_url_nonstring.py @@ -0,0 +1,22 @@ +"""Regression: validate_caldav_url must reject a non-string via its normal +ValueError path, not crash with TypeError. + +It did `(raw_url or "").strip()`, so a non-string scalar (e.g. an int from a +mis-typed config) reached `.strip()` and raised TypeError instead of the +function\'s own ValueError. +""" +import pytest + +from src.caldav_sync import validate_caldav_url + + +def test_non_string_raises_valueerror_not_typeerror(): + with pytest.raises(ValueError): + validate_caldav_url(12345) + with pytest.raises(ValueError): + validate_caldav_url(None) + + +def test_valid_url_passes(): + out = validate_caldav_url("https://dav.example.com/calendars/") + assert "example.com" in out