Skip to content

Commit 2537be8

Browse files
authored
fix(extensions): stop env-var config leaking across prefix-colliding extension IDs (github#3497)
* fix(extensions): stop env-var config leaking across prefix-colliding IDs (github#3494) Because ``_`` doubles as both the separator between an extension ID and its config path AND the substitute for ``-`` inside an extension ID, an env var like ``SPECKIT_GIT_HOOKS_URL`` starts with *both* the ``SPECKIT_GIT_`` prefix of the ``git`` extension and the ``SPECKIT_GIT_HOOKS_`` prefix of a co-installed ``git-hooks`` extension. ``ConfigManager._get_env_config`` matched only on the shorter prefix, so the same env var silently surfaced inside both extensions' configs (as ``{'hooks': {'url': ...}}`` for ``git`` and ``{'url': ...}`` for ``git-hooks``). Impact: config intended for one extension leaked into another and, worse, could flip ``config.<field> is set`` hook conditions on the wrong extension. Route the env var to the extension whose normalized ID is the longest match — the more specific one. When another installed sibling's normalized ID + ``_`` claims the remainder, skip the var here. The sibling scan reads ``.specify/extensions/`` directly and degrades to a no-op if the dir is missing (fresh project / ad-hoc harness), so the pre-fix single-extension behaviour is unchanged when there is no collision. Distinct from github#3350 (intra-extension prefix collision between two keys of the same extension) — this fixes the cross-extension case. Fixes github#3494 * fix(extensions): source sibling scan from registry, not directory Address Copilot review on github#3497: ``ExtensionManager.remove(..., keep_config=True)`` preserves the extension directory but drops the registry entry, so the previous directory-scan approach would treat a config-only leftover as an installed sibling and silently discard ``SPECKIT_<sibling>_*`` env vars into no owner. Sourced the sibling list from ``ExtensionRegistry.keys()`` — the registry is the source of truth for "installed" — and kept the same graceful ``[]`` fallback so the fresh-project / ad-hoc harness path is unaffected. Updated the ``TestConfigManagerCrossExtensionEnvLeak`` ``_install`` helper to register its fake installations and added ``test_config_only_leftover_not_treated_as_sibling`` to lock in the new behaviour for the ``keep_config=True`` scenario. Full suite: 3978 passed, 110 skipped. * fix(extensions): swallow non-UTF-8 registry in sibling scan Address Copilot follow-up on github#3497: ``ExtensionRegistry._load()`` catches ``JSONDecodeError`` / ``FileNotFoundError`` but not decode failures — a registry file with invalid text encoding would surface a ``UnicodeDecodeError`` out of ``_sibling_extension_ids`` and break every config read instead of degrading to the documented pre-fix behaviour. Extend the fallback in ``_sibling_extension_ids`` to also catch ``UnicodeError`` and add ``test_non_utf8_registry_does_not_crash`` as a regression pin (kept ``_load()`` itself out of scope — that broader hardening belongs in a separate PR since it affects all readers). Full suite: 3979 passed, 110 skipped.
1 parent 6ab0c1d commit 2537be8

2 files changed

Lines changed: 197 additions & 1 deletion

File tree

‎src/specify_cli/extensions/__init__.py‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2737,6 +2737,36 @@ def _get_local_config(self) -> Dict[str, Any]:
27372737
config_file = self.extension_dir / "local-config.yml"
27382738
return self._load_yaml_config(config_file)
27392739

2740+
def _sibling_extension_ids(self) -> list[str]:
2741+
"""Return IDs of other extensions installed alongside this one.
2742+
2743+
Sourced from ``ExtensionRegistry`` (``.specify/extensions/.registry``)
2744+
rather than a directory scan: ``ExtensionManager.remove(...,
2745+
keep_config=True)`` deliberately preserves the extension directory
2746+
while dropping the registry entry, so a directory scan would treat
2747+
that config-only leftover as an installed sibling and keep silently
2748+
absorbing its ``SPECKIT_<sibling>_*`` env vars into no one. The
2749+
registry is the source of truth for "installed".
2750+
2751+
Returns an empty list if the registry is missing or corrupted
2752+
(fresh project, ad-hoc test harness) so ``_get_env_config`` degrades
2753+
to its pre-fix behaviour rather than crashing. ``UnicodeError`` is
2754+
caught alongside ``OSError`` because ``ExtensionRegistry._load()``
2755+
opens the file in text mode and only handles ``JSONDecodeError`` /
2756+
``FileNotFoundError``, so a registry file with non-UTF-8 bytes would
2757+
otherwise surface a ``UnicodeDecodeError`` here and break *every*
2758+
config read instead of degrading gracefully.
2759+
2760+
Used by ``_get_env_config`` to detect env vars whose remainder claims
2761+
a longer, sibling-owned prefix (e.g. ``SPECKIT_GIT_HOOKS_URL`` is
2762+
owned by ``git-hooks`` when it is co-installed with ``git``).
2763+
"""
2764+
extensions_dir = self.project_root / ".specify" / "extensions"
2765+
try:
2766+
return list(ExtensionRegistry(extensions_dir).keys())
2767+
except (OSError, UnicodeError):
2768+
return []
2769+
27402770
def _get_env_config(self) -> Dict[str, Any]:
27412771
"""Get configuration from environment variables.
27422772
@@ -2756,15 +2786,49 @@ def _get_env_config(self) -> Dict[str, Any]:
27562786
ext_id_upper = self.extension_id.replace("-", "_").upper()
27572787
prefix = f"SPECKIT_{ext_id_upper}_"
27582788

2789+
# Cross-extension prefix collision: because ``_`` doubles as both the
2790+
# separator between the extension ID and the config path *and* the
2791+
# substitute for ``-`` inside an extension ID, an env var like
2792+
# ``SPECKIT_GIT_HOOKS_URL`` begins with *both* the ``SPECKIT_GIT_``
2793+
# prefix of the ``git`` extension and the ``SPECKIT_GIT_HOOKS_`` prefix
2794+
# of a co-installed ``git-hooks`` extension. It logically belongs to
2795+
# the extension whose normalized ID is the longer, more specific match
2796+
# — otherwise config intended for one extension silently surfaces
2797+
# inside another and can drive hooks that only inspect
2798+
# ``config.<field> is set``. Build the list of sibling-owned
2799+
# remainder-prefixes here so a later env var can be skipped if it
2800+
# matches one.
2801+
sibling_prefixes: list[str] = []
2802+
for sibling_id in self._sibling_extension_ids():
2803+
if sibling_id == self.extension_id:
2804+
continue
2805+
sib_upper = sibling_id.replace("-", "_").upper()
2806+
# A sibling collides only when its normalized ID *extends* our own
2807+
# (i.e. starts with ``<US>_``). ``git`` vs ``not-git`` is not a
2808+
# collision; ``git`` vs ``git-hooks`` is.
2809+
if sib_upper.startswith(ext_id_upper + "_"):
2810+
# The portion of the env-var *remainder* the sibling claims,
2811+
# including the trailing ``_`` so a shorter ID that shares a
2812+
# non-boundary prefix cannot false-positive (e.g. sibling
2813+
# ``hook`` would not eat env vars under key ``hooks``).
2814+
sibling_prefixes.append(sib_upper[len(ext_id_upper) + 1 :] + "_")
2815+
27592816
for key, value in os.environ.items():
27602817
if not key.startswith(prefix):
27612818
continue
27622819

2820+
remainder = key[len(prefix) :]
2821+
# Skip when a longer sibling ID claims this var — see the block
2822+
# above. Keeps ``SPECKIT_GIT_HOOKS_URL`` out of the ``git``
2823+
# extension's config when ``git-hooks`` is co-installed.
2824+
if any(remainder.startswith(sp) for sp in sibling_prefixes):
2825+
continue
2826+
27632827
# Remove prefix and split into parts. Drop empty components from a
27642828
# malformed name (e.g. ``SPECKIT_<EXT>_`` with no key, or
27652829
# consecutive underscores ``SPECKIT_X__Y``) so we never create an
27662830
# entry under an empty key.
2767-
config_path = [p for p in key[len(prefix) :].lower().split("_") if p]
2831+
config_path = [p for p in remainder.lower().split("_") if p]
27682832
if not config_path:
27692833
continue
27702834

‎tests/test_extensions.py‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7896,3 +7896,135 @@ def test_malformed_env_names_ignored(self, tmp_path, monkeypatch):
78967896
cfg = cm._get_env_config()
78977897
assert "" not in cfg
78987898
assert cfg == {"a": {"b": "z"}}
7899+
7900+
7901+
class TestConfigManagerCrossExtensionEnvLeak:
7902+
"""Cross-extension env-var leak: a longer, co-installed sibling ID must
7903+
own its own env vars instead of leaking them into a shorter-prefix sibling.
7904+
7905+
Before the fix, ``SPECKIT_GIT_HOOKS_URL`` (intended for a ``git-hooks``
7906+
extension) also surfaced inside the ``git`` extension's config as
7907+
``{'hooks': {'url': ...}}`` because ``SPECKIT_GIT_`` is a strict prefix of
7908+
``SPECKIT_GIT_HOOKS_``.
7909+
"""
7910+
7911+
def _install(self, project_root, ext_id):
7912+
extensions_dir = project_root / ".specify" / "extensions"
7913+
(extensions_dir / ext_id).mkdir(parents=True)
7914+
# Register in the extension registry — the registry is the source of
7915+
# truth for "installed" (a bare directory can be a config-only leftover
7916+
# from ``ExtensionManager.remove(..., keep_config=True)``).
7917+
ExtensionRegistry(extensions_dir).add(ext_id, {})
7918+
7919+
def test_sibling_owns_longer_prefix_env(self, tmp_path, monkeypatch):
7920+
"""SPECKIT_GIT_HOOKS_URL belongs to git-hooks when co-installed with git."""
7921+
self._install(tmp_path, "git")
7922+
self._install(tmp_path, "git-hooks")
7923+
monkeypatch.setenv("SPECKIT_GIT_URL", "for_git")
7924+
monkeypatch.setenv("SPECKIT_GIT_HOOKS_URL", "for_git_hooks")
7925+
7926+
git_cfg = ConfigManager(tmp_path, "git")._get_env_config()
7927+
gh_cfg = ConfigManager(tmp_path, "git-hooks")._get_env_config()
7928+
7929+
# 'git' must NOT see the git-hooks var — no cross-extension leak.
7930+
assert git_cfg == {"url": "for_git"}
7931+
# 'git-hooks' still receives its own var (unchanged behaviour).
7932+
assert gh_cfg == {"url": "for_git_hooks"}
7933+
7934+
def test_no_sibling_installed_keeps_legacy_absorption(self, tmp_path, monkeypatch):
7935+
"""Without a longer-prefix sibling installed, the legacy behaviour is
7936+
preserved: ``SPECKIT_GIT_HOOKS_URL`` is absorbed as a nested key of
7937+
the ``git`` extension. This keeps the fix strictly to the *collision*
7938+
case and avoids surprising users who deliberately set a nested key
7939+
via env with no sibling to disambiguate against.
7940+
"""
7941+
self._install(tmp_path, "git")
7942+
monkeypatch.setenv("SPECKIT_GIT_HOOKS_URL", "for_git_hooks")
7943+
7944+
cfg = ConfigManager(tmp_path, "git")._get_env_config()
7945+
assert cfg == {"hooks": {"url": "for_git_hooks"}}
7946+
7947+
def test_non_prefix_sibling_ignored(self, tmp_path, monkeypatch):
7948+
"""A sibling whose ID does not extend our own is not a collision.
7949+
7950+
e.g. current='git' and sibling='not-git' — 'not-git' normalized to
7951+
'NOT_GIT' does not start with 'GIT_', so its presence must not
7952+
influence git's env-var interpretation.
7953+
"""
7954+
self._install(tmp_path, "git")
7955+
self._install(tmp_path, "not-git")
7956+
monkeypatch.setenv("SPECKIT_GIT_HOOKS_URL", "for_git_hooks")
7957+
7958+
cfg = ConfigManager(tmp_path, "git")._get_env_config()
7959+
assert cfg == {"hooks": {"url": "for_git_hooks"}}
7960+
7961+
def test_boundary_prevents_false_positive(self, tmp_path, monkeypatch):
7962+
"""Sibling ID 'hook' (not 'hooks') must NOT eat env keys starting
7963+
with 'hooks'. The trailing-underscore boundary in the sibling prefix
7964+
prevents this false positive.
7965+
"""
7966+
self._install(tmp_path, "git")
7967+
self._install(tmp_path, "git-hook")
7968+
monkeypatch.setenv("SPECKIT_GIT_HOOKS_URL", "for_git_key_hooks")
7969+
7970+
# git-hook's prefix is 'HOOK_', which does not match 'HOOKS_URL',
7971+
# so 'git' keeps the env var (single-installed semantics).
7972+
cfg = ConfigManager(tmp_path, "git")._get_env_config()
7973+
assert cfg == {"hooks": {"url": "for_git_key_hooks"}}
7974+
7975+
def test_missing_extensions_dir_does_not_crash(self, tmp_path, monkeypatch):
7976+
"""A ConfigManager built against a project without ``.specify/extensions``
7977+
(fresh project, ad-hoc test harness) must still evaluate env config
7978+
rather than raising from the sibling scan.
7979+
"""
7980+
# Note: no _install call — extensions dir intentionally absent.
7981+
monkeypatch.setenv("SPECKIT_TESTEXT_URL", "v")
7982+
7983+
cfg = ConfigManager(tmp_path, "testext")._get_env_config()
7984+
assert cfg == {"url": "v"}
7985+
7986+
def test_config_only_leftover_not_treated_as_sibling(self, tmp_path, monkeypatch):
7987+
"""A directory left behind by ``remove(..., keep_config=True)`` must
7988+
NOT be treated as an installed sibling.
7989+
7990+
``ExtensionManager.remove(keep_config=True)`` preserves the extension
7991+
directory (config files remain, dormant, for a possible reinstall) but
7992+
removes the registry entry. The sibling scan is sourced from the
7993+
registry, so a leftover ``git-hooks/`` directory without a registry
7994+
entry must not silently discard ``SPECKIT_GIT_HOOKS_*`` from ``git``.
7995+
"""
7996+
self._install(tmp_path, "git")
7997+
# Simulate ``remove('git-hooks', keep_config=True)``: dir present,
7998+
# config file preserved, but no registry entry.
7999+
gh_dir = tmp_path / ".specify" / "extensions" / "git-hooks"
8000+
gh_dir.mkdir(parents=True)
8001+
(gh_dir / "git-hooks-config.yml").write_text("url: leftover\n")
8002+
# Sanity: git-hooks is NOT registered.
8003+
registry = ExtensionRegistry(tmp_path / ".specify" / "extensions")
8004+
assert "git-hooks" not in registry.keys()
8005+
8006+
monkeypatch.setenv("SPECKIT_GIT_HOOKS_URL", "for_git")
8007+
8008+
cfg = ConfigManager(tmp_path, "git")._get_env_config()
8009+
# git absorbs the var (no registered sibling owns it).
8010+
assert cfg == {"hooks": {"url": "for_git"}}
8011+
8012+
def test_non_utf8_registry_does_not_crash(self, tmp_path, monkeypatch):
8013+
"""A registry file with invalid text encoding must NOT propagate
8014+
``UnicodeDecodeError`` out of the sibling scan and abort every
8015+
config read. ``ExtensionRegistry._load()`` catches ``JSONDecodeError``
8016+
/ ``FileNotFoundError`` only, so ``_sibling_extension_ids`` must
8017+
additionally swallow ``UnicodeError`` and degrade to the documented
8018+
pre-fix behaviour.
8019+
"""
8020+
extensions_dir = tmp_path / ".specify" / "extensions"
8021+
extensions_dir.mkdir(parents=True)
8022+
# Write bytes that are not valid UTF-8 to the registry file.
8023+
(extensions_dir / ExtensionRegistry.REGISTRY_FILE).write_bytes(
8024+
b"\xff\xfe invalid utf-8 registry \xc3\x28"
8025+
)
8026+
monkeypatch.setenv("SPECKIT_TESTEXT_URL", "v")
8027+
8028+
# Must not raise; must fall back to the "no siblings" path.
8029+
cfg = ConfigManager(tmp_path, "testext")._get_env_config()
8030+
assert cfg == {"url": "v"}

0 commit comments

Comments
 (0)