leakwarden 0.2.0__py3-none-any.whl

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.
@@ -0,0 +1,120 @@
1
+ Metadata-Version: 2.4
2
+ Name: leakwarden
3
+ Version: 0.2.0
4
+ Summary: Find leaked API keys, tokens and passwords in your code and git history before you deploy
5
+ Author: Leon Krasnik
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://leakwarden.dev
8
+ Project-URL: Source, https://github.com/CrazyJinji/leakwarden
9
+ Keywords: security,secrets,scanner,git,supabase,ai
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Environment :: Console
12
+ Classifier: Topic :: Security
13
+ Requires-Python: >=3.8
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Dynamic: license-file
17
+
18
+ # LeakWarden
19
+
20
+ Find leaked API keys, tokens and passwords in your code and in your git history before you deploy. One Python file, standard library only, runs on your machine.
21
+
22
+ It is built for projects made with Claude Code, Cursor and Lovable, where a key often lands in a commit before anyone notices. Deleting the key from the file does not help if it is still in an old commit, so LeakWarden reads the history too.
23
+
24
+ This is the free edition, MIT licensed. [LeakWarden Pro](https://leakwarden.dev) adds checks for mistakes that are specific to AI-built apps (see the end of this page).
25
+
26
+ ## Run it
27
+
28
+ ```
29
+ curl -O https://leakwarden.dev/download/leakwarden.py
30
+ python3 leakwarden.py /path/to/your/project
31
+ ```
32
+
33
+ You need Python 3.8 or newer. The history scan needs `git`. The SHA-256 of the current file is on [leakwarden.dev](https://leakwarden.dev#free) if you want to check the download.
34
+
35
+ ## What it finds
36
+
37
+ - API keys and tokens: OpenAI, Anthropic, OpenRouter, Stripe, GitHub, Slack, SendGrid, AWS, Google, Polar, Supabase secret and `service_role` keys, private key blocks.
38
+ - Hard-coded passwords and secret-looking values, and database URLs with a password in them.
39
+ - Keys that were committed and later deleted. It reads the added lines of the last 500 commits by default.
40
+ - `.env` files that are tracked by git, or present and not covered by `.gitignore`. A committed `.env` that only holds browser-public values (`VITE_SUPABASE_URL`, `..._ANON_KEY`) is reported as low.
41
+
42
+ Keys that are public by design, such as the Supabase anon key and Stripe publishable keys, are not reported.
43
+
44
+ ## Example
45
+
46
+ ```
47
+ leakwarden 0.2.0 (free edition) ./my-app
48
+
49
+ Coverage
50
+ - secrets (working tree): checked 12 text files
51
+ - secrets (git history): checked 2 commit(s) (limit 500), added lines only
52
+ ...
53
+
54
+ Findings: 4 high, 1 medium, 0 low, 0 info
55
+
56
+ [HIGH] Stripe live secret key (secret.stripe_live)
57
+ .env:1
58
+ > STRIPE_SECRET_KEY=[REDACTED:redacted]
59
+ Fix: Rotate this credential now, then load it from an environment variable or a secret manager.
60
+
61
+ [HIGH] GitHub token (in git history) (secret.github) [history 4b9b722b]
62
+ config.js:1
63
+ > export const githubToken = '[REDACTED:redacted]'
64
+ Fix: Rotate this credential now. Rewriting history (git filter-repo) only helps after the key is rotated.
65
+
66
+ ...
67
+ ```
68
+
69
+ The report also lists the areas it did not check, so a quiet report is not mistaken for a safe one.
70
+
71
+ ## Options
72
+
73
+ ```
74
+ --format text|json|markdown report format (default: text)
75
+ --output FILE write the report to a file
76
+ --min-severity LEVEL hide findings below info, low, medium or high (default: low)
77
+ --fail-on LEVEL exit with status 1 at or above this level, or never (default: high)
78
+ --no-history skip the git history scan
79
+ --max-commits N how many commits to read (default: 500)
80
+ --history-range BASE..HEAD read only these commits, for example in CI on a pull request
81
+ ```
82
+
83
+ Exit status is 0 when nothing reaches `--fail-on`, 1 when something does, and 2 on a usage error.
84
+
85
+ ## Ignoring a finding
86
+
87
+ Put `leakwarden:ignore` anywhere on a line to skip it, in the working tree and in history.
88
+
89
+ Or add a `.leakwardenignore` file at the project root. One entry per line: a path glob, a rule filter, or both.
90
+
91
+ ```
92
+ examples/*
93
+ rule:secret.stripe_test
94
+ rule:secret.generic src/fixtures/*
95
+ ```
96
+
97
+ ## What it does not do
98
+
99
+ - It never prints a secret value. Every report format redacts them.
100
+ - It does not run the project's code, and it runs `git` with hooks and fsmonitor turned off, so it is safe to point at a repository you do not trust.
101
+ - It makes no network requests.
102
+
103
+ It is a heuristic scanner. A clean report does not prove a project is secure, and it will miss secrets in formats it has no rule for. It does not check dependencies for known vulnerabilities.
104
+
105
+ ## LeakWarden Pro
106
+
107
+ Pro adds the checks that matter for apps built with AI coding tools:
108
+
109
+ - Supabase row level security in your migrations: tables without RLS, policies that allow everything, views that bypass RLS
110
+ - Webhook handlers with no signature check, prices taken from the client, paywalls decided in the browser
111
+ - Secret keys exposed to the browser through `NEXT_PUBLIC_` or `VITE_` variables, and the service role key in client code
112
+ - CORS, Django settings, committed databases and risky code (`eval`, raw HTML, SQL built from strings)
113
+ - A Claude Code skill that reads the report and helps you fix each finding
114
+ - A GitHub Actions workflow that checks every pull request and annotates the lines
115
+
116
+ $29 one-time at [leakwarden.dev](https://leakwarden.dev).
117
+
118
+ ## License
119
+
120
+ MIT, see [LICENSE](LICENSE).
@@ -0,0 +1,7 @@
1
+ leakwarden.py,sha256=0Rmwr3zlXASmXYws0WCNL8wZheCfnebbaMg4kZ71XXU,38506
2
+ leakwarden-0.2.0.dist-info/licenses/LICENSE,sha256=Mht_6mdviiQSZ1ZHG92S9jp73PZ7YyvOEFq59Ed8mck,1069
3
+ leakwarden-0.2.0.dist-info/METADATA,sha256=tuCvqzWWn1k1cwHezJ2DOwrOmRFO0L4pvmIZiPRRLB8,5201
4
+ leakwarden-0.2.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
5
+ leakwarden-0.2.0.dist-info/entry_points.txt,sha256=eVVF1NeL5x0HHrwcOnzH-T61ek8Ueqx5_O39aCAEPs4,47
6
+ leakwarden-0.2.0.dist-info/top_level.txt,sha256=dfj-DdgG7XME22sMZW_UjVPSUIoLDfZHJai6obgV4Bg,11
7
+ leakwarden-0.2.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ leakwarden = leakwarden:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Leon Krasnik
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 @@
1
+ leakwarden
leakwarden.py ADDED
@@ -0,0 +1,885 @@
1
+ #!/usr/bin/env python3
2
+ # SPDX-License-Identifier: MIT
3
+ # LeakWarden free edition. Copyright (c) 2026 Leon Krasnik. License text: https://leakwarden.dev/download/LICENSE
4
+ """leakwarden (free edition): find leaked secrets before you deploy.
5
+
6
+ Single file, standard library only, Python 3.8+.
7
+
8
+ The free edition finds API keys, tokens, passwords and database URLs in your
9
+ files and in git history (keys you deleted but never rotated), and .env files
10
+ that are committed or not ignored. LeakWarden Pro (https://leakwarden.dev) adds
11
+ Supabase row level security, webhooks, payments, keys exposed to the browser,
12
+ framework settings, a Claude Code skill and a GitHub Actions workflow.
13
+
14
+ It is a heuristic scanner. A clean report does not mean the project is secure,
15
+ and the report says which areas it did not assess.
16
+ """
17
+ from __future__ import annotations
18
+
19
+ import argparse
20
+ import base64
21
+ import fnmatch
22
+ import hashlib
23
+ import json
24
+ import math
25
+ import os
26
+ import re
27
+ import subprocess
28
+ import stat
29
+ import sys
30
+ from dataclasses import dataclass, asdict, field
31
+
32
+ __version__ = "0.2.0"
33
+ EDITION = "free"
34
+
35
+ SEVERITIES = ["info", "low", "medium", "high"]
36
+ SEV_RANK = {s: i for i, s in enumerate(SEVERITIES)}
37
+
38
+ SKIP_DIRS = {
39
+ ".git", "node_modules", ".next", ".nuxt", ".svelte-kit", "dist", "build", "out",
40
+ ".venv", "venv", "__pycache__", ".mypy_cache", ".pytest_cache", "coverage",
41
+ ".turbo", ".vercel", ".cache", "vendor", "target", ".idea", ".vscode",
42
+ }
43
+ SKIP_FILES = {
44
+ "package-lock.json", "yarn.lock", "pnpm-lock.yaml", "poetry.lock", "Cargo.lock",
45
+ "composer.lock", "Gemfile.lock", "bun.lockb", "uv.lock", "go.sum",
46
+ }
47
+ SKIP_SUFFIXES = (
48
+ ".min.js", ".min.css", ".map", ".png", ".jpg", ".jpeg", ".gif", ".webp", ".ico",
49
+ ".svg", ".pdf", ".zip", ".gz", ".tar", ".woff", ".woff2", ".ttf", ".eot", ".mp4",
50
+ ".mp3", ".mov", ".wasm", ".so", ".dll", ".exe", ".class", ".jar", ".pyc",
51
+ )
52
+ CODE_SUFFIXES = (
53
+ ".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".vue", ".svelte", ".astro",
54
+ ".py", ".go", ".rb", ".php", ".java", ".cs", ".kt", ".swift",
55
+ )
56
+ JS_SUFFIXES = (".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".vue", ".svelte", ".astro")
57
+ MAX_FILE_BYTES = 1_000_000
58
+ MAX_LINE_CHARS = 2000
59
+
60
+
61
+ @dataclass
62
+ class Finding:
63
+ rule: str
64
+ severity: str
65
+ title: str
66
+ file: str
67
+ line: int
68
+ evidence: str
69
+ advice: str
70
+ source: str = "tree" # tree | history
71
+ commit: str = ""
72
+ vh: str = field(default="", repr=False) # hash of a secret value, never printed
73
+
74
+
75
+ # ----------------------------------------------------------------------------
76
+ # helpers
77
+ # ----------------------------------------------------------------------------
78
+
79
+ def sha(value: str) -> str:
80
+ return hashlib.sha256(value.encode("utf-8", "replace")).hexdigest()[:16]
81
+
82
+
83
+ def entropy(s: str) -> float:
84
+ if not s:
85
+ return 0.0
86
+ counts = {}
87
+ for ch in s:
88
+ counts[ch] = counts.get(ch, 0) + 1
89
+ n = len(s)
90
+ return -sum((c / n) * math.log(c / n, 2) for c in counts.values())
91
+
92
+
93
+ PLACEHOLDER_WORDS = (
94
+ "example", "your", "xxxx", "changeme", "placeholder", "dummy", "sample", "redacted",
95
+ "<", ">", "${", "{{", "%s", "todo", "insert", "replace", "fake",
96
+ )
97
+
98
+
99
+ def looks_placeholder(value: str) -> bool:
100
+ low = value.lower()
101
+ if any(w in low for w in PLACEHOLDER_WORDS):
102
+ return True
103
+ if len(set(value)) <= 3:
104
+ return True
105
+ return False
106
+
107
+
108
+ def redact(line: str, spans, rule: str) -> str:
109
+ out = line
110
+ for start, end in sorted(spans, reverse=True):
111
+ out = out[:start] + "[REDACTED:" + rule + "]" + out[end:]
112
+ out = out.strip()
113
+ if len(out) > 160:
114
+ out = out[:157] + "..."
115
+ return out
116
+
117
+
118
+ GIT_SAFE = ["-c", "core.fsmonitor=false", "-c", "core.quotepath=false", "-c", "core.hooksPath=" + os.devnull,
119
+ "-c", "log.showSignature=false", "-c", "diff.external=", "-c", "gpg.program=false",
120
+ "-c", "gpg.ssh.program=false", "-c", "gpg.x509.program=false", "-c", "gpg.openpgp.program=false",
121
+ "-c", "core.pager=cat", "-c", "core.sshCommand=false"]
122
+
123
+
124
+ def git_env():
125
+ env = {k: v for k, v in os.environ.items() if not k.startswith("GIT_")}
126
+ env.update(GIT_CONFIG_NOSYSTEM="1", GIT_TERMINAL_PROMPT="0", GIT_OPTIONAL_LOCKS="0")
127
+ return env
128
+
129
+
130
+ def redact_all(line: str) -> str:
131
+ if len(line) > MAX_LINE_CHARS:
132
+ line = line[:MAX_LINE_CHARS]
133
+ return redact(line, [h[3] for h in scan_line_for_secrets(line, False)], "redacted")
134
+
135
+
136
+ def run_git(root: str, args, timeout: int = 120):
137
+ cmd = ["git", "-C", root] + GIT_SAFE + list(args)
138
+ try:
139
+ res = subprocess.run(cmd, capture_output=True, timeout=timeout, check=False, env=git_env())
140
+ except (OSError, subprocess.SubprocessError):
141
+ return None
142
+ if res.returncode != 0:
143
+ return None
144
+ return res.stdout.decode("utf-8", "replace")
145
+
146
+
147
+ def unquote_git_path(p: str) -> str:
148
+ """Undo git's C-style quoting and the trailing TAB it adds after names with spaces."""
149
+ if p.startswith('"') and p.endswith('"') and len(p) >= 2:
150
+ body = p[1:-1]
151
+ esc = {"t": "\t", "n": "\n", '"': '"', "\\": "\\"}
152
+ raw = bytearray()
153
+ i = 0
154
+ while i < len(body):
155
+ c = body[i]
156
+ if c == "\\" and i + 1 < len(body):
157
+ if body[i + 1] in "01234567" and i + 3 < len(body) + 0 and body[i + 1:i + 4].isdigit():
158
+ raw.append(int(body[i + 1:i + 4], 8))
159
+ i += 4
160
+ continue
161
+ raw += esc.get(body[i + 1], body[i + 1]).encode()
162
+ i += 2
163
+ continue
164
+ raw += c.encode("utf-8", "surrogateescape")
165
+ i += 1
166
+ return raw.decode("utf-8", "replace")
167
+ return p.rstrip("\t")
168
+
169
+
170
+ def is_git_repo(root: str) -> bool:
171
+ return run_git(root, ["rev-parse", "--is-inside-work-tree"]) is not None
172
+
173
+
174
+ def want_file(rel: str) -> bool:
175
+ parts = rel.replace("\\", "/").split("/")
176
+ if any(p in SKIP_DIRS for p in parts[:-1]):
177
+ return False
178
+ name = parts[-1]
179
+ if name in SKIP_FILES:
180
+ return False
181
+ if name.lower().endswith(SKIP_SUFFIXES):
182
+ return False
183
+ return True
184
+
185
+
186
+ def list_files(root: str):
187
+ """Return (relative paths, tracked set or None). Uses git when available so that
188
+ gitignored files are not reported as leaks."""
189
+ files = None
190
+ tracked = None
191
+ if is_git_repo(root):
192
+ out = run_git(root, ["ls-files", "-z", "--cached", "--others", "--exclude-standard"])
193
+ cached = run_git(root, ["ls-files", "-z", "--cached"])
194
+ if out is not None:
195
+ files = [p for p in out.split("\0") if p]
196
+ tracked = set(p for p in (cached or "").split("\0") if p)
197
+ if files is None:
198
+ files = []
199
+ for dirpath, dirnames, filenames in os.walk(root):
200
+ dirnames[:] = [d for d in dirnames if d not in SKIP_DIRS]
201
+ for fn in filenames:
202
+ full = os.path.join(dirpath, fn)
203
+ files.append(os.path.relpath(full, root))
204
+ files = sorted(set(f.replace("\\", "/") for f in files if want_file(f)))
205
+ return files, tracked
206
+
207
+
208
+ def read_text(path: str):
209
+ try:
210
+ st = os.lstat(path)
211
+ if not stat.S_ISREG(st.st_mode) or st.st_size > MAX_FILE_BYTES:
212
+ return None
213
+ fd = os.open(path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0))
214
+ with os.fdopen(fd, "rb") as fh:
215
+ if not stat.S_ISREG(os.fstat(fh.fileno()).st_mode):
216
+ return None
217
+ data = fh.read(MAX_FILE_BYTES + 1)
218
+ if len(data) > MAX_FILE_BYTES:
219
+ return None
220
+ except OSError:
221
+ return None
222
+ if b"\0" in data[:4096]:
223
+ return None
224
+ return data.decode("utf-8", "replace")
225
+
226
+
227
+ # ----------------------------------------------------------------------------
228
+ # secret detection
229
+ # ----------------------------------------------------------------------------
230
+
231
+ # (rule id, title, regex, severity)
232
+ KNOWN_SECRETS = [
233
+ ("secret.aws_access_key", "AWS access key id", r"\b(?:AKIA|ASIA)[0-9A-Z]{16}\b", "high"),
234
+ ("secret.stripe_live", "Stripe live secret key", r"\b(?:sk|rk)_live_[0-9A-Za-z]{16,}\b", "high"),
235
+ ("secret.stripe_test", "Stripe test secret key", r"\bsk_test_[0-9A-Za-z]{16,}\b", "low"),
236
+ ("secret.stripe_webhook", "Stripe webhook signing secret", r"\bwhsec_[0-9A-Za-z+/=]{16,}\b", "high"),
237
+ ("secret.anthropic", "Anthropic API key", r"\bsk-ant-[A-Za-z0-9_-]{32,}\b", "high"),
238
+ ("secret.openrouter", "OpenRouter API key", r"\bsk-or-v1-[0-9a-f]{64}\b", "high"),
239
+ ("secret.openai", "OpenAI API key", r"\bsk-(?!ant-|or-v1-)(?:proj-|svcacct-|admin-)?[A-Za-z0-9_-]{32,}\b", "high"),
240
+ ("secret.github", "GitHub token", r"\b(?:gh[pousr]_[A-Za-z0-9]{36,}|github_pat_[A-Za-z0-9_]{50,})\b", "high"),
241
+ ("secret.slack", "Slack token", r"\bxox[abprs]-[A-Za-z0-9-]{10,}\b", "high"),
242
+ ("secret.sendgrid", "SendGrid API key", r"\bSG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43}\b", "high"),
243
+ ("secret.private_key", "Private key block", r"-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----", "high"),
244
+ ("secret.supabase_secret", "Supabase secret key (sb_secret_)", r"\bsb_secret_[A-Za-z0-9_-]{20,}\b", "high"),
245
+ ("secret.polar", "Polar token or webhook secret", r"\bpolar_(?:oat|whs)_[A-Za-z0-9]{16,}\b", "high"),
246
+ ("secret.google_api_key", "Google API key (may be public by design, check restrictions)",
247
+ r"\bAIza[0-9A-Za-z_-]{35}\b", "low"),
248
+ ]
249
+ KNOWN_SECRETS_C = [(r, t, re.compile(rx), sev) for r, t, rx, sev in KNOWN_SECRETS]
250
+
251
+ JWT_RE = re.compile(r"\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b")
252
+ DB_URL_RE = re.compile(
253
+ r"\b(?:postgres(?:ql)?|mysql|mariadb|mongodb(?:\+srv)?|redis|amqp)://"
254
+ r"([^\s:@/\"'`]+):([^\s@/\"'`]{3,})@([^\s/:\"'`]+)"
255
+ )
256
+ GENERIC_ASSIGN_RE = re.compile(
257
+ r"""(?<![A-Za-z0-9_.-])([A-Za-z0-9_.-]{1,80})["']?\s*[:=]\s*["']([^"'\s]{16,})["']"""
258
+ )
259
+ GENERIC_NAME_RE = re.compile(r"(?i)secret|passw(?:or)?d|token|api[_-]?key|private[_-]?key|auth[_-]?key|access[_-]?key")
260
+ ENVFILE_ASSIGN_RE = re.compile(
261
+ r"""(?im)^\s*(?:export\s+)?([A-Z0-9_]*(?:SECRET|PASSWORD|TOKEN|API_KEY|PRIVATE_KEY|ACCESS_KEY)[A-Z0-9_]*)\s*=\s*([^\s#"']{16,})\s*$"""
262
+ )
263
+ SAFE_DB_HOSTS = {"localhost", "127.0.0.1", "db", "postgres", "mysql", "redis", "host.docker.internal"}
264
+
265
+
266
+ def decode_jwt_role(token: str):
267
+ try:
268
+ payload = token.split(".")[1]
269
+ payload += "=" * (-len(payload) % 4)
270
+ data = json.loads(base64.urlsafe_b64decode(payload.encode()).decode("utf-8", "replace"))
271
+ if isinstance(data, dict) and data.get("iss") == "supabase-demo":
272
+ return None
273
+ return data.get("role") if isinstance(data, dict) else None
274
+ except Exception:
275
+ return None
276
+
277
+
278
+ def generic_value_ok(value: str) -> bool:
279
+ if looks_placeholder(value):
280
+ return False
281
+ if value.startswith(("process.env", "os.environ", "import.meta", "env(", "getenv")):
282
+ return False
283
+ if re.match(r"(?i)https?://", value) or re.match(r"(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-", value):
284
+ return False
285
+ if not (re.search(r"\d", value) and re.search(r"[A-Za-z]", value)):
286
+ return False
287
+ return entropy(value) >= 3.5
288
+
289
+
290
+ PASSWORD_ASSIGN_RE = re.compile(
291
+ r"""(?i)(?<![A-Za-z0-9_.-])([A-Za-z0-9_.-]{0,60}?(?:password|passwd|pwd))["']?\s*[:=]\s*["']([^"'\s]{6,64})["']"""
292
+ )
293
+ ENV_PASSWORD_RE = re.compile(
294
+ r"""(?i)^\s*(?:export\s+)?([A-Z0-9_]*(?:PASSWORD|PASSWD|PWD))\s*=\s*["']?([^\s#"']{6,64})["']?\s*$"""
295
+ )
296
+ SEED_PATH_RE = re.compile(r"(^|/)(seeds?|seeders?|fixtures?)(/|\.)|(^|/)seed[^/]*\.[a-z]+$")
297
+ PASSWORD_PREFIXES = ("$", "{", "%", "/", "#", ".", "<", "process.env", "os.environ", "os.getenv", "getenv",
298
+ "env(", "import.meta", "settings.", "config.")
299
+
300
+
301
+ def password_value_ok(value: str) -> bool:
302
+ """A literal that looks like a real password: not a placeholder or a reference, and it mixes
303
+ letters with digits or symbols (pure words are usually labels or field names)."""
304
+ if looks_placeholder(value) or value.startswith(PASSWORD_PREFIXES):
305
+ return False
306
+ if re.match(r"(?i)https?://", value) or value.lower() in ("password", "passwd", "secret"):
307
+ return False
308
+ if not re.search(r"[A-Za-z]", value):
309
+ return False
310
+ # identifiers, enum values and autocomplete tokens: new-password, UPDATE_PASSWORD, current_password
311
+ if re.fullmatch(r"[a-z]+(?:[-_][a-z0-9]+)+|[A-Z][A-Z0-9_]*", value):
312
+ return False
313
+ if re.fullmatch(r"(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", value):
314
+ return False
315
+ return bool(re.search(r"[0-9]", value) or re.search(r"[^A-Za-z0-9]", value))
316
+
317
+
318
+ def scan_line_for_secrets(line: str, is_env_file: bool, known_only: bool = False):
319
+ """Yield (rule, severity, title, span, value) for every secret-looking thing on a line."""
320
+ if len(line) > MAX_LINE_CHARS:
321
+ return
322
+ known = []
323
+ for rule, title, rx, sev in KNOWN_SECRETS_C:
324
+ for m in rx.finditer(line):
325
+ val = m.group(0)
326
+ low = val.lower()
327
+ if "example" in low or "xxxx" in low:
328
+ continue
329
+ known.append(m.span())
330
+ yield rule, sev, title, m.span(), val
331
+ for m in JWT_RE.finditer(line):
332
+ role = decode_jwt_role(m.group(0))
333
+ if role == "service_role":
334
+ yield ("secret.supabase_service_role", "high",
335
+ "Supabase service_role JWT (bypasses row level security)", m.span(), m.group(0))
336
+ for m in DB_URL_RE.finditer(line):
337
+ user, pw, host = m.group(1), m.group(2), m.group(3)
338
+ if looks_placeholder(pw) or pw.startswith(("$", "{", "%")) or pw.lower() in {"password", "pass", "postgres", "root", "admin"}:
339
+ continue
340
+ sev = "low" if host.lower() in SAFE_DB_HOSTS else "high"
341
+ yield "secret.db_url", sev, "Database connection string with a password", m.span(), m.group(0)
342
+ if known_only:
343
+ return
344
+ spans = set()
345
+
346
+ def overlaps_known(sp):
347
+ return any(sp[0] < k[1] and k[0] < sp[1] for k in known)
348
+
349
+ for m in GENERIC_ASSIGN_RE.finditer(line):
350
+ val = m.group(2)
351
+ if GENERIC_NAME_RE.search(m.group(1)) and generic_value_ok(val) and not overlaps_known(m.span(2)):
352
+ spans.add(m.span(2))
353
+ yield ("secret.generic", "medium",
354
+ "Hard-coded value assigned to '%s'" % m.group(1), m.span(2), val)
355
+ for m in PASSWORD_ASSIGN_RE.finditer(line):
356
+ if m.span(2) not in spans and password_value_ok(m.group(2)):
357
+ spans.add(m.span(2))
358
+ yield ("secret.hardcoded_password", "high",
359
+ "Hard-coded password assigned to '%s'" % m.group(1), m.span(2), m.group(2))
360
+ if is_env_file:
361
+ m = ENVFILE_ASSIGN_RE.match(line)
362
+ if m and generic_value_ok(m.group(2)) and not overlaps_known(m.span(2)):
363
+ spans.add(m.span(2))
364
+ yield ("secret.generic", "medium",
365
+ "Secret-looking value in env file ('%s')" % m.group(1), m.span(2), m.group(2))
366
+ m = ENV_PASSWORD_RE.match(line)
367
+ if m and m.span(2) not in spans and password_value_ok(m.group(2)):
368
+ yield ("secret.hardcoded_password", "high",
369
+ "Password in env file ('%s')" % m.group(1), m.span(2), m.group(2))
370
+
371
+
372
+ SECRET_ADVICE = ("Rotate this credential now, then load it from an environment variable or a secret "
373
+ "manager. Deleting it from the file does not make it safe, it may already be in git history.")
374
+
375
+
376
+ # ----------------------------------------------------------------------------
377
+ # project detection
378
+ # ----------------------------------------------------------------------------
379
+
380
+ @dataclass
381
+ class Project:
382
+ uses_supabase: bool = False
383
+ django: bool = False
384
+ next_js: bool = False
385
+ client_only: bool = False # vite, cra, expo, plain SPA frameworks
386
+ has_sql: bool = False
387
+ deps: set = field(default_factory=set)
388
+
389
+
390
+ def detect_project(root: str, files) -> Project:
391
+ p = Project()
392
+ pkg = os.path.join(root, "package.json")
393
+ if os.path.isfile(pkg):
394
+ txt = read_text(pkg) or ""
395
+ try:
396
+ data = json.loads(txt)
397
+ for key in ("dependencies", "devDependencies"):
398
+ p.deps.update((data.get(key) or {}).keys())
399
+ except ValueError:
400
+ pass
401
+ p.uses_supabase = any(d.startswith("@supabase/") for d in p.deps)
402
+ p.django = "manage.py" in files or any(f.endswith("/manage.py") for f in files)
403
+ p.next_js = "next" in p.deps
404
+ p.client_only = any(d in p.deps for d in ("vite", "react-scripts", "expo", "@vitejs/plugin-react")) and not p.next_js
405
+ for f in files:
406
+ if f.lower().endswith(".sql"):
407
+ p.has_sql = True
408
+ if f.startswith("supabase/"):
409
+ p.uses_supabase = True
410
+ return p
411
+
412
+
413
+ # ----------------------------------------------------------------------------
414
+ # per-file rules
415
+ # ----------------------------------------------------------------------------
416
+
417
+ PEM_BODY_RE = re.compile(r"PRIVATE KEY(?: BLOCK)?-----[\s\\n]*[A-Za-z0-9+/=]{40,}")
418
+ TEST_PATH_RE = re.compile(r"(^|/)(__tests__|tests?|spec|mocks?|fixtures?|examples?|docs?)(/|$)|\.(test|spec)\.", re.I)
419
+
420
+
421
+ def is_env_filename(rel: str) -> bool:
422
+ base = os.path.basename(rel)
423
+ return bool(re.match(r"^\.env(\..+)?$", base))
424
+
425
+
426
+ def is_env_template(rel: str) -> bool:
427
+ return os.path.basename(rel).endswith((".example", ".sample", ".template", ".dist", ".defaults"))
428
+
429
+
430
+ PUBLIC_ENV_PREFIX_RE = re.compile(r"^(?:VITE|NEXT_PUBLIC|NUXT_PUBLIC|REACT_APP|EXPO_PUBLIC|GATSBY|PUBLIC)_")
431
+ SENSITIVE_ENV_NAME_RE = re.compile(r"KEY|SECRET|TOKEN|PASS|PWD|PRIVATE|CREDENTIAL|DSN|AUTH")
432
+ PUBLIC_SAFE_ENV_NAME_RE = re.compile(r"(?:ANON_KEY|PUBLISHABLE_KEY|PUBLIC_KEY)$")
433
+ ENV_KEY_RE = re.compile(r"^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=")
434
+
435
+
436
+ def env_name_is_public(name: str) -> bool:
437
+ """A browser-public variable that is not secret by name (VITE_SUPABASE_URL, NEXT_PUBLIC_..._ANON_KEY)."""
438
+ name = name.upper()
439
+ if not PUBLIC_ENV_PREFIX_RE.match(name):
440
+ return False
441
+ return not SENSITIVE_ENV_NAME_RE.search(name) or bool(PUBLIC_SAFE_ENV_NAME_RE.search(name))
442
+
443
+
444
+ def env_text_public_only(text: str) -> bool:
445
+ names = [m.group(1) for m in (ENV_KEY_RE.match(l) for l in text.splitlines()) if m]
446
+ return bool(names) and all(env_name_is_public(n) for n in names)
447
+
448
+
449
+ class Scanner:
450
+ def __init__(self, root: str):
451
+ self.root = os.path.abspath(root)
452
+ self.findings = []
453
+ self.coverage = {}
454
+ self.files = []
455
+ self.tracked = None
456
+ self.project = Project()
457
+ self.webhook_handlers = 0
458
+ self.history_failed = False
459
+ self.ignore_rules = []
460
+
461
+ # -- bookkeeping --------------------------------------------------------
462
+ def add(self, rule, severity, title, rel, line, evidence, advice, source="tree", commit="", vh=""):
463
+ self.findings.append(Finding(rule, severity, title, rel, line, evidence, advice, source, commit, vh))
464
+
465
+ def load_ignore(self):
466
+ path = os.path.join(self.root, ".leakwardenignore")
467
+ txt = read_text(path) if os.path.isfile(path) else None
468
+ for raw in (txt or "").splitlines():
469
+ raw = raw.strip()
470
+ if not raw or raw.startswith("#"):
471
+ continue
472
+ rule = None
473
+ glob = None
474
+ parts = raw.split()
475
+ for part in parts:
476
+ if part.startswith("rule:"):
477
+ rule = part[5:]
478
+ else:
479
+ glob = part
480
+ self.ignore_rules.append((rule, glob))
481
+
482
+ def ignored(self, f: Finding) -> bool:
483
+ for rule, glob in self.ignore_rules:
484
+ if rule and not (f.rule == rule or f.rule.startswith(rule.rstrip("*"))):
485
+ continue
486
+ if glob and not fnmatch.fnmatch(f.file, glob):
487
+ continue
488
+ return True
489
+ return False
490
+
491
+ # -- main ---------------------------------------------------------------
492
+ def run(self, history: bool, max_commits: int, history_range: str = ""):
493
+ self.load_ignore()
494
+ self.files, self.tracked = list_files(self.root)
495
+ self.project = detect_project(self.root, self.files)
496
+ texts = {}
497
+ scanned = 0
498
+ root_real = os.path.realpath(self.root)
499
+ for rel in self.files:
500
+ full = os.path.join(self.root, rel)
501
+ real = os.path.realpath(full)
502
+ if real != full and not real.startswith(root_real + os.sep):
503
+ continue
504
+ text = read_text(full)
505
+ if text is None:
506
+ continue
507
+ scanned += 1
508
+ texts[rel] = text
509
+ self.scan_file(rel, text)
510
+ self.coverage["secrets (working tree)"] = "checked %d text files" % scanned
511
+ self.check_env_files(texts)
512
+ self.free_coverage()
513
+ if history:
514
+ self.scan_history(max_commits, history_range)
515
+ else:
516
+ self.coverage["secrets (git history)"] = "skipped (--no-history)"
517
+ self.finish()
518
+ return self
519
+
520
+ def free_coverage(self):
521
+ note = "not included in the free edition"
522
+ rls = note
523
+ if self.project.uses_supabase:
524
+ rls += " (this project uses Supabase; LeakWarden Pro checks the RLS policies in your migrations)"
525
+ self.coverage["supabase row level security"] = rls
526
+ self.coverage["webhook handlers"] = note
527
+ self.coverage["other checks"] = (note + ": keys exposed to the browser, payments and paywalls, CORS, "
528
+ "framework settings, committed databases, risky code")
529
+
530
+ def scan_file(self, rel: str, text: str):
531
+ lines = text.splitlines()
532
+ env_file = is_env_filename(rel)
533
+ template = is_env_template(rel)
534
+ low_rel = rel.lower()
535
+ is_code = low_rel.endswith(CODE_SUFFIXES)
536
+ in_tests = bool(TEST_PATH_RE.search(rel)) or bool(re.search(r"\.env\.test$|\.test\.[a-z]+$", low_rel))
537
+ pem_cache = []
538
+
539
+ def pem_ok():
540
+ if not pem_cache:
541
+ pem_cache.append(bool(PEM_BODY_RE.search(text)))
542
+ return pem_cache[0]
543
+
544
+ for i, line in enumerate(lines, 1):
545
+ if "leakwarden:ignore" in line:
546
+ continue
547
+ if len(line) > MAX_LINE_CHARS:
548
+ hits = []
549
+ for a in range(0, len(line), MAX_LINE_CHARS - 200):
550
+ win = line[a:a + MAX_LINE_CHARS]
551
+ for h in scan_line_for_secrets(win, False, known_only=True):
552
+ hits.append((h[0], h[1], h[2], (h[3][0] + a, h[3][1] + a), h[4]))
553
+ seen_sp = set()
554
+ for rule, sev, title, span, val in hits:
555
+ if span in seen_sp or (rule == "secret.private_key" and (in_tests or not pem_ok())):
556
+ continue
557
+ seen_sp.add(span)
558
+ self.add(rule, sev, title, rel, i, redact(line[max(0, span[0] - 40):span[1] + 40], [(min(40, span[0]), min(40, span[0]) + span[1] - span[0])], rule), SECRET_ADVICE, vh=sha(val))
559
+ continue
560
+ hits = list(scan_line_for_secrets(line, env_file and not template, known_only=template))
561
+ ev = redact(line, [h[3] for h in hits], "redacted")
562
+ for rule, sev, title, span, val in hits:
563
+ if rule in ("secret.generic", "secret.hardcoded_password") and in_tests:
564
+ continue
565
+ if rule == "secret.hardcoded_password" and SEED_PATH_RE.search(low_rel):
566
+ sev, title = "low", title + " (seed script)"
567
+ if rule == "secret.private_key" and (in_tests or not pem_ok()):
568
+ continue
569
+ self.add(rule, sev, title, rel, i, ev, SECRET_ADVICE, vh=sha(val))
570
+ def check_env_files(self, texts):
571
+ gitignore = texts.get(".gitignore", "")
572
+ ignored_env = bool(re.search(r"^[ \t]*/?\.env(\*|\.local)?[ \t]*$", gitignore, re.M))
573
+ for rel in self.files:
574
+ if not is_env_filename(rel) or is_env_template(rel):
575
+ continue
576
+ has_secret = any(f.file == rel and f.rule.startswith("secret.") and f.severity != "low" for f in self.findings)
577
+ committed = self.tracked is None or rel in self.tracked
578
+ if self.tracked is not None:
579
+ if rel in self.tracked:
580
+ public_cfg = bool(re.search(r"\.env\.(development|production|test|staging)$", rel.lower())) and not has_secret
581
+ exposed = False
582
+ public_only = not has_secret and not exposed and env_text_public_only(texts.get(rel, ""))
583
+ if public_only:
584
+ self.add("env.committed", "low", "Env file is tracked by git (browser-public values only)",
585
+ rel, 1, "(file contents not shown)",
586
+ "Nothing secret in it today, but the next key someone adds here gets committed too. "
587
+ "Move it to .env.example and add .env to .gitignore.")
588
+ else:
589
+ self.add("env.committed", "high" if has_secret else ("low" if public_cfg else "medium"),
590
+ "Env file is tracked by git", rel, 1, "(file contents not shown)",
591
+ "Remove it from git (git rm --cached), add it to .gitignore, and rotate every value "
592
+ "in it. Commit a .env.example with placeholders instead.")
593
+ elif not ignored_env:
594
+ self.add("env.not_ignored", "medium", "Env file present and .gitignore does not exclude it",
595
+ rel, 1, "(file contents not shown)", "Add .env to .gitignore before the first commit.")
596
+
597
+ def scan_history(self, max_commits: int, history_range: str = ""):
598
+ if not is_git_repo(self.root):
599
+ self.coverage["secrets (git history)"] = "skipped (not a git repository)"
600
+ return
601
+ cmd = ["git", "-C", self.root] + GIT_SAFE + [
602
+ "log", history_range or "--all", "--no-ext-diff", "--no-textconv", "--no-show-signature", "-p", "-U0", "--no-color",
603
+ "--relative", "--format=@@COMMIT %H", "-n", str(max(0, max_commits))]
604
+ try:
605
+ proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, env=git_env())
606
+ except OSError:
607
+ self.coverage["secrets (git history)"] = "skipped (git not available)"
608
+ return
609
+ commits = 0
610
+ seen = set()
611
+ tree_hashes = set(f.vh for f in self.findings if f.vh)
612
+ commit = ""
613
+ cur_file = None
614
+ lineno = 0
615
+ env_seen = set()
616
+ env_public = {}
617
+ left_old = left_new = 0
618
+ for raw in proc.stdout:
619
+ line = raw.decode("utf-8", "replace").rstrip("\n")
620
+ if line.startswith("@@COMMIT "):
621
+ commit = line.split(" ", 1)[1][:8]
622
+ commits += 1
623
+ cur_file = None
624
+ continue
625
+ if left_old <= 0 and left_new <= 0 and line.startswith("+++ "):
626
+ path = unquote_git_path(line[4:])
627
+ cur_file = path[2:] if path.startswith("b/") else None
628
+ if cur_file and not want_file(cur_file):
629
+ cur_file = None
630
+ continue
631
+ if left_old <= 0 and left_new <= 0 and line.startswith("@@ "):
632
+ m = re.match(r"@@ -\d+(?:,(\d+))? \+(\d+)(?:,(\d+))? @@", line)
633
+ if m:
634
+ left_old = int(m.group(1)) if m.group(1) is not None else 1
635
+ left_new = int(m.group(3)) if m.group(3) is not None else 1
636
+ lineno = int(m.group(2)) - 1
637
+ continue
638
+ if line.startswith("-"):
639
+ left_old -= 1
640
+ continue
641
+ if not line.startswith("+"):
642
+ continue
643
+ left_new -= 1
644
+ if cur_file is None:
645
+ continue
646
+ lineno += 1
647
+ added = line[1:]
648
+ if is_env_filename(cur_file) and not is_env_template(cur_file):
649
+ km = ENV_KEY_RE.match(added)
650
+ if km and not env_name_is_public(km.group(1)):
651
+ env_public[cur_file] = False
652
+ else:
653
+ env_public.setdefault(cur_file, True)
654
+ if (is_env_filename(cur_file) and not is_env_template(cur_file) and cur_file not in env_seen
655
+ and not (self.tracked and cur_file in self.tracked)):
656
+ env_seen.add(cur_file)
657
+ self.add("env.in_history", "medium", "Env file was committed at some point", cur_file, lineno,
658
+ "(file contents not shown)",
659
+ "Even if it is deleted now, it is in the history. Rotate any real secret that was in it.",
660
+ source="history", commit=commit)
661
+ if "leakwarden:ignore" in added:
662
+ continue
663
+ for rule, sev, title, span, val in scan_line_for_secrets(added, False, known_only=True):
664
+ if sev == "low":
665
+ continue
666
+ vh = sha(val)
667
+ if vh in tree_hashes:
668
+ continue
669
+ key = (rule, vh, cur_file)
670
+ if key in seen:
671
+ continue
672
+ seen.add(key)
673
+ self.add(rule, sev, title + " (in git history)", cur_file, lineno,
674
+ redact_all(added), SECRET_ADVICE + " Rewriting history (git filter-repo) "
675
+ "only helps after the key is rotated.", source="history", commit=commit, vh=vh)
676
+ proc.stdout.close()
677
+ proc.wait()
678
+ for f in self.findings:
679
+ if f.rule == "env.in_history" and env_public.get(f.file):
680
+ if not any(g.file == f.file and g.source == "history" and g.rule.startswith("secret.") for g in self.findings):
681
+ f.severity = "low"
682
+ f.title = "Env file was committed at some point (browser-public values only)"
683
+ if proc.returncode != 0:
684
+ self.history_failed = True
685
+ self.coverage["secrets (git history)"] = "FAILED: git log could not read %s" % (history_range or "the history")
686
+ return
687
+ where = " in range %s" % history_range if history_range else ""
688
+ self.coverage["secrets (git history)"] = "checked %d commit(s)%s (limit %d), added lines only" % (
689
+ commits, where, max_commits)
690
+
691
+ # -- finish -------------------------------------------------------------
692
+ def finish(self):
693
+ uniq = {}
694
+ for f in self.findings:
695
+ if self.ignored(f):
696
+ continue
697
+ uniq[(f.rule, f.file, f.line, f.source, f.commit, f.vh)] = f
698
+ self.findings = sorted(
699
+ uniq.values(), key=lambda f: (-SEV_RANK[f.severity], f.file, f.line))
700
+
701
+
702
+ # ----------------------------------------------------------------------------
703
+ # SQL helpers
704
+ # ----------------------------------------------------------------------------
705
+
706
+ # ----------------------------------------------------------------------------
707
+ # reporting
708
+ # ----------------------------------------------------------------------------
709
+
710
+ FREE_NOTE = ("Free edition: secrets and .env files, in your files and in git history. LeakWarden Pro adds "
711
+ "Supabase RLS, webhooks, payments, browser-exposed keys, framework settings, the Claude Code "
712
+ "skill and the CI workflow: https://leakwarden.dev")
713
+ DISCLAIMER = (
714
+ "leakwarden is a heuristic scanner. It reports patterns, not proof of an exploit, and it will miss "
715
+ "problems it has no rule for (authorization logic, business logic, dependency vulnerabilities, "
716
+ "infrastructure). A clean report is not a security guarantee."
717
+ )
718
+
719
+
720
+ def edition_label() -> str:
721
+ return " (free edition)" if EDITION == "free" else ""
722
+
723
+
724
+ def summarize(findings):
725
+ counts = {s: 0 for s in SEVERITIES}
726
+ for f in findings:
727
+ counts[f.severity] += 1
728
+ return counts
729
+
730
+
731
+ def to_dict(sc: Scanner, min_sev: str):
732
+ shown = [f for f in sc.findings if SEV_RANK[f.severity] >= SEV_RANK[min_sev]]
733
+ items = []
734
+ for f in shown:
735
+ d = asdict(f)
736
+ d.pop("vh", None)
737
+ items.append(d)
738
+ return {
739
+ "tool": "leakwarden", "version": __version__, "edition": EDITION, "target": sc.root,
740
+ "summary": summarize(shown), "coverage": sc.coverage, "findings": items, "disclaimer": DISCLAIMER,
741
+ }
742
+
743
+
744
+ _CTRL_RE = re.compile("[\x00-\x08\x0b-\x1f\x7f-\x9f\u200e\u200f\u202a-\u202e\u2066-\u2069]")
745
+
746
+
747
+ def clean(s) -> str:
748
+ s = str(s).encode("utf-8", "replace").decode("utf-8")
749
+ return _CTRL_RE.sub(lambda m: "\\x%02x" % ord(m.group(0)) if ord(m.group(0)) < 256 else "\\u%04x" % ord(m.group(0)), s)
750
+
751
+
752
+ def md_escape(s) -> str:
753
+ s = clean(s)
754
+ return re.sub(r"([\\`*_\[\]<>()|#])", r"\\\1", s)
755
+
756
+
757
+ def render_text(data) -> str:
758
+ out = []
759
+ s = data["summary"]
760
+ out.append("leakwarden %s%s %s" % (data["version"], edition_label(), data["target"]))
761
+ out.append("")
762
+ out.append("Coverage")
763
+ for k, v in data["coverage"].items():
764
+ out.append(" - %s: %s" % (k, v))
765
+ out.append("")
766
+ out.append("Findings: %d high, %d medium, %d low, %d info" % (s["high"], s["medium"], s["low"], s["info"]))
767
+ out.append("")
768
+ for f in data["findings"]:
769
+ f = {k: (clean(v) if isinstance(v, str) else v) for k, v in f.items()}
770
+ loc = "%s:%s" % (f["file"], f["line"])
771
+ extra = " [history %s]" % f["commit"] if f["source"] == "history" else ""
772
+ out.append("[%s] %s (%s)%s" % (f["severity"].upper(), f["title"], f["rule"], extra))
773
+ out.append(" %s" % loc)
774
+ if f["evidence"]:
775
+ out.append(" > %s" % f["evidence"])
776
+ out.append(" Fix: %s" % f["advice"])
777
+ out.append("")
778
+ out.append(FREE_NOTE)
779
+ out.append("")
780
+ out.append(DISCLAIMER)
781
+ return "\n".join(out) + "\n"
782
+
783
+
784
+ def render_markdown(data) -> str:
785
+ s = data["summary"]
786
+ out = ["# leakwarden report", "", "Target: `%s` " % data["target"], "Version: %s%s" % (data["version"], edition_label()), "",
787
+ "## Summary", "",
788
+ "| High | Medium | Low | Info |", "|---|---|---|---|",
789
+ "| %d | %d | %d | %d |" % (s["high"], s["medium"], s["low"], s["info"]), "", "## Coverage", ""]
790
+ for k, v in data["coverage"].items():
791
+ out.append("- **%s**: %s" % (k, v))
792
+ out += ["", "## Findings", ""]
793
+ if not data["findings"]:
794
+ out.append("No findings at the selected severity.")
795
+ for f in data["findings"]:
796
+ f = {k: (md_escape(v) if isinstance(v, str) else v) for k, v in f.items()}
797
+ where = "%s:%s" % (f["file"], f["line"])
798
+ if f["source"] == "history":
799
+ where += " (git history, commit %s)" % f["commit"]
800
+ out.append("### [%s] %s" % (f["severity"].upper(), f["title"]))
801
+ out.append("")
802
+ out.append("- Rule: `%s`" % f["rule"])
803
+ out.append("- Where: %s" % where)
804
+ if f["evidence"]:
805
+ out.append("- Evidence: `%s`" % f["evidence"].replace("`", "'"))
806
+ out.append("- Fix: %s" % f["advice"])
807
+ out.append("")
808
+ out += ["---", "", FREE_NOTE]
809
+ out += ["---", "", DISCLAIMER, ""]
810
+ return "\n".join(out)
811
+
812
+
813
+ # ----------------------------------------------------------------------------
814
+ # CLI
815
+ # ----------------------------------------------------------------------------
816
+
817
+ HISTORY_RANGE_RE = re.compile(r"^(?!-)[A-Za-z0-9._/@^~-]+(?:\.\.\.?(?!-)[A-Za-z0-9._/@^~-]+)?$")
818
+
819
+
820
+ def build_parser():
821
+ p = argparse.ArgumentParser(
822
+ prog="leakwarden",
823
+ description="Static security checks for web projects built with AI coding tools.")
824
+ p.add_argument("path", nargs="?", default=".", help="project directory (default: current directory)")
825
+ p.add_argument("--format", choices=["text", "json", "markdown"], default="text")
826
+ p.add_argument("--output", "-o", help="write the report to this file instead of stdout")
827
+ p.add_argument("--min-severity", choices=SEVERITIES, default="low",
828
+ help="hide findings below this severity (default: low)")
829
+ p.add_argument("--fail-on", choices=["high", "medium", "low", "never"], default="high",
830
+ help="exit with status 1 if a finding at or above this severity exists (default: high)")
831
+ p.add_argument("--no-history", action="store_true", help="do not scan git history")
832
+ p.add_argument("--max-commits", type=int, default=500, help="how many commits of history to scan (default: 500)")
833
+ p.add_argument("--history-range", default="",
834
+ help="scan only the commits in this git range, e.g. origin/main..HEAD (for CI on pull requests)")
835
+ p.add_argument("--version", action="version", version="leakwarden " + __version__ + edition_label())
836
+ return p
837
+
838
+
839
+ def main(argv=None) -> int:
840
+ try:
841
+ return _main(argv)
842
+ except KeyboardInterrupt:
843
+ return 130
844
+ except BrokenPipeError:
845
+ return 0
846
+ except Exception as exc:
847
+ print("leakwarden: internal error: %s: %s" % (type(exc).__name__, exc), file=sys.stderr)
848
+ return 2
849
+
850
+
851
+ def _main(argv=None) -> int:
852
+ args = build_parser().parse_args(argv)
853
+ root = args.path
854
+ if not os.path.isdir(root):
855
+ print("leakwarden: not a directory: %s" % root, file=sys.stderr)
856
+ return 2
857
+ if args.history_range and not HISTORY_RANGE_RE.match(args.history_range):
858
+ print("leakwarden: --history-range must look like BASE..HEAD (letters, digits, . _ / @ ^ ~ -)", file=sys.stderr)
859
+ return 2
860
+ sc = Scanner(root).run(history=not args.no_history, max_commits=args.max_commits,
861
+ history_range=args.history_range)
862
+ if getattr(sc, "history_failed", False) and args.history_range:
863
+ print("leakwarden: could not read git range %s (is the clone deep enough?)" % args.history_range, file=sys.stderr)
864
+ return 2
865
+ data = to_dict(sc, args.min_severity)
866
+ if args.format == "json":
867
+ text = json.dumps(data, indent=2) + "\n"
868
+ elif args.format == "markdown":
869
+ text = render_markdown(data)
870
+ else:
871
+ text = render_text(data)
872
+ if args.output:
873
+ with open(args.output, "w", encoding="utf-8", errors="replace") as fh:
874
+ fh.write(text)
875
+ else:
876
+ sys.stdout.write(text)
877
+ if args.fail_on != "never":
878
+ threshold = SEV_RANK[args.fail_on]
879
+ if any(SEV_RANK[f.severity] >= threshold for f in sc.findings):
880
+ return 1
881
+ return 0
882
+
883
+
884
+ if __name__ == "__main__":
885
+ sys.exit(main())