launch-doctor 0.4.2 → 0.4.4
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 +54 -1
- package/README.md +30 -6
- package/dist/src/baseline.d.ts +9 -0
- package/dist/src/baseline.js +26 -0
- package/dist/src/checks/email.js +3 -1
- package/dist/src/checks/secrets.js +3 -1
- package/dist/src/cli.js +240 -88
- package/dist/src/core.d.ts +85 -0
- package/dist/src/core.js +166 -0
- package/dist/src/explanations.d.ts +24 -0
- package/dist/src/explanations.js +562 -0
- package/dist/src/git.d.ts +1 -1
- package/dist/src/git.js +6 -3
- package/dist/src/hooks-setup.js +19 -6
- package/dist/src/index.js +2 -1
- package/dist/src/init.d.ts +17 -0
- package/dist/src/init.js +73 -6
- package/dist/src/live.js +3 -0
- package/dist/src/mcp.d.ts +126 -11
- package/dist/src/mcp.js +294 -148
- package/dist/src/prompt.d.ts +9 -0
- package/dist/src/prompt.js +84 -0
- package/dist/src/report-md.d.ts +14 -0
- package/dist/src/report-md.js +71 -0
- package/dist/src/safe-fs.d.ts +9 -0
- package/dist/src/safe-fs.js +27 -1
- package/dist/src/short-report.d.ts +2 -0
- package/dist/src/short-report.js +5 -0
- package/dist/src/types.d.ts +6 -0
- package/package.json +2 -2
package/dist/src/mcp.js
CHANGED
|
@@ -1,24 +1,32 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { realpathSync } from 'node:fs';
|
|
2
|
+
import { isAbsolute, relative } from 'node:path';
|
|
3
3
|
import { createInterface } from 'node:readline';
|
|
4
4
|
import { shortWhere } from './checks/common.js';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { CoreError, coverageNotes, findFinding, findingDetails, gradeHistory, normalizeRelative, projectRoot, promptNumber, rescan, scan, } from './core.js';
|
|
6
|
+
import { orderedFindings } from './fixes.js';
|
|
7
|
+
import { describeEntry } from './grades.js';
|
|
8
|
+
import { CHECKS } from './index.js';
|
|
7
9
|
import { runLiveScan } from './live.js';
|
|
8
|
-
import {
|
|
10
|
+
import { nestedReposNote } from './report.js';
|
|
11
|
+
import { stripControl } from './sanitize.js';
|
|
9
12
|
import { plural } from './util.js';
|
|
10
13
|
import { version } from './version.js';
|
|
11
14
|
/**
|
|
12
15
|
* A Model Context Protocol server over stdio, so AI coding tools (Claude Code, Cursor, ...)
|
|
13
16
|
* can run Launch Doctor themselves. One JSON-RPC 2.0 message per line on stdin and stdout.
|
|
14
17
|
* Nothing else may be written to stdout: it is the protocol channel.
|
|
18
|
+
*
|
|
19
|
+
* Every tool goes through the shared core (src/core.ts), like the CLI. The server only reads the project: it
|
|
20
|
+
* writes nothing, and it makes no network request except for scan_url, with the address the caller gives.
|
|
15
21
|
*/
|
|
16
22
|
/** Protocol versions this server speaks, newest first. */
|
|
17
23
|
export const PROTOCOL_VERSIONS = ['2025-11-25', '2025-06-18', '2025-03-26', '2024-11-05'];
|
|
18
24
|
/** Offered when a client asks for a version this server doesn't know. */
|
|
19
25
|
const DEFAULT_PROTOCOL = '2025-06-18';
|
|
20
|
-
/** Findings
|
|
21
|
-
const MAX_LISTED =
|
|
26
|
+
/** Findings listed in a scan result; the rest are counted. Each entry is one line, so this caps the size. */
|
|
27
|
+
export const MAX_LISTED = 25;
|
|
28
|
+
/** The most text one tool result may hold, so a big app can't flood the model's context. */
|
|
29
|
+
export const MAX_TEXT = 12_000;
|
|
22
30
|
class RpcError extends Error {
|
|
23
31
|
code;
|
|
24
32
|
constructor(code, message) {
|
|
@@ -27,6 +35,15 @@ class RpcError extends Error {
|
|
|
27
35
|
}
|
|
28
36
|
}
|
|
29
37
|
const UNKNOWN_METHOD = Symbol('unknown method');
|
|
38
|
+
const PATH_PROP = {
|
|
39
|
+
type: 'string',
|
|
40
|
+
description: 'Project folder inside the folder the server started in (the default). Paths outside it are refused.',
|
|
41
|
+
};
|
|
42
|
+
const ID_PROP = {
|
|
43
|
+
type: 'string',
|
|
44
|
+
description: 'A finding id from scan_project (8 letters and digits, like "3f9a1c2b"), or its number in that list.',
|
|
45
|
+
};
|
|
46
|
+
const READ_ONLY = { readOnlyHint: true, idempotentHint: true, openWorldHint: false };
|
|
30
47
|
/** The tools this server offers, with JSON Schemas for their arguments. */
|
|
31
48
|
export function mcpTools() {
|
|
32
49
|
const categories = CHECKS.map((c) => c.id);
|
|
@@ -34,15 +51,12 @@ export function mcpTools() {
|
|
|
34
51
|
{
|
|
35
52
|
name: 'scan_project',
|
|
36
53
|
title: 'Check a project before launch',
|
|
37
|
-
description: 'Runs Launch Doctor on a project folder and
|
|
38
|
-
'
|
|
54
|
+
description: 'Runs Launch Doctor on a project folder: Security and Launch readiness grades, counts, and a short list of the problems (critical first), each with an id, the rule, file and line. ' +
|
|
55
|
+
'Use explain_finding for what a finding means and fix_prompt for the change to make, then rescan. Only reads files, and makes no network requests.',
|
|
39
56
|
inputSchema: {
|
|
40
57
|
type: 'object',
|
|
41
58
|
properties: {
|
|
42
|
-
path:
|
|
43
|
-
type: 'string',
|
|
44
|
-
description: 'Project folder to check: absolute, or relative to the folder the server started in (the default).',
|
|
45
|
-
},
|
|
59
|
+
path: PATH_PROP,
|
|
46
60
|
only: {
|
|
47
61
|
type: 'array',
|
|
48
62
|
items: { type: 'string', enum: categories },
|
|
@@ -55,27 +69,72 @@ export function mcpTools() {
|
|
|
55
69
|
},
|
|
56
70
|
offline: {
|
|
57
71
|
type: 'boolean',
|
|
58
|
-
description: '
|
|
72
|
+
description: 'Accepted for compatibility: the server never makes network requests for a project scan.',
|
|
59
73
|
},
|
|
60
|
-
domain: { type: 'string', description: "Domain the app sends email from, if the code doesn't show it." },
|
|
61
74
|
history: {
|
|
62
75
|
type: 'boolean',
|
|
63
|
-
description: 'Also look for API keys
|
|
76
|
+
description: 'Also look for API keys still in git history (local git only). Slower.',
|
|
64
77
|
},
|
|
65
78
|
includeBaseline: {
|
|
66
79
|
type: 'boolean',
|
|
67
|
-
description: 'Also list findings accepted in .launchdoctor-baseline.json.
|
|
80
|
+
description: 'Also list findings accepted in .launchdoctor-baseline.json.',
|
|
68
81
|
},
|
|
69
82
|
},
|
|
70
83
|
additionalProperties: false,
|
|
71
84
|
},
|
|
72
|
-
annotations:
|
|
85
|
+
annotations: READ_ONLY,
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
name: 'explain_finding',
|
|
89
|
+
title: 'Explain one finding',
|
|
90
|
+
description: 'What one finding means: why it matters (what an attacker or a surprise bill looks like), how to confirm it by hand, how to fix it, and when it is a false alarm. Secret values are never included.',
|
|
91
|
+
inputSchema: {
|
|
92
|
+
type: 'object',
|
|
93
|
+
properties: { id: ID_PROP, path: PATH_PROP },
|
|
94
|
+
required: ['id'],
|
|
95
|
+
additionalProperties: false,
|
|
96
|
+
},
|
|
97
|
+
annotations: READ_ONLY,
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: 'fix_prompt',
|
|
101
|
+
title: 'Get the fix prompt for one finding',
|
|
102
|
+
description: 'The step-by-step fix for one finding, written as instructions for an AI coding assistant. Apply it, then call rescan.',
|
|
103
|
+
inputSchema: {
|
|
104
|
+
type: 'object',
|
|
105
|
+
properties: { id: ID_PROP, path: PATH_PROP },
|
|
106
|
+
required: ['id'],
|
|
107
|
+
additionalProperties: false,
|
|
108
|
+
},
|
|
109
|
+
annotations: READ_ONLY,
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: 'rescan',
|
|
113
|
+
title: 'Check again after a fix',
|
|
114
|
+
description: 'Checks only some files again and says which of the given findings are gone: the files of the given finding ids, the given paths, or (with neither) the files changed since the last git commit. The whole project is still read so checks across files keep working.',
|
|
115
|
+
inputSchema: {
|
|
116
|
+
type: 'object',
|
|
117
|
+
properties: {
|
|
118
|
+
path: PATH_PROP,
|
|
119
|
+
ids: { type: 'array', items: { type: 'string' }, description: 'Finding ids from scan_project.' },
|
|
120
|
+
paths: { type: 'array', items: { type: 'string' }, description: 'Files inside the project, relative to it.' },
|
|
121
|
+
},
|
|
122
|
+
additionalProperties: false,
|
|
123
|
+
},
|
|
124
|
+
annotations: READ_ONLY,
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
name: 'grade_history',
|
|
128
|
+
title: 'Grade history',
|
|
129
|
+
description: 'The grades saved with "launch-doctor --record" (newest first): date, grade, score, counts and commit.',
|
|
130
|
+
inputSchema: { type: 'object', properties: { path: PATH_PROP }, additionalProperties: false },
|
|
131
|
+
annotations: READ_ONLY,
|
|
73
132
|
},
|
|
74
133
|
{
|
|
75
134
|
name: 'scan_url',
|
|
76
135
|
title: 'Check a deployed site',
|
|
77
136
|
description: 'Checks a live website from the outside, the way an attacker would: downloadable .env, .git and backup files, public source maps, API keys in the JavaScript, Supabase tables readable with the public key, and HTTPS and security headers. ' +
|
|
78
|
-
'
|
|
137
|
+
'The only tool that uses the network: it sends ordinary GET requests to the address given, and only to public sites.',
|
|
79
138
|
inputSchema: {
|
|
80
139
|
type: 'object',
|
|
81
140
|
properties: {
|
|
@@ -95,108 +154,84 @@ export function mcpTools() {
|
|
|
95
154
|
title: 'List the checks',
|
|
96
155
|
description: 'Lists the categories and every finding and checklist id, for the only and skip options and for .launchdoctorignore.',
|
|
97
156
|
inputSchema: { type: 'object', properties: {}, additionalProperties: false },
|
|
98
|
-
annotations:
|
|
157
|
+
annotations: READ_ONLY,
|
|
99
158
|
},
|
|
100
159
|
];
|
|
101
160
|
}
|
|
102
161
|
// ---------- tool results ----------
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
162
|
+
/** Text for the model: control characters removed, and cut at MAX_TEXT with a note. */
|
|
163
|
+
export function capText(text, max = MAX_TEXT) {
|
|
164
|
+
const clean = stripControl(text);
|
|
165
|
+
if (clean.length <= max)
|
|
166
|
+
return clean;
|
|
167
|
+
return `${clean.slice(0, max)}\n...(cut at ${max} characters; ask about one finding with explain_finding)`;
|
|
108
168
|
}
|
|
109
|
-
function
|
|
110
|
-
|
|
111
|
-
const known = f.baseline ? ' [in baseline]' : '';
|
|
112
|
-
return [
|
|
113
|
-
`${n}. ${f.title}${where} · id: ${f.id}${known}`,
|
|
114
|
-
indent(`Why it matters: ${f.why}`),
|
|
115
|
-
indent(`Fix prompt:\n${indent(f.fixPrompt, ' ')}`),
|
|
116
|
-
].join('\n');
|
|
169
|
+
function result(text, data) {
|
|
170
|
+
return { content: [{ type: 'text', text: capText(text) }], structuredContent: data };
|
|
117
171
|
}
|
|
118
|
-
function
|
|
119
|
-
|
|
120
|
-
return [];
|
|
121
|
-
const out = ['', `${title} (${items.length})`, ''];
|
|
122
|
-
for (const [i, item] of items.slice(0, MAX_LISTED).entries())
|
|
123
|
-
out.push(entry(item, i + 1));
|
|
124
|
-
if (items.length > MAX_LISTED)
|
|
125
|
-
out.push(`...and ${items.length - MAX_LISTED} more.`);
|
|
126
|
-
return out;
|
|
172
|
+
function toolError(message) {
|
|
173
|
+
return { content: [{ type: 'text', text: stripControl(message) }], isError: true };
|
|
127
174
|
}
|
|
128
|
-
/**
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
175
|
+
/** One line per finding: no fix prompt and no long explanation, so the list stays small. */
|
|
176
|
+
function shortFinding(report, f) {
|
|
177
|
+
return {
|
|
178
|
+
id: f.ref ?? '',
|
|
179
|
+
number: promptNumber(report, f),
|
|
180
|
+
rule: f.id,
|
|
181
|
+
severity: f.severity,
|
|
182
|
+
title: f.display?.title ?? f.title,
|
|
183
|
+
...(f.location?.file ? { file: f.location.file } : {}),
|
|
184
|
+
...(f.location?.line ? { line: f.location.line } : {}),
|
|
185
|
+
...(f.baseline ? { baseline: true } : {}),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const NOT_ONLINE = 'This scan made no network requests (no email DNS lookups); scan_url checks the live site.';
|
|
189
|
+
/** A scan as a short summary (text) and data, with at most MAX_LISTED findings. */
|
|
190
|
+
export function scanResult(report, includeBaseline = false, extra = []) {
|
|
191
|
+
const all = orderedFindings(report);
|
|
192
|
+
const listed = all.filter((f) => includeBaseline || !f.baseline);
|
|
193
|
+
const hiddenBaseline = all.length - listed.length;
|
|
194
|
+
const shown = listed.slice(0, MAX_LISTED).map((f) => shortFinding(report, f));
|
|
195
|
+
const notes = [...coverageNotes(report), ...(report.mode === 'live' ? [] : [NOT_ONLINE])];
|
|
196
|
+
const grades = report.grades;
|
|
135
197
|
const lines = [
|
|
136
|
-
`Launch Doctor ${report.version}:
|
|
137
|
-
|
|
198
|
+
`Launch Doctor ${report.version}: ${report.projectName}. ${report.verdict}.`,
|
|
199
|
+
grades
|
|
200
|
+
? `Security ${grades.security.grade ?? '-'}, Launch readiness ${grades.launch.grade ?? '-'} (overall ${report.grade}, ${report.score}/100).`
|
|
201
|
+
: `Grade ${report.grade} (${report.score}/100).`,
|
|
202
|
+
`${plural(report.counts.critical, 'critical problem')}, ${plural(report.counts.warning, 'warning')}, ${report.counts.manual} to check by hand.`,
|
|
203
|
+
...extra,
|
|
204
|
+
'',
|
|
205
|
+
...(shown.length
|
|
206
|
+
? shown.map((f) => `${f.number}. [${f.severity}] ${f.title}${f.file ? ` (${shortWhere({ file: f.file, line: f.line })})` : ''} · id ${f.id} · rule ${f.rule}${f.baseline ? ' · in baseline' : ''}`)
|
|
207
|
+
: [
|
|
208
|
+
hiddenBaseline
|
|
209
|
+
? `No new problems found. The grade still counts the ${plural(hiddenBaseline, 'known finding')} accepted in .launchdoctor-baseline.json (set includeBaseline to list them).`
|
|
210
|
+
: 'No problems found.',
|
|
211
|
+
]),
|
|
212
|
+
...(listed.length > shown.length
|
|
213
|
+
? [`...and ${listed.length - shown.length} more (fix these first, then rescan).`]
|
|
214
|
+
: []),
|
|
215
|
+
'',
|
|
216
|
+
'Coverage:',
|
|
217
|
+
...notes.map((n) => `- ${n}`),
|
|
218
|
+
...(report.nestedRepos?.length ? [`- ${nestedReposNote(report.nestedRepos)}`] : []),
|
|
219
|
+
'',
|
|
220
|
+
'Next: explain_finding or fix_prompt with an id, apply the fix, then rescan with that id.',
|
|
138
221
|
];
|
|
139
|
-
|
|
140
|
-
lines.push(nestedReposNote(report.nestedRepos));
|
|
141
|
-
if (report.truncated)
|
|
142
|
-
lines.push('Very large project: only the first 20,000 files were checked.');
|
|
143
|
-
if (report.byteLimitReached)
|
|
144
|
-
lines.push(BYTE_LIMIT_NOTE);
|
|
145
|
-
lines.push(...listSection('Critical', shown.filter((f) => f.severity === 'critical'), (f, n) => findingEntry(n, f)), ...listSection('Warnings', shown.filter((f) => f.severity === 'warning'), (f, n) => findingEntry(n, f)), ...listSection('Check by hand, outside the code', manual, (m, n) => `${n}. ${m.title} · id: ${m.id}\n${indent(m.how)}`));
|
|
146
|
-
if (!shown.length) {
|
|
147
|
-
lines.push('', hiddenBaseline
|
|
148
|
-
? `No new problems found. The grade still counts the ${plural(hiddenBaseline, 'known finding')} accepted in .launchdoctor-baseline.json.`
|
|
149
|
-
: 'No problems found.');
|
|
150
|
-
}
|
|
151
|
-
lines.push('', `Scorecard: ${report.categories.map((c) => `${c.name} ${c.skipped ? 'skipped' : c.applicable ? (c.grade ?? '-') : 'not used'}`).join(' · ')}`);
|
|
152
|
-
if (hiddenBaseline) {
|
|
153
|
-
lines.push(`${plural(hiddenBaseline, 'known finding')} in .launchdoctor-baseline.json not listed (set includeBaseline to see them).`);
|
|
154
|
-
}
|
|
155
|
-
if (report.ignored) {
|
|
156
|
-
lines.push(`${plural(report.ignored, 'item')} silenced by .launchdoctorignore or launch-doctor-ignore comments.`);
|
|
157
|
-
}
|
|
158
|
-
return lines.join('\n');
|
|
159
|
-
}
|
|
160
|
-
/** The same report as data, for clients that read structured tool output. */
|
|
161
|
-
export function reportData(report, includeBaseline = false) {
|
|
162
|
-
return {
|
|
222
|
+
return result(lines.join('\n'), {
|
|
163
223
|
grade: report.grade,
|
|
164
224
|
score: report.score,
|
|
225
|
+
...(grades ? { grades } : {}),
|
|
165
226
|
verdict: report.verdict,
|
|
166
227
|
projectName: report.projectName,
|
|
167
228
|
counts: report.counts,
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
skipped: Boolean(c.skipped),
|
|
175
|
-
})),
|
|
176
|
-
findings: report.categories.flatMap((c) => c.findings
|
|
177
|
-
.filter((f) => includeBaseline || !f.baseline)
|
|
178
|
-
.map((f) => ({
|
|
179
|
-
id: f.id,
|
|
180
|
-
category: c.id,
|
|
181
|
-
severity: f.severity,
|
|
182
|
-
title: f.title,
|
|
183
|
-
file: f.location?.file,
|
|
184
|
-
line: f.location?.line,
|
|
185
|
-
why: f.why,
|
|
186
|
-
fixPrompt: f.fixPrompt,
|
|
187
|
-
baseline: Boolean(f.baseline),
|
|
188
|
-
}))),
|
|
189
|
-
manual: report.categories.flatMap((c) => c.manual.map((m) => ({ id: m.id, category: c.id, title: m.title, how: m.how }))),
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
function toolError(message) {
|
|
193
|
-
return { content: [{ type: 'text', text: message }], isError: true };
|
|
194
|
-
}
|
|
195
|
-
function reportResult(report, includeBaseline) {
|
|
196
|
-
return {
|
|
197
|
-
content: [{ type: 'text', text: reportText(report, includeBaseline) }],
|
|
198
|
-
structuredContent: reportData(report, includeBaseline),
|
|
199
|
-
};
|
|
229
|
+
findings: shown,
|
|
230
|
+
listed: shown.length,
|
|
231
|
+
total: listed.length,
|
|
232
|
+
coverage: notes,
|
|
233
|
+
manual: report.categories.flatMap((c) => c.manual.map((m) => ({ id: m.id, category: c.id, title: m.title }))),
|
|
234
|
+
});
|
|
200
235
|
}
|
|
201
236
|
const isStringArray = (v) => Array.isArray(v) && v.every((x) => typeof x === 'string');
|
|
202
237
|
/** Checks the arguments by hand (no schema library), so a bad call gets a message the model can act on. */
|
|
@@ -214,42 +249,146 @@ function badArgs(args, shape) {
|
|
|
214
249
|
}
|
|
215
250
|
return null;
|
|
216
251
|
}
|
|
252
|
+
/** The project folder for a call: the server's folder, or a folder inside it. Never outside. */
|
|
253
|
+
function rootFor(args, cwd) {
|
|
254
|
+
const base = projectRoot(cwd);
|
|
255
|
+
const path = args.path;
|
|
256
|
+
if (!path || path === '.')
|
|
257
|
+
return base;
|
|
258
|
+
const root = projectRoot(`${base}/${normalizeRelative(base, path)}`);
|
|
259
|
+
// normalizeRelative compares the paths as text. A link inside the folder (a symlink, or a junction on Windows)
|
|
260
|
+
// could still lead outside it, so the real paths are compared too.
|
|
261
|
+
const rel = relative(realpathSync.native(base), realpathSync.native(root));
|
|
262
|
+
if (rel.startsWith('..') || isAbsolute(rel))
|
|
263
|
+
throw new CoreError('usage', `${path} is outside the project folder`);
|
|
264
|
+
return root;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* The last full scan of each folder, so explain_finding, fix_prompt and rescan can find an id without scanning again.
|
|
268
|
+
* Only kept for this server process. An id or number that isn't in it gets a fresh scan; rescan doesn't change it, so
|
|
269
|
+
* numbers keep meaning what the last scan_project said.
|
|
270
|
+
*/
|
|
271
|
+
const lastScans = new Map();
|
|
272
|
+
async function fullScan(root, args = {}) {
|
|
273
|
+
const report = await scan({
|
|
274
|
+
root,
|
|
275
|
+
offline: true,
|
|
276
|
+
only: args.only,
|
|
277
|
+
skip: args.skip,
|
|
278
|
+
history: args.history === true,
|
|
279
|
+
});
|
|
280
|
+
if (!args.only && !args.skip)
|
|
281
|
+
lastScans.set(root, report);
|
|
282
|
+
return report;
|
|
283
|
+
}
|
|
284
|
+
/** A finding by id or number: from the last scan if it is there, otherwise from a fresh one. */
|
|
285
|
+
async function lookup(root, id) {
|
|
286
|
+
for (const report of [lastScans.get(root), undefined]) {
|
|
287
|
+
const r = report ?? (await fullScan(root));
|
|
288
|
+
const finding = findFinding(r, id);
|
|
289
|
+
if (finding)
|
|
290
|
+
return { report: r, finding };
|
|
291
|
+
if (!report)
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
throw new CoreError('usage', `No finding with id ${id}. Run scan_project to get the current ids.`);
|
|
295
|
+
}
|
|
217
296
|
async function scanProject(args, cwd) {
|
|
218
297
|
const problem = badArgs(args, {
|
|
219
298
|
path: 'string',
|
|
220
299
|
only: 'string[]',
|
|
221
300
|
skip: 'string[]',
|
|
222
301
|
offline: 'boolean',
|
|
223
|
-
domain: 'string',
|
|
224
302
|
history: 'boolean',
|
|
225
303
|
includeBaseline: 'boolean',
|
|
226
304
|
});
|
|
227
305
|
if (problem)
|
|
228
306
|
return toolError(problem);
|
|
229
|
-
const root =
|
|
230
|
-
|
|
231
|
-
return toolError(`Folder not found: ${root}`);
|
|
232
|
-
const domain = args.domain
|
|
233
|
-
?.trim()
|
|
234
|
-
.toLowerCase()
|
|
235
|
-
.replace(/^https?:\/\//, '')
|
|
236
|
-
.replace(/[/?#].*$/, '');
|
|
237
|
-
let report;
|
|
238
|
-
try {
|
|
239
|
-
report = await runDoctor(root, {
|
|
240
|
-
offline: args.offline === true,
|
|
241
|
-
domain: domain || undefined,
|
|
242
|
-
only: args.only,
|
|
243
|
-
skip: args.skip,
|
|
244
|
-
history: args.history === true ? { depth: DEFAULT_HISTORY_DEPTH } : undefined,
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
catch (err) {
|
|
248
|
-
return toolError(err.message);
|
|
249
|
-
}
|
|
307
|
+
const root = rootFor(args, cwd);
|
|
308
|
+
const report = await fullScan(root, args);
|
|
250
309
|
if (report.filesScanned === 0)
|
|
251
310
|
return toolError(`No project files found in ${root}.`);
|
|
252
|
-
return
|
|
311
|
+
return scanResult(report, args.includeBaseline === true);
|
|
312
|
+
}
|
|
313
|
+
async function explainFinding(args, cwd) {
|
|
314
|
+
const problem = badArgs(args, { id: 'string', path: 'string' });
|
|
315
|
+
if (problem)
|
|
316
|
+
return toolError(problem);
|
|
317
|
+
const { report, finding } = await lookup(rootFor(args, cwd), args.id);
|
|
318
|
+
const d = findingDetails(report, finding);
|
|
319
|
+
const e = d.explanation;
|
|
320
|
+
const where = d.file ? shortWhere({ file: d.file, line: d.line }) : 'the whole project';
|
|
321
|
+
const lines = [
|
|
322
|
+
`${d.number}. ${d.title} [${d.severity}] · id ${d.ref} · rule ${d.rule}`,
|
|
323
|
+
`Where: ${where}`,
|
|
324
|
+
`This finding: ${d.why}`,
|
|
325
|
+
...(e
|
|
326
|
+
? [
|
|
327
|
+
`Why it matters: ${e.whyItMatters}`,
|
|
328
|
+
`How to confirm: ${e.howToConfirm}`,
|
|
329
|
+
`How to fix: ${e.howToFix}`,
|
|
330
|
+
`False alarm? ${e.falsePositiveHint}`,
|
|
331
|
+
]
|
|
332
|
+
: []),
|
|
333
|
+
...(d.rotate ? [`Rotate the key first: ${d.rotate}`] : []),
|
|
334
|
+
...(d.usage ? [`Check whether someone used it: ${d.usage}`] : []),
|
|
335
|
+
'The fix prompt for this finding: fix_prompt with this id.',
|
|
336
|
+
];
|
|
337
|
+
return result(lines.join('\n'), { ...d });
|
|
338
|
+
}
|
|
339
|
+
async function fixPrompt(args, cwd) {
|
|
340
|
+
const problem = badArgs(args, { id: 'string', path: 'string' });
|
|
341
|
+
if (problem)
|
|
342
|
+
return toolError(problem);
|
|
343
|
+
const { report, finding } = await lookup(rootFor(args, cwd), args.id);
|
|
344
|
+
const d = findingDetails(report, finding);
|
|
345
|
+
return result(`Fix prompt ${d.number} (${d.title}, id ${d.ref}):\n\n${finding.fixPrompt}\n\nThen call rescan with ids ["${d.ref}"].`, {
|
|
346
|
+
id: d.ref,
|
|
347
|
+
number: d.number,
|
|
348
|
+
rule: d.rule,
|
|
349
|
+
title: d.title,
|
|
350
|
+
fixPrompt: finding.fixPrompt,
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
async function rescanTool(args, cwd) {
|
|
354
|
+
const problem = badArgs(args, { path: 'string', ids: 'string[]', paths: 'string[]' });
|
|
355
|
+
if (problem)
|
|
356
|
+
return toolError(problem);
|
|
357
|
+
const root = rootFor(args, cwd);
|
|
358
|
+
const ids = args.ids ?? [];
|
|
359
|
+
// Numbers are positions in the last scan: turn them into ids first.
|
|
360
|
+
const previous = lastScans.get(root) ?? (ids.length ? await fullScan(root) : undefined);
|
|
361
|
+
const refs = ids.map((id) => (previous ? (findFinding(previous, id)?.ref ?? id) : id));
|
|
362
|
+
const { report, files, fixed } = await rescan({
|
|
363
|
+
root,
|
|
364
|
+
offline: true,
|
|
365
|
+
...(refs.length ? { refs, previous } : {}),
|
|
366
|
+
...(args.paths ? { paths: args.paths } : {}),
|
|
367
|
+
});
|
|
368
|
+
const extra = [
|
|
369
|
+
`Rescanned ${plural(files.length, 'file')}: ${files.slice(0, 10).join(', ')}${files.length > 10 ? ', ...' : ''}.`,
|
|
370
|
+
...(refs.length
|
|
371
|
+
? [
|
|
372
|
+
`Fixed: ${fixed.length ? fixed.join(', ') : 'none of the given ids'}. Still there: ${refs.filter((r) => !fixed.includes(r)).join(', ') || 'none'}.`,
|
|
373
|
+
]
|
|
374
|
+
: []),
|
|
375
|
+
];
|
|
376
|
+
const out = scanResult(report, false, extra);
|
|
377
|
+
out.structuredContent = { ...out.structuredContent, files, fixed };
|
|
378
|
+
return out;
|
|
379
|
+
}
|
|
380
|
+
function gradesTool(args, cwd) {
|
|
381
|
+
const problem = badArgs(args, { path: 'string' });
|
|
382
|
+
if (problem)
|
|
383
|
+
return toolError(problem);
|
|
384
|
+
const entries = gradeHistory(rootFor(args, cwd)).slice().reverse();
|
|
385
|
+
const text = entries.length
|
|
386
|
+
? entries
|
|
387
|
+
.slice(0, 20)
|
|
388
|
+
.map((e) => `- ${describeEntry(e)}${e.commit ? ` (commit ${e.commit.slice(0, 7)})` : ''}`)
|
|
389
|
+
.join('\n')
|
|
390
|
+
: 'No grades recorded yet. Run "npx launch-doctor --record" to save one.';
|
|
391
|
+
return result(text, { entries: entries.slice(0, 50) });
|
|
253
392
|
}
|
|
254
393
|
async function scanUrl(args) {
|
|
255
394
|
const problem = badArgs(args, { url: 'string', skipDatabase: 'boolean' });
|
|
@@ -257,20 +396,15 @@ async function scanUrl(args) {
|
|
|
257
396
|
return toolError(problem);
|
|
258
397
|
if (typeof args.url !== 'string' || !args.url.trim())
|
|
259
398
|
return toolError('"url" is required, for example https://myapp.com.');
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
return reportResult(report, false);
|
|
265
|
-
}
|
|
266
|
-
catch (err) {
|
|
267
|
-
return toolError(err.message);
|
|
268
|
-
}
|
|
399
|
+
// An AI tool decides what to scan here, and a prompt injection could aim it at your network, so only public
|
|
400
|
+
// sites are allowed. The CLI (where you type the address yourself) also accepts your own dev server.
|
|
401
|
+
const report = await runLiveScan(args.url, { skipDatabase: args.skipDatabase === true, allowPrivateTarget: false });
|
|
402
|
+
return scanResult(report);
|
|
269
403
|
}
|
|
270
404
|
function listChecks() {
|
|
271
405
|
const categories = CHECKS.map((c) => ({ id: c.id, name: c.name, ids: [...c.ids] }));
|
|
272
406
|
const text = categories.map((c) => `${c.id} (${c.name}): ${c.ids.join(', ')}`).join('\n');
|
|
273
|
-
return
|
|
407
|
+
return result(text, { categories });
|
|
274
408
|
}
|
|
275
409
|
/** Run one tool. Unknown tool names are a protocol error; bad arguments are a tool error the model can fix. */
|
|
276
410
|
export async function callTool(name, args, cwd = process.cwd()) {
|
|
@@ -278,13 +412,24 @@ export async function callTool(name, args, cwd = process.cwd()) {
|
|
|
278
412
|
if (typeof a !== 'object' || Array.isArray(a))
|
|
279
413
|
return toolError('Arguments must be an object.');
|
|
280
414
|
const params = a;
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
415
|
+
const tools = {
|
|
416
|
+
scan_project: () => scanProject(params, cwd),
|
|
417
|
+
explain_finding: () => explainFinding(params, cwd),
|
|
418
|
+
fix_prompt: () => fixPrompt(params, cwd),
|
|
419
|
+
rescan: () => rescanTool(params, cwd),
|
|
420
|
+
grade_history: () => gradesTool(params, cwd),
|
|
421
|
+
scan_url: () => scanUrl(params),
|
|
422
|
+
list_checks: () => listChecks(),
|
|
423
|
+
};
|
|
424
|
+
const run = typeof name === 'string' && Object.hasOwn(tools, name) ? tools[name] : undefined;
|
|
425
|
+
if (!run)
|
|
426
|
+
throw new RpcError(-32602, `Unknown tool: ${String(name)}`);
|
|
427
|
+
try {
|
|
428
|
+
return await run();
|
|
429
|
+
}
|
|
430
|
+
catch (err) {
|
|
431
|
+
return toolError(err.message);
|
|
432
|
+
}
|
|
288
433
|
}
|
|
289
434
|
// ---------- JSON-RPC ----------
|
|
290
435
|
function negotiate(params) {
|
|
@@ -295,7 +440,8 @@ function negotiate(params) {
|
|
|
295
440
|
capabilities: { tools: { listChanged: false } },
|
|
296
441
|
serverInfo: { name: 'launch-doctor', title: 'Launch Doctor', version: version() },
|
|
297
442
|
instructions: 'Use scan_project before a launch or a big release to find leaked keys, open databases, unverified payment webhooks, unprotected AI endpoints and missing legal pages. ' +
|
|
298
|
-
'
|
|
443
|
+
'It lists findings with ids. For each one (critical first): explain_finding if unsure, fix_prompt for the change, apply it, then rescan with that id. ' +
|
|
444
|
+
'Use scan_url on the deployed site after deploying (the only tool that uses the network).',
|
|
299
445
|
};
|
|
300
446
|
}
|
|
301
447
|
async function dispatch(method, params, cwd) {
|
package/dist/src/prompt.d.ts
CHANGED
|
@@ -20,4 +20,13 @@ export declare function unsupportedLanguage(root: string, report: Report): {
|
|
|
20
20
|
language: string;
|
|
21
21
|
marker: string;
|
|
22
22
|
} | null;
|
|
23
|
+
/**
|
|
24
|
+
* Folders below `root` (three levels at most) that hold a project of their own, when `root` itself has no project
|
|
25
|
+
* file. Run in a home folder or on the Desktop, a scan mixes them all into one grade. Null when `root` is a project,
|
|
26
|
+
* or a workspace root (a monorepo, see hasWorkspaceConfig), or when fewer than two are found. Only folder names are
|
|
27
|
+
* read, at most 2,000 folders.
|
|
28
|
+
*/
|
|
29
|
+
export declare function separateProjects(root: string): string[] | null;
|
|
30
|
+
/** The note under the report for separateProjects: what they are, and how to check one. */
|
|
31
|
+
export declare function separateProjectsNote(root: string, projects: string[], cwd?: string): string;
|
|
23
32
|
export {};
|