spec-kit icon indicating copy to clipboard operation
spec-kit copied to clipboard

fix(extensions): stop env-var config leaking across prefix-colliding extension IDs

Open thejesh23 opened this issue 4 months ago • 4 comments

Fixes #3494.

What

ConfigManager._get_env_config reads SPECKIT_<EXT_ID>_... env vars by prefix-match. But because _ doubles as both the separator between the extension ID and the 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.

Repro on main (0.12.13.dev0):

os.environ["SPECKIT_GIT_URL"]       = "value_for_git"
os.environ["SPECKIT_GIT_HOOKS_URL"] = "value_for_git_hooks"
ConfigManager(project, "git")._get_env_config()
# -> {'url': 'value_for_git', 'hooks': {'url': 'value_for_git_hooks'}}
#                             ^^^^^^^^^^^ leaked from git-hooks

Impact:

  • config intended for one extension surfaces inside another's config namespace,
  • hook conditions like config.hooks.url is set fire on the wrong extension,
  • documented env-over-YAML precedence is silently violated for an unrelated extension.

Distinct from #3350, which fixed intra-extension collisions (two keys of the same extension colliding on a prefix). This PR fixes the cross-extension case.

Fix

Route each env var to the extension whose normalized ID is the longest match — the more specific one:

  • Look up sibling extension IDs from ExtensionRegistry (.specify/extensions/.registry) — the registry is the source of truth for "installed", not the on-disk directory. A bare <sibling>/ directory can be a config-only leftover from ExtensionManager.remove(..., keep_config=True), which preserves the dir but drops the registry entry; treating it as installed would silently discard SPECKIT_<leftover>_* env vars into no owner.
  • For each sibling whose normalized ID extends the current extension's (GIT_HOOKS starts with GIT_), record the portion of the remainder it claims, including a trailing _ boundary (so sibling hook cannot false-positive on key hooks).
  • When an env var's remainder starts with any sibling-owned prefix, skip it — it belongs to the sibling.

Fallbacks in _sibling_extension_ids:

  • Missing registry (fresh project, ad-hoc harness) → ExtensionRegistry(...).keys() returns empty; behaviour is identical to pre-fix.
  • Corrupted / non-UTF-8 registry → _sibling_extension_ids catches (OSError, UnicodeError) and returns [] so a broken registry degrades to the pre-fix single-extension path rather than aborting every config read (ExtensionRegistry._load() only handles JSONDecodeError / FileNotFoundError, so a non-UTF-8 file would otherwise surface UnicodeDecodeError). Hardening _load() itself is left for a separate PR since it affects every reader.
  • No colliding sibling installed → var is still absorbed as before (the fix is scoped strictly to the collision case, avoiding surprise for users who set a nested env key without a sibling to disambiguate against).

Tests

New class TestConfigManagerCrossExtensionEnvLeak with 7 tests:

  • test_sibling_owns_longer_prefix_env — the reproducer above, now correct.
  • test_no_sibling_installed_keeps_legacy_absorption — no regression when only one extension is installed.
  • test_non_prefix_sibling_ignored — sibling not-git does not affect git.
  • test_boundary_prevents_false_positive — sibling git-hook does not eat HOOKS_* keys.
  • test_missing_extensions_dir_does_not_crash — sibling scan degrades cleanly when .specify/extensions/ is absent (registry lookup returns empty).
  • test_config_only_leftover_not_treated_as_sibling — a git-hooks/ directory left behind by remove(..., keep_config=True) (dir + config preserved, no registry entry) is not treated as installed; git still absorbs SPECKIT_GIT_HOOKS_*.
  • test_non_utf8_registry_does_not_crash — a registry file containing invalid UTF-8 does not propagate UnicodeDecodeError out of the sibling scan.

The test helper _install now writes both the directory and an ExtensionRegistry.add(...) entry, matching the registry-as-source-of-truth model.

Adjacent TestConfigManagerEnvPrefixCollision (from #3350) still passes.

Full suite:

$ .venv/bin/python -m pytest tests -q --tb=no
========== 3979 passed, 110 skipped, 82 warnings in 67.58s (0:01:07) ===========

Manual test results

Agent: N/A — this is a config-loader fix; no slash command's behaviour changes. | OS/Shell: macOS 15 / zsh

Command tested Notes
n/a Only src/specify_cli/extensions/__init__.py (ConfigManager._get_env_config + a new private helper _sibling_extension_ids) is modified. No templates/commands/*, no scripts/bash|powershell/*, no src/specify_cli/* CLI entry point, no extension.yml/manifest surface.

AI disclosure

Per CONTRIBUTING.md#ai-contributions-in-spec-kit: this PR (bug identification, patch, and tests) was prepared with Claude (Anthropic) assistance in an autonomous agent session. Reproducer was executed against a fresh clone of main before and after the change; the full test suite (3,979 tests) was run green before submission. The fix is deliberately narrow — one new private helper (_sibling_extension_ids), one added guard in _get_env_config, and seven focused regression tests. The design intentionally preserves the pre-fix single-extension behaviour when no colliding sibling is installed, so the change is a pure additive-safety fix rather than a policy shift.

🤖 Generated with Claude Code

thejesh23 avatar Jul 13 '26 17:07 thejesh23

Please address Copilot feedback

mnriem avatar Jul 13 '26 20:07 mnriem

Please address Copilot feedback

mnriem avatar Jul 13 '26 22:07 mnriem

@mnriem addressed comments

thejesh23 avatar Jul 13 '26 22:07 thejesh23

An explanation of this PR scanned by a tool I'm working on - Hope that it helps: https://www.lenzon.ai/viewer/cmrjulyib0005m7f49oejcyx5?voice=google-chirp3

rickgonzalez avatar Jul 13 '26 23:07 rickgonzalez

Thank you!

mnriem avatar Jul 14 '26 12:07 mnriem