Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/specify_cli/extensions/_catalog_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from packaging.version import InvalidVersion, Version

from .._download_security import is_https_or_localhost_http
from . import ExtensionError

_SHA256 = re.compile(r"^[0-9a-fA-F]{64}$")
Expand Down Expand Up @@ -94,6 +95,10 @@ def _validated_releases(entry: dict[str, Any]) -> dict[str, dict[str, Any]]:
raise ExtensionError(
f"Extension '{extension_id}' release '{release_version}' needs a download_url."
)
if not is_https_or_localhost_http(record["download_url"]):
raise ExtensionError(
f"Extension '{extension_id}' release '{release_version}' has an invalid download_url."
)
digest = record.get("sha256")
if isinstance(digest, str):
digest = digest.strip()
Expand Down
80 changes: 80 additions & 0 deletions tests/specify_cli/extensions/test_catalog_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,86 @@ def test_malformed_history_is_rejected(tmp_path, monkeypatch, bad_history):
catalog.get_extension_info("demo-history", "0.4.12")


def _entry_with_release_url(download_url: str) -> dict:
return {
"id": "demo-history",
"name": "Demo History",
"version": "0.5.1",
"download_url": "https://example.com/demo-0.5.1.zip",
"sha256": "a" * 64,
"_catalog_name": "trusted",
"_install_allowed": True,
"releases": {
"0.4.12": {"download_url": download_url, "sha256": "b" * 64},
},
}


@pytest.mark.parametrize(
"download_url",
[
"http://example.com/a.zip",
"http://localhost.example.com/a.zip",
"ftp://example.com/a.zip",
"file:///tmp/a.zip",
"https:///a.zip",
],
)
def test_historical_release_rejects_unsafe_download_url(
tmp_path, monkeypatch, download_url
):
catalog = _catalog(monkeypatch, tmp_path, _entry_with_release_url(download_url))

with pytest.raises(ExtensionError, match="has an invalid download_url"):
catalog.get_extension_info("demo-history", "0.4.12")
with pytest.raises(ExtensionError, match="has an invalid download_url"):
catalog.get_extension_versions("demo-history")


@pytest.mark.parametrize(
"download_url",
[
"https://example.com/a.zip",
"http://localhost:8000/a.zip",
"http://127.0.0.1/a.zip",
"http://[::1]/a.zip",
],
)
def test_historical_release_accepts_https_or_loopback_http(
tmp_path, monkeypatch, download_url
):
catalog = _catalog(monkeypatch, tmp_path, _entry_with_release_url(download_url))

selected = catalog.get_extension_info("demo-history", "0.4.12")

assert selected["version"] == "0.4.12"
assert selected["download_url"] == download_url


@pytest.mark.parametrize("download_url", ["", " "])
def test_blank_historical_download_url_still_reports_missing(
tmp_path, monkeypatch, download_url
):
catalog = _catalog(monkeypatch, tmp_path, _entry_with_release_url(download_url))

with pytest.raises(ExtensionError, match="needs a download_url"):
catalog.get_extension_info("demo-history", "0.4.12")


def test_extension_info_reports_unsafe_historical_download_url(tmp_path, monkeypatch):
project = tmp_path / "project"
(project / ".specify").mkdir(parents=True)
_catalog(monkeypatch, project, _entry_with_release_url("http://example.com/a.zip"))
monkeypatch.chdir(project)

result = CliRunner().invoke(
app, ["extension", "info", "demo-history", "--versions"]
)

assert result.exit_code != 0
assert "has an invalid download_url" in " ".join(result.output.split())


def test_selected_release_download_uses_its_url_without_relookup(tmp_path, monkeypatch):
archive = _archive(tmp_path, "0.4.12")
entry = _entry(archive)
Expand Down
Loading