from __future__ import annotations import re import subprocess import sys from pathlib import Path import pytest PLUGIN_ROOT = Path(__file__).resolve().parents[1] # Paths that must never be tracked by git, because everything git tracks ends # up inside the published plugin archive (scripts/package_plugin.sh builds it # with ``git archive``). SECRET_PATH_PATTERN = re.compile( r"(^|/)(\.env|.*\.pem$|.*\.p12$|.*id_rsa.*|secrets?\.ya?ml)", re.IGNORECASE, ) # Literal credential field names that indicate a plaintext secret is being # persisted in a tracked file rather than referenced through an env var. PLAINTEXT_SECRET_FIELDS = ( "api_key_plaintext", "admin_token_plaintext", "admin_password_plaintext", ) def _git(*args: str) -> str: result = subprocess.run( ["git", *args], cwd=PLUGIN_ROOT, capture_output=True, text=True, timeout=60, check=False, ) if result.returncode != 0: pytest.skip(f"git unavailable or not a repository: {result.stderr.strip()}") return result.stdout def _tracked_files() -> list[str]: return [line for line in _git("ls-files").splitlines() if line] def test_no_secret_bearing_path_is_tracked_by_git() -> None: """Regression for the 2026-07 leak: real credentials shipped in 19 archives. The leak did not travel through git — ``.env*`` was ignored the whole time. It travelled because the deploy scripts copied the working directory, so an ignored file still reached the package. ``scripts/package_plugin.sh`` now builds from ``git archive``, which makes "tracked" and "published" the same set. This test guards that set. """ offenders = [path for path in _tracked_files() if SECRET_PATH_PATTERN.search(path)] assert offenders == [], ( "secret-bearing paths are tracked by git and would be published: " f"{offenders}" ) def test_release_archives_are_not_tracked() -> None: """``release/*.zip`` bloated .git to 51 MB and ships nothing the host needs.""" offenders = [path for path in _tracked_files() if path.startswith("release/")] assert offenders == [], ( "release/ build artifacts are tracked; run `git rm -r --cached release/`: " f"{offenders[:5]}{'...' if len(offenders) > 5 else ''}" ) def test_tracked_config_carries_no_plaintext_credentials() -> None: """``config.yaml`` is tracked, so any populated secret field is published.""" config_path = PLUGIN_ROOT / "config.yaml" if not config_path.exists(): pytest.skip("config.yaml missing") populated: list[str] = [] for lineno, line in enumerate(config_path.read_text(encoding="utf-8").splitlines(), 1): stripped = line.strip() if stripped.startswith("#") or ":" not in stripped: continue key, _, value = stripped.partition(":") if key.strip().lstrip("- ") not in PLAINTEXT_SECRET_FIELDS: continue if value.strip().strip("\"'"): populated.append(f"config.yaml:{lineno} {key.strip()}") assert populated == [], ( "tracked config.yaml holds plaintext credentials; move them to the " f"*_env_var fields instead: {populated}" ) @pytest.mark.skipif(sys.platform == "win32", reason="packaging script requires a POSIX shell") def test_packaging_script_refuses_to_publish_a_secret(tmp_path: Path) -> None: """The archive assertion must actually fire, not just exist in the file.""" script = PLUGIN_ROOT / "scripts" / "package_plugin.sh" assert script.exists(), "scripts/package_plugin.sh is missing" body = script.read_text(encoding="utf-8") # The guard is a grep over the archive listing followed by a hard exit. assert "git archive" in body, "packaging must build from the git view" assert "refusing to publish" in body, "packaging must abort on a secret hit" assert "exit 1" in body, "packaging must exit non-zero on a secret hit"