From 60197fdd15bf55b3d808337bc9743f6f01940f20 Mon Sep 17 00:00:00 2001 From: chelsealong Date: Mon, 23 Nov 2026 01:39:18 +0000 Subject: [PATCH] fix: prevent integration catalog removal under environment override --- src/specify_cli/integrations/__init__.py | 6 ++++++ .../catalog/test_command_remove.py | 17 +++++++++++++++++ .../specify_cli/integrations/test_catalog.py | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/src/specify_cli/integrations/__init__.py b/src/specify_cli/integrations/__init__.py index e38d471387..c43cffbb08 100644 --- a/src/specify_cli/integrations/__init__.py +++ b/src/specify_cli/integrations/__init__.py @@ -699,6 +699,12 @@ def remove_catalog(self, index: int) -> str: Returns the removed catalog's name. """ + if os.environ.get("SPECKIT_INTEGRATION_CATALOG_URL", "").strip(): + raise IntegrationValidationError( + "The active catalog comes from SPECKIT_INTEGRATION_CATALOG_URL. " + "Unset that variable to remove a project catalog source." + ) + config_path = self.project_root / ".specify" / self.CONFIG_FILENAME if not config_path.exists(): raise IntegrationValidationError("No catalog config file found.") diff --git a/tests/specify_cli/integrations/catalog/test_command_remove.py b/tests/specify_cli/integrations/catalog/test_command_remove.py index c2ba7f8e3e..3174d94173 100644 --- a/tests/specify_cli/integrations/catalog/test_command_remove.py +++ b/tests/specify_cli/integrations/catalog/test_command_remove.py @@ -10,6 +10,23 @@ from tests.specify_cli.integrations._catalog_helpers import IntegrationCatalogCliTestBase class TestIntegrationCatalogRemove(IntegrationCatalogCliTestBase): + def test_catalog_remove_refuses_environment_override(self, tmp_path, monkeypatch): + project = self._make_project(tmp_path) + config = project / ".specify" / "integration-catalogs.yml" + config.write_text( + "catalogs:\n - name: project\n url: https://project.example.com/catalog.json\n" + ) + before = config.read_bytes() + monkeypatch.setenv( + "SPECKIT_INTEGRATION_CATALOG_URL", "https://env.example.com/catalog.json" + ) + + result = self._invoke(["integration", "catalog", "remove", "0"], project) + + assert result.exit_code == 1 + assert "SPECKIT_INTEGRATION_CATALOG_URL" in result.output + assert config.read_bytes() == before + def test_catalog_remove_out_of_range(self, tmp_path, monkeypatch): project = self._make_project(tmp_path) # Need a config file for remove to attempt an index lookup diff --git a/tests/specify_cli/integrations/test_catalog.py b/tests/specify_cli/integrations/test_catalog.py index e828d61b48..96328ceb86 100644 --- a/tests/specify_cli/integrations/test_catalog.py +++ b/tests/specify_cli/integrations/test_catalog.py @@ -982,6 +982,25 @@ def test_remove_catalog_happy_path(self, tmp_path, monkeypatch): ) assert [e["name"] for e in data["catalogs"]] == ["b"] + def test_remove_catalog_refuses_inactive_project_sources(self, tmp_path, monkeypatch): + self._isolate(tmp_path, monkeypatch) + cat = IntegrationCatalog(tmp_path) + cat.add_catalog("https://a.example.com/catalog.json", name="a") + cat.add_catalog("https://b.example.com/catalog.json", name="b") + config = tmp_path / ".specify" / "integration-catalogs.yml" + before = config.read_bytes() + + monkeypatch.setenv( + "SPECKIT_INTEGRATION_CATALOG_URL", " https://env.example.com/catalog.json " + ) + with pytest.raises(IntegrationValidationError, match="SPECKIT_INTEGRATION_CATALOG_URL"): + cat.remove_catalog(0) + assert config.read_bytes() == before + + monkeypatch.setenv("SPECKIT_INTEGRATION_CATALOG_URL", " \t ") + assert cat.remove_catalog(0) == "a" + assert [e["name"] for e in yaml.safe_load(config.read_text())["catalogs"]] == ["b"] + def test_remove_catalog_index_out_of_range(self, tmp_path, monkeypatch): self._isolate(tmp_path, monkeypatch) cat = IntegrationCatalog(tmp_path)