Skip to content

Commit bfaaa5b

Browse files
tests(gitleaks): add staged-leak checks and YAML allowlist; symlink template/.gitleaks.toml
1 parent b7d6658 commit bfaaa5b

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

template/.gitleaks.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../.gitleaks.toml

tests/test_gitleaks_precommit.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from test_example import copy_project, make_venv
6+
7+
# --- Stable patterns gitleaks flags out-of-the-box (should FAIL) ---
8+
STABLE_LEAK_CASES = [
9+
("github_token.txt", "ghp_1234567890abcdefghijklmnopqrstuvwx12AB"),
10+
(
11+
"slack_webhook.txt",
12+
"https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX",
13+
),
14+
("stripe_secret.txt", "sk_test_4eC39HqLyjWDarjtT1zdp7dcFAKE"),
15+
]
16+
17+
18+
@pytest.mark.parametrize("fname, content", STABLE_LEAK_CASES)
19+
def test_gitleaks_stable_patterns_fail(tmp_path: Path, fname: str, content: str):
20+
"""
21+
Generate a project, add a known-leaky pattern, stage it,
22+
and verify tox -e pre-commit (gitleaks) fails.
23+
"""
24+
copy_project(tmp_path)
25+
run = make_venv(tmp_path)
26+
27+
(tmp_path / fname).write_text(content)
28+
run("git add -A") # pre-commit's gitleaks scans the staged index
29+
30+
with pytest.raises(AssertionError, match=r"(?i)(leak|gitleaks|secret)"):
31+
run("./venv/bin/tox -e pre-commit")
32+
33+
34+
# --- Sealed-secrets: YAML/YML allowlisted; non-YAML should be flagged ---
35+
def _fake_sealed_secret_blob(n: int = 800) -> str:
36+
body = ("Qw9+/" * ((n // 4) + 1))[:n]
37+
return "Ag" + body + "=="
38+
39+
40+
def test_gitleaks_yaml_allowlist_for_sealed_secrets(tmp_path: Path):
41+
"""
42+
Keep .gitleaks.toml as-is (realistic behavior).
43+
- In .yaml/.yml: blob under spec.encryptedData -> allowlisted -> hook PASS
44+
- In non-YAML: same blob in code -> not allowlisted -> hook FAIL
45+
"""
46+
blob = _fake_sealed_secret_blob()
47+
48+
sealed_yaml = f"""\
49+
apiVersion: bitnami.com/v1alpha1
50+
kind: SealedSecret
51+
metadata:
52+
name: demo
53+
namespace: default
54+
spec:
55+
encryptedData:
56+
token: "{blob}"
57+
"""
58+
59+
# Case 1: .yaml (allowlisted => PASS)
60+
proj_yaml = tmp_path / "proj_yaml"
61+
proj_yaml.mkdir()
62+
copy_project(proj_yaml)
63+
run_yaml = make_venv(proj_yaml)
64+
(proj_yaml / "secret.yaml").write_text(sealed_yaml)
65+
run_yaml("git add -A")
66+
run_yaml("./venv/bin/tox -e pre-commit")
67+
68+
# Case 2: .yml (allowlisted => PASS)
69+
proj_yml = tmp_path / "proj_yml"
70+
proj_yml.mkdir()
71+
copy_project(proj_yml)
72+
run_yml = make_venv(proj_yml)
73+
(proj_yml / "secret.yml").write_text(sealed_yaml)
74+
run_yml("git add -A")
75+
run_yml("./venv/bin/tox -e pre-commit")
76+
77+
# Case 3: non-YAML (should be flagged => FAIL)
78+
proj_code = tmp_path / "proj_code"
79+
proj_code.mkdir()
80+
copy_project(proj_code)
81+
run_code = make_venv(proj_code)
82+
(proj_code / "leaky.py").write_text(f'api_key = "{blob}"\n')
83+
run_code("git add -A")
84+
with pytest.raises(AssertionError, match=r"(?i)(leak|gitleaks|secret)"):
85+
run_code("./venv/bin/tox -e pre-commit")

0 commit comments

Comments
 (0)