summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/security-guidance/hooks/gitutil.py26
-rw-r--r--plugins/security-guidance/tests/test_renamed_into_review.py77
2 files changed, 99 insertions, 4 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):
diff --git a/plugins/security-guidance/tests/test_renamed_into_review.py b/plugins/security-guidance/tests/test_renamed_into_review.py
index 236b5fdc..109f5efc 100644
--- a/plugins/security-guidance/tests/test_renamed_into_review.py
+++ b/plugins/security-guidance/tests/test_renamed_into_review.py
@@ -137,7 +137,7 @@ def test_rename_added_by_an_amend(tmp_path, hook_env, stub_api):
("a.py", "pkg/b.py"), ("a.py", "notes.txt"), ("notes.txt", "other.txt"),
("a.py", "vendor/a.py"), ("vendor/a.py", "a.py"), ("server.pem", "server"),
("prod.env", "prod.sh"), (".htpasswd", "htpasswd"), ("dump.csv", "dump.sql"),
- ("credentials.txt", "credentials.sh"), ("deploy_rsa.txt", "deploy.sh"),
+ ("credentials.txt", "credentials.sh"), ("notes.txt", "credentials.sh"), ("deploy_rsa.txt", "deploy.sh"),
("passwords.md", "list.py"), ("notes.txt", "notes"), ("data.txt", "data.json"), ("notes.txt", "vendor/a.py"),
])
def test_other_renames_are_not_sent(tmp_path, hook_env, stub_api, old, new):
@@ -148,6 +148,81 @@ def test_other_renames_are_not_sent(tmp_path, hook_env, stub_api, old, new):
assert _sent(stub_api) == ""
+@pytest.mark.parametrize("name", """
+ api_keys.txt tokens.txt apiKeysProd.md KEYS.md AWSkeys.txt GCPkey.md DBpass.txt stripekey.txt
+ API_TOKEN.rst github-token-old.txt db_password.txt db_passwd.md my.creds.txt db-cred.txt
+ root_pwd.txt site_logins.txt prod.env.txt env.production.txt server.pem.txt id_ed25519.md
+ passphrase.md github-recovery-codes.txt gcp-service-account.txt service_account.md pgpass.txt
+ npmrc.txt netrc.txt my-secret.txt app_credential.md
+ secrets/prod.txt Credentials/prod.md prod-creds/a.txt api-keys/s.txt tokens/a/b.md .secrets/a.txt
+ vault/a.txt passwd/a.txt .ssh/prod.txt a/.aws/prod.txt a/.gnupg/n.md .kube/n.txt a/Passwords/b.txt
+ api_keys passwords secrets.yaml .env .env.py server.pem server.key deploy_rsa
+""".split())
+def test_name_of_a_secret(name):
+ assert gitutil._is_secret_text(name)
+
+
+@pytest.mark.parametrize("name", """
+ notes.txt compassion.md environment.md keyboard.rst login.md tokenizer.md jwt.md author.md
+ aws/setup.md ssh/setup.md password-manager/notes.txt secret-service/notes.txt keyboards/a.txt
+ tokenizer/notes.txt
+""".split())
+def test_name_of_ordinary_text(name):
+ assert not gitutil._is_secret_text(name)
+ assert gitutil._is_text_turned_code(name, "config.py")
+
+
+@pytest.mark.parametrize("name", [
+ "tokens.py", "api_keys.py", "token_store.js", "vendor/tokens.go", "secrets/load.py",
+ "vault/Dockerfile", "secrets/Makefile"])
+def test_code_is_not_a_list_of_secrets(name):
+ assert not gitutil._is_secret_text(name)
+
+
+@pytest.mark.parametrize("old,new", [
+ ("api_keys.txt", "api_keys.py"), ("tokens.txt", "config.py"), ("keys/prod.md", "config.sh")])
+def test_list_of_secrets_is_not_sent(tmp_path, hook_env, stub_api, old, new):
+ repo = make_repo(tmp_path / "repo", {old: SECRET, "app.py": "x = 1\n"})
+ _move(repo, old, new)
+ m = _commit(repo, hook_env)
+ assert m["skip_reason"] == 30 and "renamed_in" not in m, m
+ assert _sent(stub_api) == ""
+
+
+@pytest.mark.parametrize("old", ["api_keys.txt", "passwords", "secrets.yaml", "server.pem"])
+def test_list_of_secrets_renamed_in_two_steps_is_not_sent(tmp_path, hook_env, stub_api, old):
+ repo = make_repo(tmp_path / "repo", {old: SECRET})
+ _move(repo, old, "settings.txt")
+ git(repo, "commit", "-qm", "one")
+ _move(repo, "settings.txt", "settings.py")
+ assert _commit(repo, hook_env)["skip_reason"] == 30
+ assert _sent(stub_api) == ""
+
+
+def test_other_renames_of_the_commit_are_still_read(tmp_path, hook_env, stub_api):
+ repo = make_repo(tmp_path / "repo", {
+ "api_keys.txt": SECRET, "docs/hotkeys.md": "press a\n" * 9, "notes.txt": BODY})
+ _move(repo, "api_keys.txt", "settings.py")
+ _move(repo, "docs/hotkeys.md", "hotkeys.py")
+ _move(repo, "notes.txt", "payload.py")
+ assert _commit(repo, hook_env)["renamed_in"] == 1
+ assert MARK in _sent(stub_api) and "SECRET_VALUE" not in _sent(stub_api)
+
+
+def test_list_of_secrets_is_not_listed():
+ assert not gitutil._is_text_turned_code("api_keys.txt", "config.py")
+
+
+def test_code_with_a_secret_word_in_its_name_is_not_a_secret_origin(
+ tmp_path, hook_env, stub_api):
+ repo = make_repo(tmp_path / "repo", {"token_store.py": BODY, "app.py": "x = 1\n"})
+ _move(repo, "token_store.py", "notes.txt")
+ git(repo, "commit", "-qm", "one")
+ _move(repo, "notes.txt", "payload.py")
+ assert _commit(repo, hook_env)["renamed_in"] == 1
+ assert MARK in _sent(stub_api)
+
+
def test_rename_away_from_a_secret_shaped_name_stays_out(tmp_path, hook_env, stub_api):
repo = make_repo(tmp_path / "repo", {"deploy_rsa": SECRET})
_move(repo, "deploy_rsa", "settings.py")