vibe-preflight 0.0.0-stage → 0.2.0
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.
- package/.claude-plugin/marketplace.json +11 -0
- package/.claude-plugin/plugin.json +9 -0
- package/LICENSE +21 -0
- package/README.md +124 -2
- package/bin/preflight.js +4 -0
- package/commands/preflight.md +13 -0
- package/examples/vibe-app/.env +2 -0
- package/examples/vibe-app/README.md +3 -0
- package/examples/vibe-app/app/api/stripe/webhook/route.ts +7 -0
- package/examples/vibe-app/index.html +7 -0
- package/examples/vibe-app/package.json +6 -0
- package/examples/vibe-app/server.js +5 -0
- package/examples/vibe-app/src/lib/stripe.ts +3 -0
- package/examples/vibe-app/src/lib/supabase.ts +4 -0
- package/examples/vibe-app/supabase/migrations/001_init.sql +11 -0
- package/package.json +52 -4
- package/src/cli.js +157 -0
- package/src/demo.js +43 -0
- package/src/project.js +116 -0
- package/src/report/colors.js +12 -0
- package/src/report/html.js +133 -0
- package/src/report/terminal.js +59 -0
- package/src/rules/_client-paths.js +19 -0
- package/src/rules/_gitignore.js +24 -0
- package/src/rules/_template.js +33 -0
- package/src/rules/cors-wildcard.js +37 -0
- package/src/rules/env-file-committed.js +43 -0
- package/src/rules/index.js +12 -0
- package/src/rules/missing-favicon.js +43 -0
- package/src/rules/missing-meta-tags.js +55 -0
- package/src/rules/missing-robots-txt.js +39 -0
- package/src/rules/no-error-monitoring.js +17 -0
- package/src/rules/no-privacy-policy.js +31 -0
- package/src/rules/secret-in-client.js +44 -0
- package/src/rules/stripe-webhook-unverified.js +26 -0
- package/src/rules/supabase-rls-disabled.js +36 -0
- package/src/run.js +47 -0
- package/src/score.js +30 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "preflight",
|
|
3
|
+
"description": "Check whether your vibe-coded app is ready to launch: leaked keys, open databases, unverified payments and missing launch basics, with a fix for every finding.",
|
|
4
|
+
"version": "0.2.0",
|
|
5
|
+
"author": { "name": "erdellll" },
|
|
6
|
+
"homepage": "https://github.com/erdellll/preflight",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"keywords": ["launch", "security", "checklist", "vibe-coding"]
|
|
9
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 erdellll
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,125 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# ✈️ Preflight
|
|
4
|
+
|
|
5
|
+
**Is your vibe-coded app ready to launch? Find out in one command.**
|
|
6
|
+
|
|
7
|
+
Preflight scans your project for leaked keys, open databases, unverified payments and missing launch basics, then gives you a report card with a fix for every finding.
|
|
8
|
+
|
|
9
|
+
[](LICENSE)
|
|
10
|
+
[](package.json)
|
|
11
|
+
[](CONTRIBUTING.md)
|
|
12
|
+
[](https://www.npmjs.com/package/vibe-preflight)
|
|
13
|
+
[](https://www.npmjs.com/package/vibe-preflight)
|
|
14
|
+
[](https://github.com/erdellll/preflight/actions/workflows/ci.yml)
|
|
15
|
+
|
|
16
|
+
**English** · [हिन्दी](docs/README.hi.md)
|
|
17
|
+
|
|
18
|
+

|
|
19
|
+
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npx vibe-preflight
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
No install, no account, no network calls. Your code never leaves your machine.
|
|
27
|
+
|
|
28
|
+
## Why
|
|
29
|
+
|
|
30
|
+
AI tools make it easy to build an app in a weekend, and just as easy to ship it with your Stripe secret key in the browser bundle or a Supabase table anyone can read. Preflight checks for the mistakes that hurt most after launch, before you launch.
|
|
31
|
+
|
|
32
|
+
## Try it on a broken app first
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npx vibe-preflight --demo
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Scans a deliberately broken demo app (every secret is fake) so you can see what a failing report looks like.
|
|
39
|
+
|
|
40
|
+
## What it checks
|
|
41
|
+
|
|
42
|
+
10 checks today, across security, reliability and launch basics. See [all checks](docs/rules.md).
|
|
43
|
+
|
|
44
|
+
| | |
|
|
45
|
+
|---|---|
|
|
46
|
+
| 🔑 Live secrets in client code | 🗄️ Supabase tables without row-level security |
|
|
47
|
+
| 📄 `.env` files that can be committed | 💳 Stripe webhooks without signature checks |
|
|
48
|
+
| 🌍 CORS open to every website | 🚨 No error monitoring |
|
|
49
|
+
| 🔒 No privacy policy | 🔗 Missing title, description or share image |
|
|
50
|
+
|
|
51
|
+
## Options
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npx vibe-preflight [path] # scan a folder (default: current folder)
|
|
55
|
+
npx vibe-preflight --html # also write a shareable preflight-report.html
|
|
56
|
+
npx vibe-preflight --json # machine-readable output ([JSON format](docs/json.md))
|
|
57
|
+
npx vibe-preflight --ignore a,b # skip checks by id
|
|
58
|
+
npx vibe-preflight --fail-on warning # exit code policy: critical (default) | warning | never
|
|
59
|
+
```
|
|
60
|
+
Set NO_COLOR=1 to turn off coloured output.
|
|
61
|
+
|
|
62
|
+
Silence a finding that points at a line with `// preflight-ignore <check-id>` on that line or the line above. A finding about a whole file (a committed `.env`, an unverified webhook) takes the same comment in the file's first five lines. Anything you want gone project-wide, including findings with no file at all, uses `--ignore <check-id>`.
|
|
63
|
+
|
|
64
|
+
## Use it in CI
|
|
65
|
+
|
|
66
|
+
```yaml
|
|
67
|
+
- run: npx vibe-preflight --fail-on critical
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The command exits with code 1 when a finding reaches the `--fail-on` level, so a leaked key blocks the merge.
|
|
71
|
+
|
|
72
|
+
## Use it inside Claude Code
|
|
73
|
+
|
|
74
|
+
Preflight ships as a Claude Code plugin with a `/preflight` command that runs the scan, explains the results and offers to fix critical issues one at a time, asking before every change.
|
|
75
|
+
|
|
76
|
+
In Claude Code, run:
|
|
77
|
+
|
|
78
|
+
```text
|
|
79
|
+
/plugin marketplace add erdellll/preflight
|
|
80
|
+
/plugin install preflight@preflight
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Then type `/preflight` in any project.
|
|
84
|
+
|
|
85
|
+
## Add a check in 10 minutes
|
|
86
|
+
|
|
87
|
+
Every check is one small file plus two example folders, one it must flag and one it must leave alone. No build step, no dependencies. Read [CONTRIBUTING.md](CONTRIBUTING.md), or pick an issue labelled [`good first issue`](https://github.com/erdellll/preflight/issues?q=is%3Aopen+label%3A%22good+first+issue%22).
|
|
88
|
+
|
|
89
|
+
## Not a security audit
|
|
90
|
+
|
|
91
|
+
Preflight catches common, high-impact mistakes with fast pattern checks. A clean report means none of its checks fired, not that your app is secure. Use it as a seatbelt, not a substitute for a review.
|
|
92
|
+
|
|
93
|
+
## FAQ
|
|
94
|
+
|
|
95
|
+
**Does Preflight send my code anywhere?**
|
|
96
|
+
|
|
97
|
+
No. Preflight runs entirely on your local machine, makes no network calls and does not collect telemetry. Your code never leaves your computer.
|
|
98
|
+
|
|
99
|
+
**Does a 100/100 score mean my app is secure?**
|
|
100
|
+
|
|
101
|
+
No. It only means that none of Preflight's checks detected an issue. See the [Not a security audit](#not-a-security-audit) section for more details.
|
|
102
|
+
|
|
103
|
+
**How do I silence a false positive?**
|
|
104
|
+
|
|
105
|
+
Use `// preflight-ignore <check-id>` on the affected line or `--ignore <check-id>` when running Preflight. Please also open a GitHub issue labelled or titled "False positive" so the check can be improved.
|
|
106
|
+
|
|
107
|
+
**Which stacks does it understand?**
|
|
108
|
+
|
|
109
|
+
Preflight can scan any project's files, but its checks work best with Next.js, Vite, Supabase, Stripe, Express and plain HTML.
|
|
110
|
+
|
|
111
|
+
**Why is a symlinked file not checked?**
|
|
112
|
+
|
|
113
|
+
Symbolic links are skipped on purpose, both files and folders. Following them can loop — a folder linked into itself never ends — and can read files outside the project you asked Preflight to scan. A `.env` that is a symlink to a file elsewhere is therefore not reported; copy it in, or point the check at the folder that really holds it. The comment on `walk` in `src/project.js` says the same thing next to the code.
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
[MIT](LICENSE). Free for any use.
|
|
118
|
+
|
|
119
|
+
## Contributors
|
|
120
|
+
|
|
121
|
+
Thanks to everyone who has made Preflight better! 💛
|
|
122
|
+
|
|
123
|
+
<a href="https://github.com/erdellll/preflight/graphs/contributors">
|
|
124
|
+
<img src="https://contrib.rocks/image?repo=erdellll/preflight" alt="Preflight contributors" />
|
|
125
|
+
</a>
|
package/bin/preflight.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Check whether this app is ready to launch and help fix what's blocking it
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Run Preflight on the current project and help the user fix what it finds.
|
|
6
|
+
|
|
7
|
+
1. Run `node "${CLAUDE_PLUGIN_ROOT}/bin/preflight.js" --json --fail-on never` in the project root and parse the JSON report. If that path is unavailable, fall back to `npx -y vibe-preflight --json --fail-on never`.
|
|
8
|
+
2. Summarise it in a few lines: the score out of 100, the grade, and the number of critical issues, warnings and info items.
|
|
9
|
+
3. List the findings grouped by category, critical first, each with its `file:line` and the fix Preflight suggests.
|
|
10
|
+
4. Offer to fix the critical findings one at a time. Before each change, say which file you'll edit and what you'll do, and wait for the user's OK. Never print a secret value; if a key was exposed, remind the user to rotate it with the provider, since removing it from the code is not enough.
|
|
11
|
+
5. After the fixes, run the same command without `--json` and show the new score.
|
|
12
|
+
|
|
13
|
+
If the user asks for a shareable report, run the same command with `--html` and tell them where `preflight-report.html` was written.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vibe-app",
|
|
3
|
+
"private": true,
|
|
4
|
+
"description": "A deliberately broken demo app for Preflight. Every secret here is fake.",
|
|
5
|
+
"dependencies": { "next": "^16.0.0", "react": "^19.0.0", "@supabase/supabase-js": "^2.50.0", "stripe": "^19.0.0", "cors": "^2.8.5" }
|
|
6
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { createClient } from '@supabase/supabase-js';
|
|
2
|
+
// Demo only: fake service_role key that should never ship to the browser.
|
|
3
|
+
export const supabase = createClient('https://demo.supabase.co', '__SERVICE_ROLE_JWT__');
|
|
4
|
+
export const signUp = (email: string, password: string) => supabase.auth.signUp({ email, password });
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
create table public.profiles (
|
|
2
|
+
id uuid primary key references auth.users,
|
|
3
|
+
email text,
|
|
4
|
+
plan text default 'free'
|
|
5
|
+
);
|
|
6
|
+
|
|
7
|
+
create table public.payments (
|
|
8
|
+
id bigint generated always as identity primary key,
|
|
9
|
+
user_id uuid references public.profiles,
|
|
10
|
+
amount integer
|
|
11
|
+
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vibe-preflight",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Is your vibe-coded app ready to launch? One command checks for leaked keys, open databases and missing launch basics, then gives you a report card.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"vibe-preflight": "bin/preflight.js",
|
|
8
|
+
"preflight": "bin/preflight.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"bin",
|
|
12
|
+
"src",
|
|
13
|
+
"examples",
|
|
14
|
+
".claude-plugin",
|
|
15
|
+
"commands"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"test": "node --test",
|
|
19
|
+
"demo": "node bin/preflight.js --demo --fail-on never"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=20"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"vibe-coding",
|
|
26
|
+
"launch",
|
|
27
|
+
"checklist",
|
|
28
|
+
"security",
|
|
29
|
+
"supabase",
|
|
30
|
+
"stripe",
|
|
31
|
+
"cli",
|
|
32
|
+
"claude-code",
|
|
33
|
+
"nextjs",
|
|
34
|
+
"vite",
|
|
35
|
+
"expo",
|
|
36
|
+
"react-native",
|
|
37
|
+
"firebase",
|
|
38
|
+
"lovable",
|
|
39
|
+
"bolt",
|
|
40
|
+
"cursor",
|
|
41
|
+
"secrets",
|
|
42
|
+
"pre-launch"
|
|
43
|
+
],
|
|
44
|
+
"author": "erdellll",
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/erdellll/preflight.git"
|
|
49
|
+
},
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/erdellll/preflight/issues"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://github.com/erdellll/preflight#readme"
|
|
54
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util';
|
|
2
|
+
import { existsSync, statSync, writeFileSync, readFileSync, rmSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { run } from './run.js';
|
|
5
|
+
import { renderTerminal } from './report/terminal.js';
|
|
6
|
+
import { renderHtml } from './report/html.js';
|
|
7
|
+
import { buildDemo } from './demo.js';
|
|
8
|
+
|
|
9
|
+
const LEVELS = { critical: ['critical'], warning: ['critical', 'warning'], never: [] };
|
|
10
|
+
|
|
11
|
+
/** Levenshtein distance, for "did you mean" on a mistyped check id. */
|
|
12
|
+
const distance = (a, b) => {
|
|
13
|
+
let previous = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
14
|
+
for (let i = 1; i <= a.length; i++) {
|
|
15
|
+
const current = [i];
|
|
16
|
+
for (let j = 1; j <= b.length; j++) {
|
|
17
|
+
current[j] = Math.min(
|
|
18
|
+
previous[j] + 1,
|
|
19
|
+
current[j - 1] + 1,
|
|
20
|
+
previous[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1),
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
previous = current;
|
|
24
|
+
}
|
|
25
|
+
return previous[b.length];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The id closest to `typed`, or null when nothing is close enough to be worth suggesting.
|
|
30
|
+
* Half the longer id's length keeps a wildly different id from producing a silly guess.
|
|
31
|
+
*/
|
|
32
|
+
const closestId = (typed, ids) => {
|
|
33
|
+
let best = null;
|
|
34
|
+
let bestDistance = Infinity;
|
|
35
|
+
for (const id of ids) {
|
|
36
|
+
const d = distance(typed, id);
|
|
37
|
+
if (d < bestDistance) {
|
|
38
|
+
bestDistance = d;
|
|
39
|
+
best = id;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const limit = Math.max(2, Math.floor(Math.max(typed.length, (best ?? '').length) / 2));
|
|
43
|
+
return bestDistance <= limit ? best : null;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const HELP = `vibe-preflight: is your vibe-coded app ready to launch?
|
|
47
|
+
|
|
48
|
+
Usage
|
|
49
|
+
npx vibe-preflight [path] [options]
|
|
50
|
+
|
|
51
|
+
Options
|
|
52
|
+
--html [file] also write a shareable report card (default: preflight-report.html)
|
|
53
|
+
--json print the report as JSON instead of the card
|
|
54
|
+
--demo scan a deliberately broken demo app (fake secrets) to see Preflight in action
|
|
55
|
+
--ignore <ids> comma-separated check ids to skip
|
|
56
|
+
--fail-on <level> critical (default) | warning | never; sets the exit code for CI
|
|
57
|
+
-v, --version print the version
|
|
58
|
+
-h, --help show this help
|
|
59
|
+
|
|
60
|
+
Silence one finding with a comment on the line or the line above:
|
|
61
|
+
// preflight-ignore <check-id>
|
|
62
|
+
`;
|
|
63
|
+
|
|
64
|
+
function version() {
|
|
65
|
+
return JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Runs the CLI. Returns the exit code instead of exiting, so it can be tested.
|
|
70
|
+
* @param {string[]} argv
|
|
71
|
+
* @param {{ cwd: string, stdout: { write(s: string): void }, stderr: { write(s: string): void } }} io
|
|
72
|
+
*/
|
|
73
|
+
export async function main(argv, io) {
|
|
74
|
+
let args;
|
|
75
|
+
try {
|
|
76
|
+
// --html takes an optional file, which parseArgs can't express: normalise it first.
|
|
77
|
+
const norm = [];
|
|
78
|
+
for (let i = 0; i < argv.length; i++) {
|
|
79
|
+
if (argv[i] === '--html') {
|
|
80
|
+
const next = argv[i + 1];
|
|
81
|
+
norm.push('--html', next && !next.startsWith('-') && /\.html?$/i.test(next) ? argv[++i] : 'preflight-report.html');
|
|
82
|
+
} else norm.push(argv[i]);
|
|
83
|
+
}
|
|
84
|
+
args = parseArgs({
|
|
85
|
+
args: norm,
|
|
86
|
+
allowPositionals: true,
|
|
87
|
+
options: {
|
|
88
|
+
html: { type: 'string' },
|
|
89
|
+
json: { type: 'boolean' },
|
|
90
|
+
demo: { type: 'boolean' },
|
|
91
|
+
ignore: { type: 'string' },
|
|
92
|
+
'fail-on': { type: 'string', default: 'critical' },
|
|
93
|
+
version: { type: 'boolean', short: 'v' },
|
|
94
|
+
help: { type: 'boolean', short: 'h' },
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
} catch (err) {
|
|
98
|
+
io.stderr.write(`${err.message}\n\nRun with --help for usage.\n`);
|
|
99
|
+
return 2;
|
|
100
|
+
}
|
|
101
|
+
const { values, positionals } = args;
|
|
102
|
+
if (values.help) { io.stdout.write(HELP); return 0; }
|
|
103
|
+
if (values.version) { io.stdout.write(`${version()}\n`); return 0; }
|
|
104
|
+
if (!LEVELS[values['fail-on']]) {
|
|
105
|
+
io.stderr.write(`--fail-on must be critical, warning or never (got "${values['fail-on']}").\n`);
|
|
106
|
+
return 2;
|
|
107
|
+
}
|
|
108
|
+
const root = values.demo ? buildDemo() : path.resolve(io.cwd, positionals[0] ?? '.');
|
|
109
|
+
if (!existsSync(root) || !statSync(root).isDirectory()) {
|
|
110
|
+
io.stderr.write(`Folder not found: ${root}\n`);
|
|
111
|
+
return 2;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const report = await run(root, { ignore: values.ignore ? values.ignore.split(',').map((s) => s.trim()).filter(Boolean) : [] });
|
|
115
|
+
|
|
116
|
+
// A mistyped --ignore switches off nothing, which looks exactly like the check passing (#19).
|
|
117
|
+
if (report.unknownIgnore?.length) {
|
|
118
|
+
const { loadRules } = await import('./rules/index.js');
|
|
119
|
+
const ids = (await loadRules()).map((r) => r.id);
|
|
120
|
+
for (const id of report.unknownIgnore) {
|
|
121
|
+
const near = closestId(id, ids);
|
|
122
|
+
io.stderr.write(
|
|
123
|
+
`--ignore: no check has the id "${id}"${near ? ` (did you mean "${near}"?)` : ''}. ` +
|
|
124
|
+
`It ignored nothing; run --help or see docs/rules.md for the ids.\n`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// A --demo run copied the sample app into a temp folder; it is this run's to remove, and a throw
|
|
130
|
+
// in rendering or --html must not leave it behind (#25).
|
|
131
|
+
try {
|
|
132
|
+
// Checked before anything is printed: a report card that cannot be written is a usage error, and
|
|
133
|
+
// printing the whole report first then crashing with ENOENT buries the reason (#17).
|
|
134
|
+
if (values.html) {
|
|
135
|
+
const file = path.resolve(io.cwd, values.html);
|
|
136
|
+
if (!existsSync(path.dirname(file))) {
|
|
137
|
+
io.stderr.write(`Can't write ${values.html}: folder does not exist (${path.dirname(file)})\n`);
|
|
138
|
+
return 2;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (values.demo && !values.json) io.stdout.write('Scanning the Preflight demo app (deliberately broken, every secret is fake)...\n\n');
|
|
143
|
+
if (values.json) io.stdout.write(JSON.stringify(report, null, 2) + '\n');
|
|
144
|
+
else io.stdout.write(renderTerminal(report));
|
|
145
|
+
|
|
146
|
+
if (values.html) {
|
|
147
|
+
const file = path.resolve(io.cwd, values.html);
|
|
148
|
+
writeFileSync(file, renderHtml(report));
|
|
149
|
+
if (!values.json) io.stdout.write(`Report card written to ${path.relative(io.cwd, file) || file}\n`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const failing = LEVELS[values['fail-on']];
|
|
153
|
+
return report.results.some((r) => failing.includes(r.severity)) ? 1 : 0;
|
|
154
|
+
} finally {
|
|
155
|
+
if (values.demo) rmSync(root, { recursive: true, force: true });
|
|
156
|
+
}
|
|
157
|
+
}
|
package/src/demo.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { cpSync, mkdtempSync, readdirSync, readFileSync, writeFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Fake secrets are assembled at runtime instead of being committed, so the repo
|
|
8
|
+
* never contains strings that look like real keys to secret scanners.
|
|
9
|
+
*/
|
|
10
|
+
export const PLACEHOLDERS = {
|
|
11
|
+
__STRIPE_LIVE__: ['sk', 'live', '51HdemoFAKEkey'].join('_') + '0'.repeat(20),
|
|
12
|
+
__ANTHROPIC_KEY__: ['sk', 'ant', 'api03'].join('-') + '-FAKEdemoKEY' + '0'.repeat(20),
|
|
13
|
+
__OPENAI_KEY__: ['sk', 'proj'].join('-') + '-FAKEdemoKEY' + '0'.repeat(20),
|
|
14
|
+
__AWS_KEY__: 'AKIA' + 'FAKEDEMO00000000',
|
|
15
|
+
__ANON_JWT__: 'eyJ' + 'hbGciOiJIUzI1NiJ9.eyJyb2xlIjoiYW5vbiJ9.FAKEsignature',
|
|
16
|
+
__SERVICE_ROLE_JWT__: 'eyJ' + 'hbGciOiJIUzI1NiJ9.eyJyb2xlIjoic2VydmljZV9yb2xlIn0.FAKEsignature',
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Replaces every placeholder in every file under `dir`, in place. */
|
|
20
|
+
export function fillPlaceholders(dir) {
|
|
21
|
+
for (const name of readdirSync(dir)) {
|
|
22
|
+
const full = path.join(dir, name);
|
|
23
|
+
if (statSync(full).isDirectory()) fillPlaceholders(full);
|
|
24
|
+
else {
|
|
25
|
+
let text = readFileSync(full, 'utf8');
|
|
26
|
+
for (const [k, v] of Object.entries(PLACEHOLDERS)) text = text.split(k).join(v);
|
|
27
|
+
writeFileSync(full, text);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Copies a template folder to a fresh temp dir with placeholders filled. */
|
|
33
|
+
export function materialize(src, label = 'demo') {
|
|
34
|
+
const dest = mkdtempSync(path.join(tmpdir(), `preflight-${label}-`));
|
|
35
|
+
cpSync(src, dest, { recursive: true });
|
|
36
|
+
fillPlaceholders(dest);
|
|
37
|
+
return dest;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Builds the deliberately broken demo app and returns its folder. */
|
|
41
|
+
export function buildDemo() {
|
|
42
|
+
return materialize(fileURLToPath(new URL('../examples/vibe-app/', import.meta.url)), 'demo');
|
|
43
|
+
}
|
package/src/project.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { readdir, stat } from 'node:fs/promises';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { existsSync } from 'node:fs';
|
|
5
|
+
import { isGitignored } from './rules/_gitignore.js';
|
|
6
|
+
|
|
7
|
+
/** Directories that are never scanned: dependencies, VCS data and build output. */
|
|
8
|
+
export const SKIP_DIRS = new Set([
|
|
9
|
+
'node_modules', '.git', 'dist', 'build', '.next', 'out', 'coverage', '.vercel',
|
|
10
|
+
'.svelte-kit', '.nuxt', '.turbo', '.cache', '.expo', 'Pods', 'DerivedData', '.output', '.parcel-cache',
|
|
11
|
+
'.venv', 'venv', '__pycache__', 'vendor', 'target', '.gradle', '.dart_tool', '.terraform', 'bower_components', '.yarn',
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
/** Files larger than this are skipped (bundles, dumps, generated code). */
|
|
15
|
+
export const MAX_FILE_BYTES = 1024 * 1024;
|
|
16
|
+
|
|
17
|
+
const BINARY_EXT = new Set([
|
|
18
|
+
'.png', '.jpg', '.jpeg', '.gif', '.webp', '.avif', '.ico', '.icns', '.bmp', '.tiff', '.psd',
|
|
19
|
+
'.woff', '.woff2', '.ttf', '.otf', '.eot',
|
|
20
|
+
'.zip', '.gz', '.tgz', '.bz2', '.xz', '.7z', '.rar', '.jar',
|
|
21
|
+
'.mp3', '.mp4', '.mov', '.wav', '.ogg', '.webm', '.m4a', '.avi',
|
|
22
|
+
'.pdf', '.exe', '.dll', '.so', '.dylib', '.bin', '.wasm', '.sqlite', '.db', '.lockb',
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
async function walk(root, dir, out) {
|
|
26
|
+
let entries;
|
|
27
|
+
try {
|
|
28
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
29
|
+
} catch {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
for (const entry of entries) {
|
|
33
|
+
const full = path.join(dir, entry.name);
|
|
34
|
+
// readdir reports a symlink as neither a file nor a directory unless it is dereferenced, so
|
|
35
|
+
// both branches below skip one on purpose: following links can loop (a folder linked into
|
|
36
|
+
// itself) and can read outside the project, and a scan is meant to report on the tree it was
|
|
37
|
+
// given. A symlinked `.env` is therefore not checked (#24).
|
|
38
|
+
if (entry.isDirectory()) {
|
|
39
|
+
if (!SKIP_DIRS.has(entry.name)) await walk(root, full, out);
|
|
40
|
+
} else if (entry.isFile()) {
|
|
41
|
+
if (BINARY_EXT.has(path.extname(entry.name).toLowerCase())) continue;
|
|
42
|
+
try {
|
|
43
|
+
if ((await stat(full)).size > MAX_FILE_BYTES) continue;
|
|
44
|
+
} catch {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
out.push(path.relative(root, full).split(path.sep).join('/'));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Builds the view of a project that every rule receives.
|
|
54
|
+
* @param {string} root
|
|
55
|
+
*/
|
|
56
|
+
export async function loadProject(root) {
|
|
57
|
+
const files = [];
|
|
58
|
+
await walk(root, root, files);
|
|
59
|
+
files.sort();
|
|
60
|
+
const cache = new Map();
|
|
61
|
+
const read = (file) => {
|
|
62
|
+
if (!cache.has(file)) {
|
|
63
|
+
let text = '';
|
|
64
|
+
try {
|
|
65
|
+
text = readFileSync(path.join(root, file), 'utf8');
|
|
66
|
+
} catch {
|
|
67
|
+
text = '';
|
|
68
|
+
}
|
|
69
|
+
cache.set(file, text);
|
|
70
|
+
}
|
|
71
|
+
return cache.get(file);
|
|
72
|
+
};
|
|
73
|
+
const gitignore = files.includes('.gitignore')
|
|
74
|
+
? read('.gitignore').split(/\r?\n/).map((l) => l.trim()).filter((l) => l && !l.startsWith('#'))
|
|
75
|
+
: [];
|
|
76
|
+
// .gitignore files that apply: parents up to the repo root (outermost first), then nested ones.
|
|
77
|
+
const sources = [];
|
|
78
|
+
const parents = [];
|
|
79
|
+
for (let dir = path.dirname(path.resolve(root)); ; dir = path.dirname(dir)) {
|
|
80
|
+
if (existsSync(path.join(dir, '.gitignore'))) parents.unshift(dir);
|
|
81
|
+
if (existsSync(path.join(dir, '.git')) || path.dirname(dir) === dir) break;
|
|
82
|
+
}
|
|
83
|
+
if (existsSync(path.join(root, '.git'))) parents.length = 0;
|
|
84
|
+
const parse = (text) => text.split(/\r?\n/).map((l) => l.trim()).filter((l) => l && !l.startsWith('#'));
|
|
85
|
+
for (const dir of parents) {
|
|
86
|
+
let text = '';
|
|
87
|
+
try { text = readFileSync(path.join(dir, '.gitignore'), 'utf8'); } catch { text = ''; }
|
|
88
|
+
sources.push({ base: path.relative(dir, path.resolve(root)).split(path.sep).join('/'), patterns: parse(text) });
|
|
89
|
+
}
|
|
90
|
+
for (const f of files.filter((f) => f === '.gitignore' || f.endsWith('/.gitignore'))) {
|
|
91
|
+
const dir = f.includes('/') ? f.slice(0, f.lastIndexOf('/')) : '';
|
|
92
|
+
sources.push({ prefix: dir, patterns: parse(read(f)) });
|
|
93
|
+
}
|
|
94
|
+
const isIgnored = (file) => {
|
|
95
|
+
let ignored = false;
|
|
96
|
+
for (const src of sources) {
|
|
97
|
+
let rel;
|
|
98
|
+
if ('base' in src) rel = src.base ? `${src.base}/${file}` : file;
|
|
99
|
+
else if (!src.prefix) rel = file;
|
|
100
|
+
else if (file.startsWith(src.prefix + '/')) rel = file.slice(src.prefix.length + 1);
|
|
101
|
+
else continue;
|
|
102
|
+
const verdict = isGitignored(rel, src.patterns, null);
|
|
103
|
+
if (verdict !== null) ignored = verdict;
|
|
104
|
+
}
|
|
105
|
+
return ignored;
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
root,
|
|
109
|
+
isIgnored,
|
|
110
|
+
files,
|
|
111
|
+
read,
|
|
112
|
+
lines: (file) => read(file).split(/\r?\n/),
|
|
113
|
+
has: (pattern) => files.some((f) => pattern.test(f)),
|
|
114
|
+
gitignore,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const CODES = { bold: 1, dim: 2, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, gray: 90 };
|
|
2
|
+
|
|
3
|
+
/** Returns a palette of colour functions; all are identity when `enabled` is false. */
|
|
4
|
+
export function palette(enabled) {
|
|
5
|
+
const p = {};
|
|
6
|
+
for (const [name, code] of Object.entries(CODES)) p[name] = (s) => (enabled ? `\x1b[${code}m${s}\x1b[0m` : String(s));
|
|
7
|
+
return p;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const gradeColor = (grade) => ({ A: 'green', B: 'green', C: 'yellow', D: 'red', F: 'red' }[grade]);
|
|
11
|
+
export const severityColor = (sev) => ({ critical: 'red', warning: 'yellow', info: 'gray' }[sev]);
|
|
12
|
+
export const stripAnsi = (s) => s.replace(/\x1b\[[0-9;]*m/g, '');
|