launch-doctor 0.4.0 → 0.4.2
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/CHANGELOG.md +50 -7
- package/README.md +82 -402
- package/dist/src/banner.d.ts +22 -0
- package/dist/src/banner.js +35 -0
- package/dist/src/bin.cjs +35 -0
- package/dist/src/bin.d.cts +2 -0
- package/dist/src/checks/access.js +8 -0
- package/dist/src/checks/costs.js +6 -0
- package/dist/src/checks/database.js +29 -0
- package/dist/src/checks/payments.js +12 -0
- package/dist/src/checks/secrets.d.ts +26 -1
- package/dist/src/checks/secrets.js +245 -5
- package/dist/src/cli.js +275 -11
- package/dist/src/fixes.d.ts +8 -1
- package/dist/src/fixes.js +31 -10
- package/dist/src/grade.d.ts +8 -1
- package/dist/src/grade.js +13 -0
- package/dist/src/grades.d.ts +42 -0
- package/dist/src/grades.js +163 -0
- package/dist/src/history.js +9 -2
- package/dist/src/hooks-setup.d.ts +26 -0
- package/dist/src/hooks-setup.js +115 -1
- package/dist/src/index.js +6 -1
- package/dist/src/init.d.ts +32 -2
- package/dist/src/init.js +120 -6
- package/dist/src/live.d.ts +4 -1
- package/dist/src/live.js +62 -23
- package/dist/src/net-guard.d.ts +14 -6
- package/dist/src/net-guard.js +9 -6
- package/dist/src/pinned-http.d.ts +31 -0
- package/dist/src/pinned-http.js +104 -0
- package/dist/src/prompt.d.ts +23 -0
- package/dist/src/prompt.js +88 -0
- package/dist/src/report.d.ts +14 -1
- package/dist/src/report.js +29 -15
- package/dist/src/short-report.d.ts +59 -0
- package/dist/src/short-report.js +241 -0
- package/dist/src/types.d.ts +27 -0
- package/dist/src/util.d.ts +2 -0
- package/dist/src/util.js +2 -0
- package/package.json +2 -2
package/dist/src/live.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { emptyOutput, notApplicable } from './checks/common.js';
|
|
2
|
-
import { matchKeys } from './checks/secrets.js';
|
|
2
|
+
import { leakPlaybook, matchKeys, usageLink } from './checks/secrets.js';
|
|
3
3
|
import { overall, scoreCategory, toGrade } from './grade.js';
|
|
4
4
|
import { checkHost, defaultLookup } from './net-guard.js';
|
|
5
|
+
import { pinnedGet } from './pinned-http.js';
|
|
5
6
|
import { sanitizeReport } from './sanitize.js';
|
|
6
7
|
import { decodeJwtPayload, mask, plural } from './util.js';
|
|
7
8
|
import { version } from './version.js';
|
|
@@ -46,13 +47,56 @@ export function parseTarget(input) {
|
|
|
46
47
|
function isLocal(url) {
|
|
47
48
|
return /^(localhost|127\.\d+\.\d+\.\d+|\[::1\]|0\.0\.0\.0)$/.test(url.hostname);
|
|
48
49
|
}
|
|
50
|
+
function fetchTransport(f) {
|
|
51
|
+
return async (req) => {
|
|
52
|
+
const res = await f(req.url.href, {
|
|
53
|
+
method: 'GET',
|
|
54
|
+
redirect: 'manual',
|
|
55
|
+
signal: AbortSignal.timeout(req.timeoutMs),
|
|
56
|
+
headers: req.headers,
|
|
57
|
+
});
|
|
58
|
+
if (req.skipRedirectBody && res.status >= 300 && res.status < 400 && res.headers.get('location')) {
|
|
59
|
+
await res.body?.cancel().catch(() => { });
|
|
60
|
+
return { status: res.status, headers: res.headers, body: '' };
|
|
61
|
+
}
|
|
62
|
+
return { status: res.status, headers: res.headers, body: await readCapped(res, req.maxBytes) };
|
|
63
|
+
};
|
|
64
|
+
}
|
|
49
65
|
class Fetcher {
|
|
50
66
|
opts;
|
|
51
67
|
requests = 0;
|
|
52
68
|
/** Requests not sent because they would have reached a private or local address. */
|
|
53
69
|
blocked = [];
|
|
70
|
+
transport;
|
|
54
71
|
constructor(opts) {
|
|
55
72
|
this.opts = opts;
|
|
73
|
+
this.transport = opts.fetch ? fetchTransport(opts.fetch) : pinnedGet;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The addresses a request to `url` may connect to, or null when it must not be sent. Each hop is checked once and
|
|
77
|
+
* the connection is pinned to what was checked, so DNS can't hand the connection a different address.
|
|
78
|
+
*/
|
|
79
|
+
async addressesFor(url) {
|
|
80
|
+
const host = await checkHost(url, this.opts.lookup);
|
|
81
|
+
if (host.kind === 'unresolved')
|
|
82
|
+
return null;
|
|
83
|
+
if (host.kind === 'private') {
|
|
84
|
+
if (!this.opts.allowPrivate) {
|
|
85
|
+
this.blocked.push(host.reason);
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
if (host.addresses)
|
|
89
|
+
return host.addresses;
|
|
90
|
+
// localhost and friends: you chose to scan this computer, so ask DNS (or the hosts file) where it is.
|
|
91
|
+
try {
|
|
92
|
+
const local = await this.opts.lookup(url.hostname);
|
|
93
|
+
return local.length ? local : null;
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return host.addresses;
|
|
56
100
|
}
|
|
57
101
|
/**
|
|
58
102
|
* One GET. Returns null when the site can't be reached. Never throws. Redirects are followed here, one hop at a
|
|
@@ -70,33 +114,26 @@ class Fetcher {
|
|
|
70
114
|
}
|
|
71
115
|
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:')
|
|
72
116
|
return null;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
this.blocked.push(host.reason);
|
|
77
|
-
if (host.kind !== 'public')
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
117
|
+
const addresses = await this.addressesFor(parsed);
|
|
118
|
+
if (!addresses)
|
|
119
|
+
return null;
|
|
80
120
|
this.requests++;
|
|
81
121
|
try {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
122
|
+
const follow = init.follow !== false;
|
|
123
|
+
const res = await this.transport({
|
|
124
|
+
url: parsed,
|
|
125
|
+
addresses,
|
|
86
126
|
headers: { 'user-agent': USER_AGENT, accept: '*/*', ...init.headers },
|
|
127
|
+
timeoutMs: this.opts.timeoutMs,
|
|
128
|
+
maxBytes: this.opts.maxBytes,
|
|
129
|
+
skipRedirectBody: follow,
|
|
87
130
|
});
|
|
88
131
|
const location = res.headers.get('location');
|
|
89
|
-
if (
|
|
90
|
-
await res.body?.cancel().catch(() => { });
|
|
132
|
+
if (follow && res.status >= 300 && res.status < 400 && location) {
|
|
91
133
|
current = new URL(location, current).href;
|
|
92
134
|
continue;
|
|
93
135
|
}
|
|
94
|
-
return {
|
|
95
|
-
url: current,
|
|
96
|
-
status: res.status,
|
|
97
|
-
headers: res.headers,
|
|
98
|
-
body: await readCapped(res, this.opts.maxBytes),
|
|
99
|
-
};
|
|
136
|
+
return { url: current, status: res.status, headers: res.headers, body: res.body };
|
|
100
137
|
}
|
|
101
138
|
catch {
|
|
102
139
|
return null;
|
|
@@ -373,7 +410,8 @@ async function exposureCheck(fetcher, home, page) {
|
|
|
373
410
|
`1. Find where my code reads it and move that code to the server (an API route, server action or edge function), reading ${m.pattern.envName} from environment variables there.\n` +
|
|
374
411
|
`2. Make sure no NEXT_PUBLIC_/VITE_ variable holds it.\n` +
|
|
375
412
|
`3. Redeploy and confirm the key no longer appears in the built JavaScript.\n` +
|
|
376
|
-
`Then tell me how to revoke this key and create a new one (${m.pattern.rotate}), because it is already public
|
|
413
|
+
`Then tell me how to revoke this key and create a new one (${m.pattern.rotate}), because it is already public.` +
|
|
414
|
+
leakPlaybook({ usage: usageLink(m.pattern.id), from: 'live' }),
|
|
377
415
|
}));
|
|
378
416
|
}
|
|
379
417
|
}
|
|
@@ -407,7 +445,8 @@ async function databaseCheck(fetcher, page, opts) {
|
|
|
407
445
|
},
|
|
408
446
|
fixPrompt: `My live site ships a Supabase service_role key (it starts with "${mask(m[0])}") in its JavaScript. Please remove it from all frontend code: the browser may only use the anon/publishable key. ` +
|
|
409
447
|
`Move any feature that needs admin access into server-only code (API route or Edge Function) that reads SUPABASE_SERVICE_ROLE_KEY from environment variables, redeploy, ` +
|
|
410
|
-
`and tell me how to rotate the JWT secret / service_role key in Supabase, because it is already public
|
|
448
|
+
`and tell me how to rotate the JWT secret / service_role key in Supabase, because it is already public.` +
|
|
449
|
+
leakPlaybook({ usage: usageLink('supabase-service-role-key'), from: 'live' }),
|
|
411
450
|
}));
|
|
412
451
|
}
|
|
413
452
|
else if (role === 'anon' && !anonKeys.includes(m[0]))
|
|
@@ -498,7 +537,7 @@ export async function runLiveScan(input, options = {}) {
|
|
|
498
537
|
const fetcher = new Fetcher({
|
|
499
538
|
timeoutMs: options.timeoutMs ?? 10_000,
|
|
500
539
|
maxBytes: options.maxBytes ?? 3_000_000,
|
|
501
|
-
fetch: options.fetch
|
|
540
|
+
fetch: options.fetch,
|
|
502
541
|
lookup,
|
|
503
542
|
// A scan you start at a private address (your own dev server) stays usable; one that starts on a public site
|
|
504
543
|
// never reaches a private address.
|
package/dist/src/net-guard.d.ts
CHANGED
|
@@ -9,21 +9,29 @@
|
|
|
9
9
|
* Alternate spellings of an IPv4 address (2130706433, 0x7f000001, 0177.0.0.1, 127.1) need no extra handling here:
|
|
10
10
|
* the URL parser turns them all into "127.0.0.1" before this code sees the host.
|
|
11
11
|
*
|
|
12
|
-
*
|
|
13
|
-
* public address to the check and a private one to the connection (DNS rebinding)
|
|
12
|
+
* The check returns the addresses it approved, and the live scan connects only to those (see pinned-http.ts), so a
|
|
13
|
+
* DNS server that answers with a public address to the check and a private one to the connection (DNS rebinding)
|
|
14
|
+
* gets nowhere: the name is never resolved a second time.
|
|
14
15
|
*/
|
|
15
|
-
export
|
|
16
|
+
export interface ResolvedAddress {
|
|
16
17
|
address: string;
|
|
17
18
|
family: number;
|
|
18
|
-
}
|
|
19
|
+
}
|
|
20
|
+
export type Lookup = (host: string) => Promise<ResolvedAddress[]>;
|
|
19
21
|
export declare const defaultLookup: Lookup;
|
|
20
22
|
/** Is this literal IP address one anyone on the internet could reach? Null when `text` isn't an IP address. */
|
|
21
23
|
export declare function isPublicAddress(text: string): boolean | null;
|
|
22
|
-
export type HostCheck =
|
|
24
|
+
export type HostCheck =
|
|
25
|
+
/** `addresses` are the ones that were checked: a request must connect to one of them and nothing else. */
|
|
26
|
+
{
|
|
23
27
|
kind: 'public';
|
|
24
|
-
|
|
28
|
+
addresses: ResolvedAddress[];
|
|
29
|
+
}
|
|
30
|
+
/** `addresses` is missing for names that always mean this computer (localhost), which are not looked up. */
|
|
31
|
+
| {
|
|
25
32
|
kind: 'private';
|
|
26
33
|
reason: string;
|
|
34
|
+
addresses?: ResolvedAddress[];
|
|
27
35
|
}
|
|
28
36
|
/** The name doesn't resolve, so no request can reach it. */
|
|
29
37
|
| {
|
package/dist/src/net-guard.js
CHANGED
|
@@ -124,10 +124,13 @@ export async function checkHost(url, lookup = defaultLookup) {
|
|
|
124
124
|
if (isLocalName(host))
|
|
125
125
|
return { kind: 'private', reason: `${host} is this computer` };
|
|
126
126
|
const literal = isPublicAddress(host);
|
|
127
|
-
if (literal
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return
|
|
127
|
+
if (literal !== null) {
|
|
128
|
+
const bare = host.replace(/^\[|\]$/g, '');
|
|
129
|
+
const addresses = [{ address: bare, family: bare.includes(':') ? 6 : 4 }];
|
|
130
|
+
return literal
|
|
131
|
+
? { kind: 'public', addresses }
|
|
132
|
+
: { kind: 'private', reason: `${host} is a private or local network address`, addresses };
|
|
133
|
+
}
|
|
131
134
|
let addresses;
|
|
132
135
|
try {
|
|
133
136
|
addresses = await lookup(host);
|
|
@@ -139,6 +142,6 @@ export async function checkHost(url, lookup = defaultLookup) {
|
|
|
139
142
|
return { kind: 'unresolved', reason: `${host} could not be resolved` };
|
|
140
143
|
const bad = addresses.find((a) => isPublicAddress(a.address) !== true);
|
|
141
144
|
return bad
|
|
142
|
-
? { kind: 'private', reason: `${host} resolves to ${bad.address}, a private or local network address
|
|
143
|
-
: { kind: 'public' };
|
|
145
|
+
? { kind: 'private', reason: `${host} resolves to ${bad.address}, a private or local network address`, addresses }
|
|
146
|
+
: { kind: 'public', addresses };
|
|
144
147
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type LookupFunction } from 'node:net';
|
|
2
|
+
import type { ResolvedAddress } from './net-guard.js';
|
|
3
|
+
/**
|
|
4
|
+
* One GET for the live scan, connecting only to addresses that were already checked.
|
|
5
|
+
*
|
|
6
|
+
* `fetch` resolves a host name again when it connects, so a DNS server could answer the check with a public address
|
|
7
|
+
* and the connection with a private one (DNS rebinding). Here the request is made with node:http or node:https and a
|
|
8
|
+
* `lookup` that never asks DNS: it hands back the checked addresses and nothing else. The URL's host name is still
|
|
9
|
+
* used for the Host header, and for TLS (the server name and the certificate check), so HTTPS works as before.
|
|
10
|
+
*/
|
|
11
|
+
export interface PinnedRequest {
|
|
12
|
+
url: URL;
|
|
13
|
+
/** The checked addresses for `url`'s host. The connection goes to one of these, never anywhere else. */
|
|
14
|
+
addresses: ResolvedAddress[];
|
|
15
|
+
headers: Record<string, string>;
|
|
16
|
+
/** For the whole request, body included. */
|
|
17
|
+
timeoutMs: number;
|
|
18
|
+
/** Stop reading the (decompressed) body after this many bytes. */
|
|
19
|
+
maxBytes: number;
|
|
20
|
+
/** Don't read the body of a redirect that will be followed. */
|
|
21
|
+
skipRedirectBody: boolean;
|
|
22
|
+
}
|
|
23
|
+
export interface RawReply {
|
|
24
|
+
status: number;
|
|
25
|
+
headers: Headers;
|
|
26
|
+
body: string;
|
|
27
|
+
}
|
|
28
|
+
/** A `lookup` for one request that only ever returns `addresses`. */
|
|
29
|
+
export declare function pinnedLookup(addresses: ResolvedAddress[]): LookupFunction;
|
|
30
|
+
/** Rejects when the site can't be reached, times out or sends a broken body. */
|
|
31
|
+
export declare function pinnedGet(req: PinnedRequest): Promise<RawReply>;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { request as httpRequest } from 'node:http';
|
|
2
|
+
import { request as httpsRequest } from 'node:https';
|
|
3
|
+
import { isIP } from 'node:net';
|
|
4
|
+
import { createBrotliDecompress, createGunzip, createInflate } from 'node:zlib';
|
|
5
|
+
/** A `lookup` for one request that only ever returns `addresses`. */
|
|
6
|
+
export function pinnedLookup(addresses) {
|
|
7
|
+
return (_hostname, options, callback) => {
|
|
8
|
+
const family = options?.family === 4 || options?.family === 6 ? options.family : undefined;
|
|
9
|
+
const list = family ? addresses.filter((a) => a.family === family) : addresses;
|
|
10
|
+
if (!list.length) {
|
|
11
|
+
const err = new Error('No checked address for this request');
|
|
12
|
+
err.code = 'ENOTFOUND';
|
|
13
|
+
callback(err, '', 0);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (options?.all)
|
|
17
|
+
callback(null, list);
|
|
18
|
+
else
|
|
19
|
+
callback(null, list[0].address, list[0].family);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function toHeaders(res) {
|
|
23
|
+
const headers = new Headers();
|
|
24
|
+
for (const [name, value] of Object.entries(res.headers)) {
|
|
25
|
+
for (const v of Array.isArray(value) ? value : value === undefined ? [] : [value]) {
|
|
26
|
+
try {
|
|
27
|
+
headers.append(name, v);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// a value the Headers class refuses is left out
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return headers;
|
|
35
|
+
}
|
|
36
|
+
function decoded(res) {
|
|
37
|
+
const encoding = (res.headers['content-encoding'] ?? '').trim().toLowerCase();
|
|
38
|
+
if (encoding === 'gzip' || encoding === 'x-gzip')
|
|
39
|
+
return res.pipe(createGunzip());
|
|
40
|
+
if (encoding === 'deflate')
|
|
41
|
+
return res.pipe(createInflate());
|
|
42
|
+
if (encoding === 'br')
|
|
43
|
+
return res.pipe(createBrotliDecompress());
|
|
44
|
+
return res;
|
|
45
|
+
}
|
|
46
|
+
/** Rejects when the site can't be reached, times out or sends a broken body. */
|
|
47
|
+
export function pinnedGet(req) {
|
|
48
|
+
const { url } = req;
|
|
49
|
+
const host = url.hostname.replace(/^\[|\]$/g, '');
|
|
50
|
+
const send = url.protocol === 'https:' ? httpsRequest : httpRequest;
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
let settled = false;
|
|
53
|
+
const finish = (err, reply) => {
|
|
54
|
+
if (settled)
|
|
55
|
+
return;
|
|
56
|
+
settled = true;
|
|
57
|
+
clearTimeout(timer);
|
|
58
|
+
if (err)
|
|
59
|
+
reject(err);
|
|
60
|
+
else
|
|
61
|
+
resolve(reply);
|
|
62
|
+
};
|
|
63
|
+
const request = send({
|
|
64
|
+
protocol: url.protocol,
|
|
65
|
+
host,
|
|
66
|
+
port: url.port || undefined,
|
|
67
|
+
path: `${url.pathname}${url.search}`,
|
|
68
|
+
method: 'GET',
|
|
69
|
+
headers: { ...req.headers, 'accept-encoding': 'gzip, deflate, br' },
|
|
70
|
+
lookup: pinnedLookup(req.addresses),
|
|
71
|
+
// No shared agent: a pooled connection could have been made to another address for the same name.
|
|
72
|
+
agent: false,
|
|
73
|
+
// TLS checks the certificate against the host name, never the address. An IP address has no server name.
|
|
74
|
+
servername: isIP(host) ? undefined : host,
|
|
75
|
+
}, (res) => {
|
|
76
|
+
const status = res.statusCode ?? 0;
|
|
77
|
+
const headers = toHeaders(res);
|
|
78
|
+
if (req.skipRedirectBody && status >= 300 && status < 400 && res.headers.location) {
|
|
79
|
+
res.destroy();
|
|
80
|
+
finish(null, { status, headers, body: '' });
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const body = decoded(res);
|
|
84
|
+
const chunks = [];
|
|
85
|
+
let size = 0;
|
|
86
|
+
const done = () => finish(null, { status, headers, body: Buffer.concat(chunks).toString('utf8') });
|
|
87
|
+
body.on('data', (chunk) => {
|
|
88
|
+
chunks.push(chunk);
|
|
89
|
+
size += chunk.length;
|
|
90
|
+
if (size >= req.maxBytes) {
|
|
91
|
+
done();
|
|
92
|
+
res.destroy();
|
|
93
|
+
body.destroy();
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
body.on('end', done);
|
|
97
|
+
body.on('error', (err) => finish(err));
|
|
98
|
+
res.on('error', (err) => finish(err));
|
|
99
|
+
});
|
|
100
|
+
const timer = setTimeout(() => request.destroy(new Error('The request timed out')), req.timeoutMs);
|
|
101
|
+
request.on('error', (err) => finish(err));
|
|
102
|
+
request.end();
|
|
103
|
+
});
|
|
104
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Finding, Report } from './types.js';
|
|
2
|
+
/** Fix prompt n (1-based, in LAUNCH_FIXES.md order), or null when there is no such prompt. */
|
|
3
|
+
export declare function promptFor(report: Report, n: number): Finding | null;
|
|
4
|
+
interface ClipboardTool {
|
|
5
|
+
name: string;
|
|
6
|
+
args: string[];
|
|
7
|
+
/** The bytes to send on stdin. */
|
|
8
|
+
encode: (text: string) => Buffer;
|
|
9
|
+
env?: NodeJS.ProcessEnv;
|
|
10
|
+
}
|
|
11
|
+
/** The clipboard tools to try on this system, in order. None on a Linux machine without a display. */
|
|
12
|
+
export declare function clipboardTools(platform: NodeJS.Platform, env: NodeJS.ProcessEnv): ClipboardTool[];
|
|
13
|
+
/** Put text on the clipboard with the system's own tool. Returns the tool's name, or null if none worked. */
|
|
14
|
+
export declare function copyToClipboard(text: string, platform?: NodeJS.Platform, env?: NodeJS.ProcessEnv): string | null;
|
|
15
|
+
/**
|
|
16
|
+
* { language: 'Rust', marker: 'Cargo.toml' } when the folder is a project in a language Launch Doctor doesn't
|
|
17
|
+
* analyse, with nothing it does (no package.json, no detected stack, no findings to fix). Null otherwise.
|
|
18
|
+
*/
|
|
19
|
+
export declare function unsupportedLanguage(root: string, report: Report): {
|
|
20
|
+
language: string;
|
|
21
|
+
marker: string;
|
|
22
|
+
} | null;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, readdirSync } from 'node:fs';
|
|
3
|
+
import { orderedFindings } from './fixes.js';
|
|
4
|
+
/** Fix prompt n (1-based, in LAUNCH_FIXES.md order), or null when there is no such prompt. */
|
|
5
|
+
export function promptFor(report, n) {
|
|
6
|
+
return orderedFindings(report)[n - 1] ?? null;
|
|
7
|
+
}
|
|
8
|
+
const utf8 = (text) => Buffer.from(text, 'utf8');
|
|
9
|
+
// clip.exe reads UTF-8 in the console code page (so "…" breaks), but recognises UTF-16. With a byte order mark
|
|
10
|
+
// it would keep the mark as a character, so none is sent.
|
|
11
|
+
const utf16 = (text) => Buffer.from(text, 'utf16le');
|
|
12
|
+
/** The clipboard tools to try on this system, in order. None on a Linux machine without a display. */
|
|
13
|
+
export function clipboardTools(platform, env) {
|
|
14
|
+
if (platform === 'win32')
|
|
15
|
+
return [{ name: 'clip', args: [], encode: utf16 }];
|
|
16
|
+
if (platform === 'darwin') {
|
|
17
|
+
return [{ name: 'pbcopy', args: [], encode: utf8, env: { ...env, LANG: env.LANG || 'en_US.UTF-8' } }];
|
|
18
|
+
}
|
|
19
|
+
const tools = [];
|
|
20
|
+
if (env.WAYLAND_DISPLAY)
|
|
21
|
+
tools.push({ name: 'wl-copy', args: [], encode: utf8 });
|
|
22
|
+
if (env.DISPLAY)
|
|
23
|
+
tools.push({ name: 'xclip', args: ['-selection', 'clipboard'], encode: utf8 });
|
|
24
|
+
return tools;
|
|
25
|
+
}
|
|
26
|
+
/** Put text on the clipboard with the system's own tool. Returns the tool's name, or null if none worked. */
|
|
27
|
+
export function copyToClipboard(text, platform = process.platform, env = process.env) {
|
|
28
|
+
for (const tool of clipboardTools(platform, env)) {
|
|
29
|
+
const res = spawnSync(tool.name, tool.args, {
|
|
30
|
+
input: tool.encode(text),
|
|
31
|
+
stdio: ['pipe', 'ignore', 'ignore'],
|
|
32
|
+
env: tool.env ?? env,
|
|
33
|
+
timeout: 5000,
|
|
34
|
+
windowsHide: true,
|
|
35
|
+
});
|
|
36
|
+
if (!res.error && res.status === 0)
|
|
37
|
+
return tool.name;
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
/** Files that mark a project in a language whose code Launch Doctor's checks don't read. */
|
|
42
|
+
const OTHER_LANGUAGES = [
|
|
43
|
+
[/^Cargo\.toml$/, 'Rust'],
|
|
44
|
+
[/^(?:pom\.xml|build\.gradle(?:\.kts)?|settings\.gradle(?:\.kts)?)$/, 'Java or Kotlin'],
|
|
45
|
+
[/\.(?:csproj|fsproj|sln)$/, '.NET'],
|
|
46
|
+
[/^pubspec\.yaml$/, 'Flutter (Dart)'],
|
|
47
|
+
[/^Package\.swift$/, 'Swift'],
|
|
48
|
+
[/^mix\.exs$/, 'Elixir'],
|
|
49
|
+
[/^(?:CMakeLists\.txt|meson\.build)$/, 'C or C++'],
|
|
50
|
+
];
|
|
51
|
+
/** Files that mark a project Launch Doctor does read. */
|
|
52
|
+
const SUPPORTED = [
|
|
53
|
+
'package.json',
|
|
54
|
+
'requirements.txt',
|
|
55
|
+
'pyproject.toml',
|
|
56
|
+
'Pipfile',
|
|
57
|
+
'setup.py',
|
|
58
|
+
'manage.py',
|
|
59
|
+
'Gemfile',
|
|
60
|
+
'composer.json',
|
|
61
|
+
'go.mod',
|
|
62
|
+
'index.html',
|
|
63
|
+
];
|
|
64
|
+
/**
|
|
65
|
+
* { language: 'Rust', marker: 'Cargo.toml' } when the folder is a project in a language Launch Doctor doesn't
|
|
66
|
+
* analyse, with nothing it does (no package.json, no detected stack, no findings to fix). Null otherwise.
|
|
67
|
+
*/
|
|
68
|
+
export function unsupportedLanguage(root, report) {
|
|
69
|
+
if (report.stack.length)
|
|
70
|
+
return null;
|
|
71
|
+
if (report.categories.some((c) => c.findings.length))
|
|
72
|
+
return null;
|
|
73
|
+
if (SUPPORTED.some((f) => existsSync(`${root}/${f}`)))
|
|
74
|
+
return null;
|
|
75
|
+
let names;
|
|
76
|
+
try {
|
|
77
|
+
names = readdirSync(root);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
for (const [re, language] of OTHER_LANGUAGES) {
|
|
83
|
+
const marker = names.find((n) => re.test(n));
|
|
84
|
+
if (marker)
|
|
85
|
+
return { language, marker };
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
package/dist/src/report.d.ts
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
|
-
import type { Report } from './types.js';
|
|
1
|
+
import type { Grade, Report } from './types.js';
|
|
2
2
|
export declare const bold: (s: string) => string;
|
|
3
3
|
export declare const dim: (s: string) => string;
|
|
4
|
+
export declare const red: (s: string) => string;
|
|
5
|
+
export declare const green: (s: string) => string;
|
|
6
|
+
export declare const yellow: (s: string) => string;
|
|
4
7
|
/** Shown when the scan hit MAX_TOTAL_BYTES (terminal report and MCP). */
|
|
5
8
|
export declare const BYTE_LIMIT_NOTE = "Very large project: only the first 128 MB of source files were read, so some files were not checked.";
|
|
6
9
|
/** "Left out Twin/: a separate git repository inside this folder. Scan it on its own." */
|
|
7
10
|
export declare function nestedReposNote(repos: string[]): string;
|
|
11
|
+
export declare function gradeColor(grade: Grade): (s: string) => string;
|
|
12
|
+
export declare function wrap(text: string, width: number, indent: string): string[];
|
|
13
|
+
/** The banner rows, indented, in no colour. */
|
|
14
|
+
export declare function bannerLines(): string[];
|
|
15
|
+
/** Notes under the header: partial report, size limits, a long ignore file, nested repositories. */
|
|
16
|
+
export declare function noticeLines(report: Report): string[];
|
|
8
17
|
export declare function renderReport(report: Report, opts?: {
|
|
9
18
|
fixFile?: string;
|
|
19
|
+
/** The last recorded grade ("B (82) on 30 Nov 2026"), shown under the overall grade. */
|
|
20
|
+
lastRecorded?: string;
|
|
21
|
+
/** Print the ASCII banner above the title. The caller decides (TTY, full scan, width); see shouldShowBanner. */
|
|
22
|
+
banner?: boolean;
|
|
10
23
|
}): string;
|
package/dist/src/report.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
+
import { BANNER, BANNER_INDENT } from './banner.js';
|
|
1
2
|
import { shortWhere } from './checks/common.js';
|
|
2
3
|
import { ISSUES_URL, plural } from './util.js';
|
|
3
4
|
const useColor = !process.env.NO_COLOR && (process.env.FORCE_COLOR ? process.env.FORCE_COLOR !== '0' : !!process.stdout.isTTY);
|
|
4
5
|
const paint = (code) => (s) => (useColor ? `\x1b[${code}m${s}\x1b[0m` : s);
|
|
5
6
|
export const bold = paint('1');
|
|
6
7
|
export const dim = paint('2');
|
|
7
|
-
const red = paint('31');
|
|
8
|
-
const green = paint('32');
|
|
9
|
-
const yellow = paint('33');
|
|
10
|
-
const cyan = paint('36');
|
|
8
|
+
export const red = paint('31');
|
|
9
|
+
export const green = paint('32');
|
|
10
|
+
export const yellow = paint('33');
|
|
11
11
|
/** Shown when the scan hit MAX_TOTAL_BYTES (terminal report and MCP). */
|
|
12
12
|
export const BYTE_LIMIT_NOTE = 'Very large project: only the first 128 MB of source files were read, so some files were not checked.';
|
|
13
13
|
/** "Left out Twin/: a separate git repository inside this folder. Scan it on its own." */
|
|
@@ -18,7 +18,7 @@ export function nestedReposNote(repos) {
|
|
|
18
18
|
? `Left out ${names[0]}: a separate git repository inside this folder. Scan it on its own.`
|
|
19
19
|
: `Left out ${repos.length} separate git repositories inside this folder (${names.join(', ')}${more}). Scan them on their own.`;
|
|
20
20
|
}
|
|
21
|
-
function gradeColor(grade) {
|
|
21
|
+
export function gradeColor(grade) {
|
|
22
22
|
if (grade === 'A' || grade === 'B')
|
|
23
23
|
return green;
|
|
24
24
|
if (grade === 'C')
|
|
@@ -30,7 +30,7 @@ function badge(grade) {
|
|
|
30
30
|
return dim('-');
|
|
31
31
|
return bold(gradeColor(grade)(grade));
|
|
32
32
|
}
|
|
33
|
-
function wrap(text, width, indent) {
|
|
33
|
+
export function wrap(text, width, indent) {
|
|
34
34
|
const lines = [];
|
|
35
35
|
let current = '';
|
|
36
36
|
for (const word of text.split(/\s+/)) {
|
|
@@ -71,18 +71,18 @@ function findingBlock(f, width) {
|
|
|
71
71
|
const label = f.severity === 'critical' ? bold(red('[CRITICAL]')) : bold(yellow('[WARN]'));
|
|
72
72
|
const lines = [` ${label} ${bold(f.title)}`];
|
|
73
73
|
if (f.location)
|
|
74
|
-
lines.push(`${DETAIL_INDENT}${
|
|
74
|
+
lines.push(`${DETAIL_INDENT}${shortWhere(f.location)}`);
|
|
75
75
|
lines.push(...wrap(f.why, width - DETAIL_INDENT.length, DETAIL_INDENT));
|
|
76
76
|
lines.push('');
|
|
77
77
|
return lines;
|
|
78
78
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
lines
|
|
79
|
+
/** The banner rows, indented, in no colour. */
|
|
80
|
+
export function bannerLines() {
|
|
81
|
+
return BANNER.map((row) => (row ? `${BANNER_INDENT}${row}` : ''));
|
|
82
|
+
}
|
|
83
|
+
/** Notes under the header: partial report, size limits, a long ignore file, nested repositories. */
|
|
84
|
+
export function noticeLines(report) {
|
|
85
|
+
const lines = [];
|
|
86
86
|
if (report.partial) {
|
|
87
87
|
const n = report.partial.files.length;
|
|
88
88
|
lines.push(` ${yellow(`Only ${plural(n, 'changed file')} ${n === 1 ? 'was' : 'were'} reported on, not the whole project. Run without --changed or --staged for the full grade.`)}`);
|
|
@@ -96,8 +96,22 @@ export function renderReport(report, opts = {}) {
|
|
|
96
96
|
}
|
|
97
97
|
if (report.nestedRepos?.length)
|
|
98
98
|
lines.push(` ${dim(nestedReposNote(report.nestedRepos))}`);
|
|
99
|
+
return lines;
|
|
100
|
+
}
|
|
101
|
+
export function renderReport(report, opts = {}) {
|
|
102
|
+
const width = Math.max(60, Math.min(100, process.stdout.columns || 80));
|
|
103
|
+
const lines = [''];
|
|
104
|
+
if (opts.banner)
|
|
105
|
+
lines.push(...bannerLines());
|
|
106
|
+
lines.push(` ${bold('Launch Doctor')} ${dim(`v${report.version}`)}`);
|
|
107
|
+
const stack = report.stack.length ? ` · ${report.stack.join(', ')}` : '';
|
|
108
|
+
const unit = report.mode === 'live' ? 'request' : 'file';
|
|
109
|
+
lines.push(` ${dim(`Checked ${report.projectName} · ${plural(report.filesScanned, unit)}${stack}`)}`);
|
|
110
|
+
lines.push(...noticeLines(report));
|
|
99
111
|
lines.push('');
|
|
100
112
|
lines.push(` ${bold('Overall grade')} ${badge(report.grade)} ${report.verdict}`);
|
|
113
|
+
if (opts.lastRecorded)
|
|
114
|
+
lines.push(` ${dim(`Last recorded grade: ${opts.lastRecorded}`)}`);
|
|
101
115
|
lines.push('');
|
|
102
116
|
const nameWidth = Math.max(...report.categories.map((c) => c.name.length)) + 2;
|
|
103
117
|
for (const c of report.categories) {
|
|
@@ -149,7 +163,7 @@ export function renderReport(report, opts = {}) {
|
|
|
149
163
|
lines.push('');
|
|
150
164
|
}
|
|
151
165
|
if (opts.fixFile) {
|
|
152
|
-
lines.push(` ${bold('Fix prompts saved to')} ${
|
|
166
|
+
lines.push(` ${bold('Fix prompts saved to')} ${opts.fixFile}`);
|
|
153
167
|
lines.push(` ${dim('Paste them one at a time into Claude Code, Cursor, Lovable or Bolt, then run launch-doctor again.')}`);
|
|
154
168
|
lines.push('');
|
|
155
169
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Finding, Report } from './types.js';
|
|
2
|
+
/** How many findings "FIX THESE FIRST" shows. */
|
|
3
|
+
export declare const TOP_FINDINGS = 3;
|
|
4
|
+
export interface ShortReportOptions {
|
|
5
|
+
/** Where LAUNCH_FIXES.md was written, if it was. */
|
|
6
|
+
fixFile?: string;
|
|
7
|
+
/** The last recorded overall grade ("B (82) on 30 Nov 2026"). */
|
|
8
|
+
lastRecorded?: string;
|
|
9
|
+
banner?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* What to type after `npx launch-doctor` to scan the same project again: "", " ./my-app" or " --demo".
|
|
12
|
+
* Used in the "Next" commands.
|
|
13
|
+
*/
|
|
14
|
+
target?: string;
|
|
15
|
+
/** The built-in demo: LAUNCH_FIXES.md is never written for it. */
|
|
16
|
+
demo?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Every number the short report shows, worked out in one place so they always add up.
|
|
20
|
+
* Baseline findings (already accepted) are left out, as in the full report.
|
|
21
|
+
*/
|
|
22
|
+
export interface ShortCounts {
|
|
23
|
+
/** New critical findings: the verdict's number, and the sum of the critical counts on the two grade rows. */
|
|
24
|
+
critical: number;
|
|
25
|
+
security: {
|
|
26
|
+
critical: number;
|
|
27
|
+
warning: number;
|
|
28
|
+
};
|
|
29
|
+
launch: {
|
|
30
|
+
critical: number;
|
|
31
|
+
warning: number;
|
|
32
|
+
};
|
|
33
|
+
/** The findings under FIX THESE FIRST, with their fix-prompt numbers. */
|
|
34
|
+
top: {
|
|
35
|
+
finding: Finding;
|
|
36
|
+
n: number;
|
|
37
|
+
}[];
|
|
38
|
+
/** What the "Also:" line counts: everything not shown under FIX THESE FIRST. */
|
|
39
|
+
also: {
|
|
40
|
+
critical: number;
|
|
41
|
+
warning: number;
|
|
42
|
+
polish: number;
|
|
43
|
+
manual: number;
|
|
44
|
+
};
|
|
45
|
+
baseline: number;
|
|
46
|
+
}
|
|
47
|
+
export declare function shortCounts(report: Report): ShortCounts;
|
|
48
|
+
/**
|
|
49
|
+
* The default terminal report: the verdict, two grades, the three things to fix first and what to do next.
|
|
50
|
+
* `--verbose` (and --ci, --changed, --staged, --pr-comment and live scans) print the full report instead.
|
|
51
|
+
*/
|
|
52
|
+
export declare function renderShortReport(report: Report, opts?: ShortReportOptions): string;
|
|
53
|
+
/** First run in a folder with nothing to read: an explanation instead of a grade. */
|
|
54
|
+
export declare function renderEmptyFolder(report: Report, root: string): string;
|
|
55
|
+
/** A project in a language whose code the checks don't read: an explanation instead of a meaningless grade. */
|
|
56
|
+
export declare function renderOtherLanguage(report: Report, found: {
|
|
57
|
+
language: string;
|
|
58
|
+
marker: string;
|
|
59
|
+
}): string;
|