Skip to content

Commit 2b5e175

Browse files
marcelsafinCopilot
andauthored
fix(bundler): validate catalog URLs in catalog add (HTTPS-only, require host) (#3367)
* fix(bundler): validate catalog URLs in `catalog add` (HTTPS-only, require host) add_source persisted remote catalog URLs without the HTTPS/host checks that specify_cli.catalogs (#3210) and the bundler adapters (#3333) enforce, and an unclosed IPv6 bracket escaped as a raw ValueError. Mirror the catalogs.py validation for http(s) schemes and wrap urlparse so malformed input raises BundlerError. Fixes #3366 Co-authored-by: Copilot <[email protected]> * docs: correct config filename and validation reference in comment Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 0e40438 commit 2b5e175

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

‎src/specify_cli/bundler/commands_impl/catalog_config.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ def _is_local_path(url: str) -> bool:
9595
"""True when *url* denotes a local filesystem path rather than a URL."""
9696
if _WINDOWS_DRIVE_RE.match(url):
9797
return True
98-
scheme = urlparse(url).scheme.lower()
98+
try:
99+
scheme = urlparse(url).scheme.lower()
100+
except ValueError:
101+
# Malformed URLs (e.g. an unclosed IPv6 bracket) are not local paths.
102+
return False
99103
return scheme not in _REMOTE_SCHEMES
100104

101105

@@ -137,7 +141,10 @@ def add_source(
137141
url = url.strip()
138142
if not url:
139143
raise BundlerError("A catalog url is required.")
140-
parsed = urlparse(url)
144+
try:
145+
parsed = urlparse(url)
146+
except ValueError as exc:
147+
raise BundlerError(f"Invalid catalog url: '{url}'.") from exc
141148
if not (parsed.scheme or parsed.path):
142149
raise BundlerError(f"Invalid catalog url: '{url}'.")
143150
# Reject unsupported URL schemes (e.g. ssh://, ftp://) up front so they are
@@ -148,6 +155,20 @@ def add_source(
148155
f"Unsupported catalog url scheme '{parsed.scheme}://' in '{url}'. "
149156
"Use http(s)://, file://, builtin://, or a local path."
150157
)
158+
if parsed.scheme.lower() in {"http", "https"}:
159+
# Mirror specify_cli.catalogs._validate_catalog_url (#3209/#3210):
160+
# HTTPS only (HTTP just for localhost), and check hostname, not
161+
# netloc — netloc is truthy for host-less URLs like "https://:8080"
162+
# or "https://user@". Validating here keeps junk out of
163+
# bundle-catalogs.yml instead of failing later at fetch time.
164+
is_localhost = parsed.hostname in ("localhost", "127.0.0.1", "::1")
165+
if parsed.scheme.lower() != "https" and not is_localhost:
166+
raise BundlerError(
167+
f"Catalog url must use HTTPS (got {parsed.scheme}://). "
168+
"HTTP is only allowed for localhost."
169+
)
170+
if not parsed.hostname:
171+
raise BundlerError(f"Catalog url must be a valid URL with a host: {url}")
151172

152173
url = _canonicalize_url(url)
153174
install_policy = InstallPolicy.parse(policy)

‎tests/unit/test_bundler_catalog_config.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,39 @@ def test_add_source_allows_local_path_with_colon(tmp_path: Path, monkeypatch):
222222
# A relative path containing ':' but no '://' is still a local path.
223223
source = cc.add_source(project, "weird:name.json", policy="install-allowed", priority=50)
224224
assert source.url.endswith("weird:name.json") or "weird" in source.url
225+
226+
227+
def test_add_source_rejects_plain_http_for_non_localhost(tmp_path: Path):
228+
project = tmp_path / "proj"
229+
(project / ".specify").mkdir(parents=True)
230+
with pytest.raises(BundlerError, match="HTTPS"):
231+
cc.add_source(project, "http://example.com/catalog.json", policy="install-allowed", priority=50)
232+
233+
234+
def test_add_source_allows_http_for_localhost(tmp_path: Path):
235+
project = tmp_path / "proj"
236+
(project / ".specify").mkdir(parents=True)
237+
source = cc.add_source(project, "http://localhost:8080/c.json", policy="install-allowed", priority=50)
238+
assert source.url == "http://localhost:8080/c.json"
239+
240+
241+
def test_add_source_rejects_host_less_remote_urls(tmp_path: Path):
242+
project = tmp_path / "proj"
243+
(project / ".specify").mkdir(parents=True)
244+
for url in ("https://:8080", "https://user@"):
245+
with pytest.raises(BundlerError, match="host"):
246+
cc.add_source(project, url, policy="install-allowed", priority=50)
247+
248+
249+
def test_add_source_wraps_invalid_ipv6_as_bundler_error(tmp_path: Path):
250+
project = tmp_path / "proj"
251+
(project / ".specify").mkdir(parents=True)
252+
with pytest.raises(BundlerError, match="Invalid catalog url"):
253+
cc.add_source(project, "https://[::1/c.json", policy="install-allowed", priority=50)
254+
255+
256+
def test_remove_source_does_not_crash_on_invalid_ipv6(tmp_path: Path):
257+
project = tmp_path / "proj"
258+
(project / ".specify").mkdir(parents=True)
259+
with pytest.raises(BundlerError, match="No project-scoped catalog source"):
260+
cc.remove_source(project, "https://[::1/c.json")

0 commit comments

Comments
 (0)