summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/gitutil.py
diff options
context:
space:
mode:
authorClaude <noreply@anthropic.com>2026-11-16 17:43:40 +0000
committerClaude <noreply@anthropic.com>2026-11-16 17:43:40 +0000
commit8ee48731324352570a0b8d366b9c9b015bc62808 (patch)
treebe9742c6b2544296a3c0de4fbe77f3b29313e742 /plugins/security-guidance/hooks/gitutil.py
parentcf694f613b8d4568b74ce55aa306aad7cf2327a7 (diff)
security-guidance: leave renamed key lists out of the reviewmhegazy/sg-renamed-into-code
A text file whose name or folder says it holds secrets (api_keys.txt, tokens.txt, secrets/prod.md) was read whole when it was renamed to a code name. It now stays out of the review, and so does a file that recent commits renamed from such a name. Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins/security-guidance/hooks/gitutil.py')
-rw-r--r--plugins/security-guidance/hooks/gitutil.py26
1 files changed, 23 insertions, 3 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py
index 041bf7ba..f78739f3 100644
--- a/plugins/security-guidance/hooks/gitutil.py
+++ b/plugins/security-guidance/hooks/gitutil.py
@@ -2254,6 +2254,17 @@ _DATA_EXTENSIONS = frozenset((
_CODE_EXTENSIONS = SOURCE_CODE_EXTENSIONS - _DATA_EXTENSIONS
+# Text that holds secrets by its name or its folder's (`api_keys.txt`,
+# `keys/prod.md`, `.ssh/notes.txt`). It stays unread when it becomes code; so
+# does a document that only has such a word in its name (`hotkeys.md`).
+_SECRET_TEXT_RE = re.compile(
+ r"(?:secrets?|credentials?|creds?|passwords?|passwd|keys?|tokens?|vault"
+ r"|/\.(?:ssh|aws|gnupg|kube))/"
+ r"|(?:secret|credential|passw|passphrase|api.?key|recovery.?code|service.?account)[^/]*$"
+ r"|(?:keys?|tokens?|creds?|pass|pwd|env|logins|pem|ed25519|netrc|npmrc)"
+ r"(?![a-z])[^/]*$")
+
+
def _is_secret_stem(file_path):
"""`credentials.txt`, `id_rsa.md`: a secret's name with an extension."""
stem = os.path.splitext(os.path.basename(file_path))[0].lower()
@@ -2261,10 +2272,19 @@ def _is_secret_stem(file_path):
or stem.startswith('password'))
+def _is_secret_text(file_path):
+ """Whether a name says the file holds secrets, if it is not a code name."""
+ path = "/" + file_path.replace("\\", "/").lower()
+ return (_is_secret_shaped(file_path)
+ or os.path.splitext(path)[1] not in _CODE_EXTENSIONS
+ and os.path.basename(path) not in SOURCE_CODE_BASENAMES
+ and (_is_secret_stem(path) or bool(_SECRET_TEXT_RE.search(path))))
+
+
def _is_text_turned_code(old_path, new_path):
ext = os.path.splitext(new_path)[1].lower()
return (os.path.splitext(old_path)[1].lower() in _TEXT_EXTENSIONS
- and not _is_secret_stem(old_path) and not _is_secret_stem(new_path)
+ and not _is_secret_text(old_path) and not _is_secret_stem(new_path)
and (ext in _CODE_EXTENSIONS
or os.path.basename(new_path).lower() in SOURCE_CODE_BASENAMES)
and _section_is_reviewable(new_path, old_path)
@@ -2300,7 +2320,7 @@ _SECRET_ORIGIN_COMMITS = 50
def came_from_secret(repo_root, rev, paths, timeout):
"""Those of `paths` at `rev` that one of the last commits renamed from a
- secret-shaped name, whatever they were called in between. None on error."""
+ secret's name, whatever they were called in between. None on error."""
try:
result = subprocess.run(
[*GIT_CMD, "-c", "log.diffMerges=separate", "log", "-m",
@@ -2320,7 +2340,7 @@ def came_from_secret(repo_root, rev, paths, timeout):
for names in had.values():
if old and new in names:
names.add(old)
- return {path for path, names in had.items() if any(map(_is_secret_shaped, names))}
+ return {path for path, names in had.items() if any(map(_is_secret_text, names))}
def git_diff_unpaired(repo_root, revs, paths, timeout):