summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks
diff options
context:
space:
mode:
authorMohamed Hegazy <8000722+mhegazy@users.noreply.github.com>2026-11-05 14:28:26 -0800
committerGitHub <noreply@github.com>2026-11-05 14:28:26 -0800
commitd189be9fc7a63d53d4e2af59accdd70e933e41e9 (patch)
treefc78ed871ef817c56805bde79bb50fe9c91944a6 /plugins/security-guidance/hooks
parenteba976de6eed86f916e2c3ccd6f176faf19c3bc7 (diff)
parent3654d346b45836e43e013b849be19233ff0efbc3 (diff)
Merge pull request #6835 from anthropics/mhegazy/sg-secret-rename-pairing
security-guidance: keep a renamed file paired with its old name in the Stop review
Diffstat (limited to 'plugins/security-guidance/hooks')
-rw-r--r--plugins/security-guidance/hooks/diffstate.py8
-rw-r--r--plugins/security-guidance/hooks/gitutil.py29
2 files changed, 34 insertions, 3 deletions
diff --git a/plugins/security-guidance/hooks/diffstate.py b/plugins/security-guidance/hooks/diffstate.py
index de9719cf..34655335 100644
--- a/plugins/security-guidance/hooks/diffstate.py
+++ b/plugins/security-guidance/hooks/diffstate.py
@@ -18,7 +18,7 @@ from gitutil import (
GIT_CMD,
_git_dir, _git_toplevel, _git_status_porcelain,
_git_rev_parse_head, _is_ancestor, _git_name_only, _temp_index,
- index_entry_count,
+ _secret_rename_origins, index_entry_count,
)
from session_state import with_locked_state
from extensibility import _same_dir
@@ -1165,6 +1165,10 @@ def compute_v2_review_set(cwd, baseline_sha, head_at_capture, untracked_at_basel
# dangling baseline SHA was pruned). Either way, don't intersect with ∅ —
# that would silently zero the review set. Fall back to dirty_now.
review_set = (dirty_now & changed_since) if changed_since is not None else dirty_now
+ # Kept beside its new name so that the content diff pairs the two.
+ secret_origins = {
+ old for new, old in _secret_rename_origins.items() if new in review_set}
+ review_set = review_set | secret_origins
review_paths = [os.path.join(repo, p) for p in sorted(review_set)]
untracked_in_review = sorted(new_untracked & review_set)
@@ -1176,4 +1180,6 @@ def compute_v2_review_set(cwd, baseline_sha, head_at_capture, untracked_at_basel
# Only emit when nonzero to stay under the 10-key telemetry cap.
if preexisting_unchanged:
metrics["preexisting_untracked_excluded"] = len(preexisting_unchanged)
+ if secret_origins:
+ metrics["secret_renamed"] = len(secret_origins)
return review_paths, diff_base, repo, untracked_in_review, metrics
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py
index 747b95a8..c80d0600 100644
--- a/plugins/security-guidance/hooks/gitutil.py
+++ b/plugins/security-guidance/hooks/gitutil.py
@@ -1183,6 +1183,18 @@ def _git_reflog_recent_commits(repo_root, max_age_s=120, max_n=5):
return fresh, stale
+# New name -> secret-shaped old name, for the renames and copies git reported
+# during this hook run. The Stop review limits its diff to a list of paths:
+# with only the new name in it, git shows the secret's content as a new file,
+# and the diff parsers cannot tell it came from a name they leave out.
+_secret_rename_origins = {}
+
+
+def _note_rename(old_path, new_path):
+ if _is_secret_shaped(old_path):
+ _secret_rename_origins[new_path] = old_path
+
+
def _git_name_only(cwd, base, include_untracked=False):
"""Return the set of repo-root-relative paths that differ from `base`,
or None if git failed (unresolvable ref, not a repo, timeout). Callers
@@ -1202,7 +1214,7 @@ def _git_name_only(cwd, base, include_untracked=False):
def _run(env):
# core.quotePath=false comes from GIT_CMD globally (see definition).
result = subprocess.run(
- [*GIT_CMD, "diff", IGNORE_SUBMODULES, "--name-only", "-z", base],
+ [*GIT_CMD, "diff", IGNORE_SUBMODULES, "--name-status", "-z", base],
cwd=cwd, capture_output=True, timeout=30,
env=env,
)
@@ -1211,7 +1223,18 @@ def _git_name_only(cwd, base, include_untracked=False):
debug_log(f"_git_name_only({base!r}) rc={result.returncode}: {stderr_str[:200]}")
return None
stdout = (result.stdout or b"").decode("utf-8", errors="replace")
- return {p for p in stdout.split("\0") if p}
+ # Fields are `status\0path\0`, or `R<score>\0old\0new\0` for a rename
+ # or copy, which is listed by its new name only.
+ fields = stdout.split("\0")
+ names = set()
+ i = 0
+ while i + 1 < len(fields):
+ if fields[i][:1] in ("R", "C") and i + 2 < len(fields):
+ _note_rename(fields[i + 1], fields[i + 2])
+ i += 1
+ names.add(fields[i + 1])
+ i += 2
+ return names
try:
# The diff only reads the index (diff.autoRefreshIndex is off in
@@ -1272,6 +1295,8 @@ def _git_status_porcelain(cwd):
# 2-char-status entry.
if "R" in xy or "C" in xy:
i += 1
+ if i < len(entries):
+ _note_rename(entries[i], path)
i += 1
return tracked, untracked
except (subprocess.TimeoutExpired, FileNotFoundError, OSError, ValueError) as e: