credactor 2.0.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.
- credactor-2.0.0/LICENSE +201 -0
- credactor-2.0.0/PKG-INFO +125 -0
- credactor-2.0.0/README.md +98 -0
- credactor-2.0.0/credactor/__init__.py +3 -0
- credactor-2.0.0/credactor/__main__.py +6 -0
- credactor-2.0.0/credactor/cli.py +228 -0
- credactor-2.0.0/credactor/config.py +136 -0
- credactor-2.0.0/credactor/gitignore.py +73 -0
- credactor-2.0.0/credactor/patterns.py +204 -0
- credactor-2.0.0/credactor/redactor.py +319 -0
- credactor-2.0.0/credactor/report.py +219 -0
- credactor-2.0.0/credactor/scanner.py +406 -0
- credactor-2.0.0/credactor/suppressions.py +90 -0
- credactor-2.0.0/credactor/utils.py +67 -0
- credactor-2.0.0/credactor/walker.py +301 -0
- credactor-2.0.0/credactor.egg-info/PKG-INFO +125 -0
- credactor-2.0.0/credactor.egg-info/SOURCES.txt +27 -0
- credactor-2.0.0/credactor.egg-info/dependency_links.txt +1 -0
- credactor-2.0.0/credactor.egg-info/entry_points.txt +2 -0
- credactor-2.0.0/credactor.egg-info/requires.txt +7 -0
- credactor-2.0.0/credactor.egg-info/top_level.txt +1 -0
- credactor-2.0.0/pyproject.toml +53 -0
- credactor-2.0.0/setup.cfg +4 -0
- credactor-2.0.0/tests/test_patterns.py +200 -0
- credactor-2.0.0/tests/test_redactor.py +122 -0
- credactor-2.0.0/tests/test_report.py +90 -0
- credactor-2.0.0/tests/test_safe_values.py +100 -0
- credactor-2.0.0/tests/test_scanner.py +243 -0
- credactor-2.0.0/tests/test_suppressions.py +74 -0
credactor-2.0.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
credactor-2.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: credactor
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Scan and redact hardcoded credentials in source code
|
|
5
|
+
Author: Rx
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Repository, https://github.com/rxb06/Credactor
|
|
8
|
+
Keywords: security,credentials,secrets,scanner,redactor
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Topic :: Security
|
|
17
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Provides-Extra: encoding
|
|
22
|
+
Requires-Dist: charset-normalizer>=3.0; extra == "encoding"
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
25
|
+
Requires-Dist: ruff>=0.5; extra == "dev"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# Credactor
|
|
29
|
+
|
|
30
|
+
*Credential redactor* — finds and removes hardcoded secrets from source code. API keys, tokens, passwords, connection strings, private keys. 20+ file types. Zero config to start.
|
|
31
|
+
|
|
32
|
+
## Detection
|
|
33
|
+
|
|
34
|
+
| Category | Examples | Severity |
|
|
35
|
+
|---|---|---|
|
|
36
|
+
| Cloud provider keys | AWS (`AKIA...`), GCP (`AIza...`), Stripe (`sk_live_...`), Slack (`xoxb-...`) | Critical |
|
|
37
|
+
| Platform tokens | GitHub (`ghp_`, `github_pat_`), GitLab (`glpat-`), npm (`npm_`), PyPI (`pypi-`) | Critical |
|
|
38
|
+
| Private keys | PEM blocks (`-----BEGIN RSA PRIVATE KEY-----`) | Critical |
|
|
39
|
+
| JWT tokens | `eyJ...` three-segment tokens | High |
|
|
40
|
+
| Connection strings | `postgresql://user:pass@host`, `mongodb+srv://...`, `redis://...` | High |
|
|
41
|
+
| Variable assignments | `password = "..."`, `api_key = "..."`, `db_password = "..."` | High/Medium |
|
|
42
|
+
| XML attributes | `<add key="Password" value="..." />` | High |
|
|
43
|
+
| High-entropy strings | Hex (32-64 chars), Base64 (60+ chars) | Medium/Low |
|
|
44
|
+
|
|
45
|
+
## Features
|
|
46
|
+
|
|
47
|
+
- Entropy-based detection with per-pattern thresholds to cut false positives
|
|
48
|
+
- Severity levels (critical / high / medium / low) for triage
|
|
49
|
+
- Interactive or batch redaction — review one-by-one, or `--fix-all`
|
|
50
|
+
- Language-aware replacement — `--replace-with env` generates `os.environ["KEY"]` in Python, `process.env.KEY` in JS, `System.getenv("KEY")` in Java, etc.
|
|
51
|
+
- Pre-commit hook support via `--staged`
|
|
52
|
+
- Git history scanning via `--scan-history`
|
|
53
|
+
- Text, JSON, and SARIF output (SARIF integrates with GitHub Code Scanning)
|
|
54
|
+
- `.bak` backups before any file modification
|
|
55
|
+
- Inline `# credactor:ignore` suppression and `.credactorignore` allowlists
|
|
56
|
+
- Per-repo config via `.credactor.toml`
|
|
57
|
+
- Parallel scanning for large repos
|
|
58
|
+
|
|
59
|
+
## Install
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install -e .
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
This gives you the `credactor` and `credactor` commands.
|
|
66
|
+
|
|
67
|
+
## Quick Start
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
# Scan current directory
|
|
71
|
+
credactor .
|
|
72
|
+
|
|
73
|
+
# Dry run (no modifications)
|
|
74
|
+
credactor --dry-run /path/to/project
|
|
75
|
+
|
|
76
|
+
# Redact everything without prompting
|
|
77
|
+
credactor --fix-all .
|
|
78
|
+
|
|
79
|
+
# CI mode — exit 1 on findings
|
|
80
|
+
credactor --ci .
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Or run as a module without installing:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
python -m credactor .
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Scanned File Types
|
|
90
|
+
|
|
91
|
+
`.py` `.js` `.ts` `.jsx` `.tsx` `.sh` `.bash` `.env` `.env.*` `.cfg` `.ini` `.toml` `.yaml` `.yml` `.rb` `.go` `.java` `.php` `.cs` `.kt` `.tf` `.hcl` `.conf` `.properties` `.xml`
|
|
92
|
+
|
|
93
|
+
JSON files are excluded by default due to high false-positive rates from API response data. Use `--scan-json` to include them.
|
|
94
|
+
|
|
95
|
+
## Auto-Skipped
|
|
96
|
+
|
|
97
|
+
Directories: `.git`, `__pycache__`, `node_modules`, `.venv`, `venv`, `.tox`, `dist`, `build`
|
|
98
|
+
|
|
99
|
+
Files: `package-lock.json`, `yarn.lock`, `poetry.lock`, `pnpm-lock.yaml`
|
|
100
|
+
|
|
101
|
+
Values: placeholders (`your_api_key`, `changeme`), env var references (`$VAR`, `${VAR}`), function calls, file paths, URLs without credentials, dynamic lookups (`os.getenv()`, Vault/SOPS refs)
|
|
102
|
+
|
|
103
|
+
## Exit Codes
|
|
104
|
+
|
|
105
|
+
| Code | Meaning |
|
|
106
|
+
|---|---|
|
|
107
|
+
| `0` | No findings, or all resolved |
|
|
108
|
+
| `1` | Unresolved findings |
|
|
109
|
+
| `2` | Error |
|
|
110
|
+
|
|
111
|
+
## Docs
|
|
112
|
+
|
|
113
|
+
- [Setup Guide](docs/setup.md) — install, config, CI
|
|
114
|
+
- [User Guide](docs/user-guide.md) — CLI reference, feature walkthrough
|
|
115
|
+
- [Examples](docs/examples.md) — common workflows
|
|
116
|
+
- [Integration](docs/integration.md) — pre-commit hooks, CI setup
|
|
117
|
+
- [Disclaimer](docs/DISCLAIMER.md) — limitations, safe usage, warranty
|
|
118
|
+
|
|
119
|
+
## Transparancy over AI Usage
|
|
120
|
+
AI was used for code review, bug fixes, security auditing, and documentation structuring for the project.
|
|
121
|
+
All output was reviewed and validated manually.
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
Apache 2.0. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# Credactor
|
|
2
|
+
|
|
3
|
+
*Credential redactor* — finds and removes hardcoded secrets from source code. API keys, tokens, passwords, connection strings, private keys. 20+ file types. Zero config to start.
|
|
4
|
+
|
|
5
|
+
## Detection
|
|
6
|
+
|
|
7
|
+
| Category | Examples | Severity |
|
|
8
|
+
|---|---|---|
|
|
9
|
+
| Cloud provider keys | AWS (`AKIA...`), GCP (`AIza...`), Stripe (`sk_live_...`), Slack (`xoxb-...`) | Critical |
|
|
10
|
+
| Platform tokens | GitHub (`ghp_`, `github_pat_`), GitLab (`glpat-`), npm (`npm_`), PyPI (`pypi-`) | Critical |
|
|
11
|
+
| Private keys | PEM blocks (`-----BEGIN RSA PRIVATE KEY-----`) | Critical |
|
|
12
|
+
| JWT tokens | `eyJ...` three-segment tokens | High |
|
|
13
|
+
| Connection strings | `postgresql://user:pass@host`, `mongodb+srv://...`, `redis://...` | High |
|
|
14
|
+
| Variable assignments | `password = "..."`, `api_key = "..."`, `db_password = "..."` | High/Medium |
|
|
15
|
+
| XML attributes | `<add key="Password" value="..." />` | High |
|
|
16
|
+
| High-entropy strings | Hex (32-64 chars), Base64 (60+ chars) | Medium/Low |
|
|
17
|
+
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
- Entropy-based detection with per-pattern thresholds to cut false positives
|
|
21
|
+
- Severity levels (critical / high / medium / low) for triage
|
|
22
|
+
- Interactive or batch redaction — review one-by-one, or `--fix-all`
|
|
23
|
+
- Language-aware replacement — `--replace-with env` generates `os.environ["KEY"]` in Python, `process.env.KEY` in JS, `System.getenv("KEY")` in Java, etc.
|
|
24
|
+
- Pre-commit hook support via `--staged`
|
|
25
|
+
- Git history scanning via `--scan-history`
|
|
26
|
+
- Text, JSON, and SARIF output (SARIF integrates with GitHub Code Scanning)
|
|
27
|
+
- `.bak` backups before any file modification
|
|
28
|
+
- Inline `# credactor:ignore` suppression and `.credactorignore` allowlists
|
|
29
|
+
- Per-repo config via `.credactor.toml`
|
|
30
|
+
- Parallel scanning for large repos
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install -e .
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
This gives you the `credactor` and `credactor` commands.
|
|
39
|
+
|
|
40
|
+
## Quick Start
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
# Scan current directory
|
|
44
|
+
credactor .
|
|
45
|
+
|
|
46
|
+
# Dry run (no modifications)
|
|
47
|
+
credactor --dry-run /path/to/project
|
|
48
|
+
|
|
49
|
+
# Redact everything without prompting
|
|
50
|
+
credactor --fix-all .
|
|
51
|
+
|
|
52
|
+
# CI mode — exit 1 on findings
|
|
53
|
+
credactor --ci .
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Or run as a module without installing:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
python -m credactor .
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Scanned File Types
|
|
63
|
+
|
|
64
|
+
`.py` `.js` `.ts` `.jsx` `.tsx` `.sh` `.bash` `.env` `.env.*` `.cfg` `.ini` `.toml` `.yaml` `.yml` `.rb` `.go` `.java` `.php` `.cs` `.kt` `.tf` `.hcl` `.conf` `.properties` `.xml`
|
|
65
|
+
|
|
66
|
+
JSON files are excluded by default due to high false-positive rates from API response data. Use `--scan-json` to include them.
|
|
67
|
+
|
|
68
|
+
## Auto-Skipped
|
|
69
|
+
|
|
70
|
+
Directories: `.git`, `__pycache__`, `node_modules`, `.venv`, `venv`, `.tox`, `dist`, `build`
|
|
71
|
+
|
|
72
|
+
Files: `package-lock.json`, `yarn.lock`, `poetry.lock`, `pnpm-lock.yaml`
|
|
73
|
+
|
|
74
|
+
Values: placeholders (`your_api_key`, `changeme`), env var references (`$VAR`, `${VAR}`), function calls, file paths, URLs without credentials, dynamic lookups (`os.getenv()`, Vault/SOPS refs)
|
|
75
|
+
|
|
76
|
+
## Exit Codes
|
|
77
|
+
|
|
78
|
+
| Code | Meaning |
|
|
79
|
+
|---|---|
|
|
80
|
+
| `0` | No findings, or all resolved |
|
|
81
|
+
| `1` | Unresolved findings |
|
|
82
|
+
| `2` | Error |
|
|
83
|
+
|
|
84
|
+
## Docs
|
|
85
|
+
|
|
86
|
+
- [Setup Guide](docs/setup.md) — install, config, CI
|
|
87
|
+
- [User Guide](docs/user-guide.md) — CLI reference, feature walkthrough
|
|
88
|
+
- [Examples](docs/examples.md) — common workflows
|
|
89
|
+
- [Integration](docs/integration.md) — pre-commit hooks, CI setup
|
|
90
|
+
- [Disclaimer](docs/DISCLAIMER.md) — limitations, safe usage, warranty
|
|
91
|
+
|
|
92
|
+
## Transparancy over AI Usage
|
|
93
|
+
AI was used for code review, bug fixes, security auditing, and documentation structuring for the project.
|
|
94
|
+
All output was reviewed and validated manually.
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
Apache 2.0. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CLI entry point using argparse.
|
|
3
|
+
|
|
4
|
+
Addresses: #6 (--staged), #7 (--format), #8 (--dry-run), #24 (argparse),
|
|
5
|
+
#33 (--fix-all), #34 (exit codes: 0=clean, 1=findings, 2=error)
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
import os
|
|
12
|
+
import sys
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from .config import Config, apply_config_file, load_config_file
|
|
16
|
+
from .redactor import fix_all, interactive_review
|
|
17
|
+
from .report import json_report, print_gitignore_skipped, print_report, sarif_report
|
|
18
|
+
from .scanner import scan_file
|
|
19
|
+
from .suppressions import AllowList
|
|
20
|
+
from .walker import scan_git_history, scan_staged_files, select_json_files, walk_and_scan
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
24
|
+
parser = argparse.ArgumentParser(
|
|
25
|
+
prog='credactor',
|
|
26
|
+
description='Scan source files for hardcoded credentials and optionally redact them.',
|
|
27
|
+
epilog='Exit codes: 0 = clean, 1 = unresolved findings, 2 = error',
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
parser.add_argument(
|
|
31
|
+
'target', nargs='?', default='.',
|
|
32
|
+
help='Directory or file to scan (default: current directory)',
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
# Mode flags
|
|
36
|
+
mode = parser.add_argument_group('mode')
|
|
37
|
+
mode.add_argument(
|
|
38
|
+
'--ci', action='store_true',
|
|
39
|
+
help='CI mode: report only (no prompts), exit 1 on findings',
|
|
40
|
+
)
|
|
41
|
+
mode.add_argument(
|
|
42
|
+
'--dry-run', action='store_true',
|
|
43
|
+
help='Show what would be found/replaced without modifying files',
|
|
44
|
+
)
|
|
45
|
+
mode.add_argument(
|
|
46
|
+
'--fix-all', action='store_true',
|
|
47
|
+
help='Replace all findings without prompting',
|
|
48
|
+
)
|
|
49
|
+
mode.add_argument(
|
|
50
|
+
'--staged', action='store_true',
|
|
51
|
+
help='Scan only git-staged files (for pre-commit hooks)',
|
|
52
|
+
)
|
|
53
|
+
mode.add_argument(
|
|
54
|
+
'--scan-history', action='store_true',
|
|
55
|
+
help='Scan git commit history for leaked credentials',
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
# Output flags
|
|
59
|
+
output = parser.add_argument_group('output')
|
|
60
|
+
output.add_argument(
|
|
61
|
+
'--format', '-f', choices=['text', 'json', 'sarif'], default='text',
|
|
62
|
+
dest='output_format',
|
|
63
|
+
help='Output format (default: text)',
|
|
64
|
+
)
|
|
65
|
+
output.add_argument(
|
|
66
|
+
'--no-color', action='store_true',
|
|
67
|
+
help='Disable ANSI color output',
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
# Replacement flags
|
|
71
|
+
replace = parser.add_argument_group('replacement')
|
|
72
|
+
replace.add_argument(
|
|
73
|
+
'--replace-with', choices=['sentinel', 'env', 'custom'], default='sentinel',
|
|
74
|
+
dest='replace_mode',
|
|
75
|
+
help='Replacement strategy: sentinel (default), env (language-aware env var ref), custom',
|
|
76
|
+
)
|
|
77
|
+
replace.add_argument(
|
|
78
|
+
'--replacement', type=str, default='REDACTED_BY_CREDACTOR',
|
|
79
|
+
help='Custom replacement string (used with --replace-with=sentinel or custom)',
|
|
80
|
+
)
|
|
81
|
+
replace.add_argument(
|
|
82
|
+
'--no-backup', action='store_true',
|
|
83
|
+
help='Skip creating .bak backup files before modifying',
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
# Configuration
|
|
87
|
+
config_group = parser.add_argument_group('configuration')
|
|
88
|
+
config_group.add_argument(
|
|
89
|
+
'--config', type=str, default=None,
|
|
90
|
+
help='Path to .credactor.toml config file',
|
|
91
|
+
)
|
|
92
|
+
config_group.add_argument(
|
|
93
|
+
'--scan-json', action='store_true',
|
|
94
|
+
help='Include .json files in the scan',
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
return parser
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def main(argv: list[str] | None = None) -> None:
|
|
101
|
+
parser = build_parser()
|
|
102
|
+
args = parser.parse_args(argv)
|
|
103
|
+
|
|
104
|
+
# Build Config
|
|
105
|
+
config = Config(
|
|
106
|
+
ci_mode=args.ci,
|
|
107
|
+
dry_run=args.dry_run,
|
|
108
|
+
fix_all=args.fix_all,
|
|
109
|
+
staged_only=args.staged,
|
|
110
|
+
scan_history=args.scan_history,
|
|
111
|
+
scan_json=args.scan_json,
|
|
112
|
+
no_backup=args.no_backup,
|
|
113
|
+
no_color=args.no_color,
|
|
114
|
+
replace_mode=args.replace_mode,
|
|
115
|
+
custom_replacement=args.replacement,
|
|
116
|
+
output_format=args.output_format,
|
|
117
|
+
target=args.target,
|
|
118
|
+
config_path=args.config,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
# Load config file (#25)
|
|
122
|
+
target = config.target
|
|
123
|
+
if not os.path.exists(target):
|
|
124
|
+
print(f'Error: path not found: {target}', file=sys.stderr)
|
|
125
|
+
sys.exit(2)
|
|
126
|
+
|
|
127
|
+
# Guard against scanning system directories
|
|
128
|
+
_PROTECTED_DIRS = {'/', '/etc', '/usr', '/var', '/boot', '/sys', '/proc',
|
|
129
|
+
'/bin', '/sbin', '/lib', '/opt', '/root',
|
|
130
|
+
'C:\\', 'C:\\Windows', 'C:\\Program Files'}
|
|
131
|
+
resolved = str(Path(target).resolve())
|
|
132
|
+
if resolved in _PROTECTED_DIRS:
|
|
133
|
+
print(f'Error: refusing to scan system directory: {resolved}',
|
|
134
|
+
file=sys.stderr)
|
|
135
|
+
print(' Use a project directory instead.', file=sys.stderr)
|
|
136
|
+
sys.exit(2)
|
|
137
|
+
|
|
138
|
+
file_data = load_config_file(target, config.config_path)
|
|
139
|
+
if file_data:
|
|
140
|
+
apply_config_file(config, file_data)
|
|
141
|
+
|
|
142
|
+
# Suppressions (#3, #4)
|
|
143
|
+
allowlist = AllowList(target)
|
|
144
|
+
|
|
145
|
+
print(f'Scanning: {Path(target).resolve()}', file=sys.stderr)
|
|
146
|
+
|
|
147
|
+
# --- Dispatch based on mode ---
|
|
148
|
+
findings: list[dict] = []
|
|
149
|
+
|
|
150
|
+
if config.staged_only:
|
|
151
|
+
# #6 — staged files only
|
|
152
|
+
findings = scan_staged_files(target, config, allowlist)
|
|
153
|
+
elif config.scan_history:
|
|
154
|
+
# #11 — git history
|
|
155
|
+
findings = scan_git_history(target, config, allowlist)
|
|
156
|
+
else:
|
|
157
|
+
# Normal directory scan (#26 single walk)
|
|
158
|
+
dir_findings, gitignore_skipped, json_files = walk_and_scan(target, config, allowlist)
|
|
159
|
+
findings = dir_findings
|
|
160
|
+
|
|
161
|
+
# Report gitignored files
|
|
162
|
+
if config.output_format == 'text':
|
|
163
|
+
print_gitignore_skipped(gitignore_skipped, target, no_color=config.no_color)
|
|
164
|
+
|
|
165
|
+
# Optionally scan JSON files
|
|
166
|
+
if config.scan_json and json_files:
|
|
167
|
+
# Skip interactive selection when non-interactive
|
|
168
|
+
if (config.ci_mode or config.dry_run or config.fix_all
|
|
169
|
+
or config.output_format != 'text'):
|
|
170
|
+
json_paths = json_files
|
|
171
|
+
else:
|
|
172
|
+
json_paths = select_json_files(json_files, target)
|
|
173
|
+
|
|
174
|
+
for path in json_paths:
|
|
175
|
+
findings.extend(scan_file(path, config=config, allowlist=allowlist))
|
|
176
|
+
|
|
177
|
+
# --- Output ---
|
|
178
|
+
if not findings:
|
|
179
|
+
if config.output_format == 'json':
|
|
180
|
+
print(json_report(findings, target))
|
|
181
|
+
elif config.output_format == 'sarif':
|
|
182
|
+
print(sarif_report(findings, target))
|
|
183
|
+
else:
|
|
184
|
+
print('\n[OK] No hardcoded credentials detected. Safe for commits.\n')
|
|
185
|
+
sys.exit(0)
|
|
186
|
+
|
|
187
|
+
# We have findings — report them
|
|
188
|
+
if config.output_format == 'json':
|
|
189
|
+
print(json_report(findings, target))
|
|
190
|
+
elif config.output_format == 'sarif':
|
|
191
|
+
print(sarif_report(findings, target))
|
|
192
|
+
else:
|
|
193
|
+
print_report(findings, target, no_color=config.no_color)
|
|
194
|
+
|
|
195
|
+
# #34 — exit code semantics (consistent across all formats)
|
|
196
|
+
if config.ci_mode or config.dry_run:
|
|
197
|
+
sys.exit(1)
|
|
198
|
+
|
|
199
|
+
if config.fix_all:
|
|
200
|
+
# Confirmation before destructive batch operation
|
|
201
|
+
by_file: dict[str, list] = {}
|
|
202
|
+
for f in findings:
|
|
203
|
+
by_file.setdefault(f['file'], []).append(f)
|
|
204
|
+
print(f'\n --fix-all will modify {len(by_file)} file(s) '
|
|
205
|
+
f'with {len(findings)} replacement(s).')
|
|
206
|
+
if not config.no_backup:
|
|
207
|
+
print(' .bak backups will be created (contain original secrets).')
|
|
208
|
+
else:
|
|
209
|
+
print(' WARNING: --no-backup is set. No backups will be created.')
|
|
210
|
+
try:
|
|
211
|
+
answer = input(' Proceed? [y/N]: ').strip().lower()
|
|
212
|
+
except (KeyboardInterrupt, EOFError):
|
|
213
|
+
print('\n Aborted.')
|
|
214
|
+
sys.exit(1)
|
|
215
|
+
if answer not in ('y', 'yes'):
|
|
216
|
+
print(' Aborted.')
|
|
217
|
+
sys.exit(1)
|
|
218
|
+
|
|
219
|
+
unresolved = fix_all(findings, target, config)
|
|
220
|
+
sys.exit(1 if unresolved > 0 else 0)
|
|
221
|
+
|
|
222
|
+
# Non-text formats in non-CI mode: report and exit 1
|
|
223
|
+
if config.output_format != 'text':
|
|
224
|
+
sys.exit(1)
|
|
225
|
+
|
|
226
|
+
# Interactive mode (default, text only)
|
|
227
|
+
unresolved = interactive_review(findings, target, config)
|
|
228
|
+
sys.exit(1 if unresolved > 0 else 0)
|