shieldenv 1.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.
- shieldenv-1.0.0/LICENSE +21 -0
- shieldenv-1.0.0/PKG-INFO +134 -0
- shieldenv-1.0.0/README.md +121 -0
- shieldenv-1.0.0/pyproject.toml +34 -0
- shieldenv-1.0.0/setup.cfg +4 -0
- shieldenv-1.0.0/shieldenv/__init__.py +6 -0
- shieldenv-1.0.0/shieldenv/cli.py +204 -0
- shieldenv-1.0.0/shieldenv/config.py +32 -0
- shieldenv-1.0.0/shieldenv/patterns.py +32 -0
- shieldenv-1.0.0/shieldenv/scanner.py +211 -0
- shieldenv-1.0.0/shieldenv.egg-info/PKG-INFO +134 -0
- shieldenv-1.0.0/shieldenv.egg-info/SOURCES.txt +16 -0
- shieldenv-1.0.0/shieldenv.egg-info/dependency_links.txt +1 -0
- shieldenv-1.0.0/shieldenv.egg-info/entry_points.txt +2 -0
- shieldenv-1.0.0/shieldenv.egg-info/requires.txt +5 -0
- shieldenv-1.0.0/shieldenv.egg-info/top_level.txt +1 -0
- shieldenv-1.0.0/tests/test_cli.py +59 -0
- shieldenv-1.0.0/tests/test_scanner.py +186 -0
shieldenv-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ShieldRunner Contributors
|
|
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.
|
shieldenv-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: shieldenv
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Local-first secret and AI API key leak prevention CLI
|
|
5
|
+
Requires-Python: >=3.8
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: click>=8.0.0
|
|
9
|
+
Provides-Extra: dev
|
|
10
|
+
Requires-Dist: pytest>=8.0.0; extra == "dev"
|
|
11
|
+
Requires-Dist: ruff>=0.6.0; extra == "dev"
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# ShieldEnv 🛡️
|
|
15
|
+
Local-First Secret Scanning & Pre-Commit Guardraill
|
|
16
|
+
|
|
17
|
+
ShieldEnv is an open-source security engine designed to eliminate accidental credential leaks, API key exposures, and compliance violations before code ever hits version control.
|
|
18
|
+
|
|
19
|
+
### Why Local-First?
|
|
20
|
+
Unlike cloud-based PR scanners that analyze your code after it leaves your machine, ShieldEnv runs entirely local-first. Zero code, environment variables, or tokens ever leave your local environment.
|
|
21
|
+
|
|
22
|
+
## Features
|
|
23
|
+
|
|
24
|
+
- Regex detection for OpenAI, Anthropic, Google, AWS, GitHub, Slack, database URLs, private keys, and generic secret assignments.
|
|
25
|
+
- Provider rules include confidence levels (`high` or `medium`).
|
|
26
|
+
- Entropy-based detection for literal custom tokens.
|
|
27
|
+
- Built-in filtering for placeholders, environment substitutions, templates, and common generated/UI syntax.
|
|
28
|
+
- Project and user-level `.shieldignore` support.
|
|
29
|
+
- Text, JSON, and SARIF output for local and CI use.
|
|
30
|
+
- Optional Git pre-commit hook installation.
|
|
31
|
+
- Redacted findings that never print complete secret values.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
Create an environment and install ShieldEnv:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
python -m venv .venv
|
|
39
|
+
source .venv/bin/activate
|
|
40
|
+
python -m pip install -e ".[dev]"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
On Windows PowerShell, activate with:
|
|
44
|
+
|
|
45
|
+
```powershell
|
|
46
|
+
.\.venv\Scripts\Activate.ps1
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
Scan the current repository:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
shieldenv scan
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Scan another path or emit machine-readable JSON:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
shieldenv scan --path ./src --format json
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Generate GitHub Code Scanning-compatible SARIF:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
shieldenv scan --path . --format sarif > shieldenv.sarif
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Use verbose mode to inspect scan coverage and ignored paths:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
shieldenv scan --verbose
|
|
73
|
+
shieldenv scan --audit --format json
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Exit codes:
|
|
77
|
+
|
|
78
|
+
- `0`: no findings and no scan errors.
|
|
79
|
+
- `1`: one or more potential secrets were found.
|
|
80
|
+
- `2`: one or more files could not be read.
|
|
81
|
+
|
|
82
|
+
Install a Git pre-commit hook from the repository root:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
shieldenv install
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
ShieldEnv will not overwrite an existing hook unless explicitly requested:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
shieldenv install --force
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
With `--force`, an existing hook is saved as `.git/hooks/pre-commit.shieldenv-backup`.
|
|
95
|
+
|
|
96
|
+
## Ignore rules
|
|
97
|
+
|
|
98
|
+
ShieldEnv skips common dependency, build, cache, and lock-file paths by default. Add a project-level `.shieldignore` at the root being scanned, or a global `~/.shieldignore`, with one rule per line:
|
|
99
|
+
|
|
100
|
+
```text
|
|
101
|
+
# Exact directory name anywhere in the tree
|
|
102
|
+
generated
|
|
103
|
+
|
|
104
|
+
# Relative path or glob
|
|
105
|
+
backend/app/api/
|
|
106
|
+
frontend/generated/**
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Blank lines and lines beginning with `#` are ignored. Use ignore rules carefully: excluded files are not scanned. `--verbose` lists ignored paths so exclusions can be reviewed.
|
|
110
|
+
|
|
111
|
+
To suppress a deliberate test value on one line, use the explicit marker:
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
TEST_TOKEN = "not-a-credential" # shieldenv: ignore
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Suppressions are recorded in audit JSON output. Keep suppressions rare and review them during code review.
|
|
118
|
+
|
|
119
|
+
## Development
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
python -m pytest
|
|
123
|
+
ruff check .
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
ShieldEnv is heuristic-based and should complement, not replace, secret rotation and incident-response procedures. Review findings and maintain project-specific ignore rules for generated code.
|
|
127
|
+
|
|
128
|
+
ShieldEnv cannot guarantee detection of every secret format. It combines provider signatures, structured assignment patterns, and entropy heuristics, so unusual encodings may be missed and generated or identifier-like values may still require review. The scanner intentionally reports findings without exposing full values. Run it in CI on Linux, Windows, and macOS; the included workflow tests Python 3.9 through 3.13.
|
|
129
|
+
|
|
130
|
+
Provider signatures are high-confidence indicators; generic assignments and entropy findings are medium-confidence heuristics. A clean scan means no supported rule matched, not that the repository is guaranteed secret-free. Rotate any credential that may have been exposed, and use code review and repository access controls alongside ShieldEnv.
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
Distributed under the MIT License. See [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# ShieldEnv 🛡️
|
|
2
|
+
Local-First Secret Scanning & Pre-Commit Guardraill
|
|
3
|
+
|
|
4
|
+
ShieldEnv is an open-source security engine designed to eliminate accidental credential leaks, API key exposures, and compliance violations before code ever hits version control.
|
|
5
|
+
|
|
6
|
+
### Why Local-First?
|
|
7
|
+
Unlike cloud-based PR scanners that analyze your code after it leaves your machine, ShieldEnv runs entirely local-first. Zero code, environment variables, or tokens ever leave your local environment.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Regex detection for OpenAI, Anthropic, Google, AWS, GitHub, Slack, database URLs, private keys, and generic secret assignments.
|
|
12
|
+
- Provider rules include confidence levels (`high` or `medium`).
|
|
13
|
+
- Entropy-based detection for literal custom tokens.
|
|
14
|
+
- Built-in filtering for placeholders, environment substitutions, templates, and common generated/UI syntax.
|
|
15
|
+
- Project and user-level `.shieldignore` support.
|
|
16
|
+
- Text, JSON, and SARIF output for local and CI use.
|
|
17
|
+
- Optional Git pre-commit hook installation.
|
|
18
|
+
- Redacted findings that never print complete secret values.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Create an environment and install ShieldEnv:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
python -m venv .venv
|
|
26
|
+
source .venv/bin/activate
|
|
27
|
+
python -m pip install -e ".[dev]"
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
On Windows PowerShell, activate with:
|
|
31
|
+
|
|
32
|
+
```powershell
|
|
33
|
+
.\.venv\Scripts\Activate.ps1
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
Scan the current repository:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
shieldenv scan
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Scan another path or emit machine-readable JSON:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
shieldenv scan --path ./src --format json
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Generate GitHub Code Scanning-compatible SARIF:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
shieldenv scan --path . --format sarif > shieldenv.sarif
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Use verbose mode to inspect scan coverage and ignored paths:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
shieldenv scan --verbose
|
|
60
|
+
shieldenv scan --audit --format json
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Exit codes:
|
|
64
|
+
|
|
65
|
+
- `0`: no findings and no scan errors.
|
|
66
|
+
- `1`: one or more potential secrets were found.
|
|
67
|
+
- `2`: one or more files could not be read.
|
|
68
|
+
|
|
69
|
+
Install a Git pre-commit hook from the repository root:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
shieldenv install
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
ShieldEnv will not overwrite an existing hook unless explicitly requested:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
shieldenv install --force
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
With `--force`, an existing hook is saved as `.git/hooks/pre-commit.shieldenv-backup`.
|
|
82
|
+
|
|
83
|
+
## Ignore rules
|
|
84
|
+
|
|
85
|
+
ShieldEnv skips common dependency, build, cache, and lock-file paths by default. Add a project-level `.shieldignore` at the root being scanned, or a global `~/.shieldignore`, with one rule per line:
|
|
86
|
+
|
|
87
|
+
```text
|
|
88
|
+
# Exact directory name anywhere in the tree
|
|
89
|
+
generated
|
|
90
|
+
|
|
91
|
+
# Relative path or glob
|
|
92
|
+
backend/app/api/
|
|
93
|
+
frontend/generated/**
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Blank lines and lines beginning with `#` are ignored. Use ignore rules carefully: excluded files are not scanned. `--verbose` lists ignored paths so exclusions can be reviewed.
|
|
97
|
+
|
|
98
|
+
To suppress a deliberate test value on one line, use the explicit marker:
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
TEST_TOKEN = "not-a-credential" # shieldenv: ignore
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Suppressions are recorded in audit JSON output. Keep suppressions rare and review them during code review.
|
|
105
|
+
|
|
106
|
+
## Development
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
python -m pytest
|
|
110
|
+
ruff check .
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
ShieldEnv is heuristic-based and should complement, not replace, secret rotation and incident-response procedures. Review findings and maintain project-specific ignore rules for generated code.
|
|
114
|
+
|
|
115
|
+
ShieldEnv cannot guarantee detection of every secret format. It combines provider signatures, structured assignment patterns, and entropy heuristics, so unusual encodings may be missed and generated or identifier-like values may still require review. The scanner intentionally reports findings without exposing full values. Run it in CI on Linux, Windows, and macOS; the included workflow tests Python 3.9 through 3.13.
|
|
116
|
+
|
|
117
|
+
Provider signatures are high-confidence indicators; generic assignments and entropy findings are medium-confidence heuristics. A clean scan means no supported rule matched, not that the repository is guaranteed secret-free. Rotate any credential that may have been exposed, and use code review and repository access controls alongside ShieldEnv.
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
Distributed under the MIT License. See [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "shieldenv"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Local-first secret and AI API key leak prevention CLI"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"click>=8.0.0",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[project.optional-dependencies]
|
|
16
|
+
dev = [
|
|
17
|
+
"pytest>=8.0.0",
|
|
18
|
+
"ruff>=0.6.0",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.scripts]
|
|
22
|
+
shieldenv = "shieldenv.cli:main"
|
|
23
|
+
|
|
24
|
+
[tool.pytest.ini_options]
|
|
25
|
+
testpaths = ["tests"]
|
|
26
|
+
|
|
27
|
+
[tool.ruff]
|
|
28
|
+
exclude = [
|
|
29
|
+
"build",
|
|
30
|
+
"full-stack-fastapi-template",
|
|
31
|
+
"juice-shop",
|
|
32
|
+
"venv",
|
|
33
|
+
".venv",
|
|
34
|
+
]
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import codecs
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
import shlex
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import click
|
|
9
|
+
|
|
10
|
+
from .scanner import scan_path
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _configure_utf8_stream(stream):
|
|
14
|
+
"""Use UTF-8 for real Windows streams without disrupting Click capture."""
|
|
15
|
+
if sys.platform != "win32":
|
|
16
|
+
return stream
|
|
17
|
+
|
|
18
|
+
reconfigure = getattr(stream, "reconfigure", None)
|
|
19
|
+
if reconfigure is not None:
|
|
20
|
+
try:
|
|
21
|
+
reconfigure(encoding="utf-8", errors="strict")
|
|
22
|
+
return stream
|
|
23
|
+
except (AttributeError, OSError, ValueError):
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
buffer = getattr(stream, "buffer", None)
|
|
27
|
+
if buffer is not None:
|
|
28
|
+
return codecs.getwriter("utf-8")(buffer, "strict")
|
|
29
|
+
return stream
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _configure_utf8_streams():
|
|
33
|
+
sys.stdout = _configure_utf8_stream(sys.stdout)
|
|
34
|
+
sys.stderr = _configure_utf8_stream(sys.stderr)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def safe_echo(message="", **kwargs):
|
|
38
|
+
try:
|
|
39
|
+
click.echo(message, **kwargs)
|
|
40
|
+
except UnicodeEncodeError:
|
|
41
|
+
fallback = str(message).encode("ascii", "replace").decode("ascii")
|
|
42
|
+
click.echo(fallback, **kwargs)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@click.group()
|
|
46
|
+
def main():
|
|
47
|
+
"""ShieldEnv: A local-first security scanner for hardcoded secrets and API keys."""
|
|
48
|
+
_configure_utf8_streams()
|
|
49
|
+
|
|
50
|
+
@main.command()
|
|
51
|
+
@click.option("--path", default=".", help="Root directory to scan for secrets.")
|
|
52
|
+
@click.option("--format", type=click.Choice(["text", "json", "sarif"]), default="text", help="Output format for scan results.")
|
|
53
|
+
@click.option("--verbose", is_flag=True, help="Show ignored paths and scan summary.")
|
|
54
|
+
@click.option("--audit", is_flag=True, help="Include suppression and ignore audit data.")
|
|
55
|
+
def scan(path, format, verbose, audit):
|
|
56
|
+
"""Scan codebases, configuration files, and environment files for exposed secrets."""
|
|
57
|
+
if format == "text":
|
|
58
|
+
safe_echo("🛡️ ShieldEnv: Scanning repository files...")
|
|
59
|
+
|
|
60
|
+
try:
|
|
61
|
+
result = scan_path(path, verbose=verbose or audit)
|
|
62
|
+
except (RuntimeError, ValueError) as exc:
|
|
63
|
+
raise click.ClickException(str(exc)) from exc
|
|
64
|
+
violations = [finding.__dict__ for finding in result.violations]
|
|
65
|
+
|
|
66
|
+
if result.errors:
|
|
67
|
+
for error in result.errors:
|
|
68
|
+
safe_echo(f"❌ {error}", err=True)
|
|
69
|
+
raise click.exceptions.Exit(2)
|
|
70
|
+
|
|
71
|
+
if format == "sarif":
|
|
72
|
+
safe_echo(json.dumps(_to_sarif(result), indent=2))
|
|
73
|
+
if violations or result.errors:
|
|
74
|
+
raise click.exceptions.Exit(1 if violations else 2)
|
|
75
|
+
return
|
|
76
|
+
|
|
77
|
+
if format == "json":
|
|
78
|
+
payload = violations
|
|
79
|
+
if audit or verbose:
|
|
80
|
+
payload = {
|
|
81
|
+
"findings": violations,
|
|
82
|
+
"suppressed": result.suppressed,
|
|
83
|
+
"ignored": result.ignored,
|
|
84
|
+
"files_scanned": result.files_scanned,
|
|
85
|
+
}
|
|
86
|
+
safe_echo(json.dumps(payload, indent=2))
|
|
87
|
+
if violations:
|
|
88
|
+
raise click.exceptions.Exit(1)
|
|
89
|
+
return
|
|
90
|
+
|
|
91
|
+
if not violations:
|
|
92
|
+
safe_echo("✅ All clear! No leaked secrets or API keys found.")
|
|
93
|
+
if audit or verbose:
|
|
94
|
+
safe_echo(f"Scanned {result.files_scanned} files.")
|
|
95
|
+
safe_echo(f"Suppressed findings: {len(result.suppressed)}")
|
|
96
|
+
safe_echo(f"Ignored paths: {len(result.ignored)}")
|
|
97
|
+
return
|
|
98
|
+
|
|
99
|
+
safe_echo(f"\n❌ SECURITY ALERT: Found {len(violations)} potential secret leaks!\n")
|
|
100
|
+
|
|
101
|
+
for v in violations:
|
|
102
|
+
safe_echo(f" • Type: {v['type']}")
|
|
103
|
+
safe_echo(f" File: {v['file']} (Line {v['line']})")
|
|
104
|
+
safe_echo(f" Confidence: {v['confidence']}")
|
|
105
|
+
safe_echo(f" Data: {v['snippet']}")
|
|
106
|
+
safe_echo("-" * 50)
|
|
107
|
+
|
|
108
|
+
if audit or verbose:
|
|
109
|
+
safe_echo(f"\nSuppressed findings: {len(result.suppressed)}")
|
|
110
|
+
safe_echo(f"Ignored paths: {len(result.ignored)}")
|
|
111
|
+
safe_echo(f"Files scanned: {result.files_scanned}")
|
|
112
|
+
|
|
113
|
+
safe_echo("\n🚫 Commit/Build BLOCKED. Remove secrets, use environment variables, or update .shieldignore.")
|
|
114
|
+
raise click.exceptions.Exit(1)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _to_sarif(result):
|
|
118
|
+
return {
|
|
119
|
+
"version": "2.1.0",
|
|
120
|
+
"$schema": "https://json.schemastore.org/sarif-2.1.0.json",
|
|
121
|
+
"runs": [{
|
|
122
|
+
"tool": {
|
|
123
|
+
"driver": {
|
|
124
|
+
"name": "ShieldEnv",
|
|
125
|
+
"informationUri": "https://github.com/ShieldHQ/shieldenv",
|
|
126
|
+
"rules": [
|
|
127
|
+
{"id": finding.type, "name": finding.type}
|
|
128
|
+
for finding in result.violations
|
|
129
|
+
],
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
"results": [
|
|
133
|
+
{
|
|
134
|
+
"ruleId": finding.type,
|
|
135
|
+
"level": "error" if finding.confidence == "high" else "warning",
|
|
136
|
+
"message": {"text": f"Potential secret detected ({finding.confidence} confidence)."},
|
|
137
|
+
"locations": [
|
|
138
|
+
{
|
|
139
|
+
"physicalLocation": {
|
|
140
|
+
"artifactLocation": {
|
|
141
|
+
"uri": _sarif_uri(finding.file)
|
|
142
|
+
},
|
|
143
|
+
"region": {"startLine": finding.line}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
]
|
|
147
|
+
}
|
|
148
|
+
for finding in result.violations
|
|
149
|
+
]
|
|
150
|
+
}]
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _sarif_uri(file_path: str) -> str:
|
|
155
|
+
try:
|
|
156
|
+
normalized = os.path.relpath(file_path)
|
|
157
|
+
except ValueError:
|
|
158
|
+
normalized = file_path
|
|
159
|
+
return Path(normalized).as_posix()
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
@main.command()
|
|
163
|
+
@click.option("--force", is_flag=True, help="Back up and replace an existing pre-commit hook.")
|
|
164
|
+
def install(force):
|
|
165
|
+
"""Automatically install ShieldEnv as a local Git pre-commit hook."""
|
|
166
|
+
git_dir = Path(".git")
|
|
167
|
+
if not git_dir.exists():
|
|
168
|
+
safe_echo("❌ Error: Not a git repository. Run 'git init' first.")
|
|
169
|
+
raise click.exceptions.Exit(1)
|
|
170
|
+
|
|
171
|
+
hook_path = git_dir / "hooks" / "pre-commit"
|
|
172
|
+
if hook_path.exists() and not force:
|
|
173
|
+
raise click.ClickException(
|
|
174
|
+
f"{hook_path} already exists; use --force to replace it with a backup."
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
if hook_path.exists():
|
|
178
|
+
backup_path = hook_path.with_name("pre-commit.shieldenv-backup")
|
|
179
|
+
hook_path.replace(backup_path)
|
|
180
|
+
|
|
181
|
+
if getattr(sys, "frozen", False):
|
|
182
|
+
command = shlex.quote(sys.executable)
|
|
183
|
+
else:
|
|
184
|
+
command = f"{shlex.quote(sys.executable)} -m shieldenv.cli"
|
|
185
|
+
hook_script = f"""#!/bin/sh
|
|
186
|
+
# ShieldEnv automated pre-commit security hook
|
|
187
|
+
exec {command} scan
|
|
188
|
+
"""
|
|
189
|
+
try:
|
|
190
|
+
hook_path.parent.mkdir(parents=True, exist_ok=True)
|
|
191
|
+
with open(hook_path, "w", encoding="utf-8") as f:
|
|
192
|
+
f.write(hook_script)
|
|
193
|
+
|
|
194
|
+
# Make executable on Unix/Mac
|
|
195
|
+
if os.name != 'nt':
|
|
196
|
+
hook_path.chmod(0o755)
|
|
197
|
+
|
|
198
|
+
safe_echo("✅ Successfully installed ShieldEnv git pre-commit hook!")
|
|
199
|
+
except OSError as e:
|
|
200
|
+
safe_echo(f"❌ Failed to install hook: {e}")
|
|
201
|
+
raise click.exceptions.Exit(1)
|
|
202
|
+
|
|
203
|
+
if __name__ == "__main__":
|
|
204
|
+
main()
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
BUILTIN_IGNORES = {
|
|
6
|
+
"node_modules", "venv", ".venv", "dist", "build", ".next",
|
|
7
|
+
"__pycache__", ".pytest_cache", ".ruff_cache", ".git",
|
|
8
|
+
"package-lock.json", "yarn.lock", "pnpm-lock.yaml"
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
def load_shieldignore(root_dir: Path = Path(".")) -> set[str]:
|
|
12
|
+
ignore_set = set(BUILTIN_IGNORES)
|
|
13
|
+
|
|
14
|
+
def add_entries(ignore_file: Path) -> None:
|
|
15
|
+
if not ignore_file.exists():
|
|
16
|
+
return
|
|
17
|
+
try:
|
|
18
|
+
lines = ignore_file.read_text(encoding="utf-8").splitlines()
|
|
19
|
+
except OSError as exc:
|
|
20
|
+
raise RuntimeError(f"Unable to read ignore file: {ignore_file}") from exc
|
|
21
|
+
for line in lines:
|
|
22
|
+
cleaned = line.strip()
|
|
23
|
+
if cleaned and not cleaned.startswith("#"):
|
|
24
|
+
ignore_set.add(cleaned.strip("/"))
|
|
25
|
+
|
|
26
|
+
# 1. Check local project .shieldignore
|
|
27
|
+
add_entries(root_dir / ".shieldignore")
|
|
28
|
+
|
|
29
|
+
# 2. Check global user-level ignore (~/.shieldignore)
|
|
30
|
+
add_entries(Path.home() / ".shieldignore")
|
|
31
|
+
|
|
32
|
+
return ignore_set
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass(frozen=True)
|
|
8
|
+
class PatternRule:
|
|
9
|
+
name: str
|
|
10
|
+
pattern: re.Pattern[str]
|
|
11
|
+
confidence: str
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
PATTERN_RULES = (
|
|
15
|
+
PatternRule("OpenAI API Key", re.compile(r"sk-[a-zA-Z0-9]{32,}", re.IGNORECASE), "high"),
|
|
16
|
+
PatternRule("Anthropic API Key", re.compile(r"sk-ant-[a-zA-Z0-9\-_]{20,}", re.IGNORECASE), "high"),
|
|
17
|
+
PatternRule("Google Gemini / AIza Key", re.compile(r"AIza[0-9A-Za-z\-_]{35}", re.IGNORECASE), "high"),
|
|
18
|
+
PatternRule("GitHub Personal Access Token", re.compile(r"gh[pousr]_[a-zA-Z0-9_]{20,}", re.IGNORECASE), "high"),
|
|
19
|
+
PatternRule("AWS access key", re.compile(r"AKIA[0-9A-Z]{16}"), "high"),
|
|
20
|
+
PatternRule("Azure Storage Key", re.compile(r"DefaultEndpointsProtocol=https?;AccountName=[^;]+;AccountKey=[^;]+", re.IGNORECASE), "high"),
|
|
21
|
+
PatternRule("Stripe Secret Key", re.compile(r"sk_(?:live|test)_[0-9a-zA-Z]{20,}", re.IGNORECASE), "high"),
|
|
22
|
+
PatternRule("Slack Webhook / Token", re.compile(r"xox[baprs]-[0-9a-zA-Z]{10,48}"), "high"),
|
|
23
|
+
PatternRule("Database Connection URL", re.compile(r"(postgres|mysql|mongodb(\+srv)?):\//[^\s]+:[^\s]+@[^\s]+", re.IGNORECASE), "high"),
|
|
24
|
+
PatternRule("JWT", re.compile(r"\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b"), "medium"),
|
|
25
|
+
PatternRule("Generic Private Key", re.compile(r"-----BEGIN (RSA|EC|PRIVATE|OPENSSH) KEY-----"), "high"),
|
|
26
|
+
PatternRule("Generic Secret Assignment", re.compile(
|
|
27
|
+
r"""(?:"?(api_key|secret_key|password|passwd|auth_token|bearer)"?\s*(?:=|:)\s*['"][^'"]{8,200}['"])""",
|
|
28
|
+
re.IGNORECASE,
|
|
29
|
+
), "medium"),
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
SECRET_PATTERNS = {rule.name: rule.pattern for rule in PATTERN_RULES}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
import re
|
|
5
|
+
from collections import Counter
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from fnmatch import fnmatch
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from .config import load_shieldignore
|
|
11
|
+
from .patterns import PATTERN_RULES
|
|
12
|
+
|
|
13
|
+
SECRET_ASSIGNMENT = re.compile(
|
|
14
|
+
r"^\s*(?:(?:export\s+)?(?:const|let|var)\s+)?"
|
|
15
|
+
r"(?P<name>[A-Za-z_][\w.]*)"
|
|
16
|
+
r"(?:\s*:\s*[^=]+)?\s*=\s*(?P<value>.+)$"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class Finding:
|
|
21
|
+
file: str
|
|
22
|
+
line: int
|
|
23
|
+
type: str
|
|
24
|
+
snippet: str
|
|
25
|
+
confidence: str = "medium"
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def rule(self) -> str:
|
|
29
|
+
return self.type
|
|
30
|
+
|
|
31
|
+
@dataclass
|
|
32
|
+
class ScanResult:
|
|
33
|
+
violations: list[Finding] = field(default_factory=list)
|
|
34
|
+
files_scanned: int = 0
|
|
35
|
+
errors: list[str] = field(default_factory=list)
|
|
36
|
+
suppressed: list[dict[str, str | int]] = field(default_factory=list)
|
|
37
|
+
ignored: list[str] = field(default_factory=list)
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def findings(self) -> list[Finding]:
|
|
41
|
+
return self.violations
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def has_secrets(self) -> bool:
|
|
45
|
+
return len(self.violations) > 0
|
|
46
|
+
|
|
47
|
+
EXCLUDE_EXTENSIONS = {
|
|
48
|
+
".png", ".jpg", ".jpeg", ".ico", ".pdf", ".lock", ".zip", ".tar", ".gz",
|
|
49
|
+
".pyc", ".woff", ".woff2", ".bin", ".md", ".markdown", ".txt"
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
def calculate_entropy(data: str) -> float:
|
|
53
|
+
"""Calculates Shannon entropy to detect randomized strings/secrets."""
|
|
54
|
+
if not data:
|
|
55
|
+
return 0.0
|
|
56
|
+
counts = Counter(data)
|
|
57
|
+
return -sum((count / len(data)) * math.log2(count / len(data)) for count in counts.values())
|
|
58
|
+
|
|
59
|
+
def redact(text: str, match: re.Match[str] | None = None) -> str:
|
|
60
|
+
return "[REDACTED]"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def scan_file_details(file_path: Path) -> tuple[list[Finding], list[dict[str, str | int]]]:
|
|
64
|
+
violations = []
|
|
65
|
+
suppressed = []
|
|
66
|
+
file_str = str(file_path).lower()
|
|
67
|
+
|
|
68
|
+
if ".test." in file_str or ".spec." in file_str or "tests" in file_path.parts:
|
|
69
|
+
return violations, suppressed
|
|
70
|
+
|
|
71
|
+
try:
|
|
72
|
+
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
|
73
|
+
for line_no, line in enumerate(f, 1):
|
|
74
|
+
stripped = line.strip()
|
|
75
|
+
lower_line = stripped.lower()
|
|
76
|
+
suppressed_line = "shieldenv: ignore" in lower_line
|
|
77
|
+
|
|
78
|
+
if stripped.startswith(("#", "//")) or len(stripped) < 8:
|
|
79
|
+
continue
|
|
80
|
+
|
|
81
|
+
if any(term in lower_line for term in ["example", "placeholder", "dummy", "changeme", "your_", "secret123"]):
|
|
82
|
+
continue
|
|
83
|
+
|
|
84
|
+
if "{{" in stripped or "}}" in stripped or "{%" in stripped or "%}" in stripped:
|
|
85
|
+
continue
|
|
86
|
+
|
|
87
|
+
if "${" in stripped or "$(" in stripped:
|
|
88
|
+
continue
|
|
89
|
+
|
|
90
|
+
if (
|
|
91
|
+
any(safe_call in lower_line for safe_call in ["os.getenv", "settings.", "process.env", "config."])
|
|
92
|
+
and "=" in stripped
|
|
93
|
+
and not any(quote in stripped for quote in ["'", '"', "`"])
|
|
94
|
+
):
|
|
95
|
+
continue
|
|
96
|
+
|
|
97
|
+
if file_path.suffix in {".tsx", ".jsx", ".html", ".vue"}:
|
|
98
|
+
if any(tag in lower_line for tag in ["<h1", "<h3", "<span", "<div", "routerlink", "tabindex", "datakey"]):
|
|
99
|
+
continue
|
|
100
|
+
if "password" in lower_line and not "=" in stripped:
|
|
101
|
+
continue
|
|
102
|
+
|
|
103
|
+
if len(stripped) > 300:
|
|
104
|
+
continue
|
|
105
|
+
|
|
106
|
+
matched = False
|
|
107
|
+
for rule in PATTERN_RULES:
|
|
108
|
+
match = rule.pattern.search(line)
|
|
109
|
+
if match:
|
|
110
|
+
finding = Finding(
|
|
111
|
+
file=str(file_path),
|
|
112
|
+
line=line_no,
|
|
113
|
+
type=rule.name,
|
|
114
|
+
snippet=redact(stripped, match),
|
|
115
|
+
confidence=rule.confidence,
|
|
116
|
+
)
|
|
117
|
+
if suppressed_line:
|
|
118
|
+
suppressed.append({"file": str(file_path), "line": line_no, "reason": "inline suppression"})
|
|
119
|
+
else:
|
|
120
|
+
violations.append(finding)
|
|
121
|
+
matched = True
|
|
122
|
+
break
|
|
123
|
+
|
|
124
|
+
if matched:
|
|
125
|
+
continue
|
|
126
|
+
|
|
127
|
+
assignment = SECRET_ASSIGNMENT.match(stripped)
|
|
128
|
+
if assignment and any(
|
|
129
|
+
word in assignment.group("name").lower()
|
|
130
|
+
for word in ["token", "secret", "password", "api_key", "secret_key"]
|
|
131
|
+
):
|
|
132
|
+
value = assignment.group("value").strip()
|
|
133
|
+
literal = re.fullmatch(r"""(['"])(.*?)\1""", value)
|
|
134
|
+
if literal:
|
|
135
|
+
val = literal.group(2)
|
|
136
|
+
if len(val) > 20 and calculate_entropy(val) > 4.5:
|
|
137
|
+
finding = Finding(
|
|
138
|
+
file=str(file_path),
|
|
139
|
+
line=line_no,
|
|
140
|
+
type="High-Entropy Secret / Custom Token",
|
|
141
|
+
snippet=redact(stripped),
|
|
142
|
+
confidence="medium",
|
|
143
|
+
)
|
|
144
|
+
if suppressed_line:
|
|
145
|
+
suppressed.append({"file": str(file_path), "line": line_no, "reason": "inline suppression"})
|
|
146
|
+
else:
|
|
147
|
+
violations.append(finding)
|
|
148
|
+
except (OSError, UnicodeError) as exc:
|
|
149
|
+
raise RuntimeError(f"Unable to scan file: {file_path}") from exc
|
|
150
|
+
return violations, suppressed
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def scan_file(file_path: Path) -> list[Finding]:
|
|
154
|
+
return scan_file_details(file_path)[0]
|
|
155
|
+
|
|
156
|
+
def scan_path(root_dir=".", verbose=False):
|
|
157
|
+
root_path = Path(root_dir).resolve()
|
|
158
|
+
if not root_path.exists():
|
|
159
|
+
raise ValueError(f"Scan path does not exist: {root_path}")
|
|
160
|
+
if not root_path.is_dir():
|
|
161
|
+
raise ValueError(f"Scan path is not a directory: {root_path}")
|
|
162
|
+
|
|
163
|
+
ignore_list = load_shieldignore(root_path)
|
|
164
|
+
all_violations = []
|
|
165
|
+
errors = []
|
|
166
|
+
suppressed = []
|
|
167
|
+
ignored = []
|
|
168
|
+
files_count = 0
|
|
169
|
+
|
|
170
|
+
for path in root_path.rglob("*"):
|
|
171
|
+
rel_path = path.relative_to(root_path)
|
|
172
|
+
rel_str = rel_path.as_posix()
|
|
173
|
+
if any(_matches_ignore(rel_str, path.is_dir(), ignore) for ignore in ignore_list):
|
|
174
|
+
if verbose:
|
|
175
|
+
ignored.append(rel_str)
|
|
176
|
+
continue
|
|
177
|
+
|
|
178
|
+
if path.is_dir():
|
|
179
|
+
continue
|
|
180
|
+
if path.suffix.lower() in EXCLUDE_EXTENSIONS:
|
|
181
|
+
continue
|
|
182
|
+
|
|
183
|
+
files_count += 1
|
|
184
|
+
try:
|
|
185
|
+
findings, audits = scan_file_details(path)
|
|
186
|
+
all_violations.extend(findings)
|
|
187
|
+
suppressed.extend(audits)
|
|
188
|
+
except RuntimeError as exc:
|
|
189
|
+
errors.append(str(exc))
|
|
190
|
+
|
|
191
|
+
return ScanResult(
|
|
192
|
+
violations=all_violations,
|
|
193
|
+
files_scanned=files_count,
|
|
194
|
+
errors=errors,
|
|
195
|
+
suppressed=suppressed,
|
|
196
|
+
ignored=ignored,
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _matches_ignore(relative_path: str, is_directory: bool, pattern: str) -> bool:
|
|
201
|
+
normalized = pattern.strip().strip("/")
|
|
202
|
+
if not normalized:
|
|
203
|
+
return False
|
|
204
|
+
if "/" not in normalized and "\\" not in normalized:
|
|
205
|
+
return normalized in Path(relative_path).parts
|
|
206
|
+
if fnmatch(relative_path, normalized) or fnmatch(relative_path, f"{normalized}/**"):
|
|
207
|
+
return True
|
|
208
|
+
return is_directory and relative_path == normalized
|
|
209
|
+
|
|
210
|
+
def scan_directory(root_dir="."):
|
|
211
|
+
return [v.__dict__ for v in scan_path(root_dir).violations]
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: shieldenv
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Local-first secret and AI API key leak prevention CLI
|
|
5
|
+
Requires-Python: >=3.8
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: click>=8.0.0
|
|
9
|
+
Provides-Extra: dev
|
|
10
|
+
Requires-Dist: pytest>=8.0.0; extra == "dev"
|
|
11
|
+
Requires-Dist: ruff>=0.6.0; extra == "dev"
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# ShieldEnv 🛡️
|
|
15
|
+
Local-First Secret Scanning & Pre-Commit Guardraill
|
|
16
|
+
|
|
17
|
+
ShieldEnv is an open-source security engine designed to eliminate accidental credential leaks, API key exposures, and compliance violations before code ever hits version control.
|
|
18
|
+
|
|
19
|
+
### Why Local-First?
|
|
20
|
+
Unlike cloud-based PR scanners that analyze your code after it leaves your machine, ShieldEnv runs entirely local-first. Zero code, environment variables, or tokens ever leave your local environment.
|
|
21
|
+
|
|
22
|
+
## Features
|
|
23
|
+
|
|
24
|
+
- Regex detection for OpenAI, Anthropic, Google, AWS, GitHub, Slack, database URLs, private keys, and generic secret assignments.
|
|
25
|
+
- Provider rules include confidence levels (`high` or `medium`).
|
|
26
|
+
- Entropy-based detection for literal custom tokens.
|
|
27
|
+
- Built-in filtering for placeholders, environment substitutions, templates, and common generated/UI syntax.
|
|
28
|
+
- Project and user-level `.shieldignore` support.
|
|
29
|
+
- Text, JSON, and SARIF output for local and CI use.
|
|
30
|
+
- Optional Git pre-commit hook installation.
|
|
31
|
+
- Redacted findings that never print complete secret values.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
Create an environment and install ShieldEnv:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
python -m venv .venv
|
|
39
|
+
source .venv/bin/activate
|
|
40
|
+
python -m pip install -e ".[dev]"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
On Windows PowerShell, activate with:
|
|
44
|
+
|
|
45
|
+
```powershell
|
|
46
|
+
.\.venv\Scripts\Activate.ps1
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
Scan the current repository:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
shieldenv scan
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Scan another path or emit machine-readable JSON:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
shieldenv scan --path ./src --format json
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Generate GitHub Code Scanning-compatible SARIF:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
shieldenv scan --path . --format sarif > shieldenv.sarif
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Use verbose mode to inspect scan coverage and ignored paths:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
shieldenv scan --verbose
|
|
73
|
+
shieldenv scan --audit --format json
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Exit codes:
|
|
77
|
+
|
|
78
|
+
- `0`: no findings and no scan errors.
|
|
79
|
+
- `1`: one or more potential secrets were found.
|
|
80
|
+
- `2`: one or more files could not be read.
|
|
81
|
+
|
|
82
|
+
Install a Git pre-commit hook from the repository root:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
shieldenv install
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
ShieldEnv will not overwrite an existing hook unless explicitly requested:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
shieldenv install --force
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
With `--force`, an existing hook is saved as `.git/hooks/pre-commit.shieldenv-backup`.
|
|
95
|
+
|
|
96
|
+
## Ignore rules
|
|
97
|
+
|
|
98
|
+
ShieldEnv skips common dependency, build, cache, and lock-file paths by default. Add a project-level `.shieldignore` at the root being scanned, or a global `~/.shieldignore`, with one rule per line:
|
|
99
|
+
|
|
100
|
+
```text
|
|
101
|
+
# Exact directory name anywhere in the tree
|
|
102
|
+
generated
|
|
103
|
+
|
|
104
|
+
# Relative path or glob
|
|
105
|
+
backend/app/api/
|
|
106
|
+
frontend/generated/**
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Blank lines and lines beginning with `#` are ignored. Use ignore rules carefully: excluded files are not scanned. `--verbose` lists ignored paths so exclusions can be reviewed.
|
|
110
|
+
|
|
111
|
+
To suppress a deliberate test value on one line, use the explicit marker:
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
TEST_TOKEN = "not-a-credential" # shieldenv: ignore
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Suppressions are recorded in audit JSON output. Keep suppressions rare and review them during code review.
|
|
118
|
+
|
|
119
|
+
## Development
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
python -m pytest
|
|
123
|
+
ruff check .
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
ShieldEnv is heuristic-based and should complement, not replace, secret rotation and incident-response procedures. Review findings and maintain project-specific ignore rules for generated code.
|
|
127
|
+
|
|
128
|
+
ShieldEnv cannot guarantee detection of every secret format. It combines provider signatures, structured assignment patterns, and entropy heuristics, so unusual encodings may be missed and generated or identifier-like values may still require review. The scanner intentionally reports findings without exposing full values. Run it in CI on Linux, Windows, and macOS; the included workflow tests Python 3.9 through 3.13.
|
|
129
|
+
|
|
130
|
+
Provider signatures are high-confidence indicators; generic assignments and entropy findings are medium-confidence heuristics. A clean scan means no supported rule matched, not that the repository is guaranteed secret-free. Rotate any credential that may have been exposed, and use code review and repository access controls alongside ShieldEnv.
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
Distributed under the MIT License. See [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
shieldenv/__init__.py
|
|
5
|
+
shieldenv/cli.py
|
|
6
|
+
shieldenv/config.py
|
|
7
|
+
shieldenv/patterns.py
|
|
8
|
+
shieldenv/scanner.py
|
|
9
|
+
shieldenv.egg-info/PKG-INFO
|
|
10
|
+
shieldenv.egg-info/SOURCES.txt
|
|
11
|
+
shieldenv.egg-info/dependency_links.txt
|
|
12
|
+
shieldenv.egg-info/entry_points.txt
|
|
13
|
+
shieldenv.egg-info/requires.txt
|
|
14
|
+
shieldenv.egg-info/top_level.txt
|
|
15
|
+
tests/test_cli.py
|
|
16
|
+
tests/test_scanner.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
shieldenv
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
from click.testing import CliRunner
|
|
4
|
+
|
|
5
|
+
from shieldenv.cli import main
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_sarif_output_has_standard_shape(tmp_path) -> None:
|
|
9
|
+
(tmp_path / "config.env").write_text(
|
|
10
|
+
"AWS_KEY=AKIA" + "1234567890ABCDEF\n",
|
|
11
|
+
encoding="utf-8",
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
result = CliRunner().invoke(main, ["scan", "--path", str(tmp_path), "--format", "sarif"])
|
|
15
|
+
|
|
16
|
+
assert result.exit_code == 1
|
|
17
|
+
output = result.stdout
|
|
18
|
+
payload = json.loads(output)
|
|
19
|
+
assert payload["version"] == "2.1.0"
|
|
20
|
+
assert payload["runs"][0]["results"][0]["ruleId"] == "AWS access key"
|
|
21
|
+
assert "AKIA" not in output
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_audit_json_reports_suppression_without_secret(tmp_path) -> None:
|
|
25
|
+
fake_secret = "ghp_" + "abcdefghijklmnopqrstuvwxyz123456"
|
|
26
|
+
(tmp_path / "config.py").write_text(
|
|
27
|
+
f'TOKEN = "{fake_secret}" # shieldenv: ignore\n',
|
|
28
|
+
encoding="utf-8",
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
result = CliRunner().invoke(
|
|
32
|
+
main,
|
|
33
|
+
["scan", "--path", str(tmp_path), "--format", "json", "--audit"],
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
assert result.exit_code == 0
|
|
37
|
+
output = result.stdout
|
|
38
|
+
payload = json.loads(output)
|
|
39
|
+
assert payload["findings"] == []
|
|
40
|
+
assert len(payload["suppressed"]) == 1
|
|
41
|
+
assert fake_secret not in output
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_sarif_output_falls_back_when_relpath_fails(tmp_path, monkeypatch) -> None:
|
|
45
|
+
(tmp_path / "config.env").write_text(
|
|
46
|
+
"AWS_KEY=AKIA" + "1234567890ABCDEF\n",
|
|
47
|
+
encoding="utf-8",
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
def _raise_value_error(*_args, **_kwargs):
|
|
51
|
+
raise ValueError("path is on mount 'C:', start on mount 'D:'")
|
|
52
|
+
|
|
53
|
+
monkeypatch.setattr("shieldenv.cli.os.path.relpath", _raise_value_error)
|
|
54
|
+
result = CliRunner().invoke(main, ["scan", "--path", str(tmp_path), "--format", "sarif"])
|
|
55
|
+
|
|
56
|
+
assert result.exit_code == 1
|
|
57
|
+
payload = json.loads(result.stdout)
|
|
58
|
+
uri = payload["runs"][0]["results"][0]["locations"][0]["physicalLocation"]["artifactLocation"]["uri"]
|
|
59
|
+
assert uri.endswith("/config.env")
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from shieldenv.scanner import scan_path
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def fake_token(prefix: str, suffix: str) -> str:
|
|
8
|
+
return prefix + suffix
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_detects_aws_key(tmp_path: Path) -> None:
|
|
12
|
+
target = tmp_path / "settings.py"
|
|
13
|
+
target.write_text(
|
|
14
|
+
f'AWS_KEY = "{fake_token("AKIA", "1234567890ABCDEF")}"\n',
|
|
15
|
+
encoding="utf-8",
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
result = scan_path(tmp_path)
|
|
19
|
+
|
|
20
|
+
assert result.has_secrets
|
|
21
|
+
assert result.findings[0].rule == "AWS access key"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_respects_shieldignore(tmp_path: Path) -> None:
|
|
25
|
+
ignored = tmp_path / "secrets.env"
|
|
26
|
+
ignored.write_text(
|
|
27
|
+
f'TOKEN="{fake_token("ghp_", "abcdefghijklmnopqrstuvwxyz")}"\n',
|
|
28
|
+
encoding="utf-8",
|
|
29
|
+
)
|
|
30
|
+
(tmp_path / ".shieldignore").write_text("secrets.env\n", encoding="utf-8")
|
|
31
|
+
|
|
32
|
+
result = scan_path(tmp_path)
|
|
33
|
+
|
|
34
|
+
assert not result.has_secrets
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_scans_only_text_files(tmp_path: Path) -> None:
|
|
38
|
+
(tmp_path / "image.bin").write_bytes(b"\xff\xfe\x00")
|
|
39
|
+
|
|
40
|
+
result = scan_path(tmp_path)
|
|
41
|
+
|
|
42
|
+
assert result.files_scanned == 0
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_entropy_detection_requires_secret_named_literal_assignment(tmp_path: Path) -> None:
|
|
46
|
+
target = tmp_path / "source.ts"
|
|
47
|
+
target.write_text(
|
|
48
|
+
'const token = "x9Z!mQ8#vL2$pK5_wR7%tY4&hN1-jB3*fC6"\n'
|
|
49
|
+
'const loginPassword = getPassword(token)\n'
|
|
50
|
+
'const value = `reset-password?token=${token}`\n'
|
|
51
|
+
'const passwordInput = React.forwardRef<HTMLInputElement, Props>(fn)\n',
|
|
52
|
+
encoding="utf-8",
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
result = scan_path(tmp_path)
|
|
56
|
+
|
|
57
|
+
assert len(result.findings) == 1
|
|
58
|
+
assert result.findings[0].line == 1
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_shieldignore_is_loaded_from_scan_root(tmp_path: Path) -> None:
|
|
62
|
+
ignored_dir = tmp_path / "backend" / "app" / "api"
|
|
63
|
+
ignored_dir.mkdir(parents=True)
|
|
64
|
+
(ignored_dir / "secrets.py").write_text(
|
|
65
|
+
f'TOKEN = "{fake_token("ghp_", "abcdefghijklmnopqrstuvwxyz123456")}"\n',
|
|
66
|
+
encoding="utf-8",
|
|
67
|
+
)
|
|
68
|
+
(tmp_path / ".shieldignore").write_text("backend/app/api/\n", encoding="utf-8")
|
|
69
|
+
|
|
70
|
+
result = scan_path(tmp_path)
|
|
71
|
+
|
|
72
|
+
assert not result.has_secrets
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_scans_json_and_typed_assignments(tmp_path: Path) -> None:
|
|
76
|
+
(tmp_path / "config.json").write_text(
|
|
77
|
+
f'{{"password": "{fake_token("ghp_", "abcdefghijklmnopqrstuvwxyz123456")}"}}\n',
|
|
78
|
+
encoding="utf-8",
|
|
79
|
+
)
|
|
80
|
+
(tmp_path / "config.py").write_text(
|
|
81
|
+
f'password: str = "{fake_token("ghp_", "abcdefghijklmnopqrstuvwxyz123456")}"\n',
|
|
82
|
+
encoding="utf-8",
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
result = scan_path(tmp_path)
|
|
86
|
+
|
|
87
|
+
assert len(result.findings) == 2
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_ignore_names_match_path_segments_not_substrings(tmp_path: Path) -> None:
|
|
91
|
+
(tmp_path / "apiary.py").write_text(
|
|
92
|
+
f'TOKEN = "{fake_token("ghp_", "abcdefghijklmnopqrstuvwxyz123456")}"\n',
|
|
93
|
+
encoding="utf-8",
|
|
94
|
+
)
|
|
95
|
+
(tmp_path / "api").mkdir()
|
|
96
|
+
(tmp_path / "api" / "safe.py").write_text("print('safe')\n", encoding="utf-8")
|
|
97
|
+
(tmp_path / ".shieldignore").write_text("api\n", encoding="utf-8")
|
|
98
|
+
|
|
99
|
+
result = scan_path(tmp_path)
|
|
100
|
+
|
|
101
|
+
assert len(result.findings) == 1
|
|
102
|
+
assert result.findings[0].file.endswith("apiary.py")
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_provider_findings_are_redacted_and_have_confidence(tmp_path: Path) -> None:
|
|
106
|
+
(tmp_path / "providers.env").write_text(
|
|
107
|
+
f'OPENAI_KEY={fake_token("sk-", "abcdefghijklmnopqrstuvwxyz1234567890")}\n'
|
|
108
|
+
f'AWS_KEY={fake_token("AKIA", "1234567890ABCDEF")}\n'
|
|
109
|
+
f'STRIPE_KEY={fake_token("sk_live_", "12345678901234567890")}\n',
|
|
110
|
+
encoding="utf-8",
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
result = scan_path(tmp_path)
|
|
114
|
+
|
|
115
|
+
assert len(result.findings) == 3
|
|
116
|
+
assert all(finding.snippet == "[REDACTED]" for finding in result.findings)
|
|
117
|
+
assert all(finding.confidence == "high" for finding in result.findings)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def test_inline_suppression_is_recorded(tmp_path: Path) -> None:
|
|
121
|
+
(tmp_path / "source.py").write_text(
|
|
122
|
+
f'TOKEN = "{fake_token("ghp_", "abcdefghijklmnopqrstuvwxyz123456")}" # shieldenv: ignore\n',
|
|
123
|
+
encoding="utf-8",
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
result = scan_path(tmp_path)
|
|
127
|
+
|
|
128
|
+
assert not result.findings
|
|
129
|
+
assert result.suppressed[0]["line"] == 1
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def test_structured_config_extensions_are_scanned(tmp_path: Path) -> None:
|
|
133
|
+
(tmp_path / "config.yaml").write_text(
|
|
134
|
+
f'password: {fake_token("ghp_", "abcdefghijklmnopqrstuvwxyz123456")}\n',
|
|
135
|
+
encoding="utf-8",
|
|
136
|
+
)
|
|
137
|
+
(tmp_path / "config.toml").write_text(
|
|
138
|
+
f'token = "{fake_token("ghp_", "abcdefghijklmnopqrstuvwxyz123456")}"\n',
|
|
139
|
+
encoding="utf-8",
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
result = scan_path(tmp_path)
|
|
143
|
+
|
|
144
|
+
assert len(result.findings) == 2
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def test_large_repository_scan_completes(tmp_path: Path) -> None:
|
|
148
|
+
for index in range(250):
|
|
149
|
+
(tmp_path / f"module_{index}.py").write_text(
|
|
150
|
+
"def health_check():\n return True\n",
|
|
151
|
+
encoding="utf-8",
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
started = time.perf_counter()
|
|
155
|
+
result = scan_path(tmp_path)
|
|
156
|
+
elapsed = time.perf_counter() - started
|
|
157
|
+
|
|
158
|
+
assert result.files_scanned == 250
|
|
159
|
+
assert not result.findings
|
|
160
|
+
assert elapsed < 2.0
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def test_provider_fixture_detects_and_redacts_all_supported_rules(tmp_path: Path) -> None:
|
|
164
|
+
target = tmp_path / "providers.env"
|
|
165
|
+
target.write_text(
|
|
166
|
+
f'OPENAI={fake_token("sk-", "abcdefghijklmnopqrstuvwxyz1234567890")}\n'
|
|
167
|
+
f'ANTHROPIC={fake_token("sk-ant-", "abcdefghijklmnopqrstuvwxyz")}\n'
|
|
168
|
+
f'GOOGLE={fake_token("AIza", "12345678901234567890123456789012345")}\n'
|
|
169
|
+
f'AWS={fake_token("AKIA", "1234567890ABCDEF")}\n'
|
|
170
|
+
f'GITHUB={fake_token("ghp_", "abcdefghijklmnopqrstuvwxyz123456")}\n'
|
|
171
|
+
f'SLACK={fake_token("xoxb-", "1234567890-abcdefghijklmnop")}\n'
|
|
172
|
+
f'STRIPE={fake_token("sk_live_", "12345678901234567890")}\n',
|
|
173
|
+
encoding="utf-8",
|
|
174
|
+
)
|
|
175
|
+
findings = scan_file_for_fixture(target)
|
|
176
|
+
|
|
177
|
+
assert len(findings) == 7
|
|
178
|
+
assert {finding.confidence for finding in findings} == {"high"}
|
|
179
|
+
assert all(finding.snippet == "[REDACTED]" for finding in findings)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def scan_file_for_fixture(path: Path):
|
|
183
|
+
from shieldenv.scanner import scan_file_details
|
|
184
|
+
|
|
185
|
+
findings, _ = scan_file_details(path)
|
|
186
|
+
return findings
|