diff options
| author | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-10-19 16:36:23 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-10-19 16:36:23 +0000 |
| commit | dc6c4e2470abdd65ffec47b11fa8273deeed2efe (patch) | |
| tree | f81c8f65cc18720ddcfb68ee52db459798ae7c5d /plugins | |
| parent | ea10b213686c90bb1984b2016937310e05336cf3 (diff) | |
security-guidance: count secret-named data files in the Stop review setmhegazy/sg-secret-named-telemetry
Data files named like secrets.yml or client_secret.json are reviewed like any other file. The Stop hook now reports how many are in its review set, as a count only (secret_named_reviewed, emitted when nonzero), so the share of reviews that include one can be read from telemetry. No change to what is reviewed.
Refs anthropics/claude-code#96276
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/security-guidance/hooks/gitutil.py | 36 | ||||
| -rwxr-xr-x | plugins/security-guidance/hooks/security_reminder_hook.py | 3 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_secret_named_metric.py | 94 |
3 files changed, 133 insertions, 0 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py index bc4d753b..c2750afa 100644 --- a/plugins/security-guidance/hooks/gitutil.py +++ b/plugins/security-guidance/hooks/gitutil.py @@ -934,6 +934,42 @@ def _is_secret_shaped(file_path): return False +# Data files whose name says they hold secrets (`secrets.yml`, +# `client_secret.json`). They are reviewed like any other data file; the Stop +# hook only counts how many are in its review set (never the names), to show +# how common they are. +_SECRET_NAMED_STEMS = frozenset(( + 'secret', 'secrets', 'credential', 'credentials', + 'client_secret', 'client_secrets', 'client-secret', 'client-secrets', + 'service-account', 'service_account', 'serviceaccount', + 'service-account-key', 'service_account_key', 'serviceaccountkey', +)) +_SECRET_NAMED_DATA_EXTENSIONS = frozenset(( + '.json', '.yaml', '.yml', '.toml', '.properties', '.tfvars', +)) +# `secrets.example.yml` is a placeholder and `secrets.enc.yaml` is ciphertext. +_NOT_SECRET_NAME_PARTS = frozenset(( + 'example', 'sample', 'template', 'tmpl', 'dist', 'schema', + 'enc', 'encrypted', 'sops', +)) + + +def _is_secret_named(file_path): + base = os.path.basename(file_path.replace("\\", "/")).lower() + parts = base.lstrip('.').split('.') + if len(parts) < 2 or '.' + parts[-1] not in _SECRET_NAMED_DATA_EXTENSIONS: + return False + if _NOT_SECRET_NAME_PARTS.intersection(parts[1:-1]): + return False + stem = parts[0] + # Google's OAuth download is `client_secret_<client id>.json`. + return stem in _SECRET_NAMED_STEMS or stem.startswith('client_secret_') + + +def count_secret_named(diff_files): + return sum(1 for file_path, _ in diff_files if _is_secret_named(file_path)) + + # Path tokens that bump a file's review priority when a commit exceeds # MAX_DIFF_FILES and we have to pick a subset. These are exactly the surfaces # single-shot and agentic reviews disagree on most (auth, routing, IPC, diff --git a/plugins/security-guidance/hooks/security_reminder_hook.py b/plugins/security-guidance/hooks/security_reminder_hook.py index ea5bab18..9573a35c 100755 --- a/plugins/security-guidance/hooks/security_reminder_hook.py +++ b/plugins/security-guidance/hooks/security_reminder_hook.py @@ -2323,6 +2323,9 @@ def handle_stop_hook(input_data): diff_files = filter_preexisting_from_diff(diff_files, cwd, baseline_sha) debug_log(f"Stop hook: reviewing {len(diff_files)} changed files (standard diff)") + secret_named = gitutil.count_secret_named(diff_files) + if secret_named: + _TAIL_METRICS["secret_named_reviewed"] = secret_named import time as _time stop_review_start = _time.time() diff --git a/plugins/security-guidance/tests/test_secret_named_metric.py b/plugins/security-guidance/tests/test_secret_named_metric.py new file mode 100644 index 00000000..14d71923 --- /dev/null +++ b/plugins/security-guidance/tests/test_secret_named_metric.py @@ -0,0 +1,94 @@ +"""The Stop review counts the data files it sends whose name says they hold +secrets. Measurement only: the files are reviewed as before.""" +import pytest + +from conftest import ( + STUB_VULN, VULN_PY, make_repo, metrics_of, run_hook, stop_payload, ups_payload, +) + +import gitutil + + +@pytest.mark.parametrize("path", [ + "secrets.yml", + "config/secrets.yaml", + "secrets.prod.json", + ".secrets.toml", + "Secrets.YML", + "credentials.json", + "client_secret.json", + "keys/service-account.json", + "service_account.json", + "terraform/secrets.tfvars", + "client-secret.json", + "client_secret_1234-abc.apps.googleusercontent.com.json", + "serviceAccountKey.json", +]) +def test_secret_named_data_files(path): + assert gitutil._is_secret_named(path) + + +@pytest.mark.parametrize("path", [ + "secrets.py", + "src/credentials.ts", + "secrets_manager.yaml", + "app-secrets.json", + "docs/secrets.md", + "secrets", + "config.yaml", + "package.json", + "secrets/config.yaml", + "secrets.example.yml", + "credentials.sample.json", + "secrets.schema.json", + "secrets.enc.yaml", + "secrets.sops.yaml", + ".json", +]) +def test_other_files_are_not_counted(path): + assert not gitutil._is_secret_named(path) + + +def _review(tmp_path, hook_env, files): + repo = make_repo(tmp_path / "repo") + run_hook(ups_payload(repo), hook_env) + for name, body in files.items(): + (repo / name).write_text(body) + return run_hook(stop_payload(repo), hook_env) + + +def test_stop_hook_counts_secret_named_files_and_still_reviews_them(tmp_path, hook_env, stub_api): + _rc, stdout, _err = _review(tmp_path, hook_env, { + "secrets.yml": "db:\n password: example\n", + "credentials.json": '{"token": "example"}\n', + "app.py": "print(1)\n", + }) + m = metrics_of(stdout) + assert m.get("secret_named_reviewed") == 2 + assert m.get("files_reviewed") == 3 + assert "/v1/messages" in stub_api.calls + + +def test_count_is_on_the_findings_row_too(tmp_path, hook_env, stub_api): + stub_api.vulns = [STUB_VULN] + rc, stdout, _err = _review(tmp_path, hook_env, { + "secrets.yml": "db:\n password: example\n", + "app.py": VULN_PY, + }) + assert rc == 2 + assert metrics_of(stdout).get("secret_named_reviewed") == 1 + + +def test_no_key_when_no_such_file_is_reviewed(tmp_path, hook_env, stub_api): + _rc, stdout, _err = _review(tmp_path, hook_env, {"app.py": "print(1)\n"}) + m = metrics_of(stdout) + assert m.get("files_reviewed") == 1 + assert "secret_named_reviewed" not in m + + +def test_count_is_on_the_failed_review_row(tmp_path, hook_env, stub_api): + stub_api.status = 500 + _rc, stdout, _err = _review(tmp_path, hook_env, {"secrets.yml": "a: b\n"}) + m = metrics_of(stdout) + assert m.get("review_failed") is True + assert m.get("secret_named_reviewed") == 1 |
