💫 Toolkit to help you get started with SDD or any other process!
fix(extensions): guard the required manifest sections so one bad extension cannot break `extension list` (#3797)
Ali jawwad1fff7a196d9b
diff --git a/src/specify_cli/extensions/__init__.py b/src/specify_cli/extensions/__init__.pyindex 6cd48582b..94fc02177 100644--- a/src/specify_cli/extensions/__init__.py+++ b/src/specify_cli/extensions/__init__.py@@ -263,8 +263,25 @@ class ExtensionManifest:f"(expected {self.SCHEMA_VERSION})")+ # The REQUIRED_FIELDS loop above only checks key PRESENCE, so a section+ # that is written but left empty (``provides:`` -> None) or given the+ # wrong shape (``provides: []``) passes it and then fails on first use:+ # ``field not in None`` raises TypeError and ``None.get(...)`` raises+ # AttributeError. Neither is a ValidationError, so both escape the+ # callers that already handle malformed manifests -- list_installed()'s+ # "Corrupted extension" fallback catches ValidationError only, so one bad+ # extension made ``specify extension list`` exit 1 with a raw+ # AttributeError instead of listing the rest. Guard each required+ # section's shape, mirroring the nested guards below ("Invalid+ # provides.commands: expected a list", "Invalid hooks: expected a+ # mapping") and _load_yaml's document-root check.+# Validate extension metadataext = self.data["extension"]+ if not isinstance(ext, dict):+ raise ValidationError(+ f"Invalid extension: expected a mapping, got {type(ext).__name__}"+ )for field in ["id", "name", "version", "description"]:if field not in ext:raise ValidationError(f"Missing extension.{field}")@@ -299,11 +316,19 @@ class ExtensionManifest:# Validate requires sectionrequires = self.data["requires"]+ if not isinstance(requires, dict):+ raise ValidationError(+ f"Invalid requires: expected a mapping, got {type(requires).__name__}"+ )if "speckit_version" not in requires:raise ValidationError("Missing requires.speckit_version")# Validate provides sectionprovides = self.data["provides"]+ if not isinstance(provides, dict):+ raise ValidationError(+ f"Invalid provides: expected a mapping, got {type(provides).__name__}"+ )commands = provides.get("commands", [])hooks = self.data.get("hooks")diff --git a/tests/test_extensions.py b/tests/test_extensions.pyindex 44edca3d3..fbf2b29a8 100644--- a/tests/test_extensions.py+++ b/tests/test_extensions.py@@ -614,6 +614,64 @@ class TestExtensionManifest:with pytest.raises(ValidationError, match="Invalid provides.commands"):ExtensionManifest(manifest_path)+ @pytest.mark.parametrize("section", ["extension", "requires", "provides"])+ @pytest.mark.parametrize("bad", [None, [], "text"])+ def test_required_section_not_mapping_rejected(+ self, temp_dir, valid_manifest_data, section, bad+ ):+ """A required section that is written but empty or wrongly shaped must+ raise ValidationError, not a raw TypeError/AttributeError.++ REQUIRED_FIELDS only checks key presence, so `provides:` with no value+ passed it and then hit `None.get(...)`. That AttributeError escaped+ list_installed()'s ValidationError-only "Corrupted extension" fallback,+ so one bad extension made `specify extension list` exit 1 instead of+ listing the others.+ """+ import yaml++ valid_manifest_data[section] = bad++ manifest_path = temp_dir / "extension.yml"+ with open(manifest_path, 'w') as f:+ yaml.dump(valid_manifest_data, f)++ with pytest.raises(ValidationError, match=f"Invalid {section}"):+ ExtensionManifest(manifest_path)++ def test_empty_provides_mapping_is_still_accepted_with_hooks(+ self, temp_dir, valid_manifest_data+ ):+ """Regression guard: `provides: {}` is a well-SHAPED mapping, so the new+ shape check must not reject it — an extension may provide only hooks."""+ import yaml++ valid_manifest_data["provides"] = {}+ assert valid_manifest_data.get("hooks"), "fixture is expected to define hooks"++ manifest_path = temp_dir / "extension.yml"+ with open(manifest_path, 'w') as f:+ yaml.dump(valid_manifest_data, f)++ ExtensionManifest(manifest_path) # must not raise++ def test_empty_provides_and_no_hooks_keeps_its_own_message(+ self, temp_dir, valid_manifest_data+ ):+ """...and with no hooks either, it keeps the pre-existing message rather+ than the new shape error."""+ import yaml++ valid_manifest_data["provides"] = {}+ valid_manifest_data.pop("hooks", None)++ manifest_path = temp_dir / "extension.yml"+ with open(manifest_path, 'w') as f:+ yaml.dump(valid_manifest_data, f)++ with pytest.raises(ValidationError, match="at least one command or hook"):+ ExtensionManifest(manifest_path)+def test_hooks_not_dict_rejected(self, temp_dir, valid_manifest_data):"""Test manifest with hooks as a list is rejected."""import yaml