betterleaks 2.0.0rc16__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.
- betterleaks-2.0.0rc16/LICENSE +21 -0
- betterleaks-2.0.0rc16/MANIFEST.in +9 -0
- betterleaks-2.0.0rc16/PKG-INFO +239 -0
- betterleaks-2.0.0rc16/README.md +176 -0
- betterleaks-2.0.0rc16/docs/benchmarks.md +15 -0
- betterleaks-2.0.0rc16/docs/publishing.md +29 -0
- betterleaks-2.0.0rc16/pyproject.toml +82 -0
- betterleaks-2.0.0rc16/scripts/bench.py +439 -0
- betterleaks-2.0.0rc16/scripts/check-published.py +49 -0
- betterleaks-2.0.0rc16/scripts/stamp.py +34 -0
- betterleaks-2.0.0rc16/scripts/verify-release.py +111 -0
- betterleaks-2.0.0rc16/setup.cfg +4 -0
- betterleaks-2.0.0rc16/src/betterleaks/__init__.py +40 -0
- betterleaks-2.0.0rc16/src/betterleaks/_version.py +1 -0
- betterleaks-2.0.0rc16/src/betterleaks/py.typed +0 -0
- betterleaks-2.0.0rc16/src/betterleaks.egg-info/PKG-INFO +239 -0
- betterleaks-2.0.0rc16/src/betterleaks.egg-info/SOURCES.txt +20 -0
- betterleaks-2.0.0rc16/src/betterleaks.egg-info/dependency_links.txt +1 -0
- betterleaks-2.0.0rc16/src/betterleaks.egg-info/requires.txt +10 -0
- betterleaks-2.0.0rc16/src/betterleaks.egg-info/top_level.txt +1 -0
- betterleaks-2.0.0rc16/tests/test_betterleaks.py +84 -0
- betterleaks-2.0.0rc16/tests/test_version.py +38 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zachary Rice
|
|
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.
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: betterleaks
|
|
3
|
+
Version: 2.0.0rc16
|
|
4
|
+
Summary: Python bindings and CLI for Betterleaks, the high-performance secret and credential detection engine.
|
|
5
|
+
Author: Betterleaks Contributors
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Zachary Rice
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/betterleaks/betterleaks
|
|
29
|
+
Project-URL: Repository, https://github.com/betterleaks/betterleaks
|
|
30
|
+
Project-URL: Issues, https://github.com/betterleaks/betterleaks/issues
|
|
31
|
+
Project-URL: Changelog, https://github.com/betterleaks/betterleaks/releases
|
|
32
|
+
Classifier: Development Status :: 4 - Beta
|
|
33
|
+
Classifier: Intended Audience :: Developers
|
|
34
|
+
Classifier: Intended Audience :: Information Technology
|
|
35
|
+
Classifier: Intended Audience :: System Administrators
|
|
36
|
+
Classifier: Topic :: Security
|
|
37
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
38
|
+
Classifier: Programming Language :: Python :: 3
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
42
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
43
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
44
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
45
|
+
Classifier: Programming Language :: Go
|
|
46
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
47
|
+
Classifier: Typing :: Typed
|
|
48
|
+
Classifier: Operating System :: MacOS
|
|
49
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
50
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
51
|
+
Classifier: Environment :: Console
|
|
52
|
+
Requires-Python: >=3.8
|
|
53
|
+
Description-Content-Type: text/markdown
|
|
54
|
+
License-File: LICENSE
|
|
55
|
+
Requires-Dist: betterleaks-cli==2.0.0rc16
|
|
56
|
+
Requires-Dist: betterleaks-sdk==2.0.0rc16; sys_platform != "win32"
|
|
57
|
+
Provides-Extra: dev
|
|
58
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
59
|
+
Requires-Dist: build; extra == "dev"
|
|
60
|
+
Requires-Dist: twine; extra == "dev"
|
|
61
|
+
Requires-Dist: wheel; extra == "dev"
|
|
62
|
+
Dynamic: license-file
|
|
63
|
+
|
|
64
|
+
# betterleaks
|
|
65
|
+
|
|
66
|
+
Python bindings and CLI for [Betterleaks](https://github.com/betterleaks/betterleaks), the fast secret and credential detection engine written in Go.
|
|
67
|
+
|
|
68
|
+
The three packages share a release version.
|
|
69
|
+
|
|
70
|
+
- **`betterleaks-cli`** — standalone Go CLI executable (`betterleaks git .`, `betterleaks filesystem .`, `betterleaks stdin`) across macOS, Linux, and Windows.
|
|
71
|
+
- **`betterleaks-sdk`** *(macOS & glibc Linux)* — fast in-process C ABI library (`bindings/c`).
|
|
72
|
+
- **`betterleaks`** — unified `import betterleaks` API (re-exporting `betterleaks_sdk` on supported macOS and Linux targets).
|
|
73
|
+
|
|
74
|
+
You can install either component individually.
|
|
75
|
+
|
|
76
|
+
- CLI only: `pip install betterleaks-cli` ([`cli/README.md`](https://github.com/betterleaks/betterleaks/blob/main/bindings/python/cli/README.md))
|
|
77
|
+
- SDK only (macOS/Linux): `pip install betterleaks-sdk` ([`sdk/README.md`](https://github.com/betterleaks/betterleaks/blob/main/bindings/python/sdk/README.md))
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Installation
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
pip install betterleaks
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Use `pip install --pre betterleaks` to include release candidates when pip chooses a version.
|
|
88
|
+
|
|
89
|
+
### Platform Support
|
|
90
|
+
|
|
91
|
+
| Platform | Architecture | CLI (`betterleaks`) | Python API (`scan_string` / `scan_file`) |
|
|
92
|
+
| --- | --- | --- | --- |
|
|
93
|
+
| macOS | ARM64 & x86-64 | Release CLI with its minimum macOS version encoded in the wheel | Native C ABI on macOS 12+ (`betterleaks-sdk`) |
|
|
94
|
+
| Linux (glibc) | x86-64 & ARM64 | Via `betterleaks-cli` | Native C ABI (`betterleaks-sdk`) |
|
|
95
|
+
| Windows | x86-64 & ARM64 | Via `betterleaks-cli` | CLI only |
|
|
96
|
+
|
|
97
|
+
### Building from Source
|
|
98
|
+
|
|
99
|
+
Installing all workspace packages from a repository checkout requires Python `>= 3.8`, Go `>= 1.26`, and a C compiler (for `betterleaks-sdk` on macOS/Linux):
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
git clone https://github.com/betterleaks/betterleaks.git
|
|
103
|
+
cd betterleaks
|
|
104
|
+
pip install ./bindings/python/cli ./bindings/python/sdk ./bindings/python
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
For a Windows checkout install, use `pip install ./bindings/python/cli ./bindings/python`.
|
|
110
|
+
|
|
111
|
+
## Python Quickstart
|
|
112
|
+
|
|
113
|
+
The native SDK runs on the macOS and Linux targets listed above. Its scan methods return Python dataclasses (`Finding`, `Match`, `Location`) with full type annotations across `scan_string`, `scan_file`, and `Scanner`.
|
|
114
|
+
|
|
115
|
+
### Scanning Strings & Files
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
from typing import List
|
|
119
|
+
import betterleaks
|
|
120
|
+
from betterleaks import BetterleaksError, Finding
|
|
121
|
+
|
|
122
|
+
content = """
|
|
123
|
+
export AWS_ACCESS_KEY_ID="AKIAIOSFODNN7EXAMPLE"
|
|
124
|
+
export AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
|
|
125
|
+
"""
|
|
126
|
+
|
|
127
|
+
try:
|
|
128
|
+
findings: List[Finding] = betterleaks.scan_string(content)
|
|
129
|
+
for finding in findings:
|
|
130
|
+
print(f"Rule: {finding.rule_id}")
|
|
131
|
+
print(f" Confidence: {finding.confidence}")
|
|
132
|
+
print(f" Line: {finding.location.start_line}, Col: {finding.location.start_column}")
|
|
133
|
+
print(f" Fingerprint: {finding.match.fingerprint}")
|
|
134
|
+
|
|
135
|
+
if finding.component_sets:
|
|
136
|
+
print(f" Correlated components: {len(finding.component_sets)}")
|
|
137
|
+
|
|
138
|
+
file_findings: List[Finding] = betterleaks.scan_file("path/to/settings.env")
|
|
139
|
+
except BetterleaksError as err:
|
|
140
|
+
print(f"Scan failed: {err}")
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Reusable Scanners & Custom Configuration
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
from typing import List
|
|
147
|
+
import betterleaks
|
|
148
|
+
from betterleaks import Finding, Scanner
|
|
149
|
+
|
|
150
|
+
toml_rules = """
|
|
151
|
+
[extend]
|
|
152
|
+
useDefault = true
|
|
153
|
+
|
|
154
|
+
[[rules]]
|
|
155
|
+
id = "internal-token"
|
|
156
|
+
description = "Internal token"
|
|
157
|
+
regex = '''INTERNAL_[A-Z0-9]{16}'''
|
|
158
|
+
keywords = ["INTERNAL_"]
|
|
159
|
+
"""
|
|
160
|
+
|
|
161
|
+
scanner: Scanner
|
|
162
|
+
with betterleaks.create_scanner(config_toml=toml_rules) as scanner:
|
|
163
|
+
findings: List[Finding] = scanner.scan_string(
|
|
164
|
+
"token = INTERNAL_1234567890ABCDEF",
|
|
165
|
+
attributes={"path": "config.env"},
|
|
166
|
+
)
|
|
167
|
+
file_findings: List[Finding] = scanner.scan_file("config.py")
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## Public API & Typed Data Models
|
|
173
|
+
|
|
174
|
+
### Functions & Classes
|
|
175
|
+
|
|
176
|
+
| Symbol | Signature | Description |
|
|
177
|
+
| --- | --- | --- |
|
|
178
|
+
| `scan_string` | `(content: str, config_path: Optional[str] = None) -> List[Finding]` | Scan in-memory text with the selected configuration. |
|
|
179
|
+
| `scan_file` | `(file_path: str, config_path: Optional[str] = None) -> List[Finding]` | Scan a regular file with the selected configuration. |
|
|
180
|
+
| `create_scanner` | `(config_path: Optional[str] = None, config_toml: Optional[str] = None, workers: int = 0, max_decode_depth: int = 0, max_input_bytes: Optional[int] = None, max_findings: Optional[int] = None) -> Scanner` | Create a reusable `Scanner` context manager with custom rules and limits. |
|
|
181
|
+
| `Scanner` | `.scan_string(content: str, attributes: Optional[Dict[str, str]] = None) -> List[Finding]`<br>`.scan_file(file_path: str) -> List[Finding]`<br>`.close() -> None` | Handle-backed scanner supporting `with` statements. |
|
|
182
|
+
| `Scanner.scan_string_report`, `Scanner.scan_file_report` | Same arguments as the respective scan method | Return the engine report including findings, summary, config hash and engine version. |
|
|
183
|
+
| `BetterleaksError` | `class BetterleaksError(Exception)` | Raised on invalid config, missing/non-regular files, limit errors, or runtime failures. |
|
|
184
|
+
|
|
185
|
+
### `Finding` (dataclass, `collections.abc.Mapping`)
|
|
186
|
+
|
|
187
|
+
| Attribute | Type | Description |
|
|
188
|
+
| --- | --- | --- |
|
|
189
|
+
| `rule_id` | `str` | Identifier of the triggered rule (e.g. `github-pat`, `aws-access-token`). |
|
|
190
|
+
| `description` | `str` | Description of the credential risk. |
|
|
191
|
+
| `confidence` | `str` | Detection confidence rating (`low`, `medium`, `high`). |
|
|
192
|
+
| `match` | `Match` | Match details (`full`, `value`, `fingerprint`, `entropy`, `context`, `captures`). |
|
|
193
|
+
| `location` | `Location` | Source coordinates (`path`, `start_line`, `end_line`, `start_column`, `end_column`). |
|
|
194
|
+
| `tags` | `List[str]` | Rule tags. |
|
|
195
|
+
| `raw` | `Dict[str, Any]` | Raw decoded JSON object for any additional engine fields. |
|
|
196
|
+
| `secret` *(property)* | `str` | Convenience alias for `match.value`. |
|
|
197
|
+
| `file` *(property)* | `Optional[str]` | Convenience alias for `location.path`. |
|
|
198
|
+
| `component_sets` *(property)* | `List[Dict[str, Any]]` | Correlated multi-part component findings. |
|
|
199
|
+
| `encodings` *(property)* | `List[str]` | Encodings detected during decode passes. |
|
|
200
|
+
|
|
201
|
+
`Finding` implements `collections.abc.Mapping` and supports both typed attribute access (`finding.rule_id`) and dictionary subscripting (`finding["rule_id"]`, `finding.to_dict()`).
|
|
202
|
+
|
|
203
|
+
### `Match` (dataclass)
|
|
204
|
+
|
|
205
|
+
| Attribute | Type | Default | Description |
|
|
206
|
+
| --- | --- | --- | --- |
|
|
207
|
+
| `full` | `str` | `""` | Full matched text including surrounding pattern context. |
|
|
208
|
+
| `value` | `str` | `""` | Extracted secret/credential value. |
|
|
209
|
+
| `fingerprint` | `Optional[str]` | `None` | Deterministic finding fingerprint. |
|
|
210
|
+
| `line` | `Optional[str]` | `None` | Matched line text, if included. |
|
|
211
|
+
| `entropy` | `Optional[float]` | `None` | Entropy of the matched value when reported. |
|
|
212
|
+
| `context` | `Optional[str]` | `None` | Surrounding source text when requested by the rule. |
|
|
213
|
+
| `captures` | `Dict[str, str]` | `{}` | Named regex capture groups. |
|
|
214
|
+
|
|
215
|
+
### `Location` (dataclass)
|
|
216
|
+
|
|
217
|
+
| Attribute | Type | Default | Description |
|
|
218
|
+
| --- | --- | --- | --- |
|
|
219
|
+
| `path` | `Optional[str]` | `None` | Target file path (populated by `scan_file`). |
|
|
220
|
+
| `start_line` | `int` | `0` | 1-based start line number. |
|
|
221
|
+
| `end_line` | `int` | `0` | 1-based end line number. |
|
|
222
|
+
| `start_column` | `int` | `0` | 1-based start byte column. |
|
|
223
|
+
| `end_column` | `int` | `0` | 1-based end byte column. |
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## Benchmarks & Tests
|
|
228
|
+
|
|
229
|
+
See [docs/benchmarks.md](https://github.com/betterleaks/betterleaks/blob/main/bindings/python/docs/benchmarks.md) and [scripts/bench.py](https://github.com/betterleaks/betterleaks/blob/main/bindings/python/scripts/bench.py).
|
|
230
|
+
|
|
231
|
+
```bash
|
|
232
|
+
pytest -v bindings/python/tests
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
For release and account setup, see the [publishing guide](https://github.com/betterleaks/betterleaks/blob/main/bindings/python/docs/publishing.md).
|
|
236
|
+
|
|
237
|
+
## License
|
|
238
|
+
|
|
239
|
+
MIT License. See [LICENSE](https://github.com/betterleaks/betterleaks/blob/main/LICENSE) for details.
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# betterleaks
|
|
2
|
+
|
|
3
|
+
Python bindings and CLI for [Betterleaks](https://github.com/betterleaks/betterleaks), the fast secret and credential detection engine written in Go.
|
|
4
|
+
|
|
5
|
+
The three packages share a release version.
|
|
6
|
+
|
|
7
|
+
- **`betterleaks-cli`** — standalone Go CLI executable (`betterleaks git .`, `betterleaks filesystem .`, `betterleaks stdin`) across macOS, Linux, and Windows.
|
|
8
|
+
- **`betterleaks-sdk`** *(macOS & glibc Linux)* — fast in-process C ABI library (`bindings/c`).
|
|
9
|
+
- **`betterleaks`** — unified `import betterleaks` API (re-exporting `betterleaks_sdk` on supported macOS and Linux targets).
|
|
10
|
+
|
|
11
|
+
You can install either component individually.
|
|
12
|
+
|
|
13
|
+
- CLI only: `pip install betterleaks-cli` ([`cli/README.md`](https://github.com/betterleaks/betterleaks/blob/main/bindings/python/cli/README.md))
|
|
14
|
+
- SDK only (macOS/Linux): `pip install betterleaks-sdk` ([`sdk/README.md`](https://github.com/betterleaks/betterleaks/blob/main/bindings/python/sdk/README.md))
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install betterleaks
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Use `pip install --pre betterleaks` to include release candidates when pip chooses a version.
|
|
25
|
+
|
|
26
|
+
### Platform Support
|
|
27
|
+
|
|
28
|
+
| Platform | Architecture | CLI (`betterleaks`) | Python API (`scan_string` / `scan_file`) |
|
|
29
|
+
| --- | --- | --- | --- |
|
|
30
|
+
| macOS | ARM64 & x86-64 | Release CLI with its minimum macOS version encoded in the wheel | Native C ABI on macOS 12+ (`betterleaks-sdk`) |
|
|
31
|
+
| Linux (glibc) | x86-64 & ARM64 | Via `betterleaks-cli` | Native C ABI (`betterleaks-sdk`) |
|
|
32
|
+
| Windows | x86-64 & ARM64 | Via `betterleaks-cli` | CLI only |
|
|
33
|
+
|
|
34
|
+
### Building from Source
|
|
35
|
+
|
|
36
|
+
Installing all workspace packages from a repository checkout requires Python `>= 3.8`, Go `>= 1.26`, and a C compiler (for `betterleaks-sdk` on macOS/Linux):
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
git clone https://github.com/betterleaks/betterleaks.git
|
|
40
|
+
cd betterleaks
|
|
41
|
+
pip install ./bindings/python/cli ./bindings/python/sdk ./bindings/python
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
For a Windows checkout install, use `pip install ./bindings/python/cli ./bindings/python`.
|
|
47
|
+
|
|
48
|
+
## Python Quickstart
|
|
49
|
+
|
|
50
|
+
The native SDK runs on the macOS and Linux targets listed above. Its scan methods return Python dataclasses (`Finding`, `Match`, `Location`) with full type annotations across `scan_string`, `scan_file`, and `Scanner`.
|
|
51
|
+
|
|
52
|
+
### Scanning Strings & Files
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from typing import List
|
|
56
|
+
import betterleaks
|
|
57
|
+
from betterleaks import BetterleaksError, Finding
|
|
58
|
+
|
|
59
|
+
content = """
|
|
60
|
+
export AWS_ACCESS_KEY_ID="AKIAIOSFODNN7EXAMPLE"
|
|
61
|
+
export AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
findings: List[Finding] = betterleaks.scan_string(content)
|
|
66
|
+
for finding in findings:
|
|
67
|
+
print(f"Rule: {finding.rule_id}")
|
|
68
|
+
print(f" Confidence: {finding.confidence}")
|
|
69
|
+
print(f" Line: {finding.location.start_line}, Col: {finding.location.start_column}")
|
|
70
|
+
print(f" Fingerprint: {finding.match.fingerprint}")
|
|
71
|
+
|
|
72
|
+
if finding.component_sets:
|
|
73
|
+
print(f" Correlated components: {len(finding.component_sets)}")
|
|
74
|
+
|
|
75
|
+
file_findings: List[Finding] = betterleaks.scan_file("path/to/settings.env")
|
|
76
|
+
except BetterleaksError as err:
|
|
77
|
+
print(f"Scan failed: {err}")
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Reusable Scanners & Custom Configuration
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from typing import List
|
|
84
|
+
import betterleaks
|
|
85
|
+
from betterleaks import Finding, Scanner
|
|
86
|
+
|
|
87
|
+
toml_rules = """
|
|
88
|
+
[extend]
|
|
89
|
+
useDefault = true
|
|
90
|
+
|
|
91
|
+
[[rules]]
|
|
92
|
+
id = "internal-token"
|
|
93
|
+
description = "Internal token"
|
|
94
|
+
regex = '''INTERNAL_[A-Z0-9]{16}'''
|
|
95
|
+
keywords = ["INTERNAL_"]
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
scanner: Scanner
|
|
99
|
+
with betterleaks.create_scanner(config_toml=toml_rules) as scanner:
|
|
100
|
+
findings: List[Finding] = scanner.scan_string(
|
|
101
|
+
"token = INTERNAL_1234567890ABCDEF",
|
|
102
|
+
attributes={"path": "config.env"},
|
|
103
|
+
)
|
|
104
|
+
file_findings: List[Finding] = scanner.scan_file("config.py")
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Public API & Typed Data Models
|
|
110
|
+
|
|
111
|
+
### Functions & Classes
|
|
112
|
+
|
|
113
|
+
| Symbol | Signature | Description |
|
|
114
|
+
| --- | --- | --- |
|
|
115
|
+
| `scan_string` | `(content: str, config_path: Optional[str] = None) -> List[Finding]` | Scan in-memory text with the selected configuration. |
|
|
116
|
+
| `scan_file` | `(file_path: str, config_path: Optional[str] = None) -> List[Finding]` | Scan a regular file with the selected configuration. |
|
|
117
|
+
| `create_scanner` | `(config_path: Optional[str] = None, config_toml: Optional[str] = None, workers: int = 0, max_decode_depth: int = 0, max_input_bytes: Optional[int] = None, max_findings: Optional[int] = None) -> Scanner` | Create a reusable `Scanner` context manager with custom rules and limits. |
|
|
118
|
+
| `Scanner` | `.scan_string(content: str, attributes: Optional[Dict[str, str]] = None) -> List[Finding]`<br>`.scan_file(file_path: str) -> List[Finding]`<br>`.close() -> None` | Handle-backed scanner supporting `with` statements. |
|
|
119
|
+
| `Scanner.scan_string_report`, `Scanner.scan_file_report` | Same arguments as the respective scan method | Return the engine report including findings, summary, config hash and engine version. |
|
|
120
|
+
| `BetterleaksError` | `class BetterleaksError(Exception)` | Raised on invalid config, missing/non-regular files, limit errors, or runtime failures. |
|
|
121
|
+
|
|
122
|
+
### `Finding` (dataclass, `collections.abc.Mapping`)
|
|
123
|
+
|
|
124
|
+
| Attribute | Type | Description |
|
|
125
|
+
| --- | --- | --- |
|
|
126
|
+
| `rule_id` | `str` | Identifier of the triggered rule (e.g. `github-pat`, `aws-access-token`). |
|
|
127
|
+
| `description` | `str` | Description of the credential risk. |
|
|
128
|
+
| `confidence` | `str` | Detection confidence rating (`low`, `medium`, `high`). |
|
|
129
|
+
| `match` | `Match` | Match details (`full`, `value`, `fingerprint`, `entropy`, `context`, `captures`). |
|
|
130
|
+
| `location` | `Location` | Source coordinates (`path`, `start_line`, `end_line`, `start_column`, `end_column`). |
|
|
131
|
+
| `tags` | `List[str]` | Rule tags. |
|
|
132
|
+
| `raw` | `Dict[str, Any]` | Raw decoded JSON object for any additional engine fields. |
|
|
133
|
+
| `secret` *(property)* | `str` | Convenience alias for `match.value`. |
|
|
134
|
+
| `file` *(property)* | `Optional[str]` | Convenience alias for `location.path`. |
|
|
135
|
+
| `component_sets` *(property)* | `List[Dict[str, Any]]` | Correlated multi-part component findings. |
|
|
136
|
+
| `encodings` *(property)* | `List[str]` | Encodings detected during decode passes. |
|
|
137
|
+
|
|
138
|
+
`Finding` implements `collections.abc.Mapping` and supports both typed attribute access (`finding.rule_id`) and dictionary subscripting (`finding["rule_id"]`, `finding.to_dict()`).
|
|
139
|
+
|
|
140
|
+
### `Match` (dataclass)
|
|
141
|
+
|
|
142
|
+
| Attribute | Type | Default | Description |
|
|
143
|
+
| --- | --- | --- | --- |
|
|
144
|
+
| `full` | `str` | `""` | Full matched text including surrounding pattern context. |
|
|
145
|
+
| `value` | `str` | `""` | Extracted secret/credential value. |
|
|
146
|
+
| `fingerprint` | `Optional[str]` | `None` | Deterministic finding fingerprint. |
|
|
147
|
+
| `line` | `Optional[str]` | `None` | Matched line text, if included. |
|
|
148
|
+
| `entropy` | `Optional[float]` | `None` | Entropy of the matched value when reported. |
|
|
149
|
+
| `context` | `Optional[str]` | `None` | Surrounding source text when requested by the rule. |
|
|
150
|
+
| `captures` | `Dict[str, str]` | `{}` | Named regex capture groups. |
|
|
151
|
+
|
|
152
|
+
### `Location` (dataclass)
|
|
153
|
+
|
|
154
|
+
| Attribute | Type | Default | Description |
|
|
155
|
+
| --- | --- | --- | --- |
|
|
156
|
+
| `path` | `Optional[str]` | `None` | Target file path (populated by `scan_file`). |
|
|
157
|
+
| `start_line` | `int` | `0` | 1-based start line number. |
|
|
158
|
+
| `end_line` | `int` | `0` | 1-based end line number. |
|
|
159
|
+
| `start_column` | `int` | `0` | 1-based start byte column. |
|
|
160
|
+
| `end_column` | `int` | `0` | 1-based end byte column. |
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Benchmarks & Tests
|
|
165
|
+
|
|
166
|
+
See [docs/benchmarks.md](https://github.com/betterleaks/betterleaks/blob/main/bindings/python/docs/benchmarks.md) and [scripts/bench.py](https://github.com/betterleaks/betterleaks/blob/main/bindings/python/scripts/bench.py).
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
pytest -v bindings/python/tests
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
For release and account setup, see the [publishing guide](https://github.com/betterleaks/betterleaks/blob/main/bindings/python/docs/publishing.md).
|
|
173
|
+
|
|
174
|
+
## License
|
|
175
|
+
|
|
176
|
+
MIT License. See [LICENSE](https://github.com/betterleaks/betterleaks/blob/main/LICENSE) for details.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Benchmarks: Library vs. CLI Subprocess
|
|
2
|
+
|
|
3
|
+
The benchmark measures the installed SDK and CLI on the same workload. Warm runs reuse the native scanner. Cold runs include scanner initialization. The results depend on input size, rules and host.
|
|
4
|
+
|
|
5
|
+
| Mode | How it works |
|
|
6
|
+
|------|---|
|
|
7
|
+
| **Python library** (`betterleaks.scan_string` / `scan_file`) | On macOS and Linux, `betterleaks` re-exports `betterleaks_sdk`, loading the Go shared library (`libbetterleaks_ffi.so/.dylib`) **once** into the Python process via `ctypes`. |
|
|
8
|
+
| **CLI subprocess** (`subprocess.run(["betterleaks", ...])`) | Each call spawns `betterleaks-cli` as a brand-new OS process. |
|
|
9
|
+
|
|
10
|
+
The benchmark script lives in [`scripts/bench.py`](../scripts/bench.py).
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install ./bindings/python/cli ./bindings/python/sdk ./bindings/python
|
|
14
|
+
python bindings/python/scripts/bench.py
|
|
15
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Publishing the Python packages
|
|
2
|
+
|
|
3
|
+
Betterleaks publishes three projects from one v2 release tag. The CLI wheel owns the executable. The SDK wheel owns the Python bindings and native library. The umbrella wheel depends on that release’s exact CLI and SDK versions. On Windows the umbrella installs the CLI.
|
|
4
|
+
|
|
5
|
+
## Registry setup
|
|
6
|
+
|
|
7
|
+
In the Betterleaks organization on PyPI, create or select the projects `betterleaks-cli`, `betterleaks-sdk`, and `betterleaks`. On each project, open **Manage → Publishing** and register GitHub Actions with the following values.
|
|
8
|
+
|
|
9
|
+
| Field | Production | TestPyPI |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| Repository owner | `betterleaks` | `betterleaks` |
|
|
12
|
+
| Repository name | `betterleaks` | `betterleaks` |
|
|
13
|
+
| Workflow filename | `python-release.yml` | `python-release.yml` |
|
|
14
|
+
| Environment | `pypi` | `testpypi` |
|
|
15
|
+
|
|
16
|
+
Use a TestPyPI account to configure each of the three test projects. For a new project, an account-level pending publisher also works. Both registries authenticate the workflow via OIDC. The GitHub environments need no registry secrets.
|
|
17
|
+
|
|
18
|
+
Under repository **Settings → Environments**, allow only deployments from the `main` branch for both environments. Add a required reviewer to production. Protect changes to workflows, packaging code, and release tags through repository rules. The publishing workflow runs on `main` and verifies the release tag separately.
|
|
19
|
+
|
|
20
|
+
[PyPI organization projects](https://docs.pypi.org/organization-accounts/actions/project-actions/) · [Trusted publishers](https://docs.pypi.org/trusted-publishers/adding-a-publisher/) · [TestPyPI](https://packaging.python.org/en/latest/guides/using-testpypi/)
|
|
21
|
+
|
|
22
|
+
## Cutting an RC or stable release
|
|
23
|
+
|
|
24
|
+
1. Merge the packaging changes and confirm **Python distributions** passes on the intended commit. Push the normal Betterleaks release tag. For example, `v2.0.0-rc.N` maps to Python version `2.0.0rcN` (replace N with the release number).
|
|
25
|
+
2. Once **Release** succeeds, **Publish Python distributions** validates the upstream repository, run, tag, commit and ancestry on main. It verifies the signed CLI checksums and signer identity from that Release run, packages the same GoReleaser executables used by npm, and builds the SDK from the tagged commit. CI tests the native wheels on each supported platform, installs the umbrella from the wheelhouse, and checks the complete set of files.
|
|
26
|
+
3. The workflow uploads the CLI, SDK and umbrella to TestPyPI in that order and smoke-tests the exact RC from the index. Review the run, the expected version and the smoke result. Approve the production deployment in GitHub Actions. It uploads the same artifact with PyPI attestations.
|
|
27
|
+
4. In a fresh virtual environment, install the exact version, run `betterleaks --version`, and on a native SDK platform run `python -c "import betterleaks; print(betterleaks.scan_string('ordinary text'))"`. The CLI and package version use the Go and Python spellings of the same release.
|
|
28
|
+
|
|
29
|
+
For a partial upload use **Re-run failed jobs** on the original publishing run. Each upload compares bytes already on the index and resumes with the remaining files. A digest mismatch requires investigation. PyPI release files are immutable. If a new build is needed after a partial release, cut a fresh version. Manual dispatch on main takes the successful upstream Release run ID.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = [
|
|
3
|
+
"setuptools==75.3.2; python_version < '3.9'",
|
|
4
|
+
"setuptools==80.9.0; python_version >= '3.9'",
|
|
5
|
+
"wheel==0.45.1",
|
|
6
|
+
]
|
|
7
|
+
build-backend = "setuptools.build_meta"
|
|
8
|
+
|
|
9
|
+
[project]
|
|
10
|
+
name = "betterleaks"
|
|
11
|
+
dynamic = ["version"]
|
|
12
|
+
description = "Python bindings and CLI for Betterleaks, the high-performance secret and credential detection engine."
|
|
13
|
+
readme = "README.md"
|
|
14
|
+
requires-python = ">=3.8"
|
|
15
|
+
license = { file = "LICENSE" }
|
|
16
|
+
authors = [
|
|
17
|
+
{ name = "Betterleaks Contributors" }
|
|
18
|
+
]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Development Status :: 4 - Beta",
|
|
21
|
+
"Intended Audience :: Developers",
|
|
22
|
+
"Intended Audience :: Information Technology",
|
|
23
|
+
"Intended Audience :: System Administrators",
|
|
24
|
+
"Topic :: Security",
|
|
25
|
+
"Topic :: Software Development :: Quality Assurance",
|
|
26
|
+
"Programming Language :: Python :: 3",
|
|
27
|
+
"Programming Language :: Python :: 3.8",
|
|
28
|
+
"Programming Language :: Python :: 3.9",
|
|
29
|
+
"Programming Language :: Python :: 3.10",
|
|
30
|
+
"Programming Language :: Python :: 3.11",
|
|
31
|
+
"Programming Language :: Python :: 3.12",
|
|
32
|
+
"Programming Language :: Python :: 3.13",
|
|
33
|
+
"Programming Language :: Go",
|
|
34
|
+
"License :: OSI Approved :: MIT License",
|
|
35
|
+
"Typing :: Typed",
|
|
36
|
+
"Operating System :: MacOS",
|
|
37
|
+
"Operating System :: POSIX :: Linux",
|
|
38
|
+
"Operating System :: Microsoft :: Windows",
|
|
39
|
+
"Environment :: Console",
|
|
40
|
+
]
|
|
41
|
+
dependencies = [
|
|
42
|
+
"betterleaks-cli==2.0.0rc16",
|
|
43
|
+
"betterleaks-sdk==2.0.0rc16; sys_platform != 'win32'",
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
[project.optional-dependencies]
|
|
47
|
+
dev = [
|
|
48
|
+
"pytest>=7.0",
|
|
49
|
+
"build",
|
|
50
|
+
"twine",
|
|
51
|
+
"wheel",
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
[project.urls]
|
|
55
|
+
Homepage = "https://github.com/betterleaks/betterleaks"
|
|
56
|
+
Repository = "https://github.com/betterleaks/betterleaks"
|
|
57
|
+
Issues = "https://github.com/betterleaks/betterleaks/issues"
|
|
58
|
+
Changelog = "https://github.com/betterleaks/betterleaks/releases"
|
|
59
|
+
|
|
60
|
+
[tool.setuptools.dynamic]
|
|
61
|
+
version = {attr = "betterleaks._version.__version__"}
|
|
62
|
+
|
|
63
|
+
[tool.setuptools.packages.find]
|
|
64
|
+
where = ["src"]
|
|
65
|
+
include = ["betterleaks", "betterleaks.*"]
|
|
66
|
+
|
|
67
|
+
[tool.setuptools.package-data]
|
|
68
|
+
betterleaks = ["py.typed"]
|
|
69
|
+
|
|
70
|
+
[tool.pytest.ini_options]
|
|
71
|
+
testpaths = ["tests"]
|
|
72
|
+
pythonpath = ["src"]
|
|
73
|
+
|
|
74
|
+
[tool.ruff]
|
|
75
|
+
target-version = "py38"
|
|
76
|
+
src = ["src"]
|
|
77
|
+
|
|
78
|
+
[tool.ruff.lint]
|
|
79
|
+
ignore = ["UP006", "UP045", "FA100", "PYI034"]
|
|
80
|
+
|
|
81
|
+
[tool.ruff.lint.isort]
|
|
82
|
+
known-first-party = ["betterleaks", "betterleaks_cli", "betterleaks_sdk"]
|