zak-guard 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- zak_guard-0.1.0/LICENSE +21 -0
- zak_guard-0.1.0/PKG-INFO +170 -0
- zak_guard-0.1.0/README.md +140 -0
- zak_guard-0.1.0/pyproject.toml +43 -0
- zak_guard-0.1.0/setup.cfg +4 -0
- zak_guard-0.1.0/src/zak_guard/__init__.py +2 -0
- zak_guard-0.1.0/src/zak_guard/cli.py +157 -0
- zak_guard-0.1.0/src/zak_guard/rules.py +204 -0
- zak_guard-0.1.0/src/zak_guard/scanner.py +215 -0
- zak_guard-0.1.0/src/zak_guard.egg-info/PKG-INFO +170 -0
- zak_guard-0.1.0/src/zak_guard.egg-info/SOURCES.txt +15 -0
- zak_guard-0.1.0/src/zak_guard.egg-info/dependency_links.txt +1 -0
- zak_guard-0.1.0/src/zak_guard.egg-info/entry_points.txt +2 -0
- zak_guard-0.1.0/src/zak_guard.egg-info/requires.txt +5 -0
- zak_guard-0.1.0/src/zak_guard.egg-info/top_level.txt +1 -0
- zak_guard-0.1.0/tests/test_rules.py +156 -0
- zak_guard-0.1.0/tests/test_scanner.py +187 -0
zak_guard-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zak Molloy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
zak_guard-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: zak-guard
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Pre-commit secret scanner: catches AWS keys, GitHub PATs, Stripe tokens, private keys, and more before they hit your repo
|
|
5
|
+
Author-email: Zak Molloy <zakguard@proton.me>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/molloyzak13/zak-guard
|
|
8
|
+
Project-URL: Repository, https://github.com/molloyzak13/zak-guard
|
|
9
|
+
Project-URL: Issues, https://github.com/molloyzak13/zak-guard/issues
|
|
10
|
+
Keywords: pre-commit,security,secrets,git,cli
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Security
|
|
21
|
+
Classifier: Topic :: Software Development :: Version Control :: Git
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-cov>=4; extra == "dev"
|
|
28
|
+
Requires-Dist: build>=1.0; extra == "dev"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# zak-guard
|
|
32
|
+
|
|
33
|
+
A pre-commit secret scanner. Catches AWS keys, GitHub PATs, Stripe tokens, private keys, and other credentials before they land in your repo.
|
|
34
|
+
|
|
35
|
+
Works standalone as a CLI tool and as a hook in the [pre-commit](https://pre-commit.com) framework.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## What it catches
|
|
40
|
+
|
|
41
|
+
| Rule | Examples |
|
|
42
|
+
|---|---|
|
|
43
|
+
| AWS access key ID | `AKIA...` (20-char, high entropy) |
|
|
44
|
+
| AWS secret access key | `AWS_SECRET_ACCESS_KEY=...` env var assignments |
|
|
45
|
+
| GitHub PATs (classic + fine-grained) | `ghp_...`, `github_pat_...` |
|
|
46
|
+
| GitHub OAuth / Actions / refresh tokens | `gho_`, `ghs_`, `ghr_` |
|
|
47
|
+
| OpenAI keys (legacy + project) | `sk-...`, `sk-proj-...` |
|
|
48
|
+
| Anthropic keys | `sk-ant-...` |
|
|
49
|
+
| DeepSeek / generic `sk-` keys | `sk-` prefix with high entropy |
|
|
50
|
+
| Stripe live keys | `sk_live_...`, `rk_live_...` |
|
|
51
|
+
| Slack tokens | `xoxb-`, `xoxp-`, `xoxa-` |
|
|
52
|
+
| Google API keys | `AIza...` |
|
|
53
|
+
| PEM private key blocks | `-----BEGIN ... PRIVATE KEY-----` |
|
|
54
|
+
| JWTs | Three base64url segments |
|
|
55
|
+
| Generic `.env` assignments | `PASSWORD=...`, `SECRET=...`, `TOKEN=...` with high entropy |
|
|
56
|
+
|
|
57
|
+
Every finding shows `file:line`, the rule name, and a redacted match (`AKIA...Y01`). The full value is never printed.
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## Install
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
pip install zak-guard
|
|
65
|
+
# or
|
|
66
|
+
pipx install zak-guard
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## Use as a CLI
|
|
72
|
+
|
|
73
|
+
Scan staged changes (what you're about to commit):
|
|
74
|
+
|
|
75
|
+
```sh
|
|
76
|
+
zak-guard --staged
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Scan everything in the current directory:
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
zak-guard --all
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Scan a specific path:
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
zak-guard --all ./src
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Suppress known false positives with an allowlist:
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
zak-guard --staged --allowlist .zak-guard-allowlist
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Wire it as a git pre-commit hook:
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
zak-guard install
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## Use with the pre-commit framework
|
|
106
|
+
|
|
107
|
+
Add to your `.pre-commit-config.yaml`:
|
|
108
|
+
|
|
109
|
+
```yaml
|
|
110
|
+
repos:
|
|
111
|
+
- repo: https://github.com/molloyzak13/zak-guard
|
|
112
|
+
rev: v0.1.0
|
|
113
|
+
hooks:
|
|
114
|
+
- id: zak-guard
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Then install:
|
|
118
|
+
|
|
119
|
+
```sh
|
|
120
|
+
pre-commit install
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## Allowlist
|
|
126
|
+
|
|
127
|
+
Create a text file with one entry per line. Any detected value that contains an allowlist entry is suppressed. Lines starting with `#` are comments.
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
# .zak-guard-allowlist
|
|
131
|
+
AKIAEXAMPLE # known-fake key in test fixtures
|
|
132
|
+
sk_live_test_ # Stripe test mode prefix used in integration tests
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Pass it with `--allowlist <file>` or set the `ZAK_GUARD_ALLOWLIST` environment variable.
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
## How detectors work
|
|
140
|
+
|
|
141
|
+
Each rule combines a regex pattern with a Shannon entropy minimum. The entropy gate cuts false positives on placeholder values like `YOUR_API_KEY_HERE` or `xxxxxxxxxxxx` that match the pattern shape but are obviously not real credentials.
|
|
142
|
+
|
|
143
|
+
The staged scanner (`--staged`) parses `git diff --cached` and flags only added lines. It won't block you for secrets that were already in the repo before your change.
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## Exit codes
|
|
148
|
+
|
|
149
|
+
| Code | Meaning |
|
|
150
|
+
|---|---|
|
|
151
|
+
| 0 | Clean, no findings |
|
|
152
|
+
| 1 | One or more secrets detected |
|
|
153
|
+
| 2 | Error (not inside a git repo, git not found, etc.) |
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## Development
|
|
158
|
+
|
|
159
|
+
```sh
|
|
160
|
+
git clone https://github.com/molloyzak13/zak-guard
|
|
161
|
+
cd zak-guard
|
|
162
|
+
pip install -e ".[dev]"
|
|
163
|
+
pytest -v
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## License
|
|
169
|
+
|
|
170
|
+
MIT
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# zak-guard
|
|
2
|
+
|
|
3
|
+
A pre-commit secret scanner. Catches AWS keys, GitHub PATs, Stripe tokens, private keys, and other credentials before they land in your repo.
|
|
4
|
+
|
|
5
|
+
Works standalone as a CLI tool and as a hook in the [pre-commit](https://pre-commit.com) framework.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## What it catches
|
|
10
|
+
|
|
11
|
+
| Rule | Examples |
|
|
12
|
+
|---|---|
|
|
13
|
+
| AWS access key ID | `AKIA...` (20-char, high entropy) |
|
|
14
|
+
| AWS secret access key | `AWS_SECRET_ACCESS_KEY=...` env var assignments |
|
|
15
|
+
| GitHub PATs (classic + fine-grained) | `ghp_...`, `github_pat_...` |
|
|
16
|
+
| GitHub OAuth / Actions / refresh tokens | `gho_`, `ghs_`, `ghr_` |
|
|
17
|
+
| OpenAI keys (legacy + project) | `sk-...`, `sk-proj-...` |
|
|
18
|
+
| Anthropic keys | `sk-ant-...` |
|
|
19
|
+
| DeepSeek / generic `sk-` keys | `sk-` prefix with high entropy |
|
|
20
|
+
| Stripe live keys | `sk_live_...`, `rk_live_...` |
|
|
21
|
+
| Slack tokens | `xoxb-`, `xoxp-`, `xoxa-` |
|
|
22
|
+
| Google API keys | `AIza...` |
|
|
23
|
+
| PEM private key blocks | `-----BEGIN ... PRIVATE KEY-----` |
|
|
24
|
+
| JWTs | Three base64url segments |
|
|
25
|
+
| Generic `.env` assignments | `PASSWORD=...`, `SECRET=...`, `TOKEN=...` with high entropy |
|
|
26
|
+
|
|
27
|
+
Every finding shows `file:line`, the rule name, and a redacted match (`AKIA...Y01`). The full value is never printed.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
pip install zak-guard
|
|
35
|
+
# or
|
|
36
|
+
pipx install zak-guard
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Use as a CLI
|
|
42
|
+
|
|
43
|
+
Scan staged changes (what you're about to commit):
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
zak-guard --staged
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Scan everything in the current directory:
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
zak-guard --all
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Scan a specific path:
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
zak-guard --all ./src
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Suppress known false positives with an allowlist:
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
zak-guard --staged --allowlist .zak-guard-allowlist
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Wire it as a git pre-commit hook:
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
zak-guard install
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## Use with the pre-commit framework
|
|
76
|
+
|
|
77
|
+
Add to your `.pre-commit-config.yaml`:
|
|
78
|
+
|
|
79
|
+
```yaml
|
|
80
|
+
repos:
|
|
81
|
+
- repo: https://github.com/molloyzak13/zak-guard
|
|
82
|
+
rev: v0.1.0
|
|
83
|
+
hooks:
|
|
84
|
+
- id: zak-guard
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Then install:
|
|
88
|
+
|
|
89
|
+
```sh
|
|
90
|
+
pre-commit install
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## Allowlist
|
|
96
|
+
|
|
97
|
+
Create a text file with one entry per line. Any detected value that contains an allowlist entry is suppressed. Lines starting with `#` are comments.
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
# .zak-guard-allowlist
|
|
101
|
+
AKIAEXAMPLE # known-fake key in test fixtures
|
|
102
|
+
sk_live_test_ # Stripe test mode prefix used in integration tests
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Pass it with `--allowlist <file>` or set the `ZAK_GUARD_ALLOWLIST` environment variable.
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## How detectors work
|
|
110
|
+
|
|
111
|
+
Each rule combines a regex pattern with a Shannon entropy minimum. The entropy gate cuts false positives on placeholder values like `YOUR_API_KEY_HERE` or `xxxxxxxxxxxx` that match the pattern shape but are obviously not real credentials.
|
|
112
|
+
|
|
113
|
+
The staged scanner (`--staged`) parses `git diff --cached` and flags only added lines. It won't block you for secrets that were already in the repo before your change.
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## Exit codes
|
|
118
|
+
|
|
119
|
+
| Code | Meaning |
|
|
120
|
+
|---|---|
|
|
121
|
+
| 0 | Clean, no findings |
|
|
122
|
+
| 1 | One or more secrets detected |
|
|
123
|
+
| 2 | Error (not inside a git repo, git not found, etc.) |
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## Development
|
|
128
|
+
|
|
129
|
+
```sh
|
|
130
|
+
git clone https://github.com/molloyzak13/zak-guard
|
|
131
|
+
cd zak-guard
|
|
132
|
+
pip install -e ".[dev]"
|
|
133
|
+
pytest -v
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## License
|
|
139
|
+
|
|
140
|
+
MIT
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "zak-guard"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Pre-commit secret scanner: catches AWS keys, GitHub PATs, Stripe tokens, private keys, and more before they hit your repo"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
authors = [{ name = "Zak Molloy", email = "zakguard@proton.me" }]
|
|
13
|
+
keywords = ["pre-commit", "security", "secrets", "git", "cli"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Environment :: Console",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.9",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Topic :: Security",
|
|
25
|
+
"Topic :: Software Development :: Version Control :: Git",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
zak-guard = "zak_guard.cli:main"
|
|
30
|
+
|
|
31
|
+
[project.urls]
|
|
32
|
+
Homepage = "https://github.com/molloyzak13/zak-guard"
|
|
33
|
+
Repository = "https://github.com/molloyzak13/zak-guard"
|
|
34
|
+
Issues = "https://github.com/molloyzak13/zak-guard/issues"
|
|
35
|
+
|
|
36
|
+
[project.optional-dependencies]
|
|
37
|
+
dev = ["pytest>=7", "pytest-cov>=4", "build>=1.0"]
|
|
38
|
+
|
|
39
|
+
[tool.setuptools.packages.find]
|
|
40
|
+
where = ["src"]
|
|
41
|
+
|
|
42
|
+
[tool.pytest.ini_options]
|
|
43
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""
|
|
2
|
+
zak-guard CLI entry point.
|
|
3
|
+
|
|
4
|
+
Usage:
|
|
5
|
+
zak-guard [--staged] [--allowlist FILE] # scan staged changes (default)
|
|
6
|
+
zak-guard --all [--allowlist FILE] # scan entire repo from cwd
|
|
7
|
+
zak-guard --all PATH [--allowlist FILE] # scan a specific path
|
|
8
|
+
zak-guard install # wire the git pre-commit hook
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import os
|
|
13
|
+
import stat
|
|
14
|
+
import sys
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
from .scanner import scan_staged, scan_directory, scan_file, Finding
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
EXIT_CLEAN = 0
|
|
21
|
+
EXIT_FINDINGS = 1
|
|
22
|
+
EXIT_ERROR = 2
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
PRE_COMMIT_HOOK = """\
|
|
26
|
+
#!/usr/bin/env sh
|
|
27
|
+
# zak-guard pre-commit hook (installed by: zak-guard install)
|
|
28
|
+
# Scans staged changes for secrets before each commit.
|
|
29
|
+
exec zak-guard --staged
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def install_hook() -> int:
|
|
34
|
+
"""Write the pre-commit hook into .git/hooks/pre-commit."""
|
|
35
|
+
git_dir = _find_git_dir()
|
|
36
|
+
if git_dir is None:
|
|
37
|
+
print("error: not inside a git repository", file=sys.stderr)
|
|
38
|
+
return EXIT_ERROR
|
|
39
|
+
|
|
40
|
+
hooks_dir = git_dir / "hooks"
|
|
41
|
+
hooks_dir.mkdir(exist_ok=True)
|
|
42
|
+
hook_path = hooks_dir / "pre-commit"
|
|
43
|
+
|
|
44
|
+
if hook_path.exists():
|
|
45
|
+
existing = hook_path.read_text()
|
|
46
|
+
if "zak-guard" in existing:
|
|
47
|
+
print(f"zak-guard hook already installed at {hook_path}")
|
|
48
|
+
return EXIT_CLEAN
|
|
49
|
+
# Back up existing hook
|
|
50
|
+
backup = hook_path.with_suffix(".pre-zak-guard")
|
|
51
|
+
hook_path.rename(backup)
|
|
52
|
+
print(f"existing pre-commit hook backed up to {backup}")
|
|
53
|
+
|
|
54
|
+
hook_path.write_text(PRE_COMMIT_HOOK)
|
|
55
|
+
hook_path.chmod(hook_path.stat().st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH)
|
|
56
|
+
print(f"installed zak-guard pre-commit hook at {hook_path}")
|
|
57
|
+
return EXIT_CLEAN
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _find_git_dir() -> Path | None:
|
|
61
|
+
"""Walk up the directory tree looking for a .git directory."""
|
|
62
|
+
p = Path.cwd()
|
|
63
|
+
for candidate in [p, *p.parents]:
|
|
64
|
+
git = candidate / ".git"
|
|
65
|
+
if git.is_dir():
|
|
66
|
+
return git
|
|
67
|
+
return None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _print_summary(findings: list[Finding], mode: str) -> None:
|
|
71
|
+
if findings:
|
|
72
|
+
print(f"\nzak-guard found {len(findings)} potential secret(s) ({mode}):\n")
|
|
73
|
+
for f in findings:
|
|
74
|
+
print(f.format())
|
|
75
|
+
print(
|
|
76
|
+
"\nIf these are false positives, add the matching string to an allowlist file\n"
|
|
77
|
+
"and pass --allowlist <file> (or set ZAK_GUARD_ALLOWLIST in your environment).\n"
|
|
78
|
+
"Never commit real secrets. Redact them from history with git-filter-repo."
|
|
79
|
+
)
|
|
80
|
+
else:
|
|
81
|
+
print(f"zak-guard: no secrets found ({mode})")
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def main(argv: list[str] | None = None) -> int:
|
|
85
|
+
parser = argparse.ArgumentParser(
|
|
86
|
+
prog="zak-guard",
|
|
87
|
+
description="Scan staged changes (or a path) for secrets before they hit your repo.",
|
|
88
|
+
)
|
|
89
|
+
subparsers = parser.add_subparsers(dest="subcommand")
|
|
90
|
+
|
|
91
|
+
# install subcommand
|
|
92
|
+
subparsers.add_parser("install", help="Install zak-guard as the git pre-commit hook")
|
|
93
|
+
|
|
94
|
+
# Main scan flags
|
|
95
|
+
parser.add_argument(
|
|
96
|
+
"--staged",
|
|
97
|
+
action="store_true",
|
|
98
|
+
default=False,
|
|
99
|
+
help="Scan staged changes only (default mode for the pre-commit hook)",
|
|
100
|
+
)
|
|
101
|
+
parser.add_argument(
|
|
102
|
+
"--all",
|
|
103
|
+
dest="scan_all",
|
|
104
|
+
metavar="PATH",
|
|
105
|
+
nargs="?",
|
|
106
|
+
const=".",
|
|
107
|
+
default=None,
|
|
108
|
+
help="Scan all files under PATH (default: current directory)",
|
|
109
|
+
)
|
|
110
|
+
parser.add_argument(
|
|
111
|
+
"--allowlist",
|
|
112
|
+
metavar="FILE",
|
|
113
|
+
default=os.environ.get("ZAK_GUARD_ALLOWLIST"),
|
|
114
|
+
help="Path to a file of known-safe strings to ignore (one per line)",
|
|
115
|
+
)
|
|
116
|
+
parser.add_argument(
|
|
117
|
+
"--version",
|
|
118
|
+
action="version",
|
|
119
|
+
version="zak-guard 0.1.0",
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
args = parser.parse_args(argv)
|
|
123
|
+
|
|
124
|
+
if args.subcommand == "install":
|
|
125
|
+
return install_hook()
|
|
126
|
+
|
|
127
|
+
allowlist = args.allowlist
|
|
128
|
+
|
|
129
|
+
if args.scan_all is not None:
|
|
130
|
+
# --all [PATH]
|
|
131
|
+
target = args.scan_all
|
|
132
|
+
path = Path(target)
|
|
133
|
+
if path.is_file():
|
|
134
|
+
findings = scan_file(target, allowlist)
|
|
135
|
+
mode = f"file scan: {target}"
|
|
136
|
+
elif path.is_dir():
|
|
137
|
+
findings = scan_directory(target, allowlist)
|
|
138
|
+
mode = f"directory scan: {target}"
|
|
139
|
+
else:
|
|
140
|
+
print(f"error: path does not exist: {target}", file=sys.stderr)
|
|
141
|
+
return EXIT_ERROR
|
|
142
|
+
_print_summary(findings, mode)
|
|
143
|
+
return EXIT_FINDINGS if findings else EXIT_CLEAN
|
|
144
|
+
|
|
145
|
+
# Default: staged scan (also what --staged explicitly requests)
|
|
146
|
+
try:
|
|
147
|
+
findings = scan_staged(allowlist)
|
|
148
|
+
except RuntimeError as e:
|
|
149
|
+
print(f"error: {e}", file=sys.stderr)
|
|
150
|
+
return EXIT_ERROR
|
|
151
|
+
|
|
152
|
+
_print_summary(findings, "staged changes")
|
|
153
|
+
return EXIT_FINDINGS if findings else EXIT_CLEAN
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
if __name__ == "__main__":
|
|
157
|
+
sys.exit(main())
|