From e11e10b22166cdee38713ffa4fc7c2889cc96881 Mon Sep 17 00:00:00 2001
From: Tim
Date: Fri, 6 Nov 2026 10:09:52 -0500
Subject: [PATCH 01/11] feat(bundle): add allowlisted config summary and secret
list
---
selftests/test_bundle_summary.py | 363 ++++++++++++++++++++++++++++
src/vip/bundle/__init__.py | 1 +
src/vip/bundle/summary.py | 390 +++++++++++++++++++++++++++++++
3 files changed, 754 insertions(+)
create mode 100644 selftests/test_bundle_summary.py
create mode 100644 src/vip/bundle/__init__.py
create mode 100644 src/vip/bundle/summary.py
diff --git a/selftests/test_bundle_summary.py b/selftests/test_bundle_summary.py
new file mode 100644
index 000000000..42eee8b48
--- /dev/null
+++ b/selftests/test_bundle_summary.py
@@ -0,0 +1,363 @@
+"""Tests for the ``vip bundle`` configuration summary and secret list."""
+
+from __future__ import annotations
+
+import dataclasses
+import json
+import types
+import typing
+from typing import Any
+
+import pytest
+
+from vip.bundle.summary import (
+ EXCLUDED_FIELDS,
+ SECRET_FIELDS,
+ SUMMARIZED_FIELDS,
+ auth_cache_secrets,
+ build_config_summary,
+ secret_values,
+ strip_url_userinfo,
+)
+from vip.config import (
+ AuthConfig,
+ ConnectConfig,
+ DataSourceEntry,
+ GitTestConfig,
+ PackageManagerConfig,
+ VIPConfig,
+ WorkbenchConfig,
+)
+from vip.proxy import ProxyConfig
+
+# Every VIP_* variable that VIPConfig or the bundle reads. A developer's own
+# shell must not change what these tests see.
+_VIP_SECRET_ENV = (
+ "VIP_CONFIG",
+ "VIP_CONNECT_API_KEY",
+ "VIP_WORKBENCH_API_KEY",
+ "VIP_GIT_TOKEN",
+ "VIP_PACKAGE_MANAGER_TOKEN",
+ "VIP_PM_TOKEN",
+ "VIP_TEST_USERNAME",
+ "VIP_TEST_PASSWORD",
+ "VIP_TEST_TOTP_SECRET",
+)
+
+# One distinct value per credential source, so a test can name the one that leaked.
+SECRETS = {
+ "connect.api_key": "SECRET-connect-api-key",
+ "workbench.api_key": "SECRET-workbench-api-key",
+ "workbench.git_test.token": "SECRET-git-token",
+ "clone_url user": "SECRET-git-user-token",
+ "clone_url query": "SECRET-git-query-token",
+ "package_manager.token": "SECRET-pm-token",
+ "auth.password": "SECRET-auth-password",
+ "connection_string password": "SECRET-db-password",
+ "connect.url password": "SECRET-connect-url-password",
+ "proxy.url password": "SECRET-proxy-password",
+}
+
+
+@pytest.fixture(autouse=True)
+def _no_vip_env(monkeypatch):
+ for var in _VIP_SECRET_ENV:
+ monkeypatch.delenv(var, raising=False)
+
+
+@pytest.fixture
+def full_config() -> VIPConfig:
+ return VIPConfig(
+ deployment_name="Acme Prod",
+ connect=ConnectConfig(
+ url=f"https://cuser:{SECRETS['connect.url password']}@connect.example.com",
+ api_key=SECRETS["connect.api_key"],
+ ),
+ workbench=WorkbenchConfig(
+ url="https://workbench.example.com",
+ api_key=SECRETS["workbench.api_key"],
+ git_test=GitTestConfig(
+ clone_url=(
+ f"https://{SECRETS['clone_url user']}@github.com/org/repo.git"
+ f"?access_token={SECRETS['clone_url query']}"
+ ),
+ token=SECRETS["workbench.git_test.token"],
+ ),
+ ),
+ package_manager=PackageManagerConfig(
+ url="https://pm.example.com", token=SECRETS["package_manager.token"]
+ ),
+ auth=AuthConfig(username="alice", password=SECRETS["auth.password"]),
+ data_sources=[
+ DataSourceEntry(
+ name="pg",
+ type="postgres",
+ connection_string=(
+ f"postgresql://dbuser:{SECRETS['connection_string password']}@db:5432/app"
+ ),
+ )
+ ],
+ proxy=ProxyConfig(url=f"http://puser:{SECRETS['proxy.url password']}@proxy:8080"),
+ )
+
+
+def _walk_config_fields(cls: Any, prefix: str = "") -> set[str]:
+ """Return the dotted path of every leaf field of a config dataclass.
+
+ This helper lives in the test, not in the module, so that the test does
+ not trust the code that it checks. config.py uses postponed annotations,
+ so the field types come from typing.get_type_hints.
+ """
+ hints = typing.get_type_hints(cls)
+ paths: set[str] = set()
+ for f in dataclasses.fields(cls):
+ hint = hints[f.name]
+ args = typing.get_args(hint)
+ origin = typing.get_origin(hint)
+ path = prefix + f.name
+ if dataclasses.is_dataclass(hint):
+ paths |= _walk_config_fields(hint, path + ".")
+ elif origin is list and args and dataclasses.is_dataclass(args[0]):
+ paths |= _walk_config_fields(args[0], path + "[].")
+ elif origin in (typing.Union, types.UnionType) and any(
+ dataclasses.is_dataclass(a) for a in args
+ ):
+ inner = next(a for a in args if dataclasses.is_dataclass(a))
+ paths |= _walk_config_fields(inner, path + ".")
+ else:
+ paths.add(path)
+ return paths
+
+
+# Summary keys whose value is a plain dict, not a nested section.
+_DICT_VALUED_KEYS = {"node_pool_profiles", "profile_cpu_limit", "profile_memory_limit_gib"}
+
+
+def _summary_paths(node: dict, prefix: str = "") -> set[str]:
+ """Return the dotted path of every leaf key in a summary, without ``*_set`` keys."""
+ paths: set[str] = set()
+ for key, value in node.items():
+ path = prefix + key
+ if key.endswith("_set"):
+ continue
+ if isinstance(value, dict) and key not in _DICT_VALUED_KEYS:
+ paths |= _summary_paths(value, path + ".")
+ elif isinstance(value, list) and value and isinstance(value[0], dict):
+ paths |= _summary_paths(value[0], path + "[].")
+ else:
+ paths.add(path)
+ return paths
+
+
+def test_every_config_field_is_classified():
+ assert _walk_config_fields(VIPConfig) == SUMMARIZED_FIELDS | EXCLUDED_FIELDS
+ assert not (SUMMARIZED_FIELDS & EXCLUDED_FIELDS)
+ assert SECRET_FIELDS <= EXCLUDED_FIELDS
+
+
+def test_summary_has_exactly_the_summarized_fields(full_config):
+ assert _summary_paths(build_config_summary(full_config)) == SUMMARIZED_FIELDS
+
+
+def _config_classes(cls: Any, prefix: str = "") -> list[tuple[Any, str]]:
+ """Return ``(class, dotted prefix)`` for *cls* and every config dataclass under it."""
+ found = [(cls, prefix)]
+ hints = typing.get_type_hints(cls)
+ for f in dataclasses.fields(cls):
+ hint = hints[f.name]
+ args = typing.get_args(hint)
+ inner = next((a for a in (hint, *args) if dataclasses.is_dataclass(a)), None)
+ if inner is not None:
+ suffix = "[]." if typing.get_origin(hint) is list else "."
+ found += _config_classes(inner, prefix + f.name + suffix)
+ return found
+
+
+# Field values that __post_init__ needs before the other fields keep their value.
+_REQUIRED_VALUES = {GitTestConfig: {"auth_method": "https-token"}}
+
+
+def test_every_repr_masked_field_is_a_secret():
+ # A config class that hides a field in __repr__ treats it as a secret.
+ # Every str field gets a distinct sentinel, so a new hidden field is found.
+ classes = _config_classes(VIPConfig)
+ known = {ConnectConfig, WorkbenchConfig, GitTestConfig, PackageManagerConfig, AuthConfig}
+ assert known | {DataSourceEntry} <= {cls for cls, _ in classes}
+ hidden: set[str] = set()
+ for cls, prefix in classes:
+ hints = typing.get_type_hints(cls)
+ values = {
+ f.name: f"SENTINEL-{prefix}{f.name}"
+ for f in dataclasses.fields(cls)
+ if f.init and hints[f.name] is str
+ }
+ values.update(_REQUIRED_VALUES.get(cls, {}))
+ text = repr(cls(**values))
+ hidden |= {
+ prefix + name
+ for name, value in values.items()
+ if value.startswith("SENTINEL-") and value not in text
+ }
+ assert hidden == SECRET_FIELDS
+
+
+_SET_ONE_SECRET = {
+ "connect.api_key": lambda v: VIPConfig(connect=ConnectConfig(api_key=v)),
+ "workbench.api_key": lambda v: VIPConfig(workbench=WorkbenchConfig(api_key=v)),
+ "workbench.git_test.token": lambda v: VIPConfig(
+ workbench=WorkbenchConfig(git_test=GitTestConfig(token=v))
+ ),
+ "package_manager.token": lambda v: VIPConfig(package_manager=PackageManagerConfig(token=v)),
+ "auth.password": lambda v: VIPConfig(auth=AuthConfig(password=v)),
+ "data_sources[].connection_string": lambda v: VIPConfig(
+ data_sources=[DataSourceEntry(name="db", connection_string=v)]
+ ),
+}
+
+
+@pytest.mark.parametrize("path", sorted(SECRET_FIELDS))
+def test_each_secret_field_is_in_secret_values(path):
+ # A new entry in SECRET_FIELDS needs a builder here, and secret_values must return it.
+ config = _SET_ONE_SECRET[path]("SENTINEL-VALUE-0123")
+ assert "SENTINEL-VALUE-0123" in {value for _, value in secret_values(config)}
+
+
+def test_every_secret_field_has_a_builder():
+ assert set(_SET_ONE_SECRET) == SECRET_FIELDS
+
+
+def test_summary_has_no_secret_values(full_config):
+ text = json.dumps(build_config_summary(full_config))
+ for value in SECRETS.values():
+ assert value not in text
+ assert "cuser" not in text
+ assert "puser" not in text
+ assert "dbuser" not in text
+ assert "alice" not in text
+
+
+def test_summary_reports_secret_presence(full_config):
+ s = build_config_summary(full_config)
+ assert s["connect"]["api_key_set"] is True
+ assert s["workbench"]["api_key_set"] is True
+ assert s["package_manager"]["token_set"] is True
+ assert s["auth"]["password_set"] is True
+ assert s["workbench"]["git_test"]["token_set"] is True
+ assert s["data_sources"] == [{"name": "pg", "type": "postgres", "connection_string_set": True}]
+ assert "username" not in s["auth"]
+
+
+def test_summary_keeps_clone_url_path(full_config):
+ s = build_config_summary(full_config)
+ assert s["workbench"]["git_test"]["clone_url"] == "https://github.com/org/repo.git"
+ assert s["proxy"]["url"] == "http://proxy:8080"
+ assert s["connect"]["url"] == "https://connect.example.com"
+
+
+def test_summary_without_git_test():
+ assert build_config_summary(VIPConfig())["workbench"]["git_test"] is None
+
+
+@pytest.mark.parametrize(
+ ("url", "expected"),
+ [
+ (
+ "https://x-access-token:SECRET@github.com/org/repo.git",
+ "https://github.com/org/repo.git",
+ ),
+ ("https://TOKEN@github.com/org/repo.git", "https://github.com/org/repo.git"),
+ ("https://github.com/org/repo.git", "https://github.com/org/repo.git"),
+ ("https://github.com/org/repo.git?access_token=T#frag", "https://github.com/org/repo.git"),
+ ("http://u:p@[::1]:8080/x", "http://[::1]:8080/x"),
+ ("x-access-token:SECRET@github.com/org/repo.git", "github.com/org/repo.git"),
+ ("git@github.com:org/repo.git", "git@github.com:org/repo.git"),
+ ("", ""),
+ ],
+)
+def test_strip_url_userinfo(url, expected):
+ assert strip_url_userinfo(url) == expected
+
+
+def test_secret_values_cover_every_source(full_config, tmp_path):
+ meta = tmp_path / ".vip-auth-cache.meta.json"
+ meta.write_text(json.dumps({"api_key": "MINTED-KEY-0001"}), encoding="utf-8")
+ state = tmp_path / ".vip-auth-cache.json"
+ state.write_text(
+ json.dumps(
+ {
+ "cookies": [
+ {"name": "session", "value": "COOKIE-VALUE-0123456789"},
+ {"name": "flag", "value": "1"},
+ ],
+ "origins": [
+ {
+ "origin": "https://connect.example.com",
+ "localStorage": [{"name": "token", "value": "LOCAL-STORAGE-0123456789"}],
+ }
+ ],
+ }
+ ),
+ encoding="utf-8",
+ )
+ cache = auth_cache_secrets(state) + auth_cache_secrets(meta)
+ values = {value for _, value in secret_values(full_config, cache)}
+ assert set(SECRETS.values()) <= values
+ assert {"MINTED-KEY-0001", "COOKIE-VALUE-0123456789", "LOCAL-STORAGE-0123456789"} <= values
+ assert full_config.data_sources[0].connection_string in values
+ assert "1" not in values
+ assert "" not in values
+ # A user name next to a password is not a credential, except in the Git clone URL.
+ assert not {"cuser", "puser", "dbuser"} & values
+
+
+@pytest.mark.parametrize(
+ "clone_url",
+ [
+ "https://GIT-TOKEN-0123456789:x-oauth-basic@github.com/org/repo.git",
+ "https://GIT-TOKEN-0123456789:@github.com/org/repo.git",
+ "x-access-token:GIT-TOKEN-0123456789@github.com/org/repo.git",
+ ],
+)
+def test_secret_values_find_the_clone_url_token_in_each_form(clone_url):
+ config = VIPConfig(workbench=WorkbenchConfig(git_test=GitTestConfig(clone_url=clone_url)))
+ assert "GIT-TOKEN-0123456789" in {value for _, value in secret_values(config)}
+ assert "GIT-TOKEN" not in json.dumps(build_config_summary(config))
+
+
+def test_secret_values_labels_name_the_source(full_config):
+ labels = {value: label for label, value in secret_values(full_config)}
+ assert labels[SECRETS["auth.password"]] == "auth.password"
+ assert labels[SECRETS["clone_url user"]] == "workbench.git_test.clone_url credentials"
+
+
+def test_secret_values_read_env_only_secrets(monkeypatch):
+ monkeypatch.setenv("VIP_GIT_TOKEN", "ENV-GIT-TOKEN")
+ monkeypatch.setenv("VIP_TEST_TOTP_SECRET", "ENV-TOTP-SEED")
+ monkeypatch.setenv("HTTPS_PROXY", "http://u:ENV-PROXY-PASS@proxy:3128")
+ monkeypatch.setenv("VIP_CONNECT_API_KEY", "ENV-CONNECT-KEY")
+ values = {value for _, value in secret_values(VIPConfig())}
+ assert {"ENV-GIT-TOKEN", "ENV-TOTP-SEED", "ENV-PROXY-PASS", "ENV-CONNECT-KEY"} <= values
+
+
+def test_secret_values_split_key_value_connection_string():
+ conn = (
+ "Driver=Snowflake;Server=db;Uid=sa;Pwd=ODBC-PASS;token=SNOW-TOKEN-01;"
+ "AccountKey=AZURE-KEY-01==;Password={pa;ss-0123};"
+ )
+ config = VIPConfig(data_sources=[DataSourceEntry(name="db", connection_string=conn)])
+ labels = {value: label for label, value in secret_values(config)}
+ assert labels[conn] == "data_sources.db.connection_string"
+ assert labels["ODBC-PASS"] == "data_sources.db.connection_string pwd"
+ assert labels["SNOW-TOKEN-01"] == "data_sources.db.connection_string token"
+ assert labels["AZURE-KEY-01=="] == "data_sources.db.connection_string accountkey"
+ assert labels["pa;ss-0123"] == "data_sources.db.connection_string password"
+ assert "sa" not in labels
+
+
+def test_auth_cache_secrets_skip_missing_or_bad_files(tmp_path):
+ bad = tmp_path / "bad.json"
+ bad.write_text("not json", encoding="utf-8")
+ listed = tmp_path / "list.json"
+ listed.write_text("[]", encoding="utf-8")
+ for path in (bad, listed, tmp_path / "absent.json"):
+ assert auth_cache_secrets(path) == []
diff --git a/src/vip/bundle/__init__.py b/src/vip/bundle/__init__.py
new file mode 100644
index 000000000..3eaa65130
--- /dev/null
+++ b/src/vip/bundle/__init__.py
@@ -0,0 +1 @@
+"""Build a ``vip bundle`` zip: report files and a configuration summary, with no credentials."""
diff --git a/src/vip/bundle/summary.py b/src/vip/bundle/summary.py
new file mode 100644
index 000000000..cbbc9c6f1
--- /dev/null
+++ b/src/vip/bundle/summary.py
@@ -0,0 +1,390 @@
+"""Allowlisted configuration summary, and the secret values that the bundle must not hold."""
+
+from __future__ import annotations
+
+import json
+import os
+import re
+from collections.abc import Sequence
+from pathlib import Path
+from typing import Any
+from urllib.parse import parse_qsl, unquote, urlsplit, urlunsplit
+
+from vip.config import VIPConfig
+
+# Dotted paths of every VIPConfig field. selftests/test_bundle_summary.py walks
+# VIPConfig and fails when a field is in neither set, so a new field must be
+# classified before it can ship.
+SECRET_FIELDS = frozenset(
+ {
+ "connect.api_key",
+ "workbench.api_key",
+ "workbench.git_test.token",
+ "package_manager.token",
+ "auth.password",
+ "data_sources[].connection_string",
+ }
+)
+EXCLUDED_FIELDS = SECRET_FIELDS | frozenset(
+ {
+ "auth.username",
+ "connect.url_scheme_inferred",
+ "workbench.url_scheme_inferred",
+ "package_manager.url_scheme_inferred",
+ }
+)
+SUMMARIZED_FIELDS = frozenset(
+ {
+ "deployment_name",
+ "extension_dirs",
+ "connect.enabled",
+ "connect.url",
+ "connect.version",
+ "connect.deploy_timeout",
+ "workbench.enabled",
+ "workbench.url",
+ "workbench.version",
+ "workbench.session_profiles",
+ "workbench.session_count",
+ "workbench.job_timeout",
+ "workbench.test_packages",
+ "workbench.idle_timeout_minutes",
+ "workbench.idle_grace_seconds",
+ "workbench.extensions.vscode",
+ "workbench.extensions.positron",
+ "workbench.extensions.jupyterlab",
+ "workbench.kubernetes.enabled",
+ "workbench.kubernetes.namespace",
+ "workbench.kubernetes.node_pool_profiles",
+ "workbench.kubernetes.max_sessions",
+ "workbench.kubernetes.profile_cpu_limit",
+ "workbench.kubernetes.profile_memory_limit_gib",
+ "workbench.git_test.clone_url",
+ "workbench.git_test.auth_method",
+ "workbench.chronicle_data_path",
+ "package_manager.enabled",
+ "package_manager.url",
+ "package_manager.version",
+ "auth.provider",
+ "auth.idp",
+ "runtimes.r_versions",
+ "runtimes.python_versions",
+ "runtimes.r_excluded_versions",
+ "runtimes.python_excluded_versions",
+ "performance.page_load_timeout",
+ "performance.download_timeout",
+ "performance.p95_response_time",
+ "performance.avg_response_time",
+ "performance.concurrent_requests",
+ "performance.disk_usage_max_pct",
+ "performance.memory_available_min_pct",
+ "performance.load_user_counts",
+ "performance.load_max_connections",
+ "performance.load_success_rate_threshold",
+ "performance.load_test_tool",
+ "performance.load_test_duration",
+ "performance.load_test_spawn_rate",
+ "data_sources[].name",
+ "data_sources[].type",
+ "email_enabled",
+ "monitoring_enabled",
+ "chronicle_enabled",
+ "security_policy_checks_enabled",
+ "insecure",
+ "ca_bundle",
+ "cert_expiry_warning_days",
+ "proxy.url",
+ "proxy.no_proxy",
+ "proxy.enabled",
+ "proxy.trust_env",
+ }
+)
+
+# Secrets that VIP reads from the environment and that VIPConfig() does not
+# always hold: VIPConfig() has git_test=None, and the TOTP seed is never in it.
+SECRET_ENV_VARS = ("VIP_GIT_TOKEN", "VIP_TEST_TOTP_SECRET")
+# httpx reads these when proxy.trust_env is true. They can carry user:pass.
+PROXY_ENV_VARS = (
+ "HTTP_PROXY",
+ "HTTPS_PROXY",
+ "ALL_PROXY",
+ "http_proxy",
+ "https_proxy",
+ "all_proxy",
+)
+# Query keys whose values are credentials, for example ?access_token= in a clone URL.
+SECRET_QUERY_KEYS = frozenset(
+ {"access_token", "token", "password", "pwd", "api_key", "apikey", "key", "secret", "sig"}
+)
+# Short cookie values ("1", "true") match normal report text. Session cookies are long.
+MIN_COOKIE_LENGTH = 16
+# Connection string keys whose values are credentials. ODBC, JDBC, and Azure use these.
+CONNECTION_SECRET_KEYS = SECRET_QUERY_KEYS | frozenset(
+ {"accountkey", "sharedaccesskey", "sharedaccesssignature", "privatekey", "private_key"}
+)
+
+_USERINFO_RE = re.compile(r"://([^/@]*)@")
+# A Git remote with no scheme, for example x-access-token:TOKEN@github.com/org/repo.git.
+# The userinfo must hold ":", so git@github.com:org/repo.git does not match.
+_BARE_USERINFO_RE = re.compile(r"^([^/@\s:]+:[^/@\s]*)@")
+# One key=value part of a connection string. A value in braces or quotes can hold ";".
+_KV_PART_RE = re.compile(
+ r"""(?:^|;)\s*([A-Za-z_][A-Za-z0-9_ ]*?)\s*=\s*(\{[^}]*\}|"[^"]*"|'[^']*'|[^;]*)"""
+)
+
+
+def strip_url_userinfo(url: str) -> str:
+ """Return *url* without userinfo, query, or fragment. Keep the scheme, host, port, and path.
+
+ The host comes from the raw netloc, not ``hostname``, so the brackets of
+ an IPv6 address stay. A Git remote with no scheme also loses its
+ ``user:token@`` part.
+ """
+ if not url:
+ return url
+ if "://" not in url:
+ return _BARE_USERINFO_RE.sub("", url).split("?", 1)[0].split("#", 1)[0]
+ try:
+ parts = urlsplit(url)
+ except ValueError:
+ stripped = _USERINFO_RE.sub("://", url)
+ return stripped.split("?", 1)[0].split("#", 1)[0]
+ host = parts.netloc.rpartition("@")[2]
+ return urlunsplit((parts.scheme, host, parts.path, "", ""))
+
+
+def _url_credentials(url: str, *, both_parts: bool = False) -> list[str]:
+ """Return the credential parts of *url*: userinfo and secret query values.
+
+ With ``user:password``, only the password is a credential, unless
+ *both_parts* is set. A Git remote can hold the token in either part, for
+ example ``https://:x-oauth-basic@host/...``. With an empty
+ password, the user part is the credential, because a Git token is often
+ written as ``https://@host/...``. Each value is returned raw and
+ percent-decoded.
+ """
+ if not url:
+ return []
+ query: list[tuple[str, str]] = []
+ if "://" not in url:
+ match = _BARE_USERINFO_RE.match(url)
+ if match is None:
+ return []
+ userinfo = match.group(1)
+ else:
+ try:
+ parts = urlsplit(url)
+ query = parse_qsl(parts.query)
+ userinfo = parts.netloc.rpartition("@")[0]
+ except ValueError:
+ match = _USERINFO_RE.search(url)
+ userinfo = match.group(1) if match else ""
+ user, _, password = userinfo.partition(":")
+ values = [user, password] if both_parts else [password or user]
+ values += [value for key, value in query if key.lower() in SECRET_QUERY_KEYS]
+ return [v for value in values for v in (value, unquote(value)) if v]
+
+
+def _connection_string_parts(conn: str) -> list[tuple[str, str]]:
+ """Return ``(key, value)`` for each credential part of a key=value connection string."""
+ parts: list[tuple[str, str]] = []
+ for key, raw in _KV_PART_RE.findall(conn):
+ name = key.strip().lower().replace(" ", "")
+ value = raw.strip()
+ if len(value) >= 2 and value[0] in "{\"'":
+ value = value[1:-1]
+ if name in CONNECTION_SECRET_KEYS and value:
+ parts.append((name, value))
+ return parts
+
+
+def _connection_string_secrets(label: str, conn: str) -> list[tuple[str, str]]:
+ """Return the whole connection string and each credential part of it, with labels."""
+ pairs = [(label, conn)]
+ pairs += [(f"{label} credentials", v) for v in _url_credentials(conn)]
+ pairs += [(f"{label} {key}", value) for key, value in _connection_string_parts(conn)]
+ return pairs
+
+
+def auth_cache_secrets(path: Path) -> list[tuple[str, str]]:
+ """Return ``(label, value)`` for each secret in one auth cache file.
+
+ ``.vip-auth-cache.meta.json`` holds the minted Connect key.
+ ``.vip-auth-cache.json`` is a Playwright storage state with cookies and
+ localStorage values. A file that is missing or does not parse gives no values.
+ """
+ try:
+ data = json.loads(path.read_text(encoding="utf-8"))
+ except (OSError, ValueError):
+ return []
+ if not isinstance(data, dict):
+ return []
+ pairs: list[tuple[str, str]] = []
+ if isinstance(data.get("api_key"), str):
+ pairs.append(("auth cache api_key", data["api_key"]))
+ for cookie in data.get("cookies") or []:
+ value = cookie.get("value") if isinstance(cookie, dict) else None
+ if isinstance(value, str) and len(value) >= MIN_COOKIE_LENGTH:
+ pairs.append((f"auth cache cookie {cookie.get('name', '')}", value))
+ for origin in data.get("origins") or []:
+ items = origin.get("localStorage") if isinstance(origin, dict) else None
+ for item in items or []:
+ value = item.get("value") if isinstance(item, dict) else None
+ if isinstance(value, str) and len(value) >= MIN_COOKIE_LENGTH:
+ pairs.append((f"auth cache localStorage {item.get('name', '')}", value))
+ return pairs
+
+
+def secret_values(
+ config: VIPConfig, cache_secrets: Sequence[tuple[str, str]] = ()
+) -> list[tuple[str, str]]:
+ """Return ``(label, value)`` for every credential that the bundle must not hold.
+
+ *cache_secrets* are the pairs that :func:`auth_cache_secrets` read. The
+ label names the source of the value, so an error can name it without
+ printing the value. Empty values and repeated values are dropped.
+ """
+ pairs: list[tuple[str, str]] = [
+ ("connect.api_key", config.connect.api_key),
+ ("workbench.api_key", config.workbench.api_key),
+ ("package_manager.token", config.package_manager.token),
+ ("auth.password", config.auth.password),
+ ]
+ git = config.workbench.git_test
+ if git is not None:
+ pairs.append(("workbench.git_test.token", git.token))
+ label = "workbench.git_test.clone_url credentials"
+ pairs += [(label, v) for v in _url_credentials(git.clone_url, both_parts=True)]
+ for ds in config.data_sources:
+ pairs += _connection_string_secrets(
+ f"data_sources.{ds.name}.connection_string", ds.connection_string
+ )
+ for product in ("connect", "workbench", "package_manager"):
+ url = config.product_config(product).url
+ pairs += [(f"{product}.url credentials", v) for v in _url_credentials(url)]
+ pairs += [("proxy.url credentials", v) for v in _url_credentials(config.proxy.url)]
+ pairs += [(var, os.environ.get(var, "")) for var in SECRET_ENV_VARS]
+ for var in PROXY_ENV_VARS:
+ pairs += [(f"{var} credentials", v) for v in _url_credentials(os.environ.get(var, ""))]
+ pairs += list(cache_secrets)
+
+ seen: set[str] = set()
+ result: list[tuple[str, str]] = []
+ for label, value in pairs:
+ if value and value not in seen:
+ seen.add(value)
+ result.append((label, value))
+ return result
+
+
+def build_config_summary(config: VIPConfig) -> dict[str, Any]:
+ """Return the configuration summary for the bundle.
+
+ Each output key is named here. A field that is not named is not in the
+ summary. A secret field gives only a ``_set`` bool. Every URL goes
+ through :func:`strip_url_userinfo`.
+ """
+ connect = config.connect
+ wb = config.workbench
+ git = wb.git_test
+ k8s = wb.kubernetes
+ pm = config.package_manager
+ perf = config.performance
+ runtimes = config.runtimes
+ return {
+ "deployment_name": config.deployment_name,
+ "extension_dirs": list(config.extension_dirs),
+ "connect": {
+ "enabled": connect.enabled,
+ "url": strip_url_userinfo(connect.url),
+ "version": connect.version,
+ "deploy_timeout": connect.deploy_timeout,
+ "api_key_set": bool(connect.api_key),
+ },
+ "workbench": {
+ "enabled": wb.enabled,
+ "url": strip_url_userinfo(wb.url),
+ "version": wb.version,
+ "api_key_set": bool(wb.api_key),
+ "session_profiles": wb.session_profiles,
+ "session_count": wb.session_count,
+ "job_timeout": wb.job_timeout,
+ "test_packages": list(wb.test_packages),
+ "idle_timeout_minutes": wb.idle_timeout_minutes,
+ "idle_grace_seconds": wb.idle_grace_seconds,
+ "extensions": {
+ "vscode": list(wb.extensions.vscode),
+ "positron": list(wb.extensions.positron),
+ "jupyterlab": list(wb.extensions.jupyterlab),
+ },
+ "kubernetes": {
+ "enabled": k8s.enabled,
+ "namespace": k8s.namespace,
+ "node_pool_profiles": dict(k8s.node_pool_profiles),
+ "max_sessions": k8s.max_sessions,
+ "profile_cpu_limit": dict(k8s.profile_cpu_limit),
+ "profile_memory_limit_gib": dict(k8s.profile_memory_limit_gib),
+ },
+ "git_test": None
+ if git is None
+ else {
+ "clone_url": strip_url_userinfo(git.clone_url),
+ "auth_method": git.auth_method,
+ "token_set": bool(git.token),
+ },
+ "chronicle_data_path": wb.chronicle_data_path,
+ },
+ "package_manager": {
+ "enabled": pm.enabled,
+ "url": strip_url_userinfo(pm.url),
+ "version": pm.version,
+ "token_set": bool(pm.token),
+ },
+ "auth": {
+ "provider": config.auth.provider,
+ "idp": config.auth.idp,
+ "password_set": bool(config.auth.password),
+ },
+ "runtimes": {
+ "r_versions": list(runtimes.r_versions),
+ "python_versions": list(runtimes.python_versions),
+ "r_excluded_versions": list(runtimes.r_excluded_versions),
+ "python_excluded_versions": list(runtimes.python_excluded_versions),
+ },
+ "performance": {
+ "page_load_timeout": perf.page_load_timeout,
+ "download_timeout": perf.download_timeout,
+ "p95_response_time": perf.p95_response_time,
+ "avg_response_time": perf.avg_response_time,
+ "concurrent_requests": perf.concurrent_requests,
+ "disk_usage_max_pct": perf.disk_usage_max_pct,
+ "memory_available_min_pct": perf.memory_available_min_pct,
+ "load_user_counts": list(perf.load_user_counts),
+ "load_max_connections": perf.load_max_connections,
+ "load_success_rate_threshold": perf.load_success_rate_threshold,
+ "load_test_tool": perf.load_test_tool,
+ "load_test_duration": perf.load_test_duration,
+ "load_test_spawn_rate": perf.load_test_spawn_rate,
+ },
+ "data_sources": [
+ {
+ "name": ds.name,
+ "type": ds.type,
+ "connection_string_set": bool(ds.connection_string),
+ }
+ for ds in config.data_sources
+ ],
+ "email_enabled": config.email_enabled,
+ "monitoring_enabled": config.monitoring_enabled,
+ "chronicle_enabled": config.chronicle_enabled,
+ "security_policy_checks_enabled": config.security_policy_checks_enabled,
+ "insecure": config.insecure,
+ "ca_bundle": str(config.ca_bundle) if config.ca_bundle is not None else None,
+ "cert_expiry_warning_days": config.cert_expiry_warning_days,
+ "proxy": {
+ "url": strip_url_userinfo(config.proxy.url),
+ "no_proxy": list(config.proxy.no_proxy),
+ "enabled": config.proxy.enabled,
+ "trust_env": config.proxy.trust_env,
+ },
+ }
From 55c6435216649227c1d39b2b83fa7b03661aacbf Mon Sep 17 00:00:00 2001
From: Tim
Date: Fri, 6 Nov 2026 10:10:29 -0500
Subject: [PATCH 02/11] feat(bundle): collect allowlisted report files and scan
them for secrets
---
selftests/test_bundle_archive.py | 315 +++++++++++++++++++++++++++++++
selftests/test_cli_report.py | 58 ++++++
selftests/test_errors.py | 10 +-
src/vip/bundle/archive.py | 253 +++++++++++++++++++++++++
src/vip/bundle/stamp.py | 62 ++++++
src/vip/cli/report.py | 22 +++
src/vip/errors.py | 4 +
7 files changed, 723 insertions(+), 1 deletion(-)
create mode 100644 selftests/test_bundle_archive.py
create mode 100644 src/vip/bundle/archive.py
create mode 100644 src/vip/bundle/stamp.py
diff --git a/selftests/test_bundle_archive.py b/selftests/test_bundle_archive.py
new file mode 100644
index 000000000..6a460201b
--- /dev/null
+++ b/selftests/test_bundle_archive.py
@@ -0,0 +1,315 @@
+"""Tests for the ``vip bundle`` archive builder and secret scan."""
+
+from __future__ import annotations
+
+import json
+import os
+import shutil
+import zipfile
+from pathlib import Path
+
+import pytest
+
+from vip.bundle.archive import (
+ collect_members,
+ find_secret_hits,
+ short_secret_labels,
+ write_zip,
+)
+from vip.bundle.stamp import STAMP_NAME, record_rendered, results_digest
+from vip.errors import BundleError
+
+_SIDECARS = ("failures.json", "junit.xml", "results.sarif")
+_RENDERED = ("index.html", "details.html", "vip-report.pdf")
+_RESULTS = json.dumps({"deployment_name": "Acme", "results": []})
+
+
+def _write(path: Path, text: str, mtime: float) -> None:
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(text, encoding="utf-8")
+ os.utime(path, (mtime, mtime))
+
+
+def _seed(
+ report: Path,
+ sidecars=_SIDECARS,
+ rendered=_RENDERED,
+ results: str = _RESULTS,
+ results_mtime: float = 1_000_000.0,
+) -> None:
+ """Write a report directory as ``vip verify`` and ``vip report`` leave it.
+
+ Each sidecar is one second newer than results.json. The render stamp
+ names each rendered file and the digest of results.json.
+ """
+ _write(report / "results.json", results, results_mtime)
+ for name in sidecars:
+ _write(report / name, f"content of {name}", results_mtime + 1)
+ for name in rendered:
+ _write(report / "_output" / name, f"content of {name}", results_mtime + 2)
+ if rendered:
+ record_rendered(report / "_output", results_digest(results.encode()), rendered)
+
+
+def _all_members() -> set[str]:
+ return (
+ {"report/results.json"}
+ | {f"report/{n}" for n in _SIDECARS}
+ | {f"report/_output/{n}" for n in _RENDERED}
+ )
+
+
+def test_collects_only_allowlisted_files(tmp_path):
+ report = tmp_path / "report"
+ _seed(report)
+ for name in ("notes.txt", "index.qmd", "_output/search.json", "_output/site_libs/b.css"):
+ _write(report / name, "x", 2_000_000.0)
+ got = collect_members(report / "results.json", report)
+ assert set(got.members) == _all_members()
+ assert got.missing == []
+ assert got.left_out == []
+ assert got.warnings == []
+ assert got.sources["report/failures.json"] == report / "failures.json"
+
+
+def test_optional_files_absent_is_not_an_error(tmp_path):
+ report = tmp_path / "report"
+ _seed(report, sidecars=(), rendered=())
+ got = collect_members(report / "results.json", report)
+ assert set(got.members) == {"report/results.json"}
+ assert "report/failures.json" in got.missing
+ assert "report/_output/vip-report.pdf" in got.missing
+ assert any("Run 'vip report'" in w for w in got.warnings)
+
+
+def test_missing_results_raises(tmp_path):
+ with pytest.raises(BundleError, match=r"results\.json"):
+ collect_members(tmp_path / "report/results.json", tmp_path / "report")
+
+
+@pytest.mark.parametrize("name", ["vip.toml", ".vip-auth-cache.json", ".vip-install.json"])
+def test_results_cannot_name_a_credential_file(tmp_path, name):
+ path = tmp_path / name
+ path.write_text('{"results": []}', encoding="utf-8")
+ with pytest.raises(BundleError, match="is not a results file"):
+ collect_members(path, tmp_path / "report")
+
+
+@pytest.mark.parametrize("text", ["api_key = 'x'", "[]", '{"no_results": 1}'])
+def test_results_must_be_a_vip_results_file(tmp_path, text):
+ path = tmp_path / "results.json"
+ path.write_text(text, encoding="utf-8")
+ with pytest.raises(BundleError, match="is not a VIP results file"):
+ collect_members(path, tmp_path / "report")
+
+
+def test_render_from_other_results_is_left_out(tmp_path):
+ # The user ran vip verify again but not vip report.
+ report = tmp_path / "report"
+ _seed(report)
+ _write(report / "results.json", json.dumps({"results": [{"new": 1}]}), 3_000_000.0)
+ for name in _SIDECARS:
+ os.utime(report / name, (3_000_001.0, 3_000_001.0))
+ got = collect_members(report / "results.json", report)
+ assert not any(name.startswith("report/_output/") for name in got.members)
+ assert {item["name"] for item in got.left_out} == {f"report/_output/{n}" for n in _RENDERED}
+ assert all(item["reason"].startswith("was not made from") for item in got.left_out)
+ assert sum("Run 'vip report' again" in w for w in got.warnings) == 3
+
+
+def test_copied_results_with_an_old_time_does_not_let_in_an_old_render(tmp_path):
+ # vip report --results copies with shutil.copy2, which keeps the old file
+ # time. If the PDF step then fails, the PDF of the earlier run stays and
+ # is newer than results.json. Only the stamp shows that it is foreign.
+ report = tmp_path / "report"
+ _seed(report, results_mtime=2_000_000.0)
+ ci = tmp_path / "ci-results.json"
+ _write(ci, json.dumps({"results": [{"ci": 1}]}), 1_500_000.0)
+ shutil.copy2(ci, report / "results.json")
+ record_rendered(report / "_output", results_digest(ci.read_bytes()), ("index.html",))
+ got = collect_members(report / "results.json", report)
+ assert "report/_output/index.html" in got.members
+ assert "report/_output/vip-report.pdf" not in got.members
+ assert "report/_output/details.html" not in got.members
+
+
+def test_results_outside_report_dir_keeps_render_with_the_same_content(tmp_path):
+ report = tmp_path / "report"
+ _seed(report)
+ other = tmp_path / "out" / "run1.json"
+ _write(other, _RESULTS, 1_000_000.0)
+ got = collect_members(other, report)
+ assert {f"report/_output/{n}" for n in _RENDERED} <= set(got.members)
+
+
+def test_results_outside_report_dir_leaves_out_other_render(tmp_path):
+ report = tmp_path / "report"
+ _seed(report)
+ other = tmp_path / "other" / "results.json"
+ _write(other, json.dumps({"results": [{"other": 1}]}), 2_000_000.0)
+ got = collect_members(other, report)
+ assert set(got.members) == {"report/results.json"}
+ assert any(f"was not made from {other}" in w for w in got.warnings)
+
+
+def test_render_without_a_stamp_is_left_out(tmp_path):
+ # A render from a vip version that writes no stamp, or a render that failed.
+ report = tmp_path / "report"
+ _seed(report)
+ (report / "_output" / STAMP_NAME).unlink()
+ got = collect_members(report / "results.json", report)
+ assert not any(name.startswith("report/_output/") for name in got.members)
+
+
+def test_stale_sidecar_is_left_out(tmp_path):
+ # failures.json from an earlier run that failed. The new run passed, so
+ # vip verify did not write a new failures.json.
+ report = tmp_path / "report"
+ _seed(report)
+ os.utime(report / "failures.json", (999_000.0, 999_000.0))
+ got = collect_members(report / "results.json", report)
+ assert "report/failures.json" not in got.members
+ assert {"name": "report/failures.json", "reason": "is older than results.json"} in (
+ got.left_out
+ )
+ assert any(str(report / "failures.json") in w for w in got.warnings)
+
+
+@pytest.mark.skipif(os.name == "nt", reason="symlinks need extra rights on Windows")
+def test_symlinks_are_left_out(tmp_path):
+ report = tmp_path / "report"
+ _seed(report, sidecars=(), rendered=())
+ secret = tmp_path / "vip.toml"
+ secret.write_text("api_key = 'x'", encoding="utf-8")
+ (report / "failures.json").symlink_to(secret)
+ (report / "_output").mkdir()
+ (report / "_output" / "index.html").symlink_to(secret)
+ got = collect_members(report / "results.json", report)
+ assert set(got.members) == {"report/results.json"}
+ assert sum("is a link" in w for w in got.warnings) == 2
+
+
+@pytest.mark.skipif(os.name == "nt", reason="symlinks need extra rights on Windows")
+def test_symlinked_output_dir_is_left_out(tmp_path):
+ report = tmp_path / "report"
+ _seed(report, rendered=())
+ elsewhere = tmp_path / "elsewhere"
+ _seed(elsewhere)
+ (report / "_output").symlink_to(elsewhere / "_output")
+ got = collect_members(report / "results.json", report)
+ assert not any(name.startswith("report/_output/") for name in got.members)
+
+
+@pytest.mark.skipif(os.name == "nt", reason="symlinks need extra rights on Windows")
+def test_symlinked_results_raises(tmp_path):
+ report = tmp_path / "report"
+ report.mkdir()
+ real = tmp_path / "real.json"
+ real.write_text(_RESULTS, encoding="utf-8")
+ (report / "results.json").symlink_to(real)
+ with pytest.raises(BundleError, match="is a link"):
+ collect_members(report / "results.json", report)
+
+
+# Shorter than 16 characters, so only the full value is scanned. Each case
+# below then passes only through the encoded form that it names.
+SHORT = 'pa"ss&wörd-01'
+SPACED = "pass word-0123"
+BACKSLASH = "ab\\cd-Secret9"
+WRAPPED = "Wrap-Me-1234"
+# At least 16 characters, so the scan also looks for the first and the last 8.
+LONG = "K3y-Start-0123456789-a1b2c3d4e5"
+QUOTED = 'p"ss\\w0rd-ABCDEFGHIJ0123'
+PADDED = "QU1DREVGR0hJSktMTU5PUA=="
+
+
+@pytest.mark.parametrize(
+ ("secret", "stored"),
+ [
+ (SHORT, SHORT.encode()),
+ (SHORT, b'pa\\"ss&w\\u00f6rd-01'), # json.dumps with ensure_ascii
+ (SHORT, 'pa\\"ss&wörd-01'.encode()), # json.dumps without ensure_ascii
+ (SHORT, "pa"ss&wörd-01".encode()), # html.escape
+ (SHORT, 'pa"ss&wörd-01'.encode()), # html.escape with quote=False (XML text)
+ (SHORT, b"pa%22ss%26w%C3%B6rd-01"), # percent-encoded with quote
+ (SPACED, b"pass+word-0123"), # percent-encoded with quote_plus
+ (BACKSLASH, b"'ab\\\\cd-Secret9'"), # repr in error text
+ (BACKSLASH, json.dumps(repr(BACKSLASH)).encode()), # repr, then json.dumps
+ (WRAPPED, b"Wrap\n-Me-1234"), # wrapped across lines
+ (WRAPPED, b"Wrap\\n-Me-1234"), # wrapped, then json.dumps
+ (LONG, LONG[:10].encode()), # cut at a length limit
+ (LONG, b"..." + LONG[-12:].encode()), # cut in the middle by pytest saferepr
+ (QUOTED, json.dumps(QUOTED[:10])[1:-1].encode()), # cut, then json.dumps
+ (PADDED, PADDED[:12].encode()), # a value with "=" still gets a partial form
+ ],
+)
+def test_find_secret_hits_sees_encoded_forms(secret, stored):
+ members = {"report/a": b"x " + stored + b" y", "report/b": b"clean"}
+ assert find_secret_hits(members, [("auth.password", secret)]) == [("report/a", "auth.password")]
+
+
+def test_find_secret_hits_finds_nothing_in_clean_members():
+ members = {"report/a": b"nothing here", "report/b": b"clean"}
+ assert find_secret_hits(members, [("connect.api_key", "ABC123")]) == []
+
+
+@pytest.mark.parametrize(
+ ("conn", "text"),
+ [
+ ("Host=db01;Port=5432;Password=Xy9-secret-value", b"could not reach Host=db01:5432"),
+ ("pg15://dbuser:DB-PASSWORD-01@db:5432/app", b"engine pg15://..."),
+ ],
+)
+def test_partial_rule_skips_connection_strings(conn, text):
+ # The start of a whole connection string or URL is a key or a scheme, which
+ # is in normal error text. Its credential parts are separate secrets.
+ assert find_secret_hits({"report/a": text}, [("db", conn)]) == []
+
+
+def test_partial_rule_skips_the_start_of_a_jwt():
+ # Every JWT starts with eyJhbGci, which has no digit, so only the end of a
+ # JWT is a partial form.
+ jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ2aXAifQ.c2lnbmF0dXJlLX9hbH1l"
+ other = b"token eyJhbGciOiJSUzI1NiJ9.other"
+ assert find_secret_hits({"report/a": other}, [("package_manager.token", jwt)]) == []
+ tail = jwt[-10:].encode()
+ assert find_secret_hits({"report/a": tail}, [("package_manager.token", jwt)]) == [
+ ("report/a", "package_manager.token")
+ ]
+
+
+def test_partial_rule_skips_a_part_with_no_digit_or_no_letter():
+ # A password that ends in a word must not match every "password_set" key.
+ secrets = [("auth.password", "Company-Secret-password"), ("x", "abcdefgh-20261105")]
+ members = {"config-summary.json": b'"password_set": true, "date": "20261105"'}
+ assert find_secret_hits(members, secrets) == []
+
+
+def test_short_secrets_are_not_scanned_but_are_reported():
+ secrets = [("auth.password", "abc"), ("connect.api_key", "ABC123")]
+ assert find_secret_hits({"report/a": b"abc"}, secrets) == []
+ assert short_secret_labels(secrets) == ["auth.password"]
+
+
+def test_write_zip_writes_every_member(tmp_path):
+ out = tmp_path / "b.zip"
+ write_zip(out, {"y": b"2", "x": b"1"})
+ with zipfile.ZipFile(out) as zf:
+ assert zf.namelist() == ["x", "y"]
+ assert zf.read("y") == b"2"
+
+
+def test_write_zip_leaves_nothing_on_error(tmp_path, monkeypatch):
+ real_writestr = zipfile.ZipFile.writestr
+ calls: list[str] = []
+
+ def failing_writestr(self, name, data, *args, **kwargs):
+ calls.append(name)
+ if len(calls) == 2:
+ raise OSError("disk full")
+ return real_writestr(self, name, data, *args, **kwargs)
+
+ monkeypatch.setattr(zipfile.ZipFile, "writestr", failing_writestr)
+ with pytest.raises(OSError, match="disk full"):
+ write_zip(tmp_path / "b.zip", {"x": b"1", "y": b"2"})
+ assert list(tmp_path.iterdir()) == []
diff --git a/selftests/test_cli_report.py b/selftests/test_cli_report.py
index 7dad44313..6f31032e5 100644
--- a/selftests/test_cli_report.py
+++ b/selftests/test_cli_report.py
@@ -375,6 +375,64 @@ def _failing_html(cmd, cwd=None, **kwargs):
assert rendered == ["index.qmd"], "must stop at the first failed page"
+class TestRenderStamp:
+ """vip report ties each rendered file to the results.json it was made from."""
+
+ @staticmethod
+ def _report_dir(tmp_path, monkeypatch, results: str = '{"results": []}'):
+ monkeypatch.chdir(tmp_path)
+ report_dir = tmp_path / "report"
+ report_dir.mkdir(exist_ok=True)
+ (report_dir / "results.json").write_text(results, encoding="utf-8")
+ return report_dir
+
+ def test_stamp_names_every_rendered_file(self, tmp_path, monkeypatch):
+ from vip import cli
+ from vip.bundle.stamp import read_render_stamp, results_digest
+
+ report_dir = self._report_dir(tmp_path, monkeypatch)
+ monkeypatch.setattr("vip.cli.report.subprocess.run", _fake_quarto(create_output=True))
+ cli.run_report(_make_args())
+ digest = results_digest((report_dir / "results.json").read_bytes())
+ assert read_render_stamp(report_dir / "_output") == (
+ digest,
+ frozenset({"index.html", "details.html", "vip-report.pdf"}),
+ )
+
+ def test_failed_pdf_is_not_in_the_new_stamp(self, tmp_path, monkeypatch):
+ # The PDF of the earlier run stays on disk, but the stamp leaves it out.
+ from vip import cli
+ from vip.bundle.stamp import read_render_stamp
+
+ report_dir = self._report_dir(tmp_path, monkeypatch)
+ monkeypatch.setattr("vip.cli.report.subprocess.run", _fake_quarto(create_output=True))
+ cli.run_report(_make_args())
+ self._report_dir(tmp_path, monkeypatch, results='{"results": [{"n": 1}]}')
+ monkeypatch.setattr(
+ "vip.cli.report.subprocess.run", _fake_quarto(create_output=True, pdf_returncode=1)
+ )
+ cli.run_report(_make_args())
+ stamp = read_render_stamp(report_dir / "_output")
+ assert stamp is not None
+ assert stamp[1] == frozenset({"index.html", "details.html"})
+ assert (report_dir / "_output" / "vip-report.pdf").is_file()
+
+ def test_failed_html_clears_the_stamp(self, tmp_path, monkeypatch):
+ from vip import cli
+ from vip.bundle.stamp import read_render_stamp
+
+ report_dir = self._report_dir(tmp_path, monkeypatch)
+ monkeypatch.setattr("vip.cli.report.subprocess.run", _fake_quarto(create_output=True))
+ cli.run_report(_make_args())
+ monkeypatch.setattr(
+ "vip.cli.report.subprocess.run",
+ lambda cmd, cwd=None, **kwargs: types.SimpleNamespace(returncode=3),
+ )
+ with pytest.raises(SystemExit):
+ cli.run_report(_make_args())
+ assert read_render_stamp(report_dir / "_output") is None
+
+
class TestSupportFileResolution:
"""troubleshooting.toml and feature files resolve from checkout or install."""
diff --git a/selftests/test_errors.py b/selftests/test_errors.py
index 72b32b8f9..8c8665d39 100644
--- a/selftests/test_errors.py
+++ b/selftests/test_errors.py
@@ -13,6 +13,7 @@
AuthConfigError,
AuthError,
AuthTimeoutError,
+ BundleError,
ConfigError,
InstallError,
ProductUnreachableError,
@@ -33,7 +34,14 @@ def test_message_is_the_str_representation(self):
@pytest.mark.parametrize(
"cls",
- [ConfigError, AuthError, ProductUnreachableError, InstallError, ReportError],
+ [
+ ConfigError,
+ AuthError,
+ ProductUnreachableError,
+ InstallError,
+ ReportError,
+ BundleError,
+ ],
)
def test_subclasses_are_vip_errors(self, cls):
assert issubclass(cls, VipError)
diff --git a/src/vip/bundle/archive.py b/src/vip/bundle/archive.py
new file mode 100644
index 000000000..7b8b04bd7
--- /dev/null
+++ b/src/vip/bundle/archive.py
@@ -0,0 +1,253 @@
+"""Collect the allowlisted report files, scan them for secrets, and write the zip."""
+
+from __future__ import annotations
+
+import html
+import json
+import tempfile
+import zipfile
+from collections.abc import Sequence
+from dataclasses import dataclass, field
+from pathlib import Path
+from urllib.parse import quote, quote_plus
+
+from vip.bundle.stamp import read_render_stamp, results_digest
+from vip.errors import BundleError
+
+# Read from the directory of results.json. results.json must exist.
+REPORT_SIDECAR_FILES = ("results.json", "failures.json", "junit.xml", "results.sarif")
+# Read from /_output/. _quarto.yml sets embed-resources, so each
+# page is one self-contained file and no site_libs/ directory is needed.
+RENDERED_FILES = ("index.html", "details.html", "vip-report.pdf")
+# Files that hold credentials. --results must never name one of them.
+FORBIDDEN_NAMES = frozenset(
+ {"vip.toml", ".vip-auth-cache.json", ".vip-auth-cache.meta.json", ".vip-install.json"}
+)
+
+# A value shorter than this matches almost any file, so the scan skips it.
+MIN_SECRET_LENGTH = 4
+# Report text can hold only a part of a secret. failures.json and the error
+# parser cut long text, which keeps the start. pytest shortens a long repr in
+# the middle, which keeps the start and the end. So for each secret that is
+# at least PARTIAL_MIN_SECRET_LENGTH long, the scan also looks for its first
+# and its last PARTIAL_LENGTH characters, in each encoded form. A part with
+# no digit or no letter, such as "password" or "20261105", matches normal
+# report text, so the scan skips it. Random keys almost always have both. The
+# start of every JWT, "eyJhbGci", has no digit, so the scan uses only the end
+# of a JWT.
+PARTIAL_LENGTH = 8
+PARTIAL_MIN_SECRET_LENGTH = 16
+
+
+@dataclass
+class Collected:
+ """The report files for the bundle by archive name, and what was left out.
+
+ *sources* maps each archive name to the file on disk. *left_out* holds
+ ``{"name": ..., "reason": ...}`` for each file that exists but is not in
+ the bundle. *missing* holds the allowlisted files that do not exist.
+ """
+
+ members: dict[str, bytes] = field(default_factory=dict)
+ sources: dict[str, Path] = field(default_factory=dict)
+ missing: list[str] = field(default_factory=list)
+ left_out: list[dict[str, str]] = field(default_factory=list)
+ warnings: list[str] = field(default_factory=list)
+
+
+def _is_regular_file_in(path: Path, root: Path) -> bool:
+ """Return True if *path* is a regular file in *root*, and neither is a link."""
+ return (
+ not path.is_symlink()
+ and not path.parent.is_symlink()
+ and path.is_file()
+ and path.resolve().is_relative_to(root.resolve())
+ )
+
+
+def _read(path: Path) -> bytes:
+ """Read *path*. Turn a read error into BundleError."""
+ try:
+ return path.read_bytes()
+ except OSError as exc:
+ raise BundleError(f"could not read {path}: {exc}") from exc
+
+
+def _leave_out(out: Collected, archive_name: str, path: Path, reason: str, hint: str) -> None:
+ out.left_out.append({"name": archive_name, "reason": reason})
+ out.warnings.append(f"{path} is not in the bundle, because it {reason}.{hint}")
+
+
+def _check_results_file(results_path: Path) -> bytes:
+ """Return the bytes of *results_path*. Refuse a file that is not a VIP results file."""
+ if results_path.name in FORBIDDEN_NAMES:
+ raise BundleError(f"{results_path} is not a results file. Pass the results.json path.")
+ if results_path.is_symlink() or results_path.parent.is_symlink():
+ raise BundleError(f"{results_path} is a link. Pass the real path with --results PATH.")
+ if not results_path.is_file():
+ raise BundleError(
+ f"no results found at {results_path}. Run 'vip verify' first, or pass --results PATH."
+ )
+ data = _read(results_path)
+ try:
+ parsed = json.loads(data)
+ except ValueError:
+ parsed = None
+ if not isinstance(parsed, dict) or not isinstance(parsed.get("results"), list):
+ raise BundleError(
+ f"{results_path} is not a VIP results file. Pass the results.json that "
+ "'vip verify' wrote."
+ )
+ return data
+
+
+def collect_members(results_path: Path, report_dir: Path) -> Collected:
+ """Read the allowlisted report files.
+
+ Sidecar files come from the directory of *results_path*. A sidecar that is
+ older than results.json is from an earlier run, so it is left out.
+ Rendered files come from ``/_output/``. A rendered file is
+ included only if the render stamp names it and the SHA-256 of this
+ results.json. The secret scan cannot read the text in the PDF, so the PDF
+ is safe only when its source, results.json, is in the bundle and passes
+ the scan.
+ """
+ out = Collected()
+ data = _check_results_file(results_path)
+ out.members["report/results.json"] = data
+ out.sources["report/results.json"] = results_path
+ results_mtime = results_path.stat().st_mtime
+
+ sidecar_dir = results_path.parent
+ for name in REPORT_SIDECAR_FILES[1:]:
+ path, archive_name = sidecar_dir / name, f"report/{name}"
+ if not path.exists() and not path.is_symlink():
+ out.missing.append(archive_name)
+ elif not _is_regular_file_in(path, sidecar_dir):
+ _leave_out(out, archive_name, path, "is a link or is not a regular file", "")
+ elif path.stat().st_mtime < results_mtime:
+ hint = " If you copied the report directory without file times, run 'vip verify' again."
+ _leave_out(out, archive_name, path, "is older than results.json", hint)
+ else:
+ out.members[archive_name] = _read(path)
+ out.sources[archive_name] = path
+
+ output_dir = report_dir / "_output"
+ stamp = read_render_stamp(output_dir)
+ digest = results_digest(data)
+ hint = " Run 'vip report' again to include it."
+ for name in RENDERED_FILES:
+ path, archive_name = output_dir / name, f"report/_output/{name}"
+ if not path.exists() and not path.is_symlink():
+ out.missing.append(archive_name)
+ elif not _is_regular_file_in(path, output_dir):
+ _leave_out(out, archive_name, path, "is a link or is not a regular file", "")
+ elif stamp is None or stamp[0] != digest or name not in stamp[1]:
+ _leave_out(out, archive_name, path, f"was not made from {results_path}", hint)
+ else:
+ out.members[archive_name] = _read(path)
+ out.sources[archive_name] = path
+ if not any(name.startswith("report/_output/") for name in out.members):
+ out.warnings.append("the bundle has no rendered report. Run 'vip report' to include it.")
+ return out
+
+
+def _encode(value: str) -> set[str]:
+ """Return *value* in each form in which a report file can hold it.
+
+ results.json and failures.json use json.dumps with ensure_ascii. The HTML
+ pages escape markup characters. junit.xml escapes ``&`` and ``<`` in a
+ text node but keeps ``"``. URLs are percent-encoded. Error text formats
+ values with ``!r``, which doubles each backslash.
+ """
+ forms = {
+ value,
+ json.dumps(value)[1:-1],
+ json.dumps(value, ensure_ascii=False)[1:-1],
+ html.escape(value),
+ html.escape(value, quote=False),
+ quote(value, safe=""),
+ quote_plus(value, safe=""),
+ }
+ as_repr = repr(value)[1:-1]
+ forms |= {as_repr, json.dumps(as_repr)[1:-1]}
+ return forms
+
+
+def _is_compound(value: str) -> bool:
+ """Return True for a whole URL or connection string.
+
+ Its start is a scheme or a key such as ``Driver=``, which matches normal
+ text. Its credential parts are separate secrets with their own partial forms.
+ """
+ return "://" in value or (";" in value and "=" in value)
+
+
+def _is_distinct(part: str) -> bool:
+ """Return True if *part* has a digit and a letter, so it is unlikely in normal text."""
+ return any(c.isdigit() for c in part) and any(c.isalpha() for c in part)
+
+
+def _encoded_forms(value: str) -> set[bytes]:
+ """Return the byte forms of *value* and of its start and end, for the scan."""
+ forms = _encode(value)
+ if len(value) >= PARTIAL_MIN_SECRET_LENGTH and not _is_compound(value):
+ for part in (value[:PARTIAL_LENGTH], value[-PARTIAL_LENGTH:]):
+ if _is_distinct(part):
+ forms |= _encode(part)
+ return {form.encode("utf-8") for form in forms}
+
+
+def _flatten(data: bytes) -> bytes:
+ """Remove line breaks, raw or JSON-escaped, so that a wrapped secret is whole again."""
+ for line_break in (b"\\r", b"\\n", b"\r", b"\n"):
+ data = data.replace(line_break, b"")
+ return data
+
+
+def find_secret_hits(
+ members: dict[str, bytes], secrets: Sequence[tuple[str, str]]
+) -> list[tuple[str, str]]:
+ """Return sorted ``(member, label)`` pairs for each member that holds a secret.
+
+ Each member is also checked with its line breaks removed, because a
+ console log can wrap a long key across lines. Values shorter than
+ MIN_SECRET_LENGTH are not checked. See :func:`short_secret_labels`.
+ """
+ patterns = [
+ (label, _encoded_forms(value))
+ for label, value in secrets
+ if len(value) >= MIN_SECRET_LENGTH
+ ]
+ hits: set[tuple[str, str]] = set()
+ for name, data in members.items():
+ flat = _flatten(data)
+ for label, forms in patterns:
+ if any(form in data or form in flat for form in forms):
+ hits.add((name, label))
+ return sorted(hits)
+
+
+def short_secret_labels(secrets: Sequence[tuple[str, str]]) -> list[str]:
+ """Return the labels of the secrets that are too short for the scan."""
+ return sorted({label for label, value in secrets if 0 < len(value) < MIN_SECRET_LENGTH})
+
+
+def write_zip(output: Path, members: dict[str, bytes]) -> None:
+ """Write *members* to *output* through a temp file in the same directory.
+
+ The temp file replaces *output* only after every member is written. On
+ any error the temp file is deleted, so a failed run leaves no partial zip.
+ """
+ with tempfile.NamedTemporaryFile(
+ dir=output.parent, prefix=".vip-bundle-", suffix=".tmp", delete=False
+ ) as handle:
+ tmp = Path(handle.name)
+ try:
+ with zipfile.ZipFile(tmp, "w", compression=zipfile.ZIP_DEFLATED) as zf:
+ for name in sorted(members):
+ zf.writestr(name, members[name])
+ tmp.replace(output)
+ except BaseException:
+ tmp.unlink(missing_ok=True)
+ raise
diff --git a/src/vip/bundle/stamp.py b/src/vip/bundle/stamp.py
new file mode 100644
index 000000000..7a5ebc629
--- /dev/null
+++ b/src/vip/bundle/stamp.py
@@ -0,0 +1,62 @@
+"""Record which results.json each rendered report file was made from.
+
+``vip report`` writes the stamp after each render. ``vip bundle`` reads it and
+includes a rendered file only if the stamp names that file and the SHA-256 of
+the results.json in the bundle. File times cannot do this, because a copy with
+``shutil.copy2`` keeps the old time of the source file.
+"""
+
+from __future__ import annotations
+
+import hashlib
+import json
+from collections.abc import Iterable
+from pathlib import Path
+
+STAMP_NAME = ".vip-render.json"
+
+
+def results_digest(data: bytes) -> str:
+ """Return the SHA-256 hex digest of the bytes of a results.json file."""
+ return hashlib.sha256(data).hexdigest()
+
+
+def clear_render_stamp(output_dir: Path) -> None:
+ """Delete the stamp, so that a failed render leaves no stamp for old files."""
+ (output_dir / STAMP_NAME).unlink(missing_ok=True)
+
+
+def read_render_stamp(output_dir: Path) -> tuple[str, frozenset[str]] | None:
+ """Return the digest and the rendered file names in the stamp, or None.
+
+ A stamp that is missing, is a link, or does not parse gives None.
+ """
+ path = output_dir / STAMP_NAME
+ if path.is_symlink():
+ return None
+ try:
+ data = json.loads(path.read_text(encoding="utf-8"))
+ except (OSError, ValueError):
+ return None
+ if not isinstance(data, dict):
+ return None
+ digest = data.get("results_sha256")
+ files = data.get("files")
+ if not isinstance(digest, str) or not isinstance(files, list):
+ return None
+ return digest, frozenset(name for name in files if isinstance(name, str))
+
+
+def record_rendered(output_dir: Path, digest: str, names: Iterable[str]) -> None:
+ """Add *names* to the stamp for *digest*. A stamp for another digest is replaced."""
+ current = read_render_stamp(output_dir)
+ files = set(names)
+ if current is not None and current[0] == digest:
+ files |= current[1]
+ path = output_dir / STAMP_NAME
+ tmp = path.with_suffix(".tmp")
+ tmp.write_text(
+ json.dumps({"results_sha256": digest, "files": sorted(files)}, indent=2) + "\n",
+ encoding="utf-8",
+ )
+ tmp.replace(path)
diff --git a/src/vip/cli/report.py b/src/vip/cli/report.py
index a1661e510..6fa98b630 100644
--- a/src/vip/cli/report.py
+++ b/src/vip/cli/report.py
@@ -12,6 +12,7 @@
import webbrowser
from pathlib import Path
+from vip.bundle.stamp import clear_render_stamp, record_rendered, results_digest
from vip.errors import ReportError
# Quarto report template files copied into the working report/ directory.
@@ -160,6 +161,13 @@ def run_report(args: argparse.Namespace) -> None:
# is the vip install itself, which always has both. See issue #554.
env = {**os.environ, "QUARTO_PYTHON": sys.executable}
+ # vip bundle includes a rendered file only if the render stamp ties it to
+ # the results.json in the bundle. Clear the stamp first, so that a failed
+ # render leaves the old pages without a stamp.
+ output_dir = report_dir / "_output"
+ digest = results_digest(results_dest.read_bytes())
+ clear_render_stamp(output_dir)
+
# The HTML pages and the PDF render as separate quarto invocations on
# purpose. One combined `quarto render` ties their fates together: on a
# Quarto too old to know Typst (pre-1.4), the PDF document fails the
@@ -182,12 +190,14 @@ def run_report(args: argparse.Namespace) -> None:
)
print(f"Report generated: {output}")
+ _stamp(output_dir, digest, ("index.html", "details.html"))
# The PDF is the copy customers archive, so a missing one warns loudly —
# but never fails the command, and never blocks the HTML hand-off above.
pdf = report_dir / "_output" / "vip-report.pdf"
if _quarto_render("vip-report.qmd", report_dir, env) == 0 and pdf.exists():
print(f"PDF generated: {pdf}")
+ _stamp(output_dir, digest, ("vip-report.pdf",))
else:
print(
f"Warning: the HTML report rendered but {pdf} did not. "
@@ -200,6 +210,18 @@ def run_report(args: argparse.Namespace) -> None:
webbrowser.open(output.resolve().as_uri())
+def _stamp(output_dir: Path, digest: str, names: tuple[str, ...]) -> None:
+ """Record *names* in the render stamp. A failed write is a warning, not an error."""
+ try:
+ record_rendered(output_dir, digest, names)
+ except OSError as exc:
+ print(
+ f"Warning: could not write the render stamp in {output_dir}: {exc}. "
+ "'vip bundle' will leave out the rendered report.",
+ file=sys.stderr,
+ )
+
+
def _quarto_render(document: str, report_dir: Path, env: dict[str, str]) -> int:
"""Render one listed document of the report project, returning quarto's exit code.
diff --git a/src/vip/errors.py b/src/vip/errors.py
index 5a426492a..020a5dea7 100644
--- a/src/vip/errors.py
+++ b/src/vip/errors.py
@@ -42,6 +42,10 @@ class ReportError(VipError):
"""Raised when rendering or reading a VIP report fails."""
+class BundleError(VipError):
+ """Raised when ``vip bundle`` cannot write a bundle, or refuses to."""
+
+
class AuthConfigError(AuthError):
"""Raised for user-facing authentication configuration errors."""
From 559e674aceaec317c4ff214de556bcfe49dea9b2 Mon Sep 17 00:00:00 2001
From: Tim
Date: Fri, 6 Nov 2026 10:11:20 -0500
Subject: [PATCH 03/11] feat(cli): add vip bundle command
---
AGENTS.md | 5 +-
README.md | 1 +
selftests/test_cli_bundle.py | 432 ++++++++++++++++++++++++++++++++++
src/vip/cli/__init__.py | 2 +
src/vip/cli/app.py | 54 +++++
src/vip/cli/bundle.py | 265 +++++++++++++++++++++
website/src/pages/guide.astro | 35 +++
7 files changed, 792 insertions(+), 2 deletions(-)
create mode 100644 selftests/test_cli_bundle.py
create mode 100644 src/vip/cli/bundle.py
diff --git a/AGENTS.md b/AGENTS.md
index 397c07634..a3eca825d 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -181,13 +181,14 @@ Key principles:
| File | Purpose |
|------------------------------------|------------------------------------|
-| `src/vip/cli/` | CLI entry point, one module per command: `version.py`, `verify.py` (including `--basic` to skip `@slow`-tagged scenarios), `cleanup.py` (Connect content + orphaned Workbench sessions via `--workbench-url`), `install.py` (install, uninstall), `auth.py`, `report.py`, `status.py`, `scaffold.py`; `app.py` builds the parser and holds `main()` and the `--version` flag; `_common.py` holds helpers shared by several commands; `__init__.py` re-exports the names callers import from `vip.cli`, but a selftest must patch a helper on the submodule that calls it |
+| `src/vip/cli/` | CLI entry point, one module per command: `version.py`, `verify.py` (including `--basic` to skip `@slow`-tagged scenarios), `cleanup.py` (Connect content + orphaned Workbench sessions via `--workbench-url`), `install.py` (install, uninstall), `auth.py`, `report.py`, `bundle.py`, `status.py`, `scaffold.py`; `app.py` builds the parser and holds `main()` and the `--version` flag; `_common.py` holds helpers shared by several commands; `__init__.py` re-exports the names callers import from `vip.cli`, but a selftest must patch a helper on the submodule that calls it |
+| `src/vip/bundle/` | `vip bundle` internals: `summary.py` (allowlisted config summary and the secret list, with a selftest that fails on an unclassified config field), `archive.py` (allowlisted report files, encoded-form secret scan, atomic zip write), and `stamp.py` (the `_output/.vip-render.json` stamp that `vip report` writes, so that a bundle holds only rendered files made from its own `results.json`) |
| `src/vip/config.py` | TOML config loader and dataclasses (includes `[proxy]` → `ProxyConfig`) |
| `src/vip/proxy.py` | Single source of truth for outbound-proxy resolution. `ProxyConfig` + `build_proxy_map` (mirrors httpx's `get_environment_proxies`, incl. NO_PROXY formatting), `build_mounts` (per-scheme `HTTPTransport` mounts that keep `verify`), `proxy_for_url` (httpx-identical most-specific-pattern selection, used by non-httpx probes), `playwright_proxy` (renders a Playwright `launch(proxy=)` dict). Every HTTP egress path routes through this so VIP never diverges from httpx's own env-proxy behavior — see "Outbound proxy support" below |
| `src/vip/auth/` | Interactive and headless browser authentication for OIDC providers, split into `browser.py` (Chromium launch, `InteractiveAuthSession`), `cache.py`, `flows.py` (`start_interactive_auth`, `start_headless_auth`), `sso.py`, `workbench.py`, `scheme.py` (`resolve_url_scheme`) and `apikey.py` (Connect API-key minting); `__init__.py` re-exports the names callers import from `vip.auth`, but a selftest must patch a helper on the submodule that calls it; `authenticated_page` opens a headless page from a cached auth session for `vip cleanup --workbench-url`; `auth_cache_path()` is the single source of truth for the `.vip-auth-cache.json` location (both `plugin/auth.py` and `cli/cleanup.py` must use it), and `_load_cached_auth` probes Workbench before trusting a cached session; `refresh_auth_cache_from_storage_state` writes a live context's cookies back over a cache whose session has been invalidated (atomic, 0600, existing caches only); both cache writers drop Entra's long-lived `ESTSAUTHPERSISTENT` cookie (`_CACHE_DROPPED_COOKIES`) because Entra sets it even when "Stay signed in?" is answered No |
| `src/vip/idp.py` | IdP login form strategies for headless auth (Keycloak, Okta, Snowflake, Entra ID) |
| `src/vip/attest.py` | The two skip helpers (`not_applicable`, `unproven`) that record whether a skipped check was out of scope or simply never verified |
-| `src/vip/errors.py` | The `VipError` exception hierarchy for user-facing CLI failures (`ConfigError`, `AuthError`, `ProductUnreachableError`, `InstallError`, `ReportError`, `AuthConfigError`, `AuthTimeoutError`), each carrying an `exit_code`; raise one instead of calling `sys.exit` from command code — see "Error handling" below |
+| `src/vip/errors.py` | The `VipError` exception hierarchy for user-facing CLI failures (`ConfigError`, `AuthError`, `ProductUnreachableError`, `InstallError`, `ReportError`, `BundleError`, `AuthConfigError`, `AuthTimeoutError`), each carrying an `exit_code`; raise one instead of calling `sys.exit` from command code — see "Error handling" below |
| `src/vip/plugin/` | pytest plugin, split into `options.py` (`pytest_addoption`), `configure.py` (`pytest_configure`: markers, including `slow` used by `verify --basic`, and warning filters), `auth.py` (the `--interactive-auth`/`--headless-auth` login), `selection.py` (deselection and version auto-skip), `results.py` (JSON report output), `terminal.py` (concise display, heartbeat) and `state.py` (module-level mutable state; read and write it as `state.`); `__init__.py` re-exports every hook, since pytest registers only the package module, plus the names callers import from `vip.plugin`, but a selftest must patch a helper on the submodule that calls it; `pytest_configure` also registers `vip.fixtures` as its own named plugin (`"vip-fixtures"`) so core fixtures resolve regardless of directory ancestry — see that module's docstring |
| `src/vip/fixtures.py` | VIP's core pytest fixtures and shared BDD "Given" steps (`vip_config`, `connect_client`, `browser_context_args`, etc.), registered by `vip.plugin.pytest_configure` rather than defined in a `conftest.py` — pytest scopes `conftest.py` fixtures by directory ancestry, which would hide them from extension directories |
| `src/vip/version.py` | `ProductVersion` parsing/comparison for `min_version` gating; `MINIMUM_SUPPORTED_POSIT_TEAM` support floor (powers `vip version`) |
diff --git a/README.md b/README.md
index 9e8eb5885..2ed80877a 100644
--- a/README.md
+++ b/README.md
@@ -104,6 +104,7 @@ content from Connect.
| `vip status` | Quick health check for each configured product |
| `vip cleanup` | Delete VIP `_vip_test` content from Connect |
| `vip report` | Render the HTML report from test results (requires [Quarto CLI](https://quarto.org/docs/download/)) |
+| `vip bundle` | Write a zip of the report output to share. It leaves out vip.toml and the auth cache, and checks each file for known credentials. |
| `vip auth` | Authentication tools (e.g. mint Connect API keys) |
| `vip version` | Print the vip version and the minimum supported Posit Team version |
| `vip --version` | Print the installed vip version |
diff --git a/selftests/test_cli_bundle.py b/selftests/test_cli_bundle.py
new file mode 100644
index 000000000..51eb76e82
--- /dev/null
+++ b/selftests/test_cli_bundle.py
@@ -0,0 +1,432 @@
+"""Tests for the ``vip bundle`` subcommand (run_bundle in vip.cli)."""
+
+from __future__ import annotations
+
+import argparse
+import json
+import os
+import re
+import sys
+import zipfile
+from pathlib import Path
+
+import pytest
+
+import vip.cli.bundle as bundle_module
+from vip.bundle.stamp import record_rendered, results_digest
+from vip.cli import main, run_bundle
+from vip.errors import BundleError
+
+_VIP_SECRET_ENV = (
+ "VIP_CONFIG",
+ "VIP_CONNECT_API_KEY",
+ "VIP_WORKBENCH_API_KEY",
+ "VIP_GIT_TOKEN",
+ "VIP_PACKAGE_MANAGER_TOKEN",
+ "VIP_PM_TOKEN",
+ "VIP_TEST_USERNAME",
+ "VIP_TEST_PASSWORD",
+ "VIP_TEST_TOTP_SECRET",
+)
+
+CONNECT_KEY = "SECRET-connect-api-key"
+MINTED_KEY = "MINTED-KEY-0001"
+SESSION_COOKIE = "COOKIE-SESSION-0123456789"
+
+VIP_TOML = f"""\
+[general]
+deployment_name = "Acme Prod"
+
+[connect]
+url = "https://connect.example.com"
+api_key = "{CONNECT_KEY}"
+
+[workbench]
+url = "https://workbench.example.com"
+api_key = "SECRET-workbench-api-key"
+
+[workbench.git_test]
+clone_url = "https://SECRET-git-user-token@github.com/org/repo.git"
+token = "SECRET-git-token"
+
+[package_manager]
+url = "https://pm.example.com"
+token = "SECRET-pm-token"
+
+[auth]
+username = "alice"
+password = "SECRET-auth-password"
+
+[data_sources.pg]
+type = "postgres"
+connection_string = "postgresql://dbuser:SECRET-db-password@db:5432/app"
+
+[proxy]
+url = "http://puser:SECRET-proxy-password@proxy:8080"
+"""
+
+ALL_SEEDED_SECRETS = (
+ CONNECT_KEY,
+ "SECRET-workbench-api-key",
+ "SECRET-git-user-token",
+ "SECRET-git-token",
+ "SECRET-pm-token",
+ "SECRET-auth-password",
+ "SECRET-db-password",
+ "SECRET-proxy-password",
+ MINTED_KEY,
+ SESSION_COOKIE,
+)
+
+RESULTS_MTIME = 1_000_000.0
+
+
+def _args(**overrides) -> argparse.Namespace:
+ defaults: dict[str, object] = {
+ "results": None,
+ "config": None,
+ "no_config": False,
+ "output": None,
+ "force": False,
+ "allow_match": [],
+ }
+ defaults.update(overrides)
+ return argparse.Namespace(**defaults)
+
+
+def _write(path: Path, text: str, mtime: float) -> None:
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(text, encoding="utf-8")
+ os.utime(path, (mtime, mtime))
+
+
+def _write_report(
+ root: Path, results_extra: str = "", failures_extra: str = "", deployment: str = "Acme Prod"
+) -> None:
+ """Write report files as vip verify and vip report leave them.
+
+ failures.json is newer than results.json, and the render stamp ties
+ index.html to results.json.
+ """
+ report = root / "report"
+ results = {
+ "deployment_name": deployment,
+ "products": {"connect": {"url": "https://connect.example.com"}},
+ "results": [],
+ "note": results_extra,
+ }
+ _write(report / "results.json", json.dumps(results), RESULTS_MTIME)
+ failures = {"failures": [{"error_summary": f"HTTP 401 {failures_extra}"}]}
+ _write(report / "failures.json", json.dumps(failures), RESULTS_MTIME + 1)
+ _write(report / "_output" / "index.html", "report", RESULTS_MTIME + 2)
+ digest = results_digest((report / "results.json").read_bytes())
+ record_rendered(report / "_output", digest, ("index.html",))
+ _write(report / "notes.txt", "not allowlisted", RESULTS_MTIME + 2)
+
+
+@pytest.fixture(autouse=True)
+def _isolated_env(monkeypatch, tmp_path):
+ for var in _VIP_SECRET_ENV:
+ monkeypatch.delenv(var, raising=False)
+ # A later auth_cache_path() reads XDG_CACHE_HOME. Keep it out of the real home.
+ monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "xdg"))
+
+
+@pytest.fixture
+def root(tmp_path, monkeypatch) -> Path:
+ """An invocation directory as ``vip verify`` and ``vip report`` leave it."""
+ (tmp_path / "vip.toml").write_text(VIP_TOML, encoding="utf-8")
+ (tmp_path / ".vip-auth-cache.json").write_text(
+ json.dumps({"cookies": [{"name": "session", "value": SESSION_COOKIE}]}),
+ encoding="utf-8",
+ )
+ (tmp_path / ".vip-auth-cache.meta.json").write_text(
+ json.dumps({"api_key": MINTED_KEY}), encoding="utf-8"
+ )
+ (tmp_path / ".vip-install.json").write_text("{}", encoding="utf-8")
+ _write_report(tmp_path)
+ monkeypatch.chdir(tmp_path)
+ return tmp_path
+
+
+def _names(zip_path: Path) -> set[str]:
+ with zipfile.ZipFile(zip_path) as zf:
+ return set(zf.namelist())
+
+
+def _leftovers(directory: Path) -> list[str]:
+ return sorted(p.name for p in directory.iterdir() if p.suffix in {".zip", ".tmp"})
+
+
+def test_bundle_holds_only_allowlisted_files_and_no_secret(root):
+ run_bundle(_args(output="b.zip"))
+ with zipfile.ZipFile(root / "b.zip") as zf:
+ names = set(zf.namelist())
+ data = b"".join(zf.read(n) for n in names)
+ assert names == {
+ "report/results.json",
+ "report/failures.json",
+ "report/_output/index.html",
+ "config-summary.json",
+ "version.txt",
+ "manifest.json",
+ }
+ for secret in ALL_SEEDED_SECRETS:
+ assert secret.encode() not in data
+ assert b"[connect]" not in data
+
+
+def test_manifest_lists_members_and_missing_files(root):
+ run_bundle(_args(output="b.zip"))
+ with zipfile.ZipFile(root / "b.zip") as zf:
+ manifest = json.loads(zf.read("manifest.json"))
+ assert manifest["members"] == sorted(_names(root / "b.zip"))
+ assert "report/_output/vip-report.pdf" in manifest["missing"]
+ assert manifest["left_out"] == []
+ assert manifest["allowed_matches"] == []
+ assert manifest["config_summary"] is True
+ assert manifest["secret_scan"] == "config"
+
+
+def test_secret_in_failures_json_blocks_bundle(root, capsys):
+ _write_report(root, failures_extra=f"key={CONNECT_KEY}")
+ with pytest.raises(BundleError) as exc:
+ run_bundle(_args(output="b.zip"))
+ message = str(exc.value)
+ # The error names the file on disk, relative to the working directory.
+ assert f"\n {Path('report') / 'failures.json'}: connect.api_key\n" in message
+ assert "Edit each file to remove the value" in message
+ assert "--allow-match LABEL" in message
+ assert CONNECT_KEY not in message
+ captured = capsys.readouterr()
+ assert CONNECT_KEY not in captured.out + captured.err
+ assert _leftovers(root) == []
+
+
+def test_secret_in_a_made_member_names_it(root):
+ # A weak password that is also a setting that is not secret.
+ toml = (root / "vip.toml").read_text(encoding="utf-8")
+ (root / "vip.toml").write_text(
+ toml.replace('password = "SECRET-auth-password"', 'password = "postgres"'),
+ encoding="utf-8",
+ )
+ with pytest.raises(BundleError) as exc:
+ run_bundle(_args(output="b.zip"))
+ assert "config-summary.json (made by vip bundle): auth.password" in str(exc.value)
+ assert "Change the credential" in str(exc.value)
+
+
+def test_allow_match_writes_the_bundle_and_records_it(root, capsys):
+ toml = (root / "vip.toml").read_text(encoding="utf-8")
+ (root / "vip.toml").write_text(
+ toml.replace('password = "SECRET-auth-password"', 'password = "postgres"'),
+ encoding="utf-8",
+ )
+ run_bundle(_args(output="b.zip", allow_match=["auth.password"]))
+ with zipfile.ZipFile(root / "b.zip") as zf:
+ manifest = json.loads(zf.read("manifest.json"))
+ assert manifest["allowed_matches"] == [
+ {"member": "config-summary.json", "label": "auth.password"}
+ ]
+ assert "because --allow-match auth.password is set" in capsys.readouterr().err
+
+
+def test_allow_match_does_not_cover_other_labels(root):
+ _write_report(root, failures_extra=CONNECT_KEY)
+ with pytest.raises(BundleError, match=r"connect\.api_key"):
+ run_bundle(_args(output="b.zip", allow_match=["auth.password"]))
+
+
+def test_allow_match_with_an_unknown_label_fails(root):
+ with pytest.raises(BundleError, match=r"names no known credential: connect\.apikey"):
+ run_bundle(_args(output="b.zip", allow_match=["connect.apikey"]))
+
+
+def test_manifest_is_scanned_too(root, monkeypatch):
+ # Only the manifest holds the value: the path of --results is in a warning there.
+ run_dir = root / "Run-Dir-0123"
+ _write(run_dir / "results.json", '{"deployment_name": "Acme Prod", "results": []}', 5.0)
+ monkeypatch.setenv("VIP_TEST_TOTP_SECRET", "Run-Dir-0123")
+ with pytest.raises(BundleError, match=r"manifest\.json \(made by vip bundle\)"):
+ run_bundle(_args(output="b.zip", results=str(run_dir / "results.json")))
+
+
+def test_minted_key_in_legacy_cache_blocks_bundle(root, tmp_path, monkeypatch):
+ # auth_cache_path() points elsewhere, so only the legacy file holds the key.
+ other = tmp_path / "other-cache" / ".vip-auth-cache.json"
+ monkeypatch.setattr(bundle_module, "auth_cache_path", lambda: other)
+ _write_report(root, results_extra=MINTED_KEY)
+ with pytest.raises(BundleError, match="auth cache api_key"):
+ run_bundle(_args(output="b.zip"))
+
+
+def test_legacy_cache_is_read_before_auth_cache_path(root, monkeypatch):
+ # After PR 793, auth_cache_path() can move or delete the legacy files.
+ def migrating_auth_cache_path():
+ (root / ".vip-auth-cache.meta.json").unlink()
+ return root / "moved" / ".vip-auth-cache.json"
+
+ monkeypatch.setattr(bundle_module, "auth_cache_path", migrating_auth_cache_path)
+ _write_report(root, results_extra=MINTED_KEY)
+ with pytest.raises(BundleError, match="auth cache api_key"):
+ run_bundle(_args(output="b.zip"))
+
+
+def test_results_path_sets_the_root(root, tmp_path, monkeypatch):
+ # Run from another directory: vip.toml and the legacy cache come from the
+ # directory that holds report/.
+ elsewhere = tmp_path / "elsewhere"
+ elsewhere.mkdir()
+ monkeypatch.chdir(elsewhere)
+ _write_report(root, results_extra=MINTED_KEY)
+ with pytest.raises(BundleError, match="auth cache api_key"):
+ run_bundle(_args(output="b.zip", results=str(root / "report" / "results.json")))
+
+
+def test_config_for_another_deployment_fails(root):
+ _write_report(root, deployment="Acme Staging")
+ with pytest.raises(BundleError, match=r"does not match results\.json"):
+ run_bundle(_args(output="b.zip"))
+
+
+def test_config_with_another_product_url_fails(root):
+ toml = (root / "vip.toml").read_text(encoding="utf-8")
+ (root / "vip.toml").write_text(
+ toml.replace("https://connect.example.com", "https://connect.staging.example.com"),
+ encoding="utf-8",
+ )
+ with pytest.raises(BundleError, match="the connect URL is"):
+ run_bundle(_args(output="b.zip"))
+
+
+def test_key_in_auth_cache_path_blocks_bundle(root, tmp_path, monkeypatch):
+ # The auth cache can live outside the invocation directory. The bundle
+ # must read the file at auth_cache_path() too, not only the legacy file.
+ user_cache = tmp_path / "user-cache" / ".vip-auth-cache.json"
+ user_cache.parent.mkdir()
+ user_cache.with_suffix(".meta.json").write_text(
+ json.dumps({"api_key": "USER-CACHE-KEY-01"}), encoding="utf-8"
+ )
+ monkeypatch.setattr(bundle_module, "auth_cache_path", lambda: user_cache)
+ _write_report(root, results_extra="USER-CACHE-KEY-01")
+ with pytest.raises(BundleError, match="auth cache api_key"):
+ run_bundle(_args(output="b.zip"))
+
+
+def test_runs_from_inside_report_dir(root, monkeypatch):
+ monkeypatch.chdir(root / "report")
+ run_bundle(_args())
+ (zip_path,) = root.glob("vip-bundle-*.zip")
+ assert "report/results.json" in _names(zip_path)
+ assert "config-summary.json" in _names(zip_path)
+ assert not list((root / "report").glob("*.zip"))
+
+
+def test_inside_report_dir_still_scans_config_and_cache(root, monkeypatch):
+ _write_report(root, results_extra=MINTED_KEY, failures_extra=CONNECT_KEY)
+ monkeypatch.chdir(root / "report")
+ with pytest.raises(BundleError) as exc:
+ run_bundle(_args())
+ assert "connect.api_key" in str(exc.value)
+ assert "auth cache api_key" in str(exc.value)
+
+
+def test_missing_config_fails_without_flag(root):
+ (root / "vip.toml").unlink()
+ with pytest.raises(BundleError, match="--no-config"):
+ run_bundle(_args(output="b.zip"))
+ assert _leftovers(root) == []
+
+
+def test_explicit_missing_config_fails(root):
+ with pytest.raises(BundleError, match=r"no configuration file found at other\.toml"):
+ run_bundle(_args(output="b.zip", config="other.toml"))
+
+
+def test_bad_config_fails_cleanly(root):
+ (root / "vip.toml").write_text("[connect\n", encoding="utf-8")
+ with pytest.raises(BundleError, match="could not read"):
+ run_bundle(_args(output="b.zip"))
+
+
+def test_no_config_flag_bundles_and_warns(root, capsys):
+ (root / "vip.toml").unlink()
+ run_bundle(_args(output="b.zip", no_config=True))
+ with zipfile.ZipFile(root / "b.zip") as zf:
+ manifest = json.loads(zf.read("manifest.json"))
+ names = set(zf.namelist())
+ assert manifest["config_summary"] is False
+ assert manifest["secret_scan"] == "no-config"
+ assert "config-summary.json" not in names
+ assert "Warning: --no-config is set" in capsys.readouterr().err
+
+
+def test_no_config_scan_still_uses_env_key(root, monkeypatch):
+ (root / "vip.toml").unlink()
+ monkeypatch.setenv("VIP_CONNECT_API_KEY", "ENV-KEY-1234")
+ _write_report(root, results_extra="ENV-KEY-1234")
+ with pytest.raises(BundleError, match=r"connect\.api_key"):
+ run_bundle(_args(output="b.zip", no_config=True))
+
+
+def test_stale_render_is_left_out_and_warns(root, capsys):
+ # vip verify ran again, but vip report did not.
+ results = root / "report" / "results.json"
+ text = results.read_text(encoding="utf-8").replace('"results": []', '"results": [{"n": 1}]')
+ results.write_text(text, encoding="utf-8")
+ os.utime(root / "report" / "failures.json")
+ run_bundle(_args(output="b.zip"))
+ assert "report/_output/index.html" not in _names(root / "b.zip")
+ err = capsys.readouterr().err
+ assert "was not made from" in err
+ assert "Run 'vip report' again" in err
+
+
+def test_short_secret_warns(root, capsys):
+ toml = (root / "vip.toml").read_text(encoding="utf-8")
+ (root / "vip.toml").write_text(
+ toml.replace('password = "SECRET-auth-password"', 'password = "abc"'),
+ encoding="utf-8",
+ )
+ run_bundle(_args(output="b.zip"))
+ assert "the value of auth.password is shorter than 4 characters" in capsys.readouterr().err
+
+
+def test_existing_output_needs_force(root):
+ (root / "b.zip").write_bytes(b"old")
+ with pytest.raises(BundleError, match="--force"):
+ run_bundle(_args(output="b.zip"))
+ run_bundle(_args(output="b.zip", force=True))
+ assert "manifest.json" in _names(root / "b.zip")
+
+
+def test_default_output_name(root, capsys):
+ run_bundle(_args())
+ (zip_path,) = root.glob("vip-bundle-*.zip")
+ assert re.fullmatch(r"vip-bundle-acme-prod-\d{8}T\d{6}Z\.zip", zip_path.name)
+ assert "Bundle written:" in capsys.readouterr().out
+
+
+def test_unwritable_output_dir_fails_cleanly(root):
+ with pytest.raises(BundleError, match="could not write"):
+ run_bundle(_args(output="no-such-dir/b.zip"))
+
+
+def test_bundle_subcommand_is_registered(monkeypatch, capsys):
+ monkeypatch.setattr(sys, "argv", ["vip", "bundle", "--help"])
+ with pytest.raises(SystemExit) as exc:
+ main()
+ assert exc.value.code == 0
+ out = capsys.readouterr().out
+ assert "--output" in out
+ assert "--no-config" in out
+ assert "--allow-match" in out
+
+
+def test_bundle_error_exits_with_code_one(tmp_path, monkeypatch, capsys):
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setattr(sys, "argv", ["vip", "bundle", "--no-config"])
+ with pytest.raises(SystemExit) as exc:
+ main()
+ assert exc.value.code == 1
+ assert "Error: no results found" in capsys.readouterr().err
diff --git a/src/vip/cli/__init__.py b/src/vip/cli/__init__.py
index de8c79d71..6761e5bd8 100644
--- a/src/vip/cli/__init__.py
+++ b/src/vip/cli/__init__.py
@@ -8,6 +8,7 @@
from vip.cli.app import _reorder_help_args, main
from vip.cli.auth import mint_connect_key
+from vip.cli.bundle import run_bundle
from vip.cli.cleanup import _cleanup_workbench_sessions, run_cleanup
from vip.cli.install import run_install, run_uninstall
from vip.cli.report import (
@@ -44,6 +45,7 @@
"_reorder_help_args",
"main",
"mint_connect_key",
+ "run_bundle",
"run_cleanup",
"run_install",
"run_report",
diff --git a/src/vip/cli/app.py b/src/vip/cli/app.py
index 8f03554de..363ebeb1c 100644
--- a/src/vip/cli/app.py
+++ b/src/vip/cli/app.py
@@ -8,6 +8,7 @@
from vip import __version__
from vip.cli.auth import mint_connect_key
+from vip.cli.bundle import run_bundle
from vip.cli.cleanup import run_cleanup
from vip.cli.install import run_install, run_uninstall
from vip.cli.report import run_report
@@ -491,6 +492,58 @@ def main() -> None:
)
report_parser.set_defaults(func=run_report)
+ # vip bundle
+ bundle_parser = subparsers.add_parser(
+ "bundle",
+ help=(
+ "Write a zip of the report output to share. It leaves out vip.toml and "
+ "the auth cache, and checks each file for known credentials."
+ ),
+ )
+ bundle_parser.add_argument(
+ "--results",
+ default=None,
+ help="Path to results.json (default: report/results.json)",
+ )
+ bundle_config = bundle_parser.add_mutually_exclusive_group()
+ bundle_config.add_argument(
+ "--config",
+ default=None,
+ help="Path to vip.toml (default: VIP_CONFIG env var or vip.toml next to report/)",
+ )
+ bundle_config.add_argument(
+ "--no-config",
+ action="store_true",
+ default=False,
+ help=(
+ "Write the bundle with no vip.toml. The secret scan then uses only "
+ "the environment variables and the auth cache."
+ ),
+ )
+ bundle_parser.add_argument(
+ "--output",
+ default=None,
+ help="Path of the zip (default: vip-bundle--.zip next to report/)",
+ )
+ bundle_parser.add_argument(
+ "--force",
+ action="store_true",
+ default=False,
+ help="Overwrite the output file if it already exists",
+ )
+ bundle_parser.add_argument(
+ "--allow-match",
+ action="append",
+ default=[],
+ metavar="LABEL",
+ help=(
+ "Write the bundle even though a file holds the value of LABEL, for "
+ "example auth.password. Use it only when the value is not a real "
+ "secret. The manifest records it. Repeat for more labels."
+ ),
+ )
+ bundle_parser.set_defaults(func=run_bundle)
+
# vip status
status_parser = subparsers.add_parser(
"status",
@@ -563,6 +616,7 @@ def main() -> None:
"uninstall": uninstall_parser,
"auth": auth_parser,
"report": report_parser,
+ "bundle": bundle_parser,
"status": status_parser,
"scaffold": scaffold_parser,
}
diff --git a/src/vip/cli/bundle.py b/src/vip/cli/bundle.py
new file mode 100644
index 000000000..23cfaf4b3
--- /dev/null
+++ b/src/vip/cli/bundle.py
@@ -0,0 +1,265 @@
+"""``vip bundle``: write a zip of the report output, checked for known credentials."""
+
+from __future__ import annotations
+
+import argparse
+import contextlib
+import json
+import os
+import re
+import sys
+from datetime import UTC, datetime
+from pathlib import Path
+from typing import Any
+
+from vip import __version__
+from vip.auth.cache import AUTH_CACHE_FILENAME, auth_cache_path
+from vip.bundle.archive import (
+ Collected,
+ collect_members,
+ find_secret_hits,
+ short_secret_labels,
+ write_zip,
+)
+from vip.bundle.summary import (
+ auth_cache_secrets,
+ build_config_summary,
+ secret_values,
+ strip_url_userinfo,
+)
+from vip.cli.report import _resolve_report_dir
+from vip.cli.version import _format_version_details
+from vip.config import VIPConfig, load_config
+from vip.errors import BundleError
+
+_PRODUCTS = ("connect", "workbench", "package_manager")
+
+
+def _invocation_root(results: str | None) -> Path:
+ """Return the directory that holds ``vip.toml``, ``report/``, and the legacy auth cache.
+
+ With ``--results /report/results.json``, the root is ````.
+ Otherwise it is the working directory. ``_resolve_report_dir`` treats a
+ working directory named ``report`` as the report directory itself, so the
+ root is then one level up.
+ """
+ if results:
+ parent = Path(results).resolve().parent
+ if parent.name == "report":
+ return parent.parent
+ cwd = Path.cwd()
+ return cwd.parent if cwd.name == "report" else cwd
+
+
+def _config_path(args: argparse.Namespace, root: Path) -> Path | None:
+ """Return the configuration file to read, or None when --no-config is set.
+
+ The order is --config, then VIP_CONFIG, then ``/vip.toml``. A file
+ that does not exist is an error: without it the scan misses every
+ secret that only vip.toml holds.
+ """
+ if args.no_config:
+ return None
+ if args.config:
+ path = Path(args.config)
+ elif os.environ.get("VIP_CONFIG"):
+ path = Path(os.environ["VIP_CONFIG"])
+ else:
+ path = root / "vip.toml"
+ if not path.is_file():
+ raise BundleError(
+ f"no configuration file found at {path}. The secret scan needs it. "
+ "Pass --config PATH. If you ran 'vip verify' with --connect-url and no "
+ "vip.toml, pass --no-config: the scan then uses only the environment "
+ "variables and the auth cache."
+ )
+ return path
+
+
+def _load(path: Path) -> VIPConfig:
+ """Load *path*. Turn a read error or a parse error into BundleError."""
+ try:
+ return load_config(path)
+ # tomllib.TOMLDecodeError is a ValueError. A table of the wrong shape gives
+ # AttributeError or TypeError in load_config.
+ except (OSError, ValueError, AttributeError, TypeError) as exc:
+ raise BundleError(f"could not read {path}: {exc}") from exc
+
+
+def _check_config_matches(config: VIPConfig, path: Path, results: bytes) -> None:
+ """Refuse a configuration file that is not the one that ``vip verify`` used.
+
+ results.json holds the deployment name and each product URL from the
+ configuration of the run. If they differ, the scan would look for the
+ wrong secrets.
+ """
+ data = json.loads(results)
+ name = data.get("deployment_name")
+ if isinstance(name, str) and name != config.deployment_name:
+ raise BundleError(
+ f"{path} does not match results.json: the deployment is "
+ f"'{config.deployment_name}' in the configuration and '{name}' in "
+ "results.json. Pass --config with the file that 'vip verify' used."
+ )
+ products = data.get("products")
+ for product in _PRODUCTS:
+ entry = products.get(product) if isinstance(products, dict) else None
+ run_url = entry.get("url") if isinstance(entry, dict) else None
+ url = config.product_config(product).url
+ if run_url and url and run_url.rstrip("/") != url.rstrip("/"):
+ raise BundleError(
+ f"{path} does not match results.json: the {product} URL is "
+ f"{strip_url_userinfo(url)} in the configuration and "
+ f"{strip_url_userinfo(run_url)} in results.json. "
+ "Pass --config with the file that 'vip verify' used."
+ )
+
+
+def _cache_secrets(root: Path) -> list[tuple[str, str]]:
+ """Return the secrets in the auth cache files that ``vip verify`` run from *root* can write.
+
+ The legacy files in *root* are read first. ``auth_cache_path()`` can move
+ or delete a legacy file when it migrates the cache to the user cache
+ directory, so it is called only after that. It depends on the working
+ directory, so it is called from *root*. It can return None when no cache
+ directory is available.
+ """
+ legacy = root / AUTH_CACHE_FILENAME
+ pairs = auth_cache_secrets(legacy) + auth_cache_secrets(legacy.with_suffix(".meta.json"))
+ with contextlib.chdir(root):
+ cache = auth_cache_path()
+ if cache is not None and cache != legacy:
+ pairs += auth_cache_secrets(cache) + auth_cache_secrets(cache.with_suffix(".meta.json"))
+ return pairs
+
+
+def _default_output(root: Path, results: bytes) -> Path:
+ """Return ``/vip-bundle--.zip``."""
+ try:
+ name = str(json.loads(results).get("deployment_name") or "")
+ except (ValueError, AttributeError):
+ name = ""
+ slug = re.sub(r"[^a-z0-9]+", "-", name.lower()).strip("-")[:40]
+ stamp = datetime.now(UTC).strftime("%Y%m%dT%H%M%SZ")
+ return root / ("vip-bundle-" + (f"{slug}-" if slug else "") + f"{stamp}.zip")
+
+
+def _json_bytes(data: Any) -> bytes:
+ return (json.dumps(data, indent=2, sort_keys=True) + "\n").encode("utf-8")
+
+
+def _hit_error(hits: list[tuple[str, str]], collected: Collected) -> BundleError:
+ """Return the error for secret matches. Name each file and each source, never a value."""
+ lines = ["refusing to write the bundle. The secret scan found a known credential:"]
+ on_disk = made = False
+ for name in sorted({name for name, _ in hits}):
+ labels = ", ".join(sorted({label for member, label in hits if member == name}))
+ if name in collected.sources:
+ on_disk = True
+ lines.append(f" {collected.sources[name]}: {labels}")
+ else:
+ made = True
+ lines.append(f" {name} (made by vip bundle): {labels}")
+ if on_disk:
+ lines.append(
+ "Edit each file to remove the value. Then run 'vip report' again and "
+ "run 'vip bundle' again."
+ )
+ if made:
+ lines.append(
+ "A file that vip bundle makes holds the value, so a setting that is "
+ "not secret has the same value. Change the credential."
+ )
+ lines.append(
+ "If the value is not a real secret, pass --allow-match LABEL for each "
+ "label. The manifest records each one."
+ )
+ return BundleError("\n".join(lines))
+
+
+def run_bundle(args: argparse.Namespace) -> None:
+ """Write a zip of the report output. Refuse when a member holds a known secret."""
+ root = _invocation_root(args.results)
+ report_dir = _resolve_report_dir()
+ results_path = Path(args.results) if args.results else report_dir / "results.json"
+ config_path = _config_path(args, root)
+ # VIPConfig() still reads the VIP_* secret variables in its __post_init__ methods.
+ config = _load(config_path) if config_path is not None else VIPConfig()
+
+ collected = collect_members(results_path, report_dir)
+ results = collected.members["report/results.json"]
+ if config_path is not None:
+ _check_config_matches(config, config_path, results)
+ warnings = list(collected.warnings)
+ if config_path is None:
+ warnings.append(
+ "--no-config is set, so the secret scan used only the environment "
+ "variables and the auth cache. Look at the zip before you send it."
+ )
+ secrets = secret_values(config, _cache_secrets(root))
+ warnings += [
+ f"the value of {label} is shorter than 4 characters, so the secret scan "
+ "cannot look for it. Look at the zip before you send it."
+ for label in short_secret_labels(secrets)
+ ]
+ allowed = set(args.allow_match or [])
+ unknown = sorted(allowed - {label for label, _ in secrets})
+ if unknown:
+ raise BundleError(
+ f"--allow-match names no known credential: {', '.join(unknown)}. "
+ "Use a label from the secret scan error."
+ )
+
+ output = Path(args.output) if args.output else _default_output(root, results)
+ if output.exists() and not args.force:
+ raise BundleError(
+ f"{output} already exists. Pass --force to overwrite it, or pass --output PATH."
+ )
+
+ members = dict(collected.members)
+ if config_path is not None:
+ members["config-summary.json"] = _json_bytes(build_config_summary(config))
+ members["version.txt"] = (_format_version_details() + "\n").encode("utf-8")
+
+ # Scan every member before anything is written. The manifest is scanned
+ # on its own after it is built, because it lists the allowed matches.
+ hits = find_secret_hits(members, secrets)
+ blocked = [hit for hit in hits if hit[1] not in allowed]
+ if blocked:
+ raise _hit_error(blocked, collected)
+ allowed_hits = [hit for hit in hits if hit[1] in allowed]
+ warnings += [
+ f"the bundle holds the value of {label} in {name}, because --allow-match {label} is set."
+ for name, label in allowed_hits
+ ]
+
+ members["manifest.json"] = _json_bytes(
+ {
+ "created_at": datetime.now(UTC).isoformat(),
+ "vip_version": __version__,
+ "members": sorted([*members, "manifest.json"]),
+ "missing": sorted(collected.missing),
+ "left_out": collected.left_out,
+ "config_summary": config_path is not None,
+ "secret_scan": "config" if config_path is not None else "no-config",
+ "allowed_matches": [{"member": n, "label": label} for n, label in allowed_hits],
+ "warnings": warnings,
+ }
+ )
+ manifest_hits = [
+ hit
+ for hit in find_secret_hits({"manifest.json": members["manifest.json"]}, secrets)
+ if hit[1] not in allowed
+ ]
+ if manifest_hits:
+ raise _hit_error(manifest_hits, collected)
+
+ for warning in warnings:
+ print(f"Warning: {warning}", file=sys.stderr)
+ try:
+ write_zip(output, members)
+ except OSError as exc:
+ raise BundleError(
+ f"could not write {output}: {exc}. Pass --output PATH to write it somewhere else."
+ ) from exc
+ print(f"Bundle written: {output} ({len(members)} files)")
diff --git a/website/src/pages/guide.astro b/website/src/pages/guide.astro
index 0705e9bcc..bb7cedb1f 100644
--- a/website/src/pages/guide.astro
+++ b/website/src/pages/guide.astro
@@ -375,6 +375,10 @@ vip verify --connect-url https://connect.example.com --report report/results.jso
# Render the report (requires quarto CLI)
vip report
+# Write a zip of the report to send to someone else
+# (this example has no vip.toml, so it passes --no-config)
+vip bundle --no-config
+
# Publish to Connect (optional)
cd report && quarto publish connect --server https://connect.example.com
@@ -384,6 +388,37 @@ cd report && quarto publish connect --server https://connect.example.com
Quarto CLI must be
installed.
+
+ vip bundle writes
+ vip-bundle-<deployment>-<time>.zip next to
+ report/. The zip holds results.json, the
+ failure, JUnit, and SARIF files, the rendered report, a
+ configuration summary with no credentials, and the vip version.
+ It never holds vip.toml or the auth cache. Before it
+ writes the zip, the command looks in each file for each credential
+ from vip.toml, the environment, and the auth cache. If
+ it finds one, it stops and names the file and the credential. If
+ the value is not a real secret, for example a test password that
+ is also a word in the report, pass
+ --allow-match LABEL with the label from the error.
+ The manifest in the zip records it. The scan cannot find a
+ credential that it does not know, for example one that an
+ extension test printed or one that was set only in the shell that
+ ran vip verify. Look at the zip before you send it.
+
+
+ The command needs the vip.toml that
+ vip verify used, and stops if it finds none. If you
+ ran vip verify with --connect-url and no
+ vip.toml, pass --no-config. The scan then
+ uses only the environment and the auth cache. A rendered page that
+ vip report did not make from the current
+ results.json is left out. Run vip report
+ again to include it. A failure, JUnit, or SARIF file that is older
+ than results.json is from an earlier run, so it is
+ left out too. The manifest lists each file that is left out, with
+ the reason.
+
From f1165584c85e7bfe4a8f3ecdffc25686a08c210e Mon Sep 17 00:00:00 2001
From: Tim
Date: Fri, 6 Nov 2026 10:21:18 -0500
Subject: [PATCH 04/11] fix(bundle): find rendered files from --results, split
clone URL labels
Read rendered files from the report directory of --results. Give the user
and password of the clone URL their own labels, so that --allow-match for
one does not cover the other. A stamp that cannot be cleared is a
ReportError. The repr drift test now covers str | None fields.
---
selftests/test_bundle_summary.py | 46 ++++++++++++++++++++++++++------
selftests/test_cli_bundle.py | 8 ++++++
selftests/test_cli_report.py | 13 +++++++++
src/vip/bundle/summary.py | 31 +++++++++++++--------
src/vip/cli/bundle.py | 22 ++++++++++-----
src/vip/cli/report.py | 5 +++-
6 files changed, 99 insertions(+), 26 deletions(-)
diff --git a/selftests/test_bundle_summary.py b/selftests/test_bundle_summary.py
index 42eee8b48..f7f7f03f0 100644
--- a/selftests/test_bundle_summary.py
+++ b/selftests/test_bundle_summary.py
@@ -177,20 +177,39 @@ def _config_classes(cls: Any, prefix: str = "") -> list[tuple[Any, str]]:
_REQUIRED_VALUES = {GitTestConfig: {"auth_method": "https-token"}}
+def _sentinel_values(cls: type, prefix: str) -> dict[str, str]:
+ """Return a distinct sentinel for each init field that can hold a str."""
+ hints = typing.get_type_hints(cls)
+ return {
+ f.name: f"SENTINEL-{prefix}{f.name}"
+ for f in dataclasses.fields(cls)
+ if f.init and hints[f.name] in (str, str | None)
+ }
+
+
+def test_sentinel_values_cover_optional_str_fields():
+ @dataclasses.dataclass
+ class Sample:
+ name: str = ""
+ token: str | None = None
+ count: int = 0
+
+ assert _sentinel_values(Sample, "s.") == {
+ "name": "SENTINEL-s.name",
+ "token": "SENTINEL-s.token",
+ }
+
+
def test_every_repr_masked_field_is_a_secret():
# A config class that hides a field in __repr__ treats it as a secret.
- # Every str field gets a distinct sentinel, so a new hidden field is found.
+ # Every str field, optional or not, gets a distinct sentinel, so a new
+ # hidden field is found.
classes = _config_classes(VIPConfig)
known = {ConnectConfig, WorkbenchConfig, GitTestConfig, PackageManagerConfig, AuthConfig}
assert known | {DataSourceEntry} <= {cls for cls, _ in classes}
hidden: set[str] = set()
for cls, prefix in classes:
- hints = typing.get_type_hints(cls)
- values = {
- f.name: f"SENTINEL-{prefix}{f.name}"
- for f in dataclasses.fields(cls)
- if f.init and hints[f.name] is str
- }
+ values = _sentinel_values(cls, prefix)
values.update(_REQUIRED_VALUES.get(cls, {}))
text = repr(cls(**values))
hidden |= {
@@ -327,7 +346,18 @@ def test_secret_values_find_the_clone_url_token_in_each_form(clone_url):
def test_secret_values_labels_name_the_source(full_config):
labels = {value: label for label, value in secret_values(full_config)}
assert labels[SECRETS["auth.password"]] == "auth.password"
- assert labels[SECRETS["clone_url user"]] == "workbench.git_test.clone_url credentials"
+ assert labels[SECRETS["clone_url user"]] == "workbench.git_test.clone_url user"
+ assert labels[SECRETS["clone_url query"]] == "workbench.git_test.clone_url query"
+
+
+def test_clone_url_user_and_password_have_their_own_labels():
+ # --allow-match for a false match on a public user name must not also
+ # let the token through.
+ url = "https://x-access-token:GIT-TOKEN-0123456789@github.com/org/repo.git"
+ config = VIPConfig(workbench=WorkbenchConfig(git_test=GitTestConfig(clone_url=url)))
+ labels = {value: label for label, value in secret_values(config)}
+ assert labels["x-access-token"] == "workbench.git_test.clone_url user"
+ assert labels["GIT-TOKEN-0123456789"] == "workbench.git_test.clone_url password"
def test_secret_values_read_env_only_secrets(monkeypatch):
diff --git a/selftests/test_cli_bundle.py b/selftests/test_cli_bundle.py
index 51eb76e82..db86e0797 100644
--- a/selftests/test_cli_bundle.py
+++ b/selftests/test_cli_bundle.py
@@ -313,6 +313,14 @@ def test_key_in_auth_cache_path_blocks_bundle(root, tmp_path, monkeypatch):
run_bundle(_args(output="b.zip"))
+def test_results_path_sets_the_rendered_files_dir(root, tmp_path, monkeypatch):
+ elsewhere = tmp_path / "elsewhere"
+ elsewhere.mkdir()
+ monkeypatch.chdir(elsewhere)
+ run_bundle(_args(output="b.zip", results=str(root / "report" / "results.json")))
+ assert "report/_output/index.html" in _names(elsewhere / "b.zip")
+
+
def test_runs_from_inside_report_dir(root, monkeypatch):
monkeypatch.chdir(root / "report")
run_bundle(_args())
diff --git a/selftests/test_cli_report.py b/selftests/test_cli_report.py
index 6f31032e5..f396ff21d 100644
--- a/selftests/test_cli_report.py
+++ b/selftests/test_cli_report.py
@@ -432,6 +432,19 @@ def test_failed_html_clears_the_stamp(self, tmp_path, monkeypatch):
cli.run_report(_make_args())
assert read_render_stamp(report_dir / "_output") is None
+ def test_stamp_that_cannot_be_cleared_is_a_report_error(self, tmp_path, monkeypatch):
+ from vip import cli
+ from vip.errors import ReportError
+
+ self._report_dir(tmp_path, monkeypatch)
+
+ def fail(output_dir):
+ raise PermissionError("read-only file system")
+
+ monkeypatch.setattr("vip.cli.report.clear_render_stamp", fail)
+ with pytest.raises(ReportError, match="render stamp"):
+ cli.run_report(_make_args())
+
class TestSupportFileResolution:
"""troubleshooting.toml and feature files resolve from checkout or install."""
diff --git a/src/vip/bundle/summary.py b/src/vip/bundle/summary.py
index cbbc9c6f1..70c6a4d3e 100644
--- a/src/vip/bundle/summary.py
+++ b/src/vip/bundle/summary.py
@@ -153,8 +153,10 @@ def strip_url_userinfo(url: str) -> str:
return urlunsplit((parts.scheme, host, parts.path, "", ""))
-def _url_credentials(url: str, *, both_parts: bool = False) -> list[str]:
- """Return the credential parts of *url*: userinfo and secret query values.
+def _url_credentials(url: str, *, both_parts: bool = False) -> list[tuple[str, str]]:
+ """Return ``(part, value)`` for each credential in *url*.
+
+ The part is ``user``, ``password``, or ``query`` (a secret query value).
With ``user:password``, only the password is a credential, unless
*both_parts* is set. A Git remote can hold the token in either part, for
@@ -180,9 +182,12 @@ def _url_credentials(url: str, *, both_parts: bool = False) -> list[str]:
match = _USERINFO_RE.search(url)
userinfo = match.group(1) if match else ""
user, _, password = userinfo.partition(":")
- values = [user, password] if both_parts else [password or user]
- values += [value for key, value in query if key.lower() in SECRET_QUERY_KEYS]
- return [v for value in values for v in (value, unquote(value)) if v]
+ if both_parts:
+ values = [("user", user), ("password", password)]
+ else:
+ values = [("password", password) if password else ("user", user)]
+ values += [("query", value) for key, value in query if key.lower() in SECRET_QUERY_KEYS]
+ return [(part, v) for part, value in values for v in (value, unquote(value)) if v]
def _connection_string_parts(conn: str) -> list[tuple[str, str]]:
@@ -201,7 +206,7 @@ def _connection_string_parts(conn: str) -> list[tuple[str, str]]:
def _connection_string_secrets(label: str, conn: str) -> list[tuple[str, str]]:
"""Return the whole connection string and each credential part of it, with labels."""
pairs = [(label, conn)]
- pairs += [(f"{label} credentials", v) for v in _url_credentials(conn)]
+ pairs += [(f"{label} credentials", v) for _, v in _url_credentials(conn)]
pairs += [(f"{label} {key}", value) for key, value in _connection_string_parts(conn)]
return pairs
@@ -253,19 +258,23 @@ def secret_values(
git = config.workbench.git_test
if git is not None:
pairs.append(("workbench.git_test.token", git.token))
- label = "workbench.git_test.clone_url credentials"
- pairs += [(label, v) for v in _url_credentials(git.clone_url, both_parts=True)]
+ # Each part has its own label, so that --allow-match for a public user
+ # name does not also let the token through.
+ pairs += [
+ (f"workbench.git_test.clone_url {part}", v)
+ for part, v in _url_credentials(git.clone_url, both_parts=True)
+ ]
for ds in config.data_sources:
pairs += _connection_string_secrets(
f"data_sources.{ds.name}.connection_string", ds.connection_string
)
for product in ("connect", "workbench", "package_manager"):
url = config.product_config(product).url
- pairs += [(f"{product}.url credentials", v) for v in _url_credentials(url)]
- pairs += [("proxy.url credentials", v) for v in _url_credentials(config.proxy.url)]
+ pairs += [(f"{product}.url credentials", v) for _, v in _url_credentials(url)]
+ pairs += [("proxy.url credentials", v) for _, v in _url_credentials(config.proxy.url)]
pairs += [(var, os.environ.get(var, "")) for var in SECRET_ENV_VARS]
for var in PROXY_ENV_VARS:
- pairs += [(f"{var} credentials", v) for v in _url_credentials(os.environ.get(var, ""))]
+ pairs += [(f"{var} credentials", v) for _, v in _url_credentials(os.environ.get(var, ""))]
pairs += list(cache_secrets)
seen: set[str] = set()
diff --git a/src/vip/cli/bundle.py b/src/vip/cli/bundle.py
index 23cfaf4b3..82ab06f54 100644
--- a/src/vip/cli/bundle.py
+++ b/src/vip/cli/bundle.py
@@ -51,6 +51,17 @@ def _invocation_root(results: str | None) -> Path:
return cwd.parent if cwd.name == "report" else cwd
+def _report_dir(results: str | None) -> Path:
+ """Return the report directory, as a path in the form that the user typed.
+
+ With ``--results /report/results.json``, it is ``/report``, so
+ that the rendered files of that run are found from any directory.
+ """
+ if results and Path(results).resolve().parent.name == "report":
+ return Path(results).parent
+ return _resolve_report_dir()
+
+
def _config_path(args: argparse.Namespace, root: Path) -> Path | None:
"""Return the configuration file to read, or None when --no-config is set.
@@ -118,11 +129,10 @@ def _check_config_matches(config: VIPConfig, path: Path, results: bytes) -> None
def _cache_secrets(root: Path) -> list[tuple[str, str]]:
"""Return the secrets in the auth cache files that ``vip verify`` run from *root* can write.
- The legacy files in *root* are read first. ``auth_cache_path()`` can move
- or delete a legacy file when it migrates the cache to the user cache
- directory, so it is called only after that. It depends on the working
- directory, so it is called from *root*. It can return None when no cache
- directory is available.
+ The legacy files in *root* are read before ``auth_cache_path()`` is
+ called, so that a cache location that moves the legacy files cannot hide
+ them. ``auth_cache_path()`` depends on the working directory, so it is
+ called from *root*.
"""
legacy = root / AUTH_CACHE_FILENAME
pairs = auth_cache_secrets(legacy) + auth_cache_secrets(legacy.with_suffix(".meta.json"))
@@ -180,7 +190,7 @@ def _hit_error(hits: list[tuple[str, str]], collected: Collected) -> BundleError
def run_bundle(args: argparse.Namespace) -> None:
"""Write a zip of the report output. Refuse when a member holds a known secret."""
root = _invocation_root(args.results)
- report_dir = _resolve_report_dir()
+ report_dir = _report_dir(args.results)
results_path = Path(args.results) if args.results else report_dir / "results.json"
config_path = _config_path(args, root)
# VIPConfig() still reads the VIP_* secret variables in its __post_init__ methods.
diff --git a/src/vip/cli/report.py b/src/vip/cli/report.py
index 6fa98b630..6ebed1090 100644
--- a/src/vip/cli/report.py
+++ b/src/vip/cli/report.py
@@ -166,7 +166,10 @@ def run_report(args: argparse.Namespace) -> None:
# render leaves the old pages without a stamp.
output_dir = report_dir / "_output"
digest = results_digest(results_dest.read_bytes())
- clear_render_stamp(output_dir)
+ try:
+ clear_render_stamp(output_dir)
+ except OSError as exc:
+ raise ReportError(f"cannot delete the render stamp in {output_dir}: {exc}") from exc
# The HTML pages and the PDF render as separate quarto invocations on
# purpose. One combined `quarto render` ties their fates together: on a
From 43ff2463fd8bd9d021193d528a2ff54fb2a33679 Mon Sep 17 00:00:00 2001
From: Tim
Date: Thu, 12 Nov 2026 09:13:03 -0500
Subject: [PATCH 05/11] fix(bundle): do not treat an email address in the auth
cache as a secret
Okta keeps the login name in the ln cookie, and test output often prints
it, so every bundle after an Okta login stopped on a false match.
---
selftests/test_bundle_summary.py | 25 +++++++++++++++++++++++++
src/vip/bundle/summary.py | 19 +++++++++++++++----
2 files changed, 40 insertions(+), 4 deletions(-)
diff --git a/selftests/test_bundle_summary.py b/selftests/test_bundle_summary.py
index f7f7f03f0..d38460749 100644
--- a/selftests/test_bundle_summary.py
+++ b/selftests/test_bundle_summary.py
@@ -329,6 +329,31 @@ def test_secret_values_cover_every_source(full_config, tmp_path):
assert not {"cuser", "puser", "dbuser"} & values
+def test_auth_cache_secrets_skip_login_names(tmp_path):
+ # Okta keeps the login name in the "ln" cookie. Test output often prints
+ # that name, and a login name is not a credential.
+ state = tmp_path / ".vip-auth-cache.json"
+ state.write_text(
+ json.dumps(
+ {
+ "cookies": [
+ {"name": "ln", "value": "test.user@example.com"},
+ {"name": "ln2", "value": "test.user%40example.com"},
+ {"name": "sid", "value": "a1b2c3@example-0123456789"},
+ ],
+ "origins": [
+ {
+ "origin": "https://login.example.com",
+ "localStorage": [{"name": "user", "value": "test.user@example.com"}],
+ }
+ ],
+ }
+ ),
+ encoding="utf-8",
+ )
+ assert auth_cache_secrets(state) == [("auth cache cookie sid", "a1b2c3@example-0123456789")]
+
+
@pytest.mark.parametrize(
"clone_url",
[
diff --git a/src/vip/bundle/summary.py b/src/vip/bundle/summary.py
index 70c6a4d3e..a1f8c2135 100644
--- a/src/vip/bundle/summary.py
+++ b/src/vip/bundle/summary.py
@@ -7,7 +7,7 @@
import re
from collections.abc import Sequence
from pathlib import Path
-from typing import Any
+from typing import Any, TypeGuard
from urllib.parse import parse_qsl, unquote, urlsplit, urlunsplit
from vip.config import VIPConfig
@@ -118,6 +118,9 @@
)
# Short cookie values ("1", "true") match normal report text. Session cookies are long.
MIN_COOKIE_LENGTH = 16
+# A value that is only an email address is a login name, not a credential. Okta
+# keeps it in the "ln" cookie, and test output often prints it.
+_EMAIL_RE = re.compile(r"[^@\s]+@[^@\s]+\.[A-Za-z]{2,}")
# Connection string keys whose values are credentials. ODBC, JDBC, and Azure use these.
CONNECTION_SECRET_KEYS = SECRET_QUERY_KEYS | frozenset(
{"accountkey", "sharedaccesskey", "sharedaccesssignature", "privatekey", "private_key"}
@@ -211,12 +214,20 @@ def _connection_string_secrets(label: str, conn: str) -> list[tuple[str, str]]:
return pairs
+def _is_cache_secret(value: object) -> TypeGuard[str]:
+ """Return True when an auth cache value is long enough and is not a login name."""
+ if not isinstance(value, str) or len(value) < MIN_COOKIE_LENGTH:
+ return False
+ return _EMAIL_RE.fullmatch(unquote(value)) is None
+
+
def auth_cache_secrets(path: Path) -> list[tuple[str, str]]:
"""Return ``(label, value)`` for each secret in one auth cache file.
``.vip-auth-cache.meta.json`` holds the minted Connect key.
``.vip-auth-cache.json`` is a Playwright storage state with cookies and
- localStorage values. A file that is missing or does not parse gives no values.
+ localStorage values. A short value or an email address is not a secret. A
+ file that is missing or does not parse gives no values.
"""
try:
data = json.loads(path.read_text(encoding="utf-8"))
@@ -229,13 +240,13 @@ def auth_cache_secrets(path: Path) -> list[tuple[str, str]]:
pairs.append(("auth cache api_key", data["api_key"]))
for cookie in data.get("cookies") or []:
value = cookie.get("value") if isinstance(cookie, dict) else None
- if isinstance(value, str) and len(value) >= MIN_COOKIE_LENGTH:
+ if _is_cache_secret(value):
pairs.append((f"auth cache cookie {cookie.get('name', '')}", value))
for origin in data.get("origins") or []:
items = origin.get("localStorage") if isinstance(origin, dict) else None
for item in items or []:
value = item.get("value") if isinstance(item, dict) else None
- if isinstance(value, str) and len(value) >= MIN_COOKIE_LENGTH:
+ if _is_cache_secret(value):
pairs.append((f"auth cache localStorage {item.get('name', '')}", value))
return pairs
From ce2debced93ede62ba30227a9f713ac6457723d4 Mon Sep 17 00:00:00 2001
From: Tim
Date: Thu, 12 Nov 2026 09:51:13 -0500
Subject: [PATCH 06/11] fix(bundle): record what the secret scan knew, pair
failures.json by run
The manifest and the output now give the number of known credentials by
source, and the command warns if the scan knew none. failures.json is
matched to results.json by generated_at, so an edit of results.json does
not drop it. The email exemption no longer accepts a URL with
credentials or a query string. vip report gives a ReportError for a
results.json that it cannot read.
---
selftests/test_bundle_archive.py | 28 +++++++++++++++++++
selftests/test_bundle_summary.py | 18 ++++++++++++
selftests/test_cli_bundle.py | 48 ++++++++++++++++++++++++++++----
selftests/test_cli_report.py | 10 +++++++
src/vip/bundle/archive.py | 37 ++++++++++++++++++++----
src/vip/bundle/summary.py | 6 ++--
src/vip/cli/bundle.py | 38 +++++++++++++++++++------
src/vip/cli/report.py | 5 +++-
website/src/pages/guide.astro | 14 ++++++----
9 files changed, 175 insertions(+), 29 deletions(-)
diff --git a/selftests/test_bundle_archive.py b/selftests/test_bundle_archive.py
index 6a460201b..461571370 100644
--- a/selftests/test_bundle_archive.py
+++ b/selftests/test_bundle_archive.py
@@ -175,6 +175,34 @@ def test_stale_sidecar_is_left_out(tmp_path):
assert any(str(report / "failures.json") in w for w in got.warnings)
+def _seed_run_pair(report: Path, results_run: str, failures_run: str) -> None:
+ """Write results.json and failures.json with a ``generated_at`` value each."""
+ results = json.dumps({"generated_at": results_run, "results": []})
+ _seed(report, sidecars=(), rendered=(), results=results)
+ failures = json.dumps({"generated_at": failures_run, "failures": []})
+ _write(report / "failures.json", failures, 1_000_001.0)
+
+
+def test_failures_json_of_the_same_run_stays_when_results_is_newer(tmp_path):
+ # An edit of results.json gives it a newer file time than failures.json.
+ report = tmp_path / "report"
+ _seed_run_pair(report, "2026-11-12T14:00:00+00:00", "2026-11-12T14:00:00+00:00")
+ os.utime(report / "results.json", (1_000_500.0, 1_000_500.0))
+ got = collect_members(report / "results.json", report)
+ assert "report/failures.json" in got.members
+ assert got.left_out == []
+
+
+def test_failures_json_of_another_run_is_left_out_when_it_is_newer(tmp_path):
+ report = tmp_path / "report"
+ _seed_run_pair(report, "2026-11-12T14:00:00+00:00", "2026-11-12T15:00:00+00:00")
+ got = collect_members(report / "results.json", report)
+ assert "report/failures.json" not in got.members
+ assert got.left_out == [
+ {"name": "report/failures.json", "reason": "is from a different run than results.json"}
+ ]
+
+
@pytest.mark.skipif(os.name == "nt", reason="symlinks need extra rights on Windows")
def test_symlinks_are_left_out(tmp_path):
report = tmp_path / "report"
diff --git a/selftests/test_bundle_summary.py b/selftests/test_bundle_summary.py
index d38460749..968225a1d 100644
--- a/selftests/test_bundle_summary.py
+++ b/selftests/test_bundle_summary.py
@@ -416,3 +416,21 @@ def test_auth_cache_secrets_skip_missing_or_bad_files(tmp_path):
listed.write_text("[]", encoding="utf-8")
for path in (bad, listed, tmp_path / "absent.json"):
assert auth_cache_secrets(path) == []
+
+
+@pytest.mark.parametrize(
+ "value",
+ [
+ "user:PASSWORD-0123456789@host.example.com",
+ "https://user:PASSWORD-0123@host.example.com",
+ "token=ABCDEFGH01234567&email=a@example.com",
+ "TOKEN-0123456789|test.user@example.com",
+ 'prefix "test.user@example.com" TOKEN-0123456789',
+ "a@b@example.com-0123456789",
+ "no-at-sign-0123456789.example.com",
+ ],
+)
+def test_auth_cache_value_that_is_more_than_an_email_is_a_secret(tmp_path, value):
+ state = tmp_path / ".vip-auth-cache.json"
+ state.write_text(json.dumps({"cookies": [{"name": "sid", "value": value}]}), encoding="utf-8")
+ assert auth_cache_secrets(state) == [("auth cache cookie sid", value)]
diff --git a/selftests/test_cli_bundle.py b/selftests/test_cli_bundle.py
index db86e0797..f22b4a315 100644
--- a/selftests/test_cli_bundle.py
+++ b/selftests/test_cli_bundle.py
@@ -188,6 +188,41 @@ def test_manifest_lists_members_and_missing_files(root):
assert manifest["secret_scan"] == "config"
+def test_manifest_records_how_many_secrets_the_scan_knew(root, capsys):
+ run_bundle(_args(output="b.zip"))
+ with zipfile.ZipFile(root / "b.zip") as zf:
+ manifest = json.loads(zf.read("manifest.json"))
+ # vip.toml holds nine credential values. The cache holds a cookie and a minted key.
+ assert manifest["secret_scan_sources"] == {
+ "configuration_and_environment": 9,
+ "auth_cache": 2,
+ }
+ out = capsys.readouterr().out
+ assert "9 from the configuration and the environment, 2 from the auth cache" in out
+
+
+def test_scan_with_no_known_secret_warns(root, capsys):
+ (root / "vip.toml").unlink()
+ (root / ".vip-auth-cache.json").unlink()
+ (root / ".vip-auth-cache.meta.json").unlink()
+ run_bundle(_args(output="b.zip", no_config=True))
+ with zipfile.ZipFile(root / "b.zip") as zf:
+ manifest = json.loads(zf.read("manifest.json"))
+ assert manifest["secret_scan_sources"] == {
+ "configuration_and_environment": 0,
+ "auth_cache": 0,
+ }
+ assert any("knew no credential" in w for w in manifest["warnings"])
+ assert "Warning: the secret scan knew no credential" in capsys.readouterr().err
+
+
+def test_scan_with_no_auth_cache_does_not_warn_when_the_config_has_secrets(root, capsys):
+ (root / ".vip-auth-cache.json").unlink()
+ (root / ".vip-auth-cache.meta.json").unlink()
+ run_bundle(_args(output="b.zip"))
+ assert "knew no credential" not in capsys.readouterr().err
+
+
def test_secret_in_failures_json_blocks_bundle(root, capsys):
_write_report(root, failures_extra=f"key={CONNECT_KEY}")
with pytest.raises(BundleError) as exc:
@@ -251,8 +286,8 @@ def test_manifest_is_scanned_too(root, monkeypatch):
run_bundle(_args(output="b.zip", results=str(run_dir / "results.json")))
-def test_minted_key_in_legacy_cache_blocks_bundle(root, tmp_path, monkeypatch):
- # auth_cache_path() points elsewhere, so only the legacy file holds the key.
+def test_minted_key_in_root_cache_blocks_bundle(root, tmp_path, monkeypatch):
+ # auth_cache_path() points elsewhere, so only the file in the root holds the key.
other = tmp_path / "other-cache" / ".vip-auth-cache.json"
monkeypatch.setattr(bundle_module, "auth_cache_path", lambda: other)
_write_report(root, results_extra=MINTED_KEY)
@@ -260,8 +295,9 @@ def test_minted_key_in_legacy_cache_blocks_bundle(root, tmp_path, monkeypatch):
run_bundle(_args(output="b.zip"))
-def test_legacy_cache_is_read_before_auth_cache_path(root, monkeypatch):
- # After PR 793, auth_cache_path() can move or delete the legacy files.
+def test_root_cache_is_read_before_auth_cache_path(root, monkeypatch):
+ # The cache files in the root must be read before auth_cache_path() runs,
+ # because an auth_cache_path() with a side effect can remove them.
def migrating_auth_cache_path():
(root / ".vip-auth-cache.meta.json").unlink()
return root / "moved" / ".vip-auth-cache.json"
@@ -273,7 +309,7 @@ def migrating_auth_cache_path():
def test_results_path_sets_the_root(root, tmp_path, monkeypatch):
- # Run from another directory: vip.toml and the legacy cache come from the
+ # Run from another directory: vip.toml and the auth cache come from the
# directory that holds report/.
elsewhere = tmp_path / "elsewhere"
elsewhere.mkdir()
@@ -301,7 +337,7 @@ def test_config_with_another_product_url_fails(root):
def test_key_in_auth_cache_path_blocks_bundle(root, tmp_path, monkeypatch):
# The auth cache can live outside the invocation directory. The bundle
- # must read the file at auth_cache_path() too, not only the legacy file.
+ # must read the file at auth_cache_path() too, not only the file in the root.
user_cache = tmp_path / "user-cache" / ".vip-auth-cache.json"
user_cache.parent.mkdir()
user_cache.with_suffix(".meta.json").write_text(
diff --git a/selftests/test_cli_report.py b/selftests/test_cli_report.py
index f396ff21d..28cebe20b 100644
--- a/selftests/test_cli_report.py
+++ b/selftests/test_cli_report.py
@@ -445,6 +445,16 @@ def fail(output_dir):
with pytest.raises(ReportError, match="render stamp"):
cli.run_report(_make_args())
+ def test_results_that_cannot_be_read_is_a_report_error(self, tmp_path, monkeypatch):
+ from vip import cli
+ from vip.errors import ReportError
+
+ report_dir = self._report_dir(tmp_path, monkeypatch)
+ (report_dir / "results.json").unlink()
+ (report_dir / "results.json").mkdir()
+ with pytest.raises(ReportError, match="cannot read"):
+ cli.run_report(_make_args())
+
class TestSupportFileResolution:
"""troubleshooting.toml and feature files resolve from checkout or install."""
diff --git a/src/vip/bundle/archive.py b/src/vip/bundle/archive.py
index 7b8b04bd7..6d9b20aed 100644
--- a/src/vip/bundle/archive.py
+++ b/src/vip/bundle/archive.py
@@ -101,11 +101,24 @@ def _check_results_file(results_path: Path) -> bytes:
return data
+def _generated_at(data: bytes) -> str | None:
+ """Return the ``generated_at`` value of a results.json or a failures.json."""
+ try:
+ parsed = json.loads(data)
+ except (ValueError, RecursionError):
+ return None
+ value = parsed.get("generated_at") if isinstance(parsed, dict) else None
+ return value if isinstance(value, str) and value else None
+
+
def collect_members(results_path: Path, report_dir: Path) -> Collected:
"""Read the allowlisted report files.
- Sidecar files come from the directory of *results_path*. A sidecar that is
- older than results.json is from an earlier run, so it is left out.
+ Sidecar files come from the directory of *results_path*. ``vip verify``
+ writes the same ``generated_at`` value to results.json and failures.json,
+ so a failures.json with a different value is from a different run and is
+ left out. A sidecar with no such value to compare is left out if it is
+ older than results.json.
Rendered files come from ``/_output/``. A rendered file is
included only if the render stamp names it and the SHA-256 of this
results.json. The secret scan cannot read the text in the PDF, so the PDF
@@ -117,6 +130,7 @@ def collect_members(results_path: Path, report_dir: Path) -> Collected:
out.members["report/results.json"] = data
out.sources["report/results.json"] = results_path
results_mtime = results_path.stat().st_mtime
+ results_run = _generated_at(data)
sidecar_dir = results_path.parent
for name in REPORT_SIDECAR_FILES[1:]:
@@ -125,11 +139,22 @@ def collect_members(results_path: Path, report_dir: Path) -> Collected:
out.missing.append(archive_name)
elif not _is_regular_file_in(path, sidecar_dir):
_leave_out(out, archive_name, path, "is a link or is not a regular file", "")
- elif path.stat().st_mtime < results_mtime:
- hint = " If you copied the report directory without file times, run 'vip verify' again."
- _leave_out(out, archive_name, path, "is older than results.json", hint)
else:
- out.members[archive_name] = _read(path)
+ content = _read(path)
+ sidecar_run = _generated_at(content) if name == "failures.json" else None
+ if results_run is not None and sidecar_run is not None:
+ if sidecar_run != results_run:
+ reason = "is from a different run than results.json"
+ _leave_out(out, archive_name, path, reason, "")
+ continue
+ elif path.stat().st_mtime < results_mtime:
+ hint = (
+ " If you copied the report directory without file times, "
+ "run 'vip verify' again."
+ )
+ _leave_out(out, archive_name, path, "is older than results.json", hint)
+ continue
+ out.members[archive_name] = content
out.sources[archive_name] = path
output_dir = report_dir / "_output"
diff --git a/src/vip/bundle/summary.py b/src/vip/bundle/summary.py
index a1f8c2135..4fe93101c 100644
--- a/src/vip/bundle/summary.py
+++ b/src/vip/bundle/summary.py
@@ -119,8 +119,10 @@
# Short cookie values ("1", "true") match normal report text. Session cookies are long.
MIN_COOKIE_LENGTH = 16
# A value that is only an email address is a login name, not a credential. Okta
-# keeps it in the "ln" cookie, and test output often prints it.
-_EMAIL_RE = re.compile(r"[^@\s]+@[^@\s]+\.[A-Za-z]{2,}")
+# keeps it in the "ln" cookie, and test output often prints it. The pattern
+# has no ":", "/", "=", "&", or "|", so a URL with credentials, a query
+# string, or a token next to an address stays a secret.
+_EMAIL_RE = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}")
# Connection string keys whose values are credentials. ODBC, JDBC, and Azure use these.
CONNECTION_SECRET_KEYS = SECRET_QUERY_KEYS | frozenset(
{"accountkey", "sharedaccesskey", "sharedaccesssignature", "privatekey", "private_key"}
diff --git a/src/vip/cli/bundle.py b/src/vip/cli/bundle.py
index 82ab06f54..a2dc122e6 100644
--- a/src/vip/cli/bundle.py
+++ b/src/vip/cli/bundle.py
@@ -36,7 +36,7 @@
def _invocation_root(results: str | None) -> Path:
- """Return the directory that holds ``vip.toml``, ``report/``, and the legacy auth cache.
+ """Return the directory that holds ``vip.toml``, ``report/``, and the auth cache files.
With ``--results /report/results.json``, the root is ````.
Otherwise it is the working directory. ``_resolve_report_dir`` treats a
@@ -129,16 +129,16 @@ def _check_config_matches(config: VIPConfig, path: Path, results: bytes) -> None
def _cache_secrets(root: Path) -> list[tuple[str, str]]:
"""Return the secrets in the auth cache files that ``vip verify`` run from *root* can write.
- The legacy files in *root* are read before ``auth_cache_path()`` is
- called, so that a cache location that moves the legacy files cannot hide
- them. ``auth_cache_path()`` depends on the working directory, so it is
- called from *root*.
+ The files in *root* are read first, and then the files at
+ ``auth_cache_path()`` if that is a different place.
+ ``auth_cache_path()`` depends on the working directory, so it is called
+ from *root*.
"""
- legacy = root / AUTH_CACHE_FILENAME
- pairs = auth_cache_secrets(legacy) + auth_cache_secrets(legacy.with_suffix(".meta.json"))
+ in_root = root / AUTH_CACHE_FILENAME
+ pairs = auth_cache_secrets(in_root) + auth_cache_secrets(in_root.with_suffix(".meta.json"))
with contextlib.chdir(root):
cache = auth_cache_path()
- if cache is not None and cache != legacy:
+ if cache is not None and cache != in_root:
pairs += auth_cache_secrets(cache) + auth_cache_secrets(cache.with_suffix(".meta.json"))
return pairs
@@ -206,7 +206,21 @@ def run_bundle(args: argparse.Namespace) -> None:
"--no-config is set, so the secret scan used only the environment "
"variables and the auth cache. Look at the zip before you send it."
)
- secrets = secret_values(config, _cache_secrets(root))
+ cache_secrets = _cache_secrets(root)
+ secrets = secret_values(config, cache_secrets)
+ # secret_values drops repeated values, so the cache count is the remainder.
+ config_count = len(secret_values(config))
+ scan_sources = {
+ "configuration_and_environment": config_count,
+ "auth_cache": len(secrets) - config_count,
+ }
+ if not secrets:
+ warnings.append(
+ "the secret scan knew no credential: the configuration, the environment "
+ f"variables, and the auth cache for {root} hold none. If 'vip verify' ran "
+ "with a credential, run 'vip bundle' with the same configuration, "
+ "environment variables, and directory. Look at the zip before you send it."
+ )
warnings += [
f"the value of {label} is shorter than 4 characters, so the secret scan "
"cannot look for it. Look at the zip before you send it."
@@ -252,6 +266,7 @@ def run_bundle(args: argparse.Namespace) -> None:
"left_out": collected.left_out,
"config_summary": config_path is not None,
"secret_scan": "config" if config_path is not None else "no-config",
+ "secret_scan_sources": scan_sources,
"allowed_matches": [{"member": n, "label": label} for n, label in allowed_hits],
"warnings": warnings,
}
@@ -273,3 +288,8 @@ def run_bundle(args: argparse.Namespace) -> None:
f"could not write {output}: {exc}. Pass --output PATH to write it somewhere else."
) from exc
print(f"Bundle written: {output} ({len(members)} files)")
+ print(
+ f"The secret scan looked for {len(secrets)} known values: "
+ f"{scan_sources['configuration_and_environment']} from the configuration and the "
+ f"environment, {scan_sources['auth_cache']} from the auth cache."
+ )
diff --git a/src/vip/cli/report.py b/src/vip/cli/report.py
index 6ebed1090..e95217d31 100644
--- a/src/vip/cli/report.py
+++ b/src/vip/cli/report.py
@@ -165,7 +165,10 @@ def run_report(args: argparse.Namespace) -> None:
# the results.json in the bundle. Clear the stamp first, so that a failed
# render leaves the old pages without a stamp.
output_dir = report_dir / "_output"
- digest = results_digest(results_dest.read_bytes())
+ try:
+ digest = results_digest(results_dest.read_bytes())
+ except OSError as exc:
+ raise ReportError(f"cannot read {results_dest}: {exc}") from exc
try:
clear_render_stamp(output_dir)
except OSError as exc:
diff --git a/website/src/pages/guide.astro b/website/src/pages/guide.astro
index bb7cedb1f..c050a5d4d 100644
--- a/website/src/pages/guide.astro
+++ b/website/src/pages/guide.astro
@@ -404,7 +404,11 @@ cd report && quarto publish connect --server https://connect.example.com
The manifest in the zip records it. The scan cannot find a
credential that it does not know, for example one that an
extension test printed or one that was set only in the shell that
- ran vip verify. Look at the zip before you send it.
+ ran vip verify. The manifest and the last output
+ line give the number of credentials that the scan knew, from the
+ configuration and the environment and from the auth cache. If the
+ number is lower than you expect, the scan did not have a
+ credential of the run. Look at the zip before you send it.
The command needs the vip.toml that
@@ -414,10 +418,10 @@ cd report && quarto publish connect --server https://connect.example.com
uses only the environment and the auth cache. A rendered page that
vip report did not make from the current
results.json is left out. Run vip report
- again to include it. A failure, JUnit, or SARIF file that is older
- than results.json is from an earlier run, so it is
- left out too. The manifest lists each file that is left out, with
- the reason.
+ again to include it. A failures.json from a different
+ run than results.json is left out too, and so is a
+ JUnit or SARIF file that is older than results.json.
+ The manifest lists each file that is left out, with the reason.
From a6f4c9553de70dee9700a687818a1c09e7a970ac Mon Sep 17 00:00:00 2001
From: Tim
Date: Thu, 12 Nov 2026 10:06:02 -0500
Subject: [PATCH 07/11] test(bundle): cover the secret scan cases that mutation
tests found
Add tests for the email rule shape, the 16-character cache floor, URL
credentials for each product, the proxy variables, a secret in each
report file, the session cookie from both cache places, links in the
report directory, and two more encoded forms.
---
selftests/test_bundle_archive.py | 31 +++++++++++++++-
selftests/test_bundle_summary.py | 63 ++++++++++++++++++++++++++++++++
selftests/test_cli_bundle.py | 40 ++++++++++++++++++++
3 files changed, 133 insertions(+), 1 deletion(-)
diff --git a/selftests/test_bundle_archive.py b/selftests/test_bundle_archive.py
index 461571370..7481dc8fb 100644
--- a/selftests/test_bundle_archive.py
+++ b/selftests/test_bundle_archive.py
@@ -87,7 +87,10 @@ def test_missing_results_raises(tmp_path):
collect_members(tmp_path / "report/results.json", tmp_path / "report")
-@pytest.mark.parametrize("name", ["vip.toml", ".vip-auth-cache.json", ".vip-install.json"])
+@pytest.mark.parametrize(
+ "name",
+ ["vip.toml", ".vip-auth-cache.json", ".vip-auth-cache.meta.json", ".vip-install.json"],
+)
def test_results_cannot_name_a_credential_file(tmp_path, name):
path = tmp_path / name
path.write_text('{"results": []}', encoding="utf-8")
@@ -217,6 +220,19 @@ def test_symlinks_are_left_out(tmp_path):
assert sum("is a link" in w for w in got.warnings) == 2
+@pytest.mark.skipif(os.name == "nt", reason="symlinks need extra rights on Windows")
+def test_links_to_files_in_the_same_directory_are_left_out(tmp_path):
+ report = tmp_path / "report"
+ _seed(report, sidecars=(), rendered=("index.html",))
+ _write(report / "other.json", "x", 1_000_001.0)
+ (report / "failures.json").symlink_to(report / "other.json")
+ stamp = report / "_output" / STAMP_NAME
+ stamp.rename(report / "_output" / "real-stamp.json")
+ stamp.symlink_to(report / "_output" / "real-stamp.json")
+ got = collect_members(report / "results.json", report)
+ assert set(got.members) == {"report/results.json"}
+
+
@pytest.mark.skipif(os.name == "nt", reason="symlinks need extra rights on Windows")
def test_symlinked_output_dir_is_left_out(tmp_path):
report = tmp_path / "report"
@@ -276,6 +292,19 @@ def test_find_secret_hits_sees_encoded_forms(secret, stored):
assert find_secret_hits(members, [("auth.password", secret)]) == [("report/a", "auth.password")]
+@pytest.mark.parametrize(
+ ("secret", "stored"),
+ [
+ ("pass word-0123", b"pass%20word-0123"), # quote gives %20, quote_plus gives +
+ ("Pa\\nss-w0rd9", b"Pa\\nss-w0rd9"), # the value holds a backslash and an n
+ ],
+)
+def test_find_secret_hits_sees_a_space_and_a_backslash(secret, stored):
+ assert find_secret_hits({"report/a": b"x " + stored + b" y"}, [("k", secret)]) == [
+ ("report/a", "k")
+ ]
+
+
def test_find_secret_hits_finds_nothing_in_clean_members():
members = {"report/a": b"nothing here", "report/b": b"clean"}
assert find_secret_hits(members, [("connect.api_key", "ABC123")]) == []
diff --git a/selftests/test_bundle_summary.py b/selftests/test_bundle_summary.py
index 968225a1d..cd08de477 100644
--- a/selftests/test_bundle_summary.py
+++ b/selftests/test_bundle_summary.py
@@ -427,10 +427,73 @@ def test_auth_cache_secrets_skip_missing_or_bad_files(tmp_path):
"TOKEN-0123456789|test.user@example.com",
'prefix "test.user@example.com" TOKEN-0123456789',
"a@b@example.com-0123456789",
+ "tok@en-0123456789@example.com",
"no-at-sign-0123456789.example.com",
+ '{"login":"test.user@example.com","t":"abc"}',
+ "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ2aXAifQ.signature",
],
)
def test_auth_cache_value_that_is_more_than_an_email_is_a_secret(tmp_path, value):
state = tmp_path / ".vip-auth-cache.json"
state.write_text(json.dumps({"cookies": [{"name": "sid", "value": value}]}), encoding="utf-8")
assert auth_cache_secrets(state) == [("auth cache cookie sid", value)]
+
+
+def test_auth_cache_value_floor_is_exactly_16_characters(tmp_path):
+ at, below = "a1b2c3d4e5f6g7h8", "a1b2c3d4e5f6g7h"
+ state = tmp_path / ".vip-auth-cache.json"
+ cookies = [{"name": "at", "value": at}, {"name": "below", "value": below}]
+ state.write_text(json.dumps({"cookies": cookies}), encoding="utf-8")
+ assert auth_cache_secrets(state) == [("auth cache cookie at", at)]
+
+
+def test_email_shaped_password_and_minted_key_are_secrets(tmp_path):
+ # The login name rule is only for cookies and localStorage values.
+ meta = tmp_path / ".vip-auth-cache.meta.json"
+ meta.write_text(json.dumps({"api_key": "svc.account@example.com"}), encoding="utf-8")
+ config = VIPConfig(auth=AuthConfig(password="svc.user@example.com"))
+ values = {value for _, value in secret_values(config, auth_cache_secrets(meta))}
+ assert {"svc.user@example.com", "svc.account@example.com"} <= values
+
+
+def test_each_product_url_credential_is_a_secret_and_is_not_in_the_summary():
+ config = VIPConfig(
+ connect=ConnectConfig(url="https://u:URL-PASS-connect1@connect.example.com"),
+ workbench=WorkbenchConfig(url="https://u:URL-PASS-workbench1@wb.example.com"),
+ package_manager=PackageManagerConfig(url="https://u:URL-PASS-pm1@pm.example.com"),
+ )
+ values = {value for _, value in secret_values(config)}
+ assert {"URL-PASS-connect1", "URL-PASS-workbench1", "URL-PASS-pm1"} <= values
+ assert "URL-PASS" not in json.dumps(build_config_summary(config))
+
+
+def test_url_credential_is_a_secret_raw_and_percent_decoded():
+ config = VIPConfig(connect=ConnectConfig(url="https://u:p%40ss-0123@connect.example.com"))
+ assert {"p%40ss-0123", "p@ss-0123"} <= {value for _, value in secret_values(config)}
+
+
+_PROXY_VARS = ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy")
+
+
+@pytest.mark.parametrize("var", _PROXY_VARS)
+def test_each_proxy_variable_is_read(monkeypatch, var):
+ for other in _PROXY_VARS:
+ monkeypatch.delenv(other, raising=False)
+ monkeypatch.setenv(var, "http://u:ENV-PROXY-PASS@proxy:3128")
+ assert "ENV-PROXY-PASS" in {value for _, value in secret_values(VIPConfig())}
+
+
+@pytest.mark.parametrize(
+ ("url", "expected"),
+ [
+ # urlsplit raises ValueError for this host.
+ ("http://u:SECRET@[::1/x?token=T", "http://[::1/x"),
+ # A Git remote with no scheme.
+ (
+ "x-access-token:SECRET@github.com/org/repo.git?access_token=T#f",
+ "github.com/org/repo.git",
+ ),
+ ],
+)
+def test_strip_url_userinfo_handles_a_bad_host_and_a_bare_remote(url, expected):
+ assert strip_url_userinfo(url) == expected
diff --git a/selftests/test_cli_bundle.py b/selftests/test_cli_bundle.py
index f22b4a315..35c750bbc 100644
--- a/selftests/test_cli_bundle.py
+++ b/selftests/test_cli_bundle.py
@@ -238,6 +238,46 @@ def test_secret_in_failures_json_blocks_bundle(root, capsys):
assert _leftovers(root) == []
+@pytest.mark.parametrize(
+ "member",
+ [
+ "junit.xml",
+ "results.sarif",
+ "_output/index.html",
+ "_output/details.html",
+ "_output/vip-report.pdf",
+ ],
+)
+def test_secret_in_each_report_file_blocks_bundle(root, member):
+ report = root / "report"
+ _write(report / member, "x SECRET-pm-token y", RESULTS_MTIME + 5)
+ digest = results_digest((report / "results.json").read_bytes())
+ record_rendered(report / "_output", digest, ("index.html", "details.html", "vip-report.pdf"))
+ with pytest.raises(BundleError, match=r"package_manager\.token") as exc:
+ run_bundle(_args(output="b.zip"))
+ assert Path(member).name in str(exc.value)
+ assert not (root / "b.zip").exists()
+
+
+def test_session_cookie_in_root_cache_blocks_bundle(root):
+ _write_report(root, results_extra=SESSION_COOKIE)
+ with pytest.raises(BundleError, match="auth cache cookie session"):
+ run_bundle(_args(output="b.zip"))
+
+
+def test_session_cookie_in_auth_cache_path_blocks_bundle(root, tmp_path, monkeypatch):
+ user_cache = tmp_path / "user-cache" / ".vip-auth-cache.json"
+ user_cache.parent.mkdir()
+ user_cache.write_text(
+ json.dumps({"cookies": [{"name": "sid", "value": "USER-CACHE-COOKIE-0123"}]}),
+ encoding="utf-8",
+ )
+ monkeypatch.setattr(bundle_module, "auth_cache_path", lambda: user_cache)
+ _write_report(root, results_extra="USER-CACHE-COOKIE-0123")
+ with pytest.raises(BundleError, match="auth cache cookie sid"):
+ run_bundle(_args(output="b.zip"))
+
+
def test_secret_in_a_made_member_names_it(root):
# A weak password that is also a setting that is not secret.
toml = (root / "vip.toml").read_text(encoding="utf-8")
From a3967fbe8889202d45d23bc0aff866a7361ed7b8 Mon Sep 17 00:00:00 2001
From: Tim
Date: Thu, 12 Nov 2026 11:18:30 -0500
Subject: [PATCH 08/11] fix(bundle): pair junit and sarif by run, show run
credentials in the summary
vip verify now writes generated_at to junit.xml and results.sarif, and
the bundle keeps a file that holds the value of results.json. The
configuration summary says what the auth cache of the run holds and
what the *_set flags describe. A directory with no run gives the error
for the missing results.json first.
---
selftests/test_bundle_archive.py | 23 +++++++++++++++++++++
selftests/test_cli_bundle.py | 24 ++++++++++++++++++++++
selftests/test_reporting.py | 27 ++++++++++++++++++++++++
src/vip/bundle/archive.py | 10 +++++----
src/vip/cli/bundle.py | 26 ++++++++++++++++++++++--
src/vip/reporting.py | 35 ++++++++++++++++++++------------
website/src/pages/guide.astro | 12 ++++++++---
7 files changed, 135 insertions(+), 22 deletions(-)
diff --git a/selftests/test_bundle_archive.py b/selftests/test_bundle_archive.py
index 7481dc8fb..6c2bfbac2 100644
--- a/selftests/test_bundle_archive.py
+++ b/selftests/test_bundle_archive.py
@@ -196,6 +196,29 @@ def test_failures_json_of_the_same_run_stays_when_results_is_newer(tmp_path):
assert got.left_out == []
+@pytest.mark.parametrize("name", ["junit.xml", "results.sarif"])
+def test_sidecar_with_the_run_time_stays_when_results_is_newer(tmp_path, name):
+ # An edit of results.json gives it a newer file time than the sidecar.
+ report = tmp_path / "report"
+ run = "2026-11-12T14:00:00+00:00"
+ results = json.dumps({"generated_at": run, "results": []})
+ _seed(report, sidecars=(), rendered=(), results=results, results_mtime=1_000_500.0)
+ _write(report / name, f'', 1_000_001.0)
+ got = collect_members(report / "results.json", report)
+ assert f"report/{name}" in got.members
+ assert got.left_out == []
+
+
+@pytest.mark.parametrize("name", ["junit.xml", "results.sarif"])
+def test_older_sidecar_with_another_run_time_is_left_out(tmp_path, name):
+ report = tmp_path / "report"
+ results = json.dumps({"generated_at": "2026-11-12T14:00:00+00:00", "results": []})
+ _seed(report, sidecars=(), rendered=(), results=results, results_mtime=1_000_500.0)
+ _write(report / name, '', 1_000_001.0)
+ got = collect_members(report / "results.json", report)
+ assert got.left_out == [{"name": f"report/{name}", "reason": "is older than results.json"}]
+
+
def test_failures_json_of_another_run_is_left_out_when_it_is_newer(tmp_path):
report = tmp_path / "report"
_seed_run_pair(report, "2026-11-12T14:00:00+00:00", "2026-11-12T15:00:00+00:00")
diff --git a/selftests/test_cli_bundle.py b/selftests/test_cli_bundle.py
index 35c750bbc..3328b3566 100644
--- a/selftests/test_cli_bundle.py
+++ b/selftests/test_cli_bundle.py
@@ -415,6 +415,30 @@ def test_inside_report_dir_still_scans_config_and_cache(root, monkeypatch):
assert "auth cache api_key" in str(exc.value)
+def test_directory_with_no_run_names_the_missing_results_first(tmp_path, monkeypatch):
+ monkeypatch.chdir(tmp_path)
+ with pytest.raises(BundleError, match="no results found") as exc:
+ run_bundle(_args())
+ assert "vip.toml" not in str(exc.value)
+
+
+def test_summary_says_what_the_auth_cache_of_the_run_holds(root):
+ run_bundle(_args(output="b.zip"))
+ with zipfile.ZipFile(root / "b.zip") as zf:
+ summary = json.loads(zf.read("config-summary.json"))
+ assert summary["auth_cache"] == {"session": True, "minted_connect_api_key": True}
+ assert "vip bundle" in summary["credential_flags"]
+
+
+def test_summary_says_when_the_run_left_no_auth_cache(root):
+ (root / ".vip-auth-cache.json").unlink()
+ (root / ".vip-auth-cache.meta.json").unlink()
+ run_bundle(_args(output="b.zip"))
+ with zipfile.ZipFile(root / "b.zip") as zf:
+ summary = json.loads(zf.read("config-summary.json"))
+ assert summary["auth_cache"] == {"session": False, "minted_connect_api_key": False}
+
+
def test_missing_config_fails_without_flag(root):
(root / "vip.toml").unlink()
with pytest.raises(BundleError, match="--no-config"):
diff --git a/selftests/test_reporting.py b/selftests/test_reporting.py
index 6c73d3e49..83a1e2fa5 100644
--- a/selftests/test_reporting.py
+++ b/selftests/test_reporting.py
@@ -640,6 +640,21 @@ def test_failure_fallback_when_no_error_details(self, tmp_path):
assert failure is not None
assert failure.get("message") == "test failed"
+ def test_generated_at_is_a_suite_property(self, tmp_path):
+ out = tmp_path / "junit.xml"
+ write_junit_xml(self._sample(), out)
+ suite = ET.parse(out).getroot().find("testsuite")
+ prop = suite.find("properties/property[@name='generated_at']")
+ assert prop.get("value") == "2026-07-21T12:00:00+00:00"
+ # The properties element must come before the test cases.
+ assert suite[0].tag == "properties"
+ assert b"2026-07-21T12:00:00+00:00" in out.read_bytes()
+
+ def test_no_generated_at_gives_no_properties(self, tmp_path):
+ out = tmp_path / "junit.xml"
+ write_junit_xml(ReportData(), out)
+ assert ET.parse(out).getroot().find("testsuite/properties") is None
+
def test_empty_report_data_is_well_formed(self, tmp_path):
out = tmp_path / "junit.xml"
write_junit_xml(ReportData(), out)
@@ -703,6 +718,18 @@ def test_na_version_wording_is_fallback_without_skip_reason(self, tmp_path):
class TestWriteSarif:
+ def test_generated_at_is_a_run_property(self, tmp_path):
+ out = tmp_path / "results.sarif"
+ write_sarif(ReportData(generated_at="2026-07-21T12:00:00+00:00"), out)
+ run = json.loads(out.read_text(encoding="utf-8"))["runs"][0]
+ assert run["properties"] == {"generated_at": "2026-07-21T12:00:00+00:00"}
+ assert b"2026-07-21T12:00:00+00:00" in out.read_bytes()
+
+ def test_no_generated_at_gives_no_run_properties(self, tmp_path):
+ out = tmp_path / "results.sarif"
+ write_sarif(ReportData(), out)
+ assert "properties" not in json.loads(out.read_text(encoding="utf-8"))["runs"][0]
+
def _sample(self) -> ReportData:
return ReportData(
results=[
diff --git a/src/vip/bundle/archive.py b/src/vip/bundle/archive.py
index 6d9b20aed..17d6b8a87 100644
--- a/src/vip/bundle/archive.py
+++ b/src/vip/bundle/archive.py
@@ -115,10 +115,10 @@ def collect_members(results_path: Path, report_dir: Path) -> Collected:
"""Read the allowlisted report files.
Sidecar files come from the directory of *results_path*. ``vip verify``
- writes the same ``generated_at`` value to results.json and failures.json,
- so a failures.json with a different value is from a different run and is
- left out. A sidecar with no such value to compare is left out if it is
- older than results.json.
+ writes the ``generated_at`` value of results.json to each sidecar. A
+ failures.json with a different value is from a different run and is left
+ out. A junit.xml or results.sarif that holds the value is from the same
+ run. Any other sidecar is left out if it is older than results.json.
Rendered files come from ``/_output/``. A rendered file is
included only if the render stamp names it and the SHA-256 of this
results.json. The secret scan cannot read the text in the PDF, so the PDF
@@ -147,6 +147,8 @@ def collect_members(results_path: Path, report_dir: Path) -> Collected:
reason = "is from a different run than results.json"
_leave_out(out, archive_name, path, reason, "")
continue
+ elif results_run is not None and results_run.encode() in content:
+ pass
elif path.stat().st_mtime < results_mtime:
hint = (
" If you copied the report directory without file times, "
diff --git a/src/vip/cli/bundle.py b/src/vip/cli/bundle.py
index a2dc122e6..cc8946b8c 100644
--- a/src/vip/cli/bundle.py
+++ b/src/vip/cli/bundle.py
@@ -143,6 +143,19 @@ def _cache_secrets(root: Path) -> list[tuple[str, str]]:
return pairs
+def _auth_cache_summary(cache_secrets: list[tuple[str, str]]) -> dict[str, bool]:
+ """Say what the auth cache of the run holds, with no values.
+
+ A run with ``--interactive-auth`` or ``--headless-auth`` saves a browser
+ session, and can mint a Connect API key.
+ """
+ labels = [label for label, _ in cache_secrets]
+ return {
+ "session": any(label != "auth cache api_key" for label in labels),
+ "minted_connect_api_key": "auth cache api_key" in labels,
+ }
+
+
def _default_output(root: Path, results: bytes) -> Path:
"""Return ``/vip-bundle--.zip``."""
try:
@@ -192,11 +205,13 @@ def run_bundle(args: argparse.Namespace) -> None:
root = _invocation_root(args.results)
report_dir = _report_dir(args.results)
results_path = Path(args.results) if args.results else report_dir / "results.json"
+ # The report files are read first, so that a directory with no run gives
+ # the error for the missing results.json and not for the missing vip.toml.
+ collected = collect_members(results_path, report_dir)
config_path = _config_path(args, root)
# VIPConfig() still reads the VIP_* secret variables in its __post_init__ methods.
config = _load(config_path) if config_path is not None else VIPConfig()
- collected = collect_members(results_path, report_dir)
results = collected.members["report/results.json"]
if config_path is not None:
_check_config_matches(config, config_path, results)
@@ -242,7 +257,14 @@ def run_bundle(args: argparse.Namespace) -> None:
members = dict(collected.members)
if config_path is not None:
- members["config-summary.json"] = _json_bytes(build_config_summary(config))
+ summary = build_config_summary(config)
+ summary["credential_flags"] = (
+ "Each *_set value is true if the configuration file or the environment of "
+ "the vip bundle command holds that credential. The auth_cache values come "
+ "from the files that the run saved."
+ )
+ summary["auth_cache"] = _auth_cache_summary(cache_secrets)
+ members["config-summary.json"] = _json_bytes(summary)
members["version.txt"] = (_format_version_details() + "\n").encode("utf-8")
# Scan every member before anything is written. The manifest is scanned
diff --git a/src/vip/reporting.py b/src/vip/reporting.py
index 08fe6bb72..869021a7d 100644
--- a/src/vip/reporting.py
+++ b/src/vip/reporting.py
@@ -9,6 +9,7 @@
from dataclasses import dataclass, field
from datetime import UTC, datetime
from pathlib import Path
+from typing import Any
import vip_tests
from vip import __version__
@@ -314,6 +315,11 @@ def write_junit_xml(data: ReportData, path: str | Path) -> None:
skipped=str(data.skipped),
time=f"{sum(r.duration for r in data.results):.3f}",
)
+ # The same value is in results.json, so a reader can tell that the two
+ # files are from one run.
+ if data.generated_at:
+ properties = ET.SubElement(suite, "properties")
+ ET.SubElement(properties, "property", name="generated_at", value=data.generated_at)
for r in data.results:
case = ET.SubElement(
suite,
@@ -384,22 +390,25 @@ def write_sarif(data: ReportData, path: str | Path) -> None:
}
)
+ run: dict[str, Any] = {
+ "tool": {
+ "driver": {
+ "name": "vip",
+ "version": __version__,
+ "informationUri": "https://github.com/posit-dev/vip",
+ "rules": list(rules.values()),
+ }
+ },
+ "results": results,
+ }
+ # The same value is in results.json, so a reader can tell that the two
+ # files are from one run.
+ if data.generated_at:
+ run["properties"] = {"generated_at": data.generated_at}
doc = {
"$schema": "https://json.schemastore.org/sarif-2.1.0.json",
"version": "2.1.0",
- "runs": [
- {
- "tool": {
- "driver": {
- "name": "vip",
- "version": __version__,
- "informationUri": "https://github.com/posit-dev/vip",
- "rules": list(rules.values()),
- }
- },
- "results": results,
- }
- ],
+ "runs": [run],
}
p = Path(path)
diff --git a/website/src/pages/guide.astro b/website/src/pages/guide.astro
index c050a5d4d..db49bbd5f 100644
--- a/website/src/pages/guide.astro
+++ b/website/src/pages/guide.astro
@@ -419,9 +419,15 @@ cd report && quarto publish connect --server https://connect.example.com
vip report did not make from the current
results.json is left out. Run vip report
again to include it. A failures.json from a different
- run than results.json is left out too, and so is a
- JUnit or SARIF file that is older than results.json.
- The manifest lists each file that is left out, with the reason.
+ run than results.json is left out too. A JUnit or
+ SARIF file is included if it holds the run time of
+ results.json. If it does not, it is left out when
+ it is older than results.json. The manifest lists
+ each file that is left out, with the reason. In the configuration
+ summary, each *_set value says if the configuration
+ file or the environment of vip bundle holds that
+ credential. The auth_cache values say if the run
+ saved a browser session or minted a Connect API key.
From 4fe7ae14b6cd1972e498c7cbd353289aa30495aa Mon Sep 17 00:00:00 2001
From: Tim
Date: Thu, 12 Nov 2026 13:52:24 -0500
Subject: [PATCH 09/11] fix(bundle): scan env and inline credentials, compare
hosts, pair sidecars by run
The scan reads each credential variable and each inline connection
string directly, because the loaded configuration holds only one of an
inline value and its variable. The configuration match compares the
host and the port of each product URL, and stops if only one side has
a URL. A junit.xml or results.sarif with a different generated_at is
left out at any file time.
---
selftests/test_bundle_archive.py | 35 +++++++++++++++--
selftests/test_cli_bundle.py | 67 ++++++++++++++++++++++++++++++++
src/vip/bundle/archive.py | 32 ++++++++++-----
src/vip/bundle/summary.py | 37 +++++++++++++++---
src/vip/cli/bundle.py | 48 +++++++++++++++++++----
website/src/pages/guide.astro | 10 ++---
6 files changed, 198 insertions(+), 31 deletions(-)
diff --git a/selftests/test_bundle_archive.py b/selftests/test_bundle_archive.py
index 6c2bfbac2..f7a1b6ec5 100644
--- a/selftests/test_bundle_archive.py
+++ b/selftests/test_bundle_archive.py
@@ -196,6 +196,16 @@ def test_failures_json_of_the_same_run_stays_when_results_is_newer(tmp_path):
assert got.left_out == []
+def _sidecar_text(name: str, run: str) -> str:
+ """Return a junit.xml or a results.sarif that holds *run* in the form that vip writes."""
+ if name == "junit.xml":
+ return (
+ ''
+ f''
+ )
+ return json.dumps({"version": "2.1.0", "runs": [{"properties": {"generated_at": run}}]})
+
+
@pytest.mark.parametrize("name", ["junit.xml", "results.sarif"])
def test_sidecar_with_the_run_time_stays_when_results_is_newer(tmp_path, name):
# An edit of results.json gives it a newer file time than the sidecar.
@@ -203,20 +213,37 @@ def test_sidecar_with_the_run_time_stays_when_results_is_newer(tmp_path, name):
run = "2026-11-12T14:00:00+00:00"
results = json.dumps({"generated_at": run, "results": []})
_seed(report, sidecars=(), rendered=(), results=results, results_mtime=1_000_500.0)
- _write(report / name, f'', 1_000_001.0)
+ _write(report / name, _sidecar_text(name, run), 1_000_001.0)
got = collect_members(report / "results.json", report)
assert f"report/{name}" in got.members
assert got.left_out == []
@pytest.mark.parametrize("name", ["junit.xml", "results.sarif"])
-def test_older_sidecar_with_another_run_time_is_left_out(tmp_path, name):
+@pytest.mark.parametrize("sidecar_mtime", [1_000_001.0, 1_000_900.0])
+def test_sidecar_with_another_run_time_is_left_out(tmp_path, name, sidecar_mtime):
+ # The file time does not matter: a copy can make an old sidecar newer.
report = tmp_path / "report"
results = json.dumps({"generated_at": "2026-11-12T14:00:00+00:00", "results": []})
_seed(report, sidecars=(), rendered=(), results=results, results_mtime=1_000_500.0)
- _write(report / name, '', 1_000_001.0)
+ _write(report / name, _sidecar_text(name, "2026-11-12T13:00:00+00:00"), sidecar_mtime)
+ got = collect_members(report / "results.json", report)
+ reason = "is from a different run than results.json"
+ assert got.left_out == [{"name": f"report/{name}", "reason": reason}]
+
+
+@pytest.mark.parametrize("name", ["junit.xml", "results.sarif"])
+@pytest.mark.parametrize(("sidecar_mtime", "included"), [(1_000_001.0, False), (1_000_900.0, True)])
+def test_sidecar_with_no_run_time_uses_the_file_time(tmp_path, name, sidecar_mtime, included):
+ # A file from an older vip has no generated_at value. The text of the
+ # value in some other place of the file does not count.
+ report = tmp_path / "report"
+ run = "2026-11-12T14:00:00+00:00"
+ results = json.dumps({"generated_at": run, "results": []})
+ _seed(report, sidecars=(), rendered=(), results=results, results_mtime=1_000_500.0)
+ _write(report / name, json.dumps({"runs": [{"note": run}]}), sidecar_mtime)
got = collect_members(report / "results.json", report)
- assert got.left_out == [{"name": f"report/{name}", "reason": "is older than results.json"}]
+ assert (f"report/{name}" in got.members) is included
def test_failures_json_of_another_run_is_left_out_when_it_is_newer(tmp_path):
diff --git a/selftests/test_cli_bundle.py b/selftests/test_cli_bundle.py
index 3328b3566..ae3b62316 100644
--- a/selftests/test_cli_bundle.py
+++ b/selftests/test_cli_bundle.py
@@ -15,6 +15,7 @@
import vip.cli.bundle as bundle_module
from vip.bundle.stamp import record_rendered, results_digest
from vip.cli import main, run_bundle
+from vip.config import VIPConfig
from vip.errors import BundleError
_VIP_SECRET_ENV = (
@@ -375,6 +376,72 @@ def test_config_with_another_product_url_fails(root):
run_bundle(_args(output="b.zip"))
+def test_config_with_another_scheme_and_path_passes(root):
+ # A browser login can change the scheme and the path of the URL that the
+ # run records. The host is the same, so the file is the correct one.
+ toml = (root / "vip.toml").read_text(encoding="utf-8")
+ (root / "vip.toml").write_text(
+ toml.replace("https://connect.example.com", "http://connect.example.com/connect/"),
+ encoding="utf-8",
+ )
+ _write_report(root)
+ run_bundle(_args(output="b.zip"))
+ assert (root / "b.zip").is_file()
+
+
+def test_config_with_another_port_fails(root):
+ toml = (root / "vip.toml").read_text(encoding="utf-8")
+ (root / "vip.toml").write_text(
+ toml.replace("https://connect.example.com", "https://connect.example.com:8443"),
+ encoding="utf-8",
+ )
+ _write_report(root)
+ with pytest.raises(BundleError, match="the connect URL is"):
+ run_bundle(_args(output="b.zip"))
+
+
+def test_config_with_no_url_for_a_product_of_the_run_fails(root):
+ toml = (root / "vip.toml").read_text(encoding="utf-8")
+ (root / "vip.toml").write_text(
+ toml.replace("https://connect.example.com", ""), encoding="utf-8"
+ )
+ _write_report(root)
+ with pytest.raises(BundleError, match="the connect URL is not set in the configuration"):
+ run_bundle(_args(output="b.zip"))
+
+
+def test_config_with_a_url_for_a_product_that_the_run_did_not_have_fails(tmp_path):
+ config = VIPConfig()
+ config.workbench.url = "https://workbench.example.com"
+ results = json.dumps({"products": {"workbench": {"url": ""}, "connect": {"url": ""}}})
+ with pytest.raises(BundleError, match=r"not set in results\.json"):
+ bundle_module._check_config_matches(config, tmp_path / "vip.toml", results.encode())
+
+
+def test_inline_connection_string_blocks_bundle_when_the_env_variable_replaces_it(
+ root, monkeypatch
+):
+ # load_config gives the value of the variable, not the inline string.
+ toml = (root / "vip.toml").read_text(encoding="utf-8")
+ (root / "vip.toml").write_text(
+ toml.replace("[data_sources.pg]", '[data_sources.pg]\nconnection_string_env = "PG_CONN"'),
+ encoding="utf-8",
+ )
+ monkeypatch.setenv("PG_CONN", "postgresql://other:OTHER-db-password-9@db:5432/app")
+ _write_report(root, results_extra="SECRET-db-password")
+ with pytest.raises(BundleError, match=r"data_sources\.pg\.connection_string"):
+ run_bundle(_args(output="b.zip"))
+
+
+def test_env_credential_blocks_bundle_when_the_config_has_an_inline_value(root, monkeypatch):
+ # The loaded configuration keeps the inline key, so the scan must read
+ # the variable directly.
+ monkeypatch.setenv("VIP_CONNECT_API_KEY", "ENV-ONLY-CONNECT-KEY-77")
+ _write_report(root, results_extra="ENV-ONLY-CONNECT-KEY-77")
+ with pytest.raises(BundleError, match="VIP_CONNECT_API_KEY"):
+ run_bundle(_args(output="b.zip"))
+
+
def test_key_in_auth_cache_path_blocks_bundle(root, tmp_path, monkeypatch):
# The auth cache can live outside the invocation directory. The bundle
# must read the file at auth_cache_path() too, not only the file in the root.
diff --git a/src/vip/bundle/archive.py b/src/vip/bundle/archive.py
index 17d6b8a87..80668e13d 100644
--- a/src/vip/bundle/archive.py
+++ b/src/vip/bundle/archive.py
@@ -4,6 +4,7 @@
import html
import json
+import re
import tempfile
import zipfile
from collections.abc import Sequence
@@ -101,12 +102,26 @@ def _check_results_file(results_path: Path) -> bytes:
return data
-def _generated_at(data: bytes) -> str | None:
- """Return the ``generated_at`` value of a results.json or a failures.json."""
+_JUNIT_GENERATED_AT = re.compile(rb' str | None:
+ """Return the ``generated_at`` value that ``vip verify`` wrote to the file *name*.
+
+ results.json and failures.json hold it as a top-level key, junit.xml as a
+ suite property, and results.sarif as a property of the first run.
+ """
+ if name == "junit.xml":
+ match = _JUNIT_GENERATED_AT.search(data)
+ return html.unescape(match.group(1).decode("utf-8", "replace")) or None if match else None
try:
parsed = json.loads(data)
except (ValueError, RecursionError):
return None
+ if name == "results.sarif":
+ runs = parsed.get("runs") if isinstance(parsed, dict) else None
+ first = runs[0] if isinstance(runs, list) and runs else None
+ parsed = first.get("properties") if isinstance(first, dict) else None
value = parsed.get("generated_at") if isinstance(parsed, dict) else None
return value if isinstance(value, str) and value else None
@@ -116,9 +131,10 @@ def collect_members(results_path: Path, report_dir: Path) -> Collected:
Sidecar files come from the directory of *results_path*. ``vip verify``
writes the ``generated_at`` value of results.json to each sidecar. A
- failures.json with a different value is from a different run and is left
- out. A junit.xml or results.sarif that holds the value is from the same
- run. Any other sidecar is left out if it is older than results.json.
+ sidecar with a different value is from a different run and is left out. A
+ sidecar with the same value is from the same run. If the sidecar or
+ results.json has no value, the sidecar is left out if it is older than
+ results.json.
Rendered files come from ``/_output/``. A rendered file is
included only if the render stamp names it and the SHA-256 of this
results.json. The secret scan cannot read the text in the PDF, so the PDF
@@ -130,7 +146,7 @@ def collect_members(results_path: Path, report_dir: Path) -> Collected:
out.members["report/results.json"] = data
out.sources["report/results.json"] = results_path
results_mtime = results_path.stat().st_mtime
- results_run = _generated_at(data)
+ results_run = _generated_at("results.json", data)
sidecar_dir = results_path.parent
for name in REPORT_SIDECAR_FILES[1:]:
@@ -141,14 +157,12 @@ def collect_members(results_path: Path, report_dir: Path) -> Collected:
_leave_out(out, archive_name, path, "is a link or is not a regular file", "")
else:
content = _read(path)
- sidecar_run = _generated_at(content) if name == "failures.json" else None
+ sidecar_run = _generated_at(name, content)
if results_run is not None and sidecar_run is not None:
if sidecar_run != results_run:
reason = "is from a different run than results.json"
_leave_out(out, archive_name, path, reason, "")
continue
- elif results_run is not None and results_run.encode() in content:
- pass
elif path.stat().st_mtime < results_mtime:
hint = (
" If you copied the report directory without file times, "
diff --git a/src/vip/bundle/summary.py b/src/vip/bundle/summary.py
index 4fe93101c..5da8ae690 100644
--- a/src/vip/bundle/summary.py
+++ b/src/vip/bundle/summary.py
@@ -100,9 +100,19 @@
}
)
-# Secrets that VIP reads from the environment and that VIPConfig() does not
-# always hold: VIPConfig() has git_test=None, and the TOTP seed is never in it.
-SECRET_ENV_VARS = ("VIP_GIT_TOKEN", "VIP_TEST_TOTP_SECRET")
+# Each credential variable that VIP reads. The scan reads them all directly,
+# because the loaded configuration holds a variable only if the matching field
+# in vip.toml is empty, has git_test=None with no [workbench.git_test] table,
+# and never holds the TOTP seed.
+SECRET_ENV_VARS = (
+ "VIP_CONNECT_API_KEY",
+ "VIP_WORKBENCH_API_KEY",
+ "VIP_PACKAGE_MANAGER_TOKEN",
+ "VIP_PM_TOKEN",
+ "VIP_TEST_PASSWORD",
+ "VIP_GIT_TOKEN",
+ "VIP_TEST_TOTP_SECRET",
+)
# httpx reads these when proxy.trust_env is true. They can carry user:pass.
PROXY_ENV_VARS = (
"HTTP_PROXY",
@@ -253,13 +263,30 @@ def auth_cache_secrets(path: Path) -> list[tuple[str, str]]:
return pairs
+def inline_connection_string_secrets(raw: Any) -> list[tuple[str, str]]:
+ """Return the secrets of each ``connection_string`` in the parsed text of vip.toml.
+
+ The loaded configuration holds the value of ``connection_string_env``
+ in place of the inline string if that variable is set. The inline string
+ is a credential in both cases.
+ """
+ sources = raw.get("data_sources") if isinstance(raw, dict) else None
+ pairs: list[tuple[str, str]] = []
+ for name, entry in sources.items() if isinstance(sources, dict) else ():
+ conn = entry.get("connection_string") if isinstance(entry, dict) else None
+ if isinstance(conn, str):
+ pairs += _connection_string_secrets(f"data_sources.{name}.connection_string", conn)
+ return pairs
+
+
def secret_values(
config: VIPConfig, cache_secrets: Sequence[tuple[str, str]] = ()
) -> list[tuple[str, str]]:
"""Return ``(label, value)`` for every credential that the bundle must not hold.
- *cache_secrets* are the pairs that :func:`auth_cache_secrets` read. The
- label names the source of the value, so an error can name it without
+ *cache_secrets* are extra pairs, such as the ones that
+ :func:`auth_cache_secrets` and :func:`inline_connection_string_secrets`
+ read. The label names the source of the value, so an error can name it without
printing the value. Empty values and repeated values are dropped.
"""
pairs: list[tuple[str, str]] = [
diff --git a/src/vip/cli/bundle.py b/src/vip/cli/bundle.py
index cc8946b8c..3225110ce 100644
--- a/src/vip/cli/bundle.py
+++ b/src/vip/cli/bundle.py
@@ -8,9 +8,11 @@
import os
import re
import sys
+import tomllib
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
+from urllib.parse import urlsplit
from vip import __version__
from vip.auth.cache import AUTH_CACHE_FILENAME, auth_cache_path
@@ -24,6 +26,7 @@
from vip.bundle.summary import (
auth_cache_secrets,
build_config_summary,
+ inline_connection_string_secrets,
secret_values,
strip_url_userinfo,
)
@@ -97,12 +100,36 @@ def _load(path: Path) -> VIPConfig:
raise BundleError(f"could not read {path}: {exc}") from exc
+def _inline_secrets(path: Path) -> list[tuple[str, str]]:
+ """Return the connection string secrets in the text of *path*."""
+ try:
+ raw = tomllib.loads(path.read_text(encoding="utf-8"))
+ except (OSError, ValueError) as exc:
+ raise BundleError(f"could not read {path}: {exc}") from exc
+ return inline_connection_string_secrets(raw)
+
+
+def _host(url: str) -> tuple[str, int | None]:
+ """Return the host name and the explicit port of *url*.
+
+ A login with ``--interactive-auth`` or ``--headless-auth`` can change the
+ scheme and the path of a product URL before the run records it. The host
+ and the port stay the same.
+ """
+ try:
+ parts = urlsplit(url if "//" in url else f"//{url}")
+ return (parts.hostname or "").lower(), parts.port
+ except ValueError:
+ return url, None
+
+
def _check_config_matches(config: VIPConfig, path: Path, results: bytes) -> None:
"""Refuse a configuration file that is not the one that ``vip verify`` used.
results.json holds the deployment name and each product URL from the
- configuration of the run. If they differ, the scan would look for the
- wrong secrets.
+ configuration of the run. If the name differs, if the host or the port of
+ a URL differs, or if only one of the two has a URL for a product, the scan
+ would look for the wrong secrets.
"""
data = json.loads(results)
name = data.get("deployment_name")
@@ -115,13 +142,17 @@ def _check_config_matches(config: VIPConfig, path: Path, results: bytes) -> None
products = data.get("products")
for product in _PRODUCTS:
entry = products.get(product) if isinstance(products, dict) else None
- run_url = entry.get("url") if isinstance(entry, dict) else None
+ if not isinstance(entry, dict) or not isinstance(entry.get("url"), str):
+ continue
+ run_url = entry["url"]
url = config.product_config(product).url
- if run_url and url and run_url.rstrip("/") != url.rstrip("/"):
+ if not run_url and not url:
+ continue
+ if not run_url or not url or _host(run_url) != _host(url):
raise BundleError(
f"{path} does not match results.json: the {product} URL is "
- f"{strip_url_userinfo(url)} in the configuration and "
- f"{strip_url_userinfo(run_url)} in results.json. "
+ f"{strip_url_userinfo(url) or 'not set'} in the configuration and "
+ f"{strip_url_userinfo(run_url) or 'not set'} in results.json. "
"Pass --config with the file that 'vip verify' used."
)
@@ -221,10 +252,11 @@ def run_bundle(args: argparse.Namespace) -> None:
"--no-config is set, so the secret scan used only the environment "
"variables and the auth cache. Look at the zip before you send it."
)
+ inline = _inline_secrets(config_path) if config_path is not None else []
cache_secrets = _cache_secrets(root)
- secrets = secret_values(config, cache_secrets)
+ secrets = secret_values(config, [*inline, *cache_secrets])
# secret_values drops repeated values, so the cache count is the remainder.
- config_count = len(secret_values(config))
+ config_count = len(secret_values(config, inline))
scan_sources = {
"configuration_and_environment": config_count,
"auth_cache": len(secrets) - config_count,
diff --git a/website/src/pages/guide.astro b/website/src/pages/guide.astro
index db49bbd5f..653011e0c 100644
--- a/website/src/pages/guide.astro
+++ b/website/src/pages/guide.astro
@@ -418,11 +418,11 @@ cd report && quarto publish connect --server https://connect.example.com
uses only the environment and the auth cache. A rendered page that
vip report did not make from the current
results.json is left out. Run vip report
- again to include it. A failures.json from a different
- run than results.json is left out too. A JUnit or
- SARIF file is included if it holds the run time of
- results.json. If it does not, it is left out when
- it is older than results.json. The manifest lists
+ again to include it. A failures.json, JUnit, or SARIF
+ file from a different run than results.json is left
+ out too. If a JUnit or SARIF file from an older VIP has no run
+ time and is older than results.json, it is also left
+ out. The manifest lists
each file that is left out, with the reason. In the configuration
summary, each *_set value says if the configuration
file or the environment of vip bundle holds that
From 0d160e6faf22622f1966236bc729f6e397ce2cdd Mon Sep 17 00:00:00 2001
From: Tim
Date: Thu, 12 Nov 2026 15:53:59 -0500
Subject: [PATCH 10/11] fix(bundle): stamp rendered file digests, scan the raw
git token
The render stamp holds the SHA-256 of each rendered file, and vip
report stamps only a file that the render wrote. vip bundle leaves out
a rendered file that changed after the stamp, and includes the PDF
only together with both HTML pages, because the scan cannot read PDF
text. The scan also reads workbench.git_test.token from the text of
vip.toml, because the loaded configuration drops it for an anonymous
clone.
---
selftests/test_bundle_archive.py | 25 ++++++++++++++++++++
selftests/test_cli_bundle.py | 17 +++++++++++++-
selftests/test_cli_report.py | 40 ++++++++++++++++++++++++++++----
src/vip/bundle/archive.py | 26 ++++++++++++++++-----
src/vip/bundle/stamp.py | 40 +++++++++++++++++++++-----------
src/vip/bundle/summary.py | 16 +++++++++----
src/vip/cli/bundle.py | 6 ++---
src/vip/cli/report.py | 36 ++++++++++++++++++++++++----
website/src/pages/guide.astro | 7 ++++--
9 files changed, 173 insertions(+), 40 deletions(-)
diff --git a/selftests/test_bundle_archive.py b/selftests/test_bundle_archive.py
index f7a1b6ec5..56d25d162 100644
--- a/selftests/test_bundle_archive.py
+++ b/selftests/test_bundle_archive.py
@@ -178,6 +178,31 @@ def test_stale_sidecar_is_left_out(tmp_path):
assert any(str(report / "failures.json") in w for w in got.warnings)
+@pytest.mark.parametrize("name", ["index.html", "vip-report.pdf"])
+def test_rendered_file_that_changed_after_the_stamp_is_left_out(tmp_path, name):
+ # A copy of an older file over the stamped file keeps the name.
+ report = tmp_path / "report"
+ _seed(report)
+ _write(report / "_output" / name, "content of an older render", 1_000_002.0)
+ got = collect_members(report / "results.json", report)
+ assert f"report/_output/{name}" not in got.members
+ reason = "changed after 'vip report' made it"
+ assert {"name": f"report/_output/{name}", "reason": reason} in got.left_out
+
+
+@pytest.mark.parametrize("absent", ["index.html", "details.html"])
+def test_pdf_without_both_html_pages_is_left_out(tmp_path, absent):
+ # The scan cannot read the text in the PDF. The HTML pages hold the same text.
+ report = tmp_path / "report"
+ _seed(report)
+ (report / "_output" / absent).unlink()
+ got = collect_members(report / "results.json", report)
+ assert "report/_output/vip-report.pdf" not in got.members
+ assert "report/_output/vip-report.pdf" not in got.sources
+ reason = "cannot be checked without the HTML pages of the same render"
+ assert {"name": "report/_output/vip-report.pdf", "reason": reason} in got.left_out
+
+
def _seed_run_pair(report: Path, results_run: str, failures_run: str) -> None:
"""Write results.json and failures.json with a ``generated_at`` value each."""
results = json.dumps({"generated_at": results_run, "results": []})
diff --git a/selftests/test_cli_bundle.py b/selftests/test_cli_bundle.py
index ae3b62316..13f1c1599 100644
--- a/selftests/test_cli_bundle.py
+++ b/selftests/test_cli_bundle.py
@@ -251,9 +251,12 @@ def test_secret_in_failures_json_blocks_bundle(root, capsys):
)
def test_secret_in_each_report_file_blocks_bundle(root, member):
report = root / "report"
+ rendered = ("index.html", "details.html", "vip-report.pdf")
+ for name in rendered:
+ _write(report / "_output" / name, "a clean file", RESULTS_MTIME + 5)
_write(report / member, "x SECRET-pm-token y", RESULTS_MTIME + 5)
digest = results_digest((report / "results.json").read_bytes())
- record_rendered(report / "_output", digest, ("index.html", "details.html", "vip-report.pdf"))
+ record_rendered(report / "_output", digest, rendered)
with pytest.raises(BundleError, match=r"package_manager\.token") as exc:
run_bundle(_args(output="b.zip"))
assert Path(member).name in str(exc.value)
@@ -433,6 +436,18 @@ def test_inline_connection_string_blocks_bundle_when_the_env_variable_replaces_i
run_bundle(_args(output="b.zip"))
+def test_inline_git_token_blocks_bundle_when_the_clone_is_anonymous(root):
+ # With auth_method "none", the loaded configuration has no token.
+ toml = (root / "vip.toml").read_text(encoding="utf-8")
+ (root / "vip.toml").write_text(
+ toml.replace("[workbench.git_test]", '[workbench.git_test]\nauth_method = "none"'),
+ encoding="utf-8",
+ )
+ _write_report(root, results_extra="SECRET-git-token")
+ with pytest.raises(BundleError, match=r"workbench\.git_test\.token"):
+ run_bundle(_args(output="b.zip"))
+
+
def test_env_credential_blocks_bundle_when_the_config_has_an_inline_value(root, monkeypatch):
# The loaded configuration keeps the inline key, so the scan must read
# the variable directly.
diff --git a/selftests/test_cli_report.py b/selftests/test_cli_report.py
index 28cebe20b..ef0f97c08 100644
--- a/selftests/test_cli_report.py
+++ b/selftests/test_cli_report.py
@@ -9,6 +9,7 @@
from __future__ import annotations
import argparse
+import hashlib
import os
import subprocess
import sys
@@ -386,6 +387,12 @@ def _report_dir(tmp_path, monkeypatch, results: str = '{"results": []}'):
(report_dir / "results.json").write_text(results, encoding="utf-8")
return report_dir
+ @staticmethod
+ def _age_outputs(report_dir):
+ """Give each rendered file an old file time, as files of an earlier render have."""
+ for path in (report_dir / "_output").iterdir():
+ os.utime(path, (1_000_000.0, 1_000_000.0))
+
def test_stamp_names_every_rendered_file(self, tmp_path, monkeypatch):
from vip import cli
from vip.bundle.stamp import read_render_stamp, results_digest
@@ -394,10 +401,14 @@ def test_stamp_names_every_rendered_file(self, tmp_path, monkeypatch):
monkeypatch.setattr("vip.cli.report.subprocess.run", _fake_quarto(create_output=True))
cli.run_report(_make_args())
digest = results_digest((report_dir / "results.json").read_bytes())
- assert read_render_stamp(report_dir / "_output") == (
- digest,
- frozenset({"index.html", "details.html", "vip-report.pdf"}),
- )
+ stamp = read_render_stamp(report_dir / "_output")
+ assert stamp is not None
+ assert stamp[0] == digest
+ assert stamp[1] == {
+ "index.html": hashlib.sha256(b"report").hexdigest(),
+ "details.html": hashlib.sha256(b"report").hexdigest(),
+ "vip-report.pdf": hashlib.sha256(b"%PDF-fake").hexdigest(),
+ }
def test_failed_pdf_is_not_in_the_new_stamp(self, tmp_path, monkeypatch):
# The PDF of the earlier run stays on disk, but the stamp leaves it out.
@@ -407,6 +418,7 @@ def test_failed_pdf_is_not_in_the_new_stamp(self, tmp_path, monkeypatch):
report_dir = self._report_dir(tmp_path, monkeypatch)
monkeypatch.setattr("vip.cli.report.subprocess.run", _fake_quarto(create_output=True))
cli.run_report(_make_args())
+ self._age_outputs(report_dir)
self._report_dir(tmp_path, monkeypatch, results='{"results": [{"n": 1}]}')
monkeypatch.setattr(
"vip.cli.report.subprocess.run", _fake_quarto(create_output=True, pdf_returncode=1)
@@ -414,9 +426,27 @@ def test_failed_pdf_is_not_in_the_new_stamp(self, tmp_path, monkeypatch):
cli.run_report(_make_args())
stamp = read_render_stamp(report_dir / "_output")
assert stamp is not None
- assert stamp[1] == frozenset({"index.html", "details.html"})
+ assert set(stamp[1]) == {"index.html", "details.html"}
assert (report_dir / "_output" / "vip-report.pdf").is_file()
+ def test_file_that_the_render_did_not_write_gets_no_stamp(self, tmp_path, monkeypatch, capsys):
+ # The second render exits with 0 and writes nothing, so each file on
+ # disk is from the first results.json.
+ from vip import cli
+ from vip.bundle.stamp import read_render_stamp
+
+ report_dir = self._report_dir(tmp_path, monkeypatch)
+ monkeypatch.setattr("vip.cli.report.subprocess.run", _fake_quarto(create_output=True))
+ cli.run_report(_make_args())
+ self._age_outputs(report_dir)
+ self._report_dir(tmp_path, monkeypatch, results='{"results": [{"n": 1}]}')
+ monkeypatch.setattr("vip.cli.report.subprocess.run", _fake_quarto(create_output=False))
+ cli.run_report(_make_args())
+ stamp = read_render_stamp(report_dir / "_output")
+ assert stamp is not None
+ assert stamp[1] == {}
+ assert "did not write" in capsys.readouterr().err
+
def test_failed_html_clears_the_stamp(self, tmp_path, monkeypatch):
from vip import cli
from vip.bundle.stamp import read_render_stamp
diff --git a/src/vip/bundle/archive.py b/src/vip/bundle/archive.py
index 80668e13d..f95f22a77 100644
--- a/src/vip/bundle/archive.py
+++ b/src/vip/bundle/archive.py
@@ -12,7 +12,7 @@
from pathlib import Path
from urllib.parse import quote, quote_plus
-from vip.bundle.stamp import read_render_stamp, results_digest
+from vip.bundle.stamp import file_digest, read_render_stamp, results_digest
from vip.errors import BundleError
# Read from the directory of results.json. results.json must exist.
@@ -20,6 +20,9 @@
# Read from /_output/. _quarto.yml sets embed-resources, so each
# page is one self-contained file and no site_libs/ directory is needed.
RENDERED_FILES = ("index.html", "details.html", "vip-report.pdf")
+# The secret scan cannot read the compressed text in the PDF. The HTML pages
+# hold the same text, so the PDF is in the bundle only together with them.
+PDF_FILE = "vip-report.pdf"
# Files that hold credentials. --results must never name one of them.
FORBIDDEN_NAMES = frozenset(
{"vip.toml", ".vip-auth-cache.json", ".vip-auth-cache.meta.json", ".vip-install.json"}
@@ -136,10 +139,10 @@ def collect_members(results_path: Path, report_dir: Path) -> Collected:
results.json has no value, the sidecar is left out if it is older than
results.json.
Rendered files come from ``/_output/``. A rendered file is
- included only if the render stamp names it and the SHA-256 of this
- results.json. The secret scan cannot read the text in the PDF, so the PDF
- is safe only when its source, results.json, is in the bundle and passes
- the scan.
+ included only if the render stamp holds its SHA-256 and the SHA-256 of
+ this results.json. The secret scan cannot read the text in the PDF, so
+ the PDF is included only if both HTML pages, which hold the same text,
+ are in the bundle and pass the scan.
"""
out = Collected()
data = _check_results_file(results_path)
@@ -186,8 +189,19 @@ def collect_members(results_path: Path, report_dir: Path) -> Collected:
elif stamp is None or stamp[0] != digest or name not in stamp[1]:
_leave_out(out, archive_name, path, f"was not made from {results_path}", hint)
else:
- out.members[archive_name] = _read(path)
+ content = _read(path)
+ if file_digest(content) != stamp[1][name]:
+ _leave_out(out, archive_name, path, "changed after 'vip report' made it", hint)
+ continue
+ out.members[archive_name] = content
out.sources[archive_name] = path
+ pdf = f"report/_output/{PDF_FILE}"
+ pages = [f"report/_output/{name}" for name in RENDERED_FILES if name != PDF_FILE]
+ if pdf in out.members and not all(page in out.members for page in pages):
+ del out.members[pdf]
+ path = out.sources.pop(pdf)
+ reason = "cannot be checked without the HTML pages of the same render"
+ _leave_out(out, pdf, path, reason, hint)
if not any(name.startswith("report/_output/") for name in out.members):
out.warnings.append("the bundle has no rendered report. Run 'vip report' to include it.")
return out
diff --git a/src/vip/bundle/stamp.py b/src/vip/bundle/stamp.py
index 7a5ebc629..5f2f96c0d 100644
--- a/src/vip/bundle/stamp.py
+++ b/src/vip/bundle/stamp.py
@@ -1,9 +1,10 @@
"""Record which results.json each rendered report file was made from.
-``vip report`` writes the stamp after each render. ``vip bundle`` reads it and
-includes a rendered file only if the stamp names that file and the SHA-256 of
-the results.json in the bundle. File times cannot do this, because a copy with
-``shutil.copy2`` keeps the old time of the source file.
+``vip report`` writes the stamp after each render. The stamp holds the SHA-256
+of the results.json and the SHA-256 of each rendered file. ``vip bundle``
+includes a rendered file only if both digests match. File times cannot do
+this, because a copy with ``shutil.copy2`` keeps the old time of the source
+file.
"""
from __future__ import annotations
@@ -26,8 +27,13 @@ def clear_render_stamp(output_dir: Path) -> None:
(output_dir / STAMP_NAME).unlink(missing_ok=True)
-def read_render_stamp(output_dir: Path) -> tuple[str, frozenset[str]] | None:
- """Return the digest and the rendered file names in the stamp, or None.
+def file_digest(data: bytes) -> str:
+ """Return the SHA-256 hex digest of the bytes of a rendered file."""
+ return hashlib.sha256(data).hexdigest()
+
+
+def read_render_stamp(output_dir: Path) -> tuple[str, dict[str, str]] | None:
+ """Return the results digest and the digest of each rendered file, or None.
A stamp that is missing, is a link, or does not parse gives None.
"""
@@ -42,21 +48,29 @@ def read_render_stamp(output_dir: Path) -> tuple[str, frozenset[str]] | None:
return None
digest = data.get("results_sha256")
files = data.get("files")
- if not isinstance(digest, str) or not isinstance(files, list):
+ if not isinstance(digest, str) or not isinstance(files, dict):
return None
- return digest, frozenset(name for name in files if isinstance(name, str))
+ return digest, {
+ name: value
+ for name, value in files.items()
+ if isinstance(name, str) and isinstance(value, str)
+ }
def record_rendered(output_dir: Path, digest: str, names: Iterable[str]) -> None:
- """Add *names* to the stamp for *digest*. A stamp for another digest is replaced."""
+ """Add the files *names* in *output_dir* to the stamp for *digest*.
+
+ The stamp holds the SHA-256 of each file as it is now. A stamp for
+ another digest is replaced.
+ """
current = read_render_stamp(output_dir)
- files = set(names)
- if current is not None and current[0] == digest:
- files |= current[1]
+ files = dict(current[1]) if current is not None and current[0] == digest else {}
+ for name in names:
+ files[name] = file_digest((output_dir / name).read_bytes())
path = output_dir / STAMP_NAME
tmp = path.with_suffix(".tmp")
tmp.write_text(
- json.dumps({"results_sha256": digest, "files": sorted(files)}, indent=2) + "\n",
+ json.dumps({"results_sha256": digest, "files": files}, indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
tmp.replace(path)
diff --git a/src/vip/bundle/summary.py b/src/vip/bundle/summary.py
index 5da8ae690..1ab06392e 100644
--- a/src/vip/bundle/summary.py
+++ b/src/vip/bundle/summary.py
@@ -263,12 +263,13 @@ def auth_cache_secrets(path: Path) -> list[tuple[str, str]]:
return pairs
-def inline_connection_string_secrets(raw: Any) -> list[tuple[str, str]]:
- """Return the secrets of each ``connection_string`` in the parsed text of vip.toml.
+def raw_config_secrets(raw: Any) -> list[tuple[str, str]]:
+ """Return the secrets in the parsed text of vip.toml that the loaded configuration can drop.
The loaded configuration holds the value of ``connection_string_env``
- in place of the inline string if that variable is set. The inline string
- is a credential in both cases.
+ in place of an inline ``connection_string`` if that variable is set. It
+ has no ``workbench.git_test.token`` if ``auth_method`` is ``"none"``. Each
+ inline value is a credential in both cases.
"""
sources = raw.get("data_sources") if isinstance(raw, dict) else None
pairs: list[tuple[str, str]] = []
@@ -276,6 +277,11 @@ def inline_connection_string_secrets(raw: Any) -> list[tuple[str, str]]:
conn = entry.get("connection_string") if isinstance(entry, dict) else None
if isinstance(conn, str):
pairs += _connection_string_secrets(f"data_sources.{name}.connection_string", conn)
+ workbench = raw.get("workbench") if isinstance(raw, dict) else None
+ git = workbench.get("git_test") if isinstance(workbench, dict) else None
+ token = git.get("token") if isinstance(git, dict) else None
+ if isinstance(token, str):
+ pairs.append(("workbench.git_test.token", token))
return pairs
@@ -285,7 +291,7 @@ def secret_values(
"""Return ``(label, value)`` for every credential that the bundle must not hold.
*cache_secrets* are extra pairs, such as the ones that
- :func:`auth_cache_secrets` and :func:`inline_connection_string_secrets`
+ :func:`auth_cache_secrets` and :func:`raw_config_secrets`
read. The label names the source of the value, so an error can name it without
printing the value. Empty values and repeated values are dropped.
"""
diff --git a/src/vip/cli/bundle.py b/src/vip/cli/bundle.py
index 3225110ce..f01db3106 100644
--- a/src/vip/cli/bundle.py
+++ b/src/vip/cli/bundle.py
@@ -26,7 +26,7 @@
from vip.bundle.summary import (
auth_cache_secrets,
build_config_summary,
- inline_connection_string_secrets,
+ raw_config_secrets,
secret_values,
strip_url_userinfo,
)
@@ -101,12 +101,12 @@ def _load(path: Path) -> VIPConfig:
def _inline_secrets(path: Path) -> list[tuple[str, str]]:
- """Return the connection string secrets in the text of *path*."""
+ """Return the secrets in the text of *path* that the loaded configuration can drop."""
try:
raw = tomllib.loads(path.read_text(encoding="utf-8"))
except (OSError, ValueError) as exc:
raise BundleError(f"could not read {path}: {exc}") from exc
- return inline_connection_string_secrets(raw)
+ return raw_config_secrets(raw)
def _host(url: str) -> tuple[str, int | None]:
diff --git a/src/vip/cli/report.py b/src/vip/cli/report.py
index e95217d31..25e6936e1 100644
--- a/src/vip/cli/report.py
+++ b/src/vip/cli/report.py
@@ -12,6 +12,7 @@
import webbrowser
from pathlib import Path
+from vip.bundle.archive import RENDERED_FILES
from vip.bundle.stamp import clear_render_stamp, record_rendered, results_digest
from vip.errors import ReportError
@@ -183,6 +184,7 @@ def run_report(args: argparse.Namespace) -> None:
# the PDF degrades to a warning. All three documents stay in
# _quarto.yml's render list because a single-document render only lands
# in _output/ for listed files.
+ before = {name: _file_time(output_dir / name) for name in RENDERED_FILES}
for page in ("index.qmd", "details.qmd"):
returncode = _quarto_render(page, report_dir, env)
if returncode != 0:
@@ -196,14 +198,14 @@ def run_report(args: argparse.Namespace) -> None:
)
print(f"Report generated: {output}")
- _stamp(output_dir, digest, ("index.html", "details.html"))
+ _stamp(output_dir, digest, before, ("index.html", "details.html"))
# The PDF is the copy customers archive, so a missing one warns loudly —
# but never fails the command, and never blocks the HTML hand-off above.
pdf = report_dir / "_output" / "vip-report.pdf"
if _quarto_render("vip-report.qmd", report_dir, env) == 0 and pdf.exists():
print(f"PDF generated: {pdf}")
- _stamp(output_dir, digest, ("vip-report.pdf",))
+ _stamp(output_dir, digest, before, ("vip-report.pdf",))
else:
print(
f"Warning: the HTML report rendered but {pdf} did not. "
@@ -216,10 +218,34 @@ def run_report(args: argparse.Namespace) -> None:
webbrowser.open(output.resolve().as_uri())
-def _stamp(output_dir: Path, digest: str, names: tuple[str, ...]) -> None:
- """Record *names* in the render stamp. A failed write is a warning, not an error."""
+def _file_time(path: Path) -> int | None:
+ """Return the modification time of *path* in nanoseconds, or None if it does not exist."""
try:
- record_rendered(output_dir, digest, names)
+ return path.stat().st_mtime_ns
+ except OSError:
+ return None
+
+
+def _stamp(
+ output_dir: Path, digest: str, before: dict[str, int | None], names: tuple[str, ...]
+) -> None:
+ """Record in the render stamp each file in *names* that the render wrote.
+
+ *before* holds the file times from before the render. A file with the
+ same time is from an earlier render, so it gets no stamp. A failed write
+ is a warning, not an error.
+ """
+ fresh = tuple(
+ name for name in names if _file_time(output_dir / name) not in (None, before.get(name))
+ )
+ for name in sorted(set(names) - set(fresh)):
+ print(
+ f"Warning: this render did not write {output_dir / name}, so "
+ "'vip bundle' will not include it.",
+ file=sys.stderr,
+ )
+ try:
+ record_rendered(output_dir, digest, fresh)
except OSError as exc:
print(
f"Warning: could not write the render stamp in {output_dir}: {exc}. "
diff --git a/website/src/pages/guide.astro b/website/src/pages/guide.astro
index 653011e0c..9872b2a24 100644
--- a/website/src/pages/guide.astro
+++ b/website/src/pages/guide.astro
@@ -417,8 +417,11 @@ cd report && quarto publish connect --server https://connect.example.com
vip.toml, pass --no-config. The scan then
uses only the environment and the auth cache. A rendered page that
vip report did not make from the current
- results.json is left out. Run vip report
- again to include it. A failures.json, JUnit, or SARIF
+ results.json is left out. A rendered file that changed
+ after vip report made it is left out too. The PDF is
+ included only together with both HTML pages, because the scan
+ cannot read the text in a PDF. Run vip report
+ again to include a file that was left out. A failures.json, JUnit, or SARIF
file from a different run than results.json is left
out too. If a JUnit or SARIF file from an older VIP has no run
time and is older than results.json, it is also left
From a3d946f5a00b1434d19f678e9e262a6d0e293d37 Mon Sep 17 00:00:00 2001
From: Tim
Date: Mon, 16 Nov 2026 15:40:32 -0500
Subject: [PATCH 11/11] fix(bundle): close secret scan gaps from review
A damaged auth cache file or a report file that is not UTF-8 stops the
bundle. The scan reads the product URL credentials in results.json, finds
a URL password with a raw "#", "?", or "/", gives each URL part and each
cookie domain its own label, and skips data: URIs, scripts, and styles in
HTML pages. If results.json has a run time, a sidecar with none is left
out. Tests cover the scan limits, each credential variable, main(),
VIP_CONFIG, and cleanup after a refusal.
---
selftests/test_bundle_archive.py | 105 ++++++++++++--
selftests/test_bundle_summary.py | 226 +++++++++++++++++++++++++++----
selftests/test_cli_bundle.py | 117 +++++++++++++++-
src/vip/bundle/archive.py | 99 ++++++++++++--
src/vip/bundle/summary.py | 200 +++++++++++++++++++--------
src/vip/cli/app.py | 11 +-
src/vip/cli/bundle.py | 45 ++++--
website/src/pages/guide.astro | 85 +++++++-----
8 files changed, 729 insertions(+), 159 deletions(-)
diff --git a/selftests/test_bundle_archive.py b/selftests/test_bundle_archive.py
index 56d25d162..3ce53f23f 100644
--- a/selftests/test_bundle_archive.py
+++ b/selftests/test_bundle_archive.py
@@ -228,6 +228,8 @@ def _sidecar_text(name: str, run: str) -> str:
''
f''
)
+ if name == "failures.json":
+ return json.dumps({"generated_at": run, "failures": []})
return json.dumps({"version": "2.1.0", "runs": [{"properties": {"generated_at": run}}]})
@@ -257,16 +259,30 @@ def test_sidecar_with_another_run_time_is_left_out(tmp_path, name, sidecar_mtime
assert got.left_out == [{"name": f"report/{name}", "reason": reason}]
-@pytest.mark.parametrize("name", ["junit.xml", "results.sarif"])
-@pytest.mark.parametrize(("sidecar_mtime", "included"), [(1_000_001.0, False), (1_000_900.0, True)])
-def test_sidecar_with_no_run_time_uses_the_file_time(tmp_path, name, sidecar_mtime, included):
- # A file from an older vip has no generated_at value. The text of the
- # value in some other place of the file does not count.
+@pytest.mark.parametrize("name", _SIDECARS)
+@pytest.mark.parametrize("sidecar_mtime", [1_000_001.0, 1_000_900.0])
+@pytest.mark.parametrize("text", ['{"runs": [{"note": "2026-11-12T14:00:00+00:00"}]}', "{not json"])
+def test_sidecar_with_no_run_time_is_left_out_when_results_has_one(
+ tmp_path, name, sidecar_mtime, text
+):
+ # If results.json has a run time, a sidecar with none is not from this
+ # run, even if it is newer, for example a later pytest --junitxml. The
+ # text of the value in some other place of the file does not count.
report = tmp_path / "report"
- run = "2026-11-12T14:00:00+00:00"
- results = json.dumps({"generated_at": run, "results": []})
+ results = json.dumps({"generated_at": "2026-11-12T14:00:00+00:00", "results": []})
_seed(report, sidecars=(), rendered=(), results=results, results_mtime=1_000_500.0)
- _write(report / name, json.dumps({"runs": [{"note": run}]}), sidecar_mtime)
+ _write(report / name, text, sidecar_mtime)
+ got = collect_members(report / "results.json", report)
+ reason = "has no run time, so it is not from the run of results.json"
+ assert got.left_out == [{"name": f"report/{name}", "reason": reason}]
+
+
+@pytest.mark.parametrize("name", _SIDECARS)
+@pytest.mark.parametrize(("sidecar_mtime", "included"), [(1_000_001.0, True), (999_999.0, False)])
+def test_with_no_run_time_in_results_the_file_time_decides(tmp_path, name, sidecar_mtime, included):
+ report = tmp_path / "report"
+ _seed(report, sidecars=(), rendered=())
+ _write(report / name, _sidecar_text(name, "2026-11-12T14:00:00+00:00"), sidecar_mtime)
got = collect_members(report / "results.json", report)
assert (f"report/{name}" in got.members) is included
@@ -351,6 +367,7 @@ def test_symlinked_results_raises(tmp_path):
(SHORT, "pa"ss&wörd-01".encode()), # html.escape
(SHORT, 'pa"ss&wörd-01'.encode()), # html.escape with quote=False (XML text)
(SHORT, b"pa%22ss%26w%C3%B6rd-01"), # percent-encoded with quote
+ (SHORT, b"pa%22ss%26w%c3%b6rd-01"), # percent-encoded with lowercase hex
(SPACED, b"pass+word-0123"), # percent-encoded with quote_plus
(BACKSLASH, b"'ab\\\\cd-Secret9'"), # repr in error text
(BACKSLASH, json.dumps(repr(BACKSLASH)).encode()), # repr, then json.dumps
@@ -423,6 +440,78 @@ def test_short_secrets_are_not_scanned_but_are_reported():
assert short_secret_labels(secrets) == ["auth.password"]
+def test_scan_threshold_boundaries():
+ # A value of MIN_SECRET_LENGTH (4) is scanned. A value of
+ # PARTIAL_MIN_SECRET_LENGTH (16) also gets its first and last
+ # PARTIAL_LENGTH (8) characters as forms. A value of 15 does not.
+ assert find_secret_hits({"a": b"x ab1c y"}, [("k", "ab1c")]) == [("a", "k")]
+ at, below = "Abcdefg1-xyz-23Q", "Abcdefg1-xyz-23"
+ assert find_secret_hits({"a": b"x Abcdefg1 y"}, [("k", at)]) == [("a", "k")]
+ assert find_secret_hits({"a": b"x Abcdefg y"}, [("k", at)]) == []
+ assert find_secret_hits({"a": b"x Abcdefg1 y"}, [("k", below)]) == []
+
+
+def test_scan_reads_a_value_with_bytes_that_are_not_utf8():
+ # Python keeps such environment bytes as surrogates.
+ assert find_secret_hits({"a": b"x pw\xff1234 y"}, [("k", "pw\udcff1234")]) == [("a", "k")]
+ assert find_secret_hits({"a": b"x"}, [("k", "pw\ud8001234")]) == []
+
+
+_PAGE = (
+ b""
+ b''
+ b'
metadata:K3yText01
'
+)
+
+
+@pytest.mark.parametrize("secret", ["K3yFont1", "K3yScript1", "K3yImage1"])
+def test_scan_skips_data_uris_scripts_and_styles_in_html(secret):
+ assert find_secret_hits({"report/_output/index.html": _PAGE}, [("k", secret)]) == []
+ assert find_secret_hits({"report/results.json": _PAGE}, [("k", secret)]) == [
+ ("report/results.json", "k")
+ ]
+
+
+def test_scan_reads_the_text_of_an_html_page():
+ hits = find_secret_hits({"report/_output/index.html": _PAGE}, [("k", "K3yText01")])
+ assert hits == [("report/_output/index.html", "k")]
+
+
+@pytest.mark.parametrize(
+ "data",
+ [
+ _RESULTS.encode("utf-16"),
+ b"\xef\xbb\xbf" + _RESULTS.encode(),
+ b'{"results": [], "x": "\xff"}',
+ ],
+)
+def test_results_that_is_not_utf8_raises(tmp_path, data):
+ report = tmp_path / "report"
+ report.mkdir()
+ (report / "results.json").write_bytes(data)
+ with pytest.raises(BundleError, match="not UTF-8"):
+ collect_members(report / "results.json", report)
+
+
+@pytest.mark.parametrize("name", [*_SIDECARS, "_output/index.html"])
+def test_sidecar_or_page_that_is_not_utf8_raises(tmp_path, name):
+ report = tmp_path / "report"
+ _seed(report)
+ (report / name).write_bytes(f"content of {name}".encode("utf-16"))
+ os.utime(report / name, (1_000_001.0, 1_000_001.0))
+ if name.startswith("_output/"):
+ record_rendered(report / "_output", results_digest(_RESULTS.encode()), _RENDERED)
+ with pytest.raises(BundleError, match="not UTF-8"):
+ collect_members(report / "results.json", report)
+
+
+def test_deeply_nested_results_raises_bundle_error(tmp_path):
+ report = tmp_path / "report"
+ _seed(report, sidecars=(), rendered=(), results="[" * 100_000 + "]" * 100_000)
+ with pytest.raises(BundleError, match="is not a VIP results file"):
+ collect_members(report / "results.json", report)
+
+
def test_write_zip_writes_every_member(tmp_path):
out = tmp_path / "b.zip"
write_zip(out, {"y": b"2", "x": b"1"})
diff --git a/selftests/test_bundle_summary.py b/selftests/test_bundle_summary.py
index cd08de477..5e58ea0ad 100644
--- a/selftests/test_bundle_summary.py
+++ b/selftests/test_bundle_summary.py
@@ -12,6 +12,7 @@
from vip.bundle.summary import (
EXCLUDED_FIELDS,
+ SECRET_ENV_VARS,
SECRET_FIELDS,
SUMMARIZED_FIELDS,
auth_cache_secrets,
@@ -28,6 +29,7 @@
VIPConfig,
WorkbenchConfig,
)
+from vip.errors import BundleError
from vip.proxy import ProxyConfig
# Every VIP_* variable that VIPConfig or the bundle reads. A developer's own
@@ -177,49 +179,84 @@ def _config_classes(cls: Any, prefix: str = "") -> list[tuple[Any, str]]:
_REQUIRED_VALUES = {GitTestConfig: {"auth_method": "https-token"}}
-def _sentinel_values(cls: type, prefix: str) -> dict[str, str]:
- """Return a distinct sentinel for each init field that can hold a str."""
+_STR_TYPES = (str, str | None)
+_LIST_TYPES = (list[str], list[str] | None)
+_DICT_TYPES = (dict[str, str], dict[str, str] | None)
+
+
+def _sentinel_values(cls: type, prefix: str) -> dict[str, tuple[object, str]]:
+ """Return ``{field: (value, sentinel)}`` for each init field that can hold a str.
+
+ A str field gets the sentinel. A list field gets a list that holds it,
+ and a dict field gets a dict with it as the value.
+ """
hints = typing.get_type_hints(cls)
- return {
- f.name: f"SENTINEL-{prefix}{f.name}"
- for f in dataclasses.fields(cls)
- if f.init and hints[f.name] in (str, str | None)
- }
+ found: dict[str, tuple[object, str]] = {}
+ for f in dataclasses.fields(cls):
+ sentinel = f"SENTINEL-{prefix}{f.name}"
+ hint = hints[f.name]
+ if not f.init:
+ continue
+ if hint in _STR_TYPES:
+ found[f.name] = (sentinel, sentinel)
+ elif hint in _LIST_TYPES:
+ found[f.name] = ([sentinel], sentinel)
+ elif hint in _DICT_TYPES:
+ found[f.name] = ({"key": sentinel}, sentinel)
+ return found
-def test_sentinel_values_cover_optional_str_fields():
+def test_sentinel_values_cover_str_list_and_dict_fields():
@dataclasses.dataclass
class Sample:
name: str = ""
token: str | None = None
+ hosts: list[str] = dataclasses.field(default_factory=list)
+ keys: list[str] | None = None
+ headers: dict[str, str] = dataclasses.field(default_factory=dict)
count: int = 0
+ limits: dict[str, float] = dataclasses.field(default_factory=dict)
assert _sentinel_values(Sample, "s.") == {
- "name": "SENTINEL-s.name",
- "token": "SENTINEL-s.token",
+ "name": ("SENTINEL-s.name", "SENTINEL-s.name"),
+ "token": ("SENTINEL-s.token", "SENTINEL-s.token"),
+ "hosts": (["SENTINEL-s.hosts"], "SENTINEL-s.hosts"),
+ "keys": (["SENTINEL-s.keys"], "SENTINEL-s.keys"),
+ "headers": ({"key": "SENTINEL-s.headers"}, "SENTINEL-s.headers"),
}
def test_every_repr_masked_field_is_a_secret():
# A config class that hides a field in __repr__ treats it as a secret.
- # Every str field, optional or not, gets a distinct sentinel, so a new
- # hidden field is found.
+ # Every field that can hold a str, in a list, or in a dict gets a distinct
+ # sentinel, so a new hidden field is found.
classes = _config_classes(VIPConfig)
known = {ConnectConfig, WorkbenchConfig, GitTestConfig, PackageManagerConfig, AuthConfig}
assert known | {DataSourceEntry} <= {cls for cls, _ in classes}
hidden: set[str] = set()
for cls, prefix in classes:
- values = _sentinel_values(cls, prefix)
- values.update(_REQUIRED_VALUES.get(cls, {}))
- text = repr(cls(**values))
- hidden |= {
- prefix + name
- for name, value in values.items()
- if value.startswith("SENTINEL-") and value not in text
+ required = _REQUIRED_VALUES.get(cls, {})
+ sentinels = {
+ name: pair
+ for name, pair in _sentinel_values(cls, prefix).items()
+ if name not in required
}
+ text = repr(cls(**{name: value for name, (value, _) in sentinels.items()}, **required))
+ hidden |= {prefix + name for name, (_, s) in sentinels.items() if s not in text}
assert hidden == SECRET_FIELDS
+def test_fields_left_out_of_the_summary_that_are_not_secrets_are_pinned():
+ # The scan looks only for SECRET_FIELDS. A new field here is left out of
+ # the summary but is not scanned, so it must be a reviewed choice.
+ assert {
+ "auth.username",
+ "connect.url_scheme_inferred",
+ "workbench.url_scheme_inferred",
+ "package_manager.url_scheme_inferred",
+ } == EXCLUDED_FIELDS - SECRET_FIELDS
+
+
_SET_ONE_SECRET = {
"connect.api_key": lambda v: VIPConfig(connect=ConnectConfig(api_key=v)),
"workbench.api_key": lambda v: VIPConfig(workbench=WorkbenchConfig(api_key=v)),
@@ -372,7 +409,7 @@ def test_secret_values_labels_name_the_source(full_config):
labels = {value: label for label, value in secret_values(full_config)}
assert labels[SECRETS["auth.password"]] == "auth.password"
assert labels[SECRETS["clone_url user"]] == "workbench.git_test.clone_url user"
- assert labels[SECRETS["clone_url query"]] == "workbench.git_test.clone_url query"
+ assert labels[SECRETS["clone_url query"]] == "workbench.git_test.clone_url query access_token"
def test_clone_url_user_and_password_have_their_own_labels():
@@ -409,13 +446,57 @@ def test_secret_values_split_key_value_connection_string():
assert "sa" not in labels
-def test_auth_cache_secrets_skip_missing_or_bad_files(tmp_path):
- bad = tmp_path / "bad.json"
- bad.write_text("not json", encoding="utf-8")
- listed = tmp_path / "list.json"
- listed.write_text("[]", encoding="utf-8")
- for path in (bad, listed, tmp_path / "absent.json"):
- assert auth_cache_secrets(path) == []
+def test_auth_cache_secrets_skip_a_missing_file(tmp_path):
+ assert auth_cache_secrets(tmp_path / "absent.json") == []
+
+
+_GOOD_CACHE = json.dumps({"cookies": [{"name": "sid", "value": "COOKIE-VALUE-0123456789"}]})
+
+
+@pytest.mark.parametrize(
+ "data",
+ [
+ b"not json",
+ _GOOD_CACHE[:-3].encode(), # cut before the closing brackets
+ b"\xef\xbb\xbf" + _GOOD_CACHE.encode(), # UTF-8 BOM
+ _GOOD_CACHE.encode("utf-16"),
+ b"[]",
+ b'{"cookies": 5}',
+ b'{"cookies": ["sid"]}',
+ b'{"origins": {"localStorage": []}}',
+ b'{"origins": [{"localStorage": 5}]}',
+ ],
+)
+def test_auth_cache_file_that_cannot_be_used_raises(tmp_path, data):
+ path = tmp_path / ".vip-auth-cache.json"
+ path.write_bytes(data)
+ with pytest.raises(BundleError, match="could not read the auth cache file"):
+ auth_cache_secrets(path)
+
+
+def test_auth_cache_directory_raises(tmp_path):
+ with pytest.raises(BundleError, match="could not read the auth cache file"):
+ auth_cache_secrets(tmp_path)
+
+
+def test_auth_cache_labels_name_the_domain_and_the_origin(tmp_path):
+ state = tmp_path / ".vip-auth-cache.json"
+ cookies = [
+ {"name": "sid", "domain": "a.example.com", "value": "COOKIE-A-0123456789"},
+ {"name": "sid", "domain": "b.example.com", "value": "COOKIE-B-0123456789"},
+ ]
+ origins = [
+ {
+ "origin": "https://a.example.com",
+ "localStorage": [{"name": "t", "value": "LOCAL-A-0123456789"}],
+ }
+ ]
+ state.write_text(json.dumps({"cookies": cookies, "origins": origins}), encoding="utf-8")
+ assert auth_cache_secrets(state) == [
+ ("auth cache cookie a.example.com sid", "COOKIE-A-0123456789"),
+ ("auth cache cookie b.example.com sid", "COOKIE-B-0123456789"),
+ ("auth cache localStorage https://a.example.com t", "LOCAL-A-0123456789"),
+ ]
@pytest.mark.parametrize(
@@ -486,8 +567,8 @@ def test_each_proxy_variable_is_read(monkeypatch, var):
@pytest.mark.parametrize(
("url", "expected"),
[
- # urlsplit raises ValueError for this host.
- ("http://u:SECRET@[::1/x?token=T", "http://[::1/x"),
+ # urlsplit raises ValueError for this host. It is not a plain host.
+ ("http://u:SECRET@[::1/x?token=T", "http:///x"),
# A Git remote with no scheme.
(
"x-access-token:SECRET@github.com/org/repo.git?access_token=T#f",
@@ -497,3 +578,90 @@ def test_each_proxy_variable_is_read(monkeypatch, var):
)
def test_strip_url_userinfo_handles_a_bad_host_and_a_bare_remote(url, expected):
assert strip_url_userinfo(url) == expected
+
+
+@pytest.mark.parametrize(
+ ("url", "secret", "summary"),
+ [
+ (
+ "https://admin:Abc123#Secret9@connect.example.com",
+ "Abc123#Secret9",
+ "https://connect.example.com",
+ ),
+ (
+ "https://admin:Abc?123x@connect.example.com/p",
+ "Abc?123x",
+ "https://connect.example.com/p",
+ ),
+ (
+ "https://admin:Ab/c123x@connect.example.com/p",
+ "Ab/c123x",
+ "https://connect.example.com/p",
+ ),
+ ("https://Tok#en-0123@connect.example.com", "Tok#en-0123", "https://connect.example.com"),
+ ],
+)
+def test_password_with_a_raw_delimiter_is_a_secret_and_is_not_in_the_summary(url, secret, summary):
+ config = VIPConfig(connect=ConnectConfig(url=url))
+ assert secret in {value for _, value in secret_values(config)}
+ assert strip_url_userinfo(url) == summary
+
+
+def test_url_with_an_at_sign_in_the_path_keeps_the_real_password_too():
+ # The two parsers differ when the URL holds two "@". The scan looks for both values.
+ url = "https://u:REAL-PASS-0123@connect.example.com/a@b"
+ values = {value for _, value in secret_values(VIPConfig(connect=ConnectConfig(url=url)))}
+ assert "REAL-PASS-0123" in values
+
+
+@pytest.mark.parametrize(
+ ("url", "summary"),
+ [
+ ("https://admin:Abc123", "https://"),
+ ("https://connect.example.com:8443/x", "https://connect.example.com:8443/x"),
+ ],
+)
+def test_summary_redacts_a_host_that_is_not_a_plain_host(url, summary):
+ assert strip_url_userinfo(url) == summary
+
+
+def test_each_part_of_a_url_has_its_own_label():
+ conn = "postgresql://appuser@db.example.com:5432/app?sslmode=require&password=PG-PASS-0123"
+ config = VIPConfig(
+ data_sources=[DataSourceEntry(name="pg", connection_string=conn)],
+ connect=ConnectConfig(url="https://u:URL-PASS-0123@c.example.com/?token=URL-TOKEN-0123"),
+ )
+ labels = {value: label for label, value in secret_values(config)}
+ assert labels["appuser"] == "data_sources.pg.connection_string user"
+ assert labels["PG-PASS-0123"] == "data_sources.pg.connection_string query password"
+ assert labels["URL-PASS-0123"] == "connect.url password"
+ assert labels["URL-TOKEN-0123"] == "connect.url query token"
+
+
+_INLINE_FOR_ENV = {
+ "VIP_CONNECT_API_KEY": lambda: VIPConfig(connect=ConnectConfig(api_key="INLINE-0123")),
+ "VIP_WORKBENCH_API_KEY": lambda: VIPConfig(workbench=WorkbenchConfig(api_key="INLINE-0123")),
+ "VIP_PACKAGE_MANAGER_TOKEN": lambda: VIPConfig(
+ package_manager=PackageManagerConfig(token="INLINE-0123")
+ ),
+ "VIP_PM_TOKEN": lambda: VIPConfig(package_manager=PackageManagerConfig(token="INLINE-0123")),
+ "VIP_TEST_PASSWORD": lambda: VIPConfig(auth=AuthConfig(password="INLINE-0123")),
+ "VIP_GIT_TOKEN": lambda: VIPConfig(
+ workbench=WorkbenchConfig(git_test=GitTestConfig(token="INLINE-0123"))
+ ),
+ "VIP_TEST_TOTP_SECRET": VIPConfig,
+}
+
+
+@pytest.mark.parametrize("var", SECRET_ENV_VARS)
+def test_each_env_variable_is_read_next_to_an_inline_value(monkeypatch, var):
+ # The configuration keeps the inline value, so only SECRET_ENV_VARS
+ # gives the scan the value of the variable.
+ monkeypatch.setenv(var, f"ENV-{var}-0123")
+ config = _INLINE_FOR_ENV[var]()
+ assert f"ENV-{var}-0123" not in repr(dataclasses.asdict(config))
+ assert f"ENV-{var}-0123" in {value for _, value in secret_values(config)}
+
+
+def test_every_env_variable_has_an_inline_builder():
+ assert set(_INLINE_FOR_ENV) == set(SECRET_ENV_VARS)
diff --git a/selftests/test_cli_bundle.py b/selftests/test_cli_bundle.py
index 13f1c1599..39ae4b144 100644
--- a/selftests/test_cli_bundle.py
+++ b/selftests/test_cli_bundle.py
@@ -196,10 +196,14 @@ def test_manifest_records_how_many_secrets_the_scan_knew(root, capsys):
# vip.toml holds nine credential values. The cache holds a cookie and a minted key.
assert manifest["secret_scan_sources"] == {
"configuration_and_environment": 9,
+ "results_product_urls": 0,
"auth_cache": 2,
}
out = capsys.readouterr().out
- assert "9 from the configuration and the environment, 2 from the auth cache" in out
+ assert (
+ "9 from the configuration and the environment, 0 from the product URLs in "
+ "results.json, 2 from the auth cache"
+ ) in out
def test_scan_with_no_known_secret_warns(root, capsys):
@@ -211,6 +215,7 @@ def test_scan_with_no_known_secret_warns(root, capsys):
manifest = json.loads(zf.read("manifest.json"))
assert manifest["secret_scan_sources"] == {
"configuration_and_environment": 0,
+ "results_product_urls": 0,
"auth_cache": 0,
}
assert any("knew no credential" in w for w in manifest["warnings"])
@@ -328,6 +333,48 @@ def test_manifest_is_scanned_too(root, monkeypatch):
monkeypatch.setenv("VIP_TEST_TOTP_SECRET", "Run-Dir-0123")
with pytest.raises(BundleError, match=r"manifest\.json \(made by vip bundle\)"):
run_bundle(_args(output="b.zip", results=str(run_dir / "results.json")))
+ assert _leftovers(root) == []
+
+
+def _refuse_with_a_secret_hit(root, monkeypatch):
+ _write_report(root, results_extra=CONNECT_KEY)
+
+
+def _refuse_with_a_manifest_hit(root, monkeypatch):
+ run_dir = root / "Run-Dir-0123"
+ _write(run_dir / "results.json", '{"deployment_name": "Acme Prod", "results": []}', 5.0)
+ monkeypatch.setenv("VIP_TEST_TOTP_SECRET", "Run-Dir-0123")
+ return {"results": str(run_dir / "results.json")}
+
+
+def _refuse_with_an_unknown_label(root, monkeypatch):
+ return {"allow_match": ["connect.apikey"]}
+
+
+def _refuse_with_a_bad_auth_cache(root, monkeypatch):
+ (root / ".vip-auth-cache.json").write_text('{"cookies": [', encoding="utf-8")
+
+
+def _refuse_with_a_config_mismatch(root, monkeypatch):
+ _write_report(root, deployment="Other")
+
+
+@pytest.mark.parametrize(
+ "refuse",
+ [
+ _refuse_with_a_secret_hit,
+ _refuse_with_a_manifest_hit,
+ _refuse_with_an_unknown_label,
+ _refuse_with_a_bad_auth_cache,
+ _refuse_with_a_config_mismatch,
+ ],
+)
+def test_each_refusal_leaves_no_zip_and_no_temp_file(root, monkeypatch, refuse):
+ overrides = refuse(root, monkeypatch) or {}
+ with pytest.raises(BundleError):
+ run_bundle(_args(output="b.zip", **overrides))
+ assert _leftovers(root) == []
+ assert _leftovers(root / "report") == []
def test_minted_key_in_root_cache_blocks_bundle(root, tmp_path, monkeypatch):
@@ -620,3 +667,71 @@ def test_bundle_error_exits_with_code_one(tmp_path, monkeypatch, capsys):
main()
assert exc.value.code == 1
assert "Error: no results found" in capsys.readouterr().err
+
+
+def test_main_writes_a_bundle_from_real_arguments(root, monkeypatch, capsys):
+ # The parser, not a hand-built Namespace, sets each option. A change to an
+ # option's action or dest fails here.
+ _write_report(root, results_extra="SECRET-auth-password")
+ argv = ["vip", "bundle", "--output", "b.zip", "--force", "--allow-match", "auth.password"]
+ (root / "b.zip").write_bytes(b"old")
+ monkeypatch.setattr(sys, "argv", argv)
+ main()
+ with zipfile.ZipFile(root / "b.zip") as zf:
+ manifest = json.loads(zf.read("manifest.json"))
+ assert manifest["allowed_matches"] == [
+ {"member": "report/results.json", "label": "auth.password"}
+ ]
+ assert "Bundle written" in capsys.readouterr().out
+
+
+def test_main_repeats_allow_match(root, monkeypatch):
+ _write_report(root, results_extra="SECRET-auth-password SECRET-pm-token")
+ argv = ["vip", "bundle", "--output", "b.zip"]
+ argv += ["--allow-match", "auth.password", "--allow-match", "package_manager.token"]
+ monkeypatch.setattr(sys, "argv", argv)
+ main()
+ assert (root / "b.zip").is_file()
+
+
+def test_vip_config_selects_the_configuration_file(root, tmp_path, monkeypatch):
+ elsewhere = tmp_path / "conf" / "site.toml"
+ elsewhere.parent.mkdir()
+ (root / "vip.toml").rename(elsewhere)
+ monkeypatch.setenv("VIP_CONFIG", str(elsewhere))
+ _write_report(root, results_extra="SECRET-pm-token")
+ with pytest.raises(BundleError, match=r"package_manager\.token"):
+ run_bundle(_args(output="b.zip"))
+
+
+_RUN_URL = "https://admin:RUN-URL-PASS-0123@connect.example.com"
+
+
+def _write_report_with_run_url(root: Path) -> None:
+ report = root / "report"
+ results = {
+ "deployment_name": "Acme Prod",
+ "products": {"connect": {"url": _RUN_URL}},
+ "results": [],
+ }
+ _write(report / "results.json", json.dumps(results), RESULTS_MTIME)
+
+
+def test_no_config_scan_uses_the_product_url_credentials_in_results(root):
+ (root / "vip.toml").unlink()
+ _write_report_with_run_url(root)
+ with pytest.raises(BundleError, match=r"results\.json connect\.url password"):
+ run_bundle(_args(output="b.zip", no_config=True))
+
+
+def test_config_scan_uses_the_product_url_credentials_in_results(root):
+ # vip.toml has the same host with no userinfo, so it passes the match check.
+ _write_report_with_run_url(root)
+ with pytest.raises(BundleError, match=r"results\.json connect\.url password"):
+ run_bundle(_args(output="b.zip"))
+
+
+def test_bad_auth_cache_stops_the_bundle(root):
+ (root / ".vip-auth-cache.meta.json").write_text('{"api_key": "x"', encoding="utf-8")
+ with pytest.raises(BundleError, match="could not read the auth cache file"):
+ run_bundle(_args(output="b.zip"))
diff --git a/src/vip/bundle/archive.py b/src/vip/bundle/archive.py
index f95f22a77..463b0cbc1 100644
--- a/src/vip/bundle/archive.py
+++ b/src/vip/bundle/archive.py
@@ -42,6 +42,19 @@
PARTIAL_LENGTH = 8
PARTIAL_MIN_SECRET_LENGTH = 16
+# A rendered page is about 2 MB, most of it fonts and images in data: URIs and
+# the bodies of