launch-doctor 0.3.0 → 0.4.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/CHANGELOG.md +77 -6
- package/LICENSE +1 -0
- package/README.md +95 -20
- package/dist/src/access.d.ts +52 -0
- package/dist/src/access.js +482 -0
- package/dist/src/baseline.d.ts +6 -3
- package/dist/src/baseline.js +15 -8
- package/dist/src/changed.d.ts +12 -0
- package/dist/src/changed.js +34 -0
- package/dist/src/checks/access.d.ts +7 -0
- package/dist/src/checks/access.js +868 -0
- package/dist/src/checks/costs.d.ts +7 -0
- package/dist/src/checks/costs.js +14 -6
- package/dist/src/checks/database.js +79 -3
- package/dist/src/checks/first-impressions.js +22 -2
- package/dist/src/checks/legal.js +3 -1
- package/dist/src/checks/monitoring.js +8 -5
- package/dist/src/checks/payments.js +10 -2
- package/dist/src/checks/secrets.d.ts +8 -0
- package/dist/src/checks/secrets.js +82 -9
- package/dist/src/checks/supabase-storage.d.ts +31 -0
- package/dist/src/checks/supabase-storage.js +216 -0
- package/dist/src/cli.js +111 -9
- package/dist/src/demo.js +1 -0
- package/dist/src/detect.d.ts +1 -1
- package/dist/src/detect.js +1 -1
- package/dist/src/fixes.js +8 -5
- package/dist/src/git.d.ts +25 -0
- package/dist/src/git.js +121 -0
- package/dist/src/github.d.ts +11 -1
- package/dist/src/github.js +56 -14
- package/dist/src/history.js +17 -3
- package/dist/src/hook.d.ts +13 -0
- package/dist/src/hook.js +96 -0
- package/dist/src/hooks-setup.d.ts +24 -0
- package/dist/src/hooks-setup.js +187 -0
- package/dist/src/ignore.d.ts +36 -0
- package/dist/src/ignore.js +164 -0
- package/dist/src/index.js +28 -3
- package/dist/src/live.d.ts +9 -0
- package/dist/src/live.js +73 -22
- package/dist/src/mcp.js +7 -2
- package/dist/src/net-guard.d.ts +34 -0
- package/dist/src/net-guard.js +144 -0
- package/dist/src/project.d.ts +34 -5
- package/dist/src/project.js +145 -111
- package/dist/src/report.d.ts +2 -0
- package/dist/src/report.js +11 -0
- package/dist/src/routes.d.ts +10 -0
- package/dist/src/routes.js +21 -4
- package/dist/src/safe-fs.d.ts +19 -0
- package/dist/src/safe-fs.js +77 -0
- package/dist/src/sanitize.d.ts +17 -0
- package/dist/src/sanitize.js +90 -0
- package/dist/src/sarif.js +1 -1
- package/dist/src/summary.d.ts +4 -0
- package/dist/src/summary.js +9 -4
- package/dist/src/types.d.ts +24 -0
- package/dist/src/util.d.ts +1 -1
- package/dist/src/util.js +1 -1
- package/package.json +9 -5
package/dist/src/project.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
+
import { matchesGitignore } from './ignore.js';
|
|
1
2
|
import type { Options } from './types.js';
|
|
3
|
+
/** Largest .launchdoctorignore or baseline file read; anything bigger is treated as missing. */
|
|
4
|
+
export declare const CONFIG_FILE_MAX_BYTES: number;
|
|
5
|
+
/** Lines of .launchdoctorignore used; the rest are skipped and the report says so. */
|
|
6
|
+
export declare const MAX_IGNORE_LINES = 1000;
|
|
2
7
|
export interface SourceFile {
|
|
3
8
|
/** Path relative to the project root, always with forward slashes. */
|
|
4
9
|
path: string;
|
|
@@ -11,6 +16,8 @@ export interface Match {
|
|
|
11
16
|
match: RegExpExecArray;
|
|
12
17
|
}
|
|
13
18
|
export type FileFilter = (file: SourceFile) => boolean;
|
|
19
|
+
/** Total size of the text files read in one scan (128 MB). Past it, the rest are skipped and the report says so. */
|
|
20
|
+
export declare const MAX_TOTAL_BYTES: number;
|
|
14
21
|
export declare function isCodeFile(file: SourceFile | string): boolean;
|
|
15
22
|
export declare function isEnvPath(p: string): boolean;
|
|
16
23
|
export declare function isExampleEnvPath(p: string): boolean;
|
|
@@ -29,6 +36,9 @@ export declare class ProjectContext {
|
|
|
29
36
|
/** Text files we read. */
|
|
30
37
|
readonly files: SourceFile[];
|
|
31
38
|
truncated: boolean;
|
|
39
|
+
/** Did the scan stop reading files because MAX_TOTAL_BYTES were read? */
|
|
40
|
+
byteLimitReached: boolean;
|
|
41
|
+
private bytesRead;
|
|
32
42
|
/** Folders left out because they are separate git repositories (they have their own .git folder). */
|
|
33
43
|
readonly nestedRepos: string[];
|
|
34
44
|
readonly deps: Set<string>;
|
|
@@ -45,15 +55,21 @@ export declare class ProjectContext {
|
|
|
45
55
|
readonly isMobileApp: boolean;
|
|
46
56
|
/** Every package.json folder ("" for the root), and whether it is a mobile app. */
|
|
47
57
|
private readonly packageDirs;
|
|
58
|
+
/** Does the root package.json declare a "bin" (is this a command-line tool)? */
|
|
59
|
+
private rootIsCli;
|
|
48
60
|
/** Finding / checklist ids silenced in .launchdoctorignore. */
|
|
49
61
|
readonly ignoredIds: Set<string>;
|
|
62
|
+
/** Lines of .launchdoctorignore past the first MAX_IGNORE_LINES, which were not used. */
|
|
63
|
+
ignoreLinesSkipped: number;
|
|
50
64
|
private ignoredPathRules;
|
|
51
65
|
private readonly tracked;
|
|
52
66
|
private readonly byPath;
|
|
53
67
|
private readonly clientCache;
|
|
54
68
|
private readonly lineStarts;
|
|
55
69
|
private readonly codeCache;
|
|
70
|
+
private readonly addedCache;
|
|
56
71
|
private readonly maxFiles;
|
|
72
|
+
private readonly maxBytes;
|
|
57
73
|
constructor(root: string, options?: Options);
|
|
58
74
|
/**
|
|
59
75
|
* .launchdoctorignore: one entry per line.
|
|
@@ -72,6 +88,13 @@ export declare class ProjectContext {
|
|
|
72
88
|
/** Is this package actually imported in code (not just listed in package.json)? "/" suffix matches a scope. */
|
|
73
89
|
imported(name: string): boolean;
|
|
74
90
|
file(path: string): SourceFile | undefined;
|
|
91
|
+
/**
|
|
92
|
+
* With `configFrom`: was this line added or changed since that git ref? Used so an inline
|
|
93
|
+
* "launch-doctor-ignore" comment a pull request adds can't silence the pull request's own finding.
|
|
94
|
+
* Without `configFrom` this is always false. If git can't tell (it fails, or the file is new),
|
|
95
|
+
* the answer is true, so the comment is not trusted.
|
|
96
|
+
*/
|
|
97
|
+
addedSinceConfigRef(relPath: string, line: number): boolean;
|
|
75
98
|
hasPath(re: RegExp): boolean;
|
|
76
99
|
/** Find every regex match across files (optionally filtered). */
|
|
77
100
|
find(re: RegExp, filter?: FileFilter, limit?: number): Match[];
|
|
@@ -96,10 +119,16 @@ export declare class ProjectContext {
|
|
|
96
119
|
/** Short list of the main tools this project uses, for the report header. */
|
|
97
120
|
stack(): string[];
|
|
98
121
|
}
|
|
122
|
+
export { git, gitArgs } from './git.js';
|
|
123
|
+
/** Throws unless `ref` names a commit in the git repository that holds `root`. */
|
|
124
|
+
export declare function assertConfigRef(root: string, ref: string): void;
|
|
99
125
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
126
|
+
* The line numbers (in the working copy) of `file` that were added or changed since git ref `ref`,
|
|
127
|
+
* or 'all' when the file is new or git can't say. `file` is relative to `root`.
|
|
102
128
|
*/
|
|
103
|
-
export declare function
|
|
104
|
-
/**
|
|
105
|
-
export declare function
|
|
129
|
+
export declare function addedLinesSince(root: string, ref: string, file: string): Set<number> | 'all';
|
|
130
|
+
/** The text of `file` (a name inside `root`) as it was at git ref `ref`, or null if it didn't exist there. */
|
|
131
|
+
export declare function readAtRef(root: string, ref: string, file: string): string | null;
|
|
132
|
+
export declare function findGitRoot(root: string): string | null;
|
|
133
|
+
/** Minimal .gitignore matching, used when git isn't available (see src/ignore.ts). */
|
|
134
|
+
export { matchesGitignore };
|
package/dist/src/project.js
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import { execFileSync } from 'node:child_process';
|
|
2
1
|
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
3
2
|
import { homedir } from 'node:os';
|
|
4
3
|
import { basename, dirname, extname, join, resolve } from 'node:path';
|
|
5
4
|
import { stripComments } from './comments.js';
|
|
5
|
+
import { git, gitStatus, SAFE_DIFF } from './git.js';
|
|
6
|
+
import { compileIgnore, matchesGitignore, matchesIgnore } from './ignore.js';
|
|
7
|
+
import { entryKind, readRegularFile } from './safe-fs.js';
|
|
8
|
+
/** Largest .launchdoctorignore or baseline file read; anything bigger is treated as missing. */
|
|
9
|
+
export const CONFIG_FILE_MAX_BYTES = 16 * 1024 * 1024;
|
|
10
|
+
/** Lines of .launchdoctorignore used; the rest are skipped and the report says so. */
|
|
11
|
+
export const MAX_IGNORE_LINES = 1000;
|
|
6
12
|
const SKIP_DIRS = new Set([
|
|
7
13
|
'node_modules',
|
|
8
14
|
'dist',
|
|
@@ -71,6 +77,8 @@ const SKIP_FILES = new Set([
|
|
|
71
77
|
]);
|
|
72
78
|
const MAX_FILES = 20_000;
|
|
73
79
|
const MAX_FILE_SIZE = 1_000_000;
|
|
80
|
+
/** Total size of the text files read in one scan (128 MB). Past it, the rest are skipped and the report says so. */
|
|
81
|
+
export const MAX_TOTAL_BYTES = 128 * 1024 * 1024;
|
|
74
82
|
export function isCodeFile(file) {
|
|
75
83
|
const p = typeof file === 'string' ? file : file.path;
|
|
76
84
|
return CODE_EXT.has(extname(p).toLowerCase());
|
|
@@ -80,7 +88,8 @@ export function isEnvPath(p) {
|
|
|
80
88
|
return name === '.env' || name.startsWith('.env.') || name.endsWith('.env');
|
|
81
89
|
}
|
|
82
90
|
export function isExampleEnvPath(p) {
|
|
83
|
-
|
|
91
|
+
// .env.example, .env.sample, and .example.env (the word can come before or after "env")
|
|
92
|
+
return (isEnvPath(p) && /(?:^|[._-])(?:example|sample|template|defaults?|dist|schema|tpl)(?:[._-]|$)/i.test(basename(p)));
|
|
84
93
|
}
|
|
85
94
|
export function isRealEnvPath(p) {
|
|
86
95
|
return isEnvPath(p) && !isExampleEnvPath(p);
|
|
@@ -142,6 +151,8 @@ const FRONTEND_PACKAGES = [
|
|
|
142
151
|
'@remix-run/react',
|
|
143
152
|
'lit',
|
|
144
153
|
];
|
|
154
|
+
/** Server frameworks: a command-line tool that depends on one may well be serving a website. */
|
|
155
|
+
const SERVER_FRAMEWORKS = ['express', 'fastify', 'hono', '@hono/node-server', 'koa', '@nestjs/core', '@hapi/hapi'];
|
|
145
156
|
/** Turn an import specifier into a package name ("@scope/pkg/sub@1" -> "@scope/pkg"). */
|
|
146
157
|
export function packageNameOf(spec) {
|
|
147
158
|
let s = spec
|
|
@@ -156,7 +167,9 @@ export function packageNameOf(spec) {
|
|
|
156
167
|
s = s.replace(/(?<=.)@.*$/, '');
|
|
157
168
|
return s || null;
|
|
158
169
|
}
|
|
159
|
-
|
|
170
|
+
// "^[ \t]*import", not "^\s*import": with the m flag, \s* ran across every following newline from each line start,
|
|
171
|
+
// so a file of blank lines took quadratic time (100 KB of newlines hung the scan).
|
|
172
|
+
const IMPORT_RE = /(?:\bfrom\s*|\bimport\s*\(\s*|\brequire\s*\(\s*|^[ \t]*import\s+)["']([^"'\n]+)["']/gm;
|
|
160
173
|
const PY_IMPORT_RE = /^[ \t]*(?:from|import)[ \t]+([A-Za-z_][\w.]*)/gm;
|
|
161
174
|
/** Python package/module names mapped to the npm names the checks look for. */
|
|
162
175
|
const PY_ALIASES = {
|
|
@@ -196,6 +209,10 @@ const PY_ALIASES = {
|
|
|
196
209
|
'flask-limiter': 'limiter',
|
|
197
210
|
slowapi: 'limiter',
|
|
198
211
|
'django-ratelimit': 'limiter',
|
|
212
|
+
jwt: 'pyjwt',
|
|
213
|
+
pyjwt: 'pyjwt',
|
|
214
|
+
jose: 'python-jose',
|
|
215
|
+
'python-jose': 'python-jose',
|
|
199
216
|
};
|
|
200
217
|
function pythonDep(name) {
|
|
201
218
|
const lower = name.toLowerCase();
|
|
@@ -210,6 +227,9 @@ export class ProjectContext {
|
|
|
210
227
|
/** Text files we read. */
|
|
211
228
|
files = [];
|
|
212
229
|
truncated = false;
|
|
230
|
+
/** Did the scan stop reading files because MAX_TOTAL_BYTES were read? */
|
|
231
|
+
byteLimitReached = false;
|
|
232
|
+
bytesRead = 0;
|
|
213
233
|
/** Folders left out because they are separate git repositories (they have their own .git folder). */
|
|
214
234
|
nestedRepos = [];
|
|
215
235
|
deps = new Set();
|
|
@@ -226,19 +246,26 @@ export class ProjectContext {
|
|
|
226
246
|
isMobileApp;
|
|
227
247
|
/** Every package.json folder ("" for the root), and whether it is a mobile app. */
|
|
228
248
|
packageDirs = [];
|
|
249
|
+
/** Does the root package.json declare a "bin" (is this a command-line tool)? */
|
|
250
|
+
rootIsCli = false;
|
|
229
251
|
/** Finding / checklist ids silenced in .launchdoctorignore. */
|
|
230
252
|
ignoredIds = new Set();
|
|
231
|
-
|
|
253
|
+
/** Lines of .launchdoctorignore past the first MAX_IGNORE_LINES, which were not used. */
|
|
254
|
+
ignoreLinesSkipped = 0;
|
|
255
|
+
ignoredPathRules = [];
|
|
232
256
|
tracked;
|
|
233
257
|
byPath = new Map();
|
|
234
258
|
clientCache = new Map();
|
|
235
259
|
lineStarts = new Map();
|
|
236
260
|
codeCache = new Map();
|
|
261
|
+
addedCache = new Map();
|
|
237
262
|
maxFiles;
|
|
263
|
+
maxBytes;
|
|
238
264
|
constructor(root, options = {}) {
|
|
239
265
|
this.root = resolve(root);
|
|
240
266
|
this.options = options;
|
|
241
267
|
this.maxFiles = options.maxFiles ?? MAX_FILES;
|
|
268
|
+
this.maxBytes = options.maxBytes ?? MAX_TOTAL_BYTES;
|
|
242
269
|
// Known before the walk, so folders that are separate git repositories can be left out.
|
|
243
270
|
this.gitRoot = options.useGit === false ? null : findGitRoot(this.root);
|
|
244
271
|
this.readIgnoreFile();
|
|
@@ -277,10 +304,15 @@ export class ProjectContext {
|
|
|
277
304
|
const indexHtml = this.paths.some((p) => /^(?:public\/)?index\.html?$/i.test(p));
|
|
278
305
|
// HTML under docs/ (and coverage/, node_modules/, which are never read) doesn't make a project a website.
|
|
279
306
|
// A React Native app uses react too, and its server folder is the app's backend, not a website.
|
|
307
|
+
// A command-line tool (package.json "bin") often contains route-like text as data, such as
|
|
308
|
+
// regexes or sample code, so that alone doesn't make it a website unless it also depends on a
|
|
309
|
+
// server framework. Frontend packages and an index.html still count.
|
|
310
|
+
const routeEvidence = !this.rootIsCli || this.usesAny(SERVER_FRAMEWORKS);
|
|
280
311
|
this.isWebApp = this.isMobileApp
|
|
281
312
|
? this.usesAny(WEB_FRAMEWORKS) || indexHtml
|
|
282
313
|
: this.usesAny(FRONTEND_PACKAGES) ||
|
|
283
|
-
|
|
314
|
+
(routeEvidence &&
|
|
315
|
+
this.files.some((f) => isCodeFile(f) && !isTestPath(f.path) && !/(^|\/)docs\//.test(f.path) && isRouteLike(f))) ||
|
|
284
316
|
indexHtml;
|
|
285
317
|
}
|
|
286
318
|
// ---------- reading the project ----------
|
|
@@ -292,27 +324,43 @@ export class ProjectContext {
|
|
|
292
324
|
*/
|
|
293
325
|
readIgnoreFile() {
|
|
294
326
|
let text;
|
|
295
|
-
|
|
296
|
-
|
|
327
|
+
if (this.options.configFrom) {
|
|
328
|
+
assertConfigRef(this.root, this.options.configFrom);
|
|
329
|
+
const atRef = readAtRef(this.root, this.options.configFrom, '.launchdoctorignore');
|
|
330
|
+
if (atRef === null)
|
|
331
|
+
return;
|
|
332
|
+
text = atRef;
|
|
297
333
|
}
|
|
298
|
-
|
|
299
|
-
|
|
334
|
+
else {
|
|
335
|
+
// Only a regular file in the project: a symlink (to a file outside it, or /dev/zero) or a FIFO is skipped.
|
|
336
|
+
const read = readRegularFile(join(this.root, '.launchdoctorignore'), CONFIG_FILE_MAX_BYTES);
|
|
337
|
+
if (read === null)
|
|
338
|
+
return;
|
|
339
|
+
text = read;
|
|
300
340
|
}
|
|
301
341
|
const pathRules = [];
|
|
342
|
+
let used = 0;
|
|
302
343
|
for (const raw of text.split(/\r?\n/)) {
|
|
303
344
|
const line = raw.replace(/#.*$/, '').trim();
|
|
304
345
|
if (!line)
|
|
305
346
|
continue;
|
|
347
|
+
// Real ignore files are a few dozen lines. A huge one is cut off (and the report says so), so it can't
|
|
348
|
+
// slow every scan down.
|
|
349
|
+
if (used >= MAX_IGNORE_LINES) {
|
|
350
|
+
this.ignoreLinesSkipped++;
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
used++;
|
|
306
354
|
// A bare word is a finding id, unless a file or folder with that name exists (then it's a path).
|
|
307
355
|
if (/^[a-z0-9-]+$/.test(line) && !existsSync(join(this.root, line)))
|
|
308
356
|
this.ignoredIds.add(line);
|
|
309
357
|
else
|
|
310
358
|
pathRules.push(line);
|
|
311
359
|
}
|
|
312
|
-
this.ignoredPathRules = pathRules.join('\n');
|
|
360
|
+
this.ignoredPathRules = compileIgnore(pathRules.join('\n'));
|
|
313
361
|
}
|
|
314
362
|
isIgnoredPath(relPath) {
|
|
315
|
-
return this.ignoredPathRules
|
|
363
|
+
return this.ignoredPathRules.length > 0 && matchesIgnore(this.ignoredPathRules, relPath);
|
|
316
364
|
}
|
|
317
365
|
walk() {
|
|
318
366
|
const stack = [''];
|
|
@@ -354,8 +402,16 @@ export class ProjectContext {
|
|
|
354
402
|
continue;
|
|
355
403
|
try {
|
|
356
404
|
const full = join(abs, entry.name);
|
|
357
|
-
|
|
405
|
+
const size = statSync(full).size;
|
|
406
|
+
if (size > MAX_FILE_SIZE)
|
|
358
407
|
continue;
|
|
408
|
+
// A cap on the total read keeps memory bounded (1 GB of source used about 2 GB). The largest of the 50
|
|
409
|
+
// corpus apps reads 12.7 MB, so real projects are far below it.
|
|
410
|
+
if (this.bytesRead + size > this.maxBytes) {
|
|
411
|
+
this.byteLimitReached = true;
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
this.bytesRead += size;
|
|
359
415
|
// Strip the byte-order mark some Windows editors add, or JSON.parse fails.
|
|
360
416
|
const file = { path: relPath, content: readFileSync(full, 'utf8').replace(/^/, '') };
|
|
361
417
|
this.files.push(file);
|
|
@@ -385,6 +441,8 @@ export class ProjectContext {
|
|
|
385
441
|
}
|
|
386
442
|
const dir = dirname(file.path);
|
|
387
443
|
this.packageDirs.push({ dir: dir === '.' ? '' : dir, mobile });
|
|
444
|
+
if (dir === '.' && pkg.bin)
|
|
445
|
+
this.rootIsCli = true;
|
|
388
446
|
for (const script of Object.values(pkg.scripts ?? {})) {
|
|
389
447
|
if (typeof script === 'string')
|
|
390
448
|
this.scripts.push(script);
|
|
@@ -477,6 +535,23 @@ export class ProjectContext {
|
|
|
477
535
|
file(path) {
|
|
478
536
|
return this.byPath.get(path);
|
|
479
537
|
}
|
|
538
|
+
/**
|
|
539
|
+
* With `configFrom`: was this line added or changed since that git ref? Used so an inline
|
|
540
|
+
* "launch-doctor-ignore" comment a pull request adds can't silence the pull request's own finding.
|
|
541
|
+
* Without `configFrom` this is always false. If git can't tell (it fails, or the file is new),
|
|
542
|
+
* the answer is true, so the comment is not trusted.
|
|
543
|
+
*/
|
|
544
|
+
addedSinceConfigRef(relPath, line) {
|
|
545
|
+
const ref = this.options.configFrom;
|
|
546
|
+
if (!ref)
|
|
547
|
+
return false;
|
|
548
|
+
let added = this.addedCache.get(relPath);
|
|
549
|
+
if (!added) {
|
|
550
|
+
added = addedLinesSince(this.root, ref, relPath);
|
|
551
|
+
this.addedCache.set(relPath, added);
|
|
552
|
+
}
|
|
553
|
+
return added === 'all' || added.has(line);
|
|
554
|
+
}
|
|
480
555
|
hasPath(re) {
|
|
481
556
|
return this.paths.some((p) => re.test(p));
|
|
482
557
|
}
|
|
@@ -685,24 +760,48 @@ function looksLikeServerCode(content) {
|
|
|
685
760
|
return (/(?:from\s*|require\s*\(\s*)["'](?:node:)?(express|fastify|hono|koa|http|https|fs|child_process|@hono\/node-server)["']/.test(content) || /\.listen\s*\(\s*(?:\d|port|process\.env)/i.test(content));
|
|
686
761
|
}
|
|
687
762
|
// ---------- git helpers ----------
|
|
763
|
+
// Every git call goes through src/git.ts, which stops the scanned repository's config from running programs.
|
|
764
|
+
export { git, gitArgs } from './git.js';
|
|
765
|
+
/** A ref is a branch, tag or commit name. A leading "-" would be read by git as an option. */
|
|
766
|
+
const SAFE_REF = /^[\w][\w./@^~-]*$/;
|
|
767
|
+
/** Throws unless `ref` names a commit in the git repository that holds `root`. */
|
|
768
|
+
export function assertConfigRef(root, ref) {
|
|
769
|
+
if (!SAFE_REF.test(ref))
|
|
770
|
+
throw new Error(`"${ref}" is not a valid git ref`);
|
|
771
|
+
const gitRoot = findGitRoot(resolve(root));
|
|
772
|
+
if (!gitRoot)
|
|
773
|
+
throw new Error('--config-from needs a folder inside a git repository');
|
|
774
|
+
if (git(gitRoot, ['rev-parse', '--verify', '--quiet', `${ref}^{commit}`]) === null) {
|
|
775
|
+
throw new Error(`Git ref "${ref}" was not found. On GitHub Actions, fetch the base commit first (git fetch --depth=1 origin <sha>).`);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
688
778
|
/**
|
|
689
|
-
*
|
|
690
|
-
*
|
|
779
|
+
* The line numbers (in the working copy) of `file` that were added or changed since git ref `ref`,
|
|
780
|
+
* or 'all' when the file is new or git can't say. `file` is relative to `root`.
|
|
691
781
|
*/
|
|
692
|
-
export function
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
782
|
+
export function addedLinesSince(root, ref, file) {
|
|
783
|
+
const cwd = resolve(root);
|
|
784
|
+
// A file that isn't at the ref is new: every line is added.
|
|
785
|
+
if (git(cwd, ['cat-file', '-e', `${ref}:./${file}`]) === null)
|
|
786
|
+
return 'all';
|
|
787
|
+
const diff = git(cwd, ['diff', ...SAFE_DIFF, '--no-color', '--unified=0', '--no-renames', ref, '--', file]);
|
|
788
|
+
if (diff === null)
|
|
789
|
+
return 'all';
|
|
790
|
+
const out = new Set();
|
|
791
|
+
for (const m of diff.matchAll(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/gm)) {
|
|
792
|
+
const start = Number(m[1]);
|
|
793
|
+
const count = m[2] === undefined ? 1 : Number(m[2]);
|
|
794
|
+
for (let i = 0; i < count; i++)
|
|
795
|
+
out.add(start + i);
|
|
705
796
|
}
|
|
797
|
+
return out;
|
|
798
|
+
}
|
|
799
|
+
/** The text of `file` (a name inside `root`) as it was at git ref `ref`, or null if it didn't exist there. */
|
|
800
|
+
export function readAtRef(root, ref, file) {
|
|
801
|
+
// "<ref>:./<file>" is resolved against the folder git runs in, so a project in a subfolder
|
|
802
|
+
// reads its own file, and short Windows paths (C:\Users\NAME~1) don't confuse it.
|
|
803
|
+
// cat-file prints the stored bytes and, unlike git show, applies no filter or textconv.
|
|
804
|
+
return git(resolve(root), ['cat-file', 'blob', `${ref}:./${file}`]);
|
|
706
805
|
}
|
|
707
806
|
function samePath(a, b) {
|
|
708
807
|
const norm = (p) => resolve(p).replace(/\\/g, '/').replace(/\/+$/, '').toLowerCase();
|
|
@@ -715,19 +814,19 @@ function samePath(a, b) {
|
|
|
715
814
|
*/
|
|
716
815
|
function isSeparateRepo(dir) {
|
|
717
816
|
const dotGit = join(dir, '.git');
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
if (!stat)
|
|
721
|
-
return false;
|
|
722
|
-
if (stat.isDirectory())
|
|
723
|
-
return true;
|
|
724
|
-
return /[\\/]worktrees[\\/]/.test(readFileSync(dotGit, 'utf8'));
|
|
725
|
-
}
|
|
726
|
-
catch {
|
|
817
|
+
const kind = entryKind(dotGit);
|
|
818
|
+
if (kind === 'missing')
|
|
727
819
|
return false;
|
|
728
|
-
|
|
820
|
+
if (kind === 'dir')
|
|
821
|
+
return true;
|
|
822
|
+
// Anything but a regular file (a FIFO, a device, a symlink) is never opened: the folder is skipped as if it
|
|
823
|
+
// were another repository. A real .git file is one short line ("gitdir: ...").
|
|
824
|
+
if (kind === 'other')
|
|
825
|
+
return true;
|
|
826
|
+
const text = readRegularFile(dotGit, 64 * 1024);
|
|
827
|
+
return text === null || /[\\/]worktrees[\\/]/.test(text);
|
|
729
828
|
}
|
|
730
|
-
function findGitRoot(root) {
|
|
829
|
+
export function findGitRoot(root) {
|
|
731
830
|
const out = git(root, ['rev-parse', '--show-toplevel']);
|
|
732
831
|
if (!out)
|
|
733
832
|
return null;
|
|
@@ -744,77 +843,12 @@ function gitLsFiles(root) {
|
|
|
744
843
|
return new Set(out.split('\0').filter(Boolean));
|
|
745
844
|
}
|
|
746
845
|
function gitCheckIgnore(root, paths) {
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
return new Set(out.split('\0').filter(Boolean));
|
|
754
|
-
}
|
|
755
|
-
catch (err) {
|
|
756
|
-
const e = err;
|
|
757
|
-
// Exit code 1 means "none of these are ignored".
|
|
758
|
-
if (e.status === 1)
|
|
759
|
-
return new Set();
|
|
760
|
-
return null;
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
function globToRegex(glob) {
|
|
764
|
-
let re = '';
|
|
765
|
-
for (let i = 0; i < glob.length; i++) {
|
|
766
|
-
const ch = glob[i];
|
|
767
|
-
if (ch === '*') {
|
|
768
|
-
if (glob[i + 1] === '*') {
|
|
769
|
-
re += '.*';
|
|
770
|
-
i++;
|
|
771
|
-
}
|
|
772
|
-
else {
|
|
773
|
-
re += '[^/]*';
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
else if (ch === '?') {
|
|
777
|
-
re += '[^/]';
|
|
778
|
-
}
|
|
779
|
-
else {
|
|
780
|
-
re += ch.replace(/[.+^${}()|[\]\\]/g, '\\$&');
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
return new RegExp(`^${re}$`);
|
|
784
|
-
}
|
|
785
|
-
/** Minimal .gitignore matching, used when git isn't available. */
|
|
786
|
-
export function matchesGitignore(gitignore, path) {
|
|
787
|
-
let ignored = false;
|
|
788
|
-
const name = basename(path);
|
|
789
|
-
for (const raw of gitignore.split(/\r?\n/)) {
|
|
790
|
-
let line = raw.trim();
|
|
791
|
-
if (!line || line.startsWith('#'))
|
|
792
|
-
continue;
|
|
793
|
-
const negate = line.startsWith('!');
|
|
794
|
-
if (negate)
|
|
795
|
-
line = line.slice(1);
|
|
796
|
-
line = line.replace(/\/+$/, '');
|
|
797
|
-
// "**/x" means "x in any folder".
|
|
798
|
-
let anyDepth = false;
|
|
799
|
-
while (line.startsWith('**/')) {
|
|
800
|
-
line = line.slice(3);
|
|
801
|
-
anyDepth = true;
|
|
802
|
-
}
|
|
803
|
-
const anchored = line.startsWith('/') || line.includes('/');
|
|
804
|
-
line = line.replace(/^\//, '');
|
|
805
|
-
if (!line)
|
|
806
|
-
continue;
|
|
807
|
-
const re = globToRegex(line);
|
|
808
|
-
const segments = path.split('/');
|
|
809
|
-
let hit;
|
|
810
|
-
if (!anchored)
|
|
811
|
-
hit = re.test(name) || segments.some((seg) => re.test(seg));
|
|
812
|
-
else if (anyDepth)
|
|
813
|
-
hit = segments.some((_, i) => re.test(segments.slice(i).join('/')));
|
|
814
|
-
else
|
|
815
|
-
hit = re.test(path);
|
|
816
|
-
if (hit)
|
|
817
|
-
ignored = !negate;
|
|
818
|
-
}
|
|
819
|
-
return ignored;
|
|
846
|
+
// -z keeps unusual file names (spaces, accents) unquoted in the output.
|
|
847
|
+
const { stdout, status } = gitStatus(root, ['check-ignore', '-z', '--', ...paths]);
|
|
848
|
+
if (stdout !== null)
|
|
849
|
+
return new Set(stdout.split('\0').filter(Boolean));
|
|
850
|
+
// Exit code 1 means "none of these are ignored".
|
|
851
|
+
return status === 1 ? new Set() : null;
|
|
820
852
|
}
|
|
853
|
+
/** Minimal .gitignore matching, used when git isn't available (see src/ignore.ts). */
|
|
854
|
+
export { matchesGitignore };
|
package/dist/src/report.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { Report } from './types.js';
|
|
2
2
|
export declare const bold: (s: string) => string;
|
|
3
3
|
export declare const dim: (s: string) => string;
|
|
4
|
+
/** Shown when the scan hit MAX_TOTAL_BYTES (terminal report and MCP). */
|
|
5
|
+
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.";
|
|
4
6
|
/** "Left out Twin/: a separate git repository inside this folder. Scan it on its own." */
|
|
5
7
|
export declare function nestedReposNote(repos: string[]): string;
|
|
6
8
|
export declare function renderReport(report: Report, opts?: {
|
package/dist/src/report.js
CHANGED
|
@@ -8,6 +8,8 @@ const red = paint('31');
|
|
|
8
8
|
const green = paint('32');
|
|
9
9
|
const yellow = paint('33');
|
|
10
10
|
const cyan = paint('36');
|
|
11
|
+
/** Shown when the scan hit MAX_TOTAL_BYTES (terminal report and MCP). */
|
|
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.';
|
|
11
13
|
/** "Left out Twin/: a separate git repository inside this folder. Scan it on its own." */
|
|
12
14
|
export function nestedReposNote(repos) {
|
|
13
15
|
const names = repos.slice(0, 3).map((p) => `${p}/`);
|
|
@@ -81,8 +83,17 @@ export function renderReport(report, opts = {}) {
|
|
|
81
83
|
const stack = report.stack.length ? ` · ${report.stack.join(', ')}` : '';
|
|
82
84
|
const unit = report.mode === 'live' ? 'request' : 'file';
|
|
83
85
|
lines.push(` ${dim(`Checked ${report.projectName} · ${plural(report.filesScanned, unit)}${stack}`)}`);
|
|
86
|
+
if (report.partial) {
|
|
87
|
+
const n = report.partial.files.length;
|
|
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.`)}`);
|
|
89
|
+
}
|
|
84
90
|
if (report.truncated)
|
|
85
91
|
lines.push(` ${yellow('Very large project: only the first 20,000 files were checked.')}`);
|
|
92
|
+
if (report.byteLimitReached)
|
|
93
|
+
lines.push(` ${yellow(BYTE_LIMIT_NOTE)}`);
|
|
94
|
+
if (report.ignoreLinesSkipped) {
|
|
95
|
+
lines.push(` ${yellow(`.launchdoctorignore is very long: only its first 1,000 lines were used (${plural(report.ignoreLinesSkipped, 'line')} skipped).`)}`);
|
|
96
|
+
}
|
|
86
97
|
if (report.nestedRepos?.length)
|
|
87
98
|
lines.push(` ${dim(nestedReposNote(report.nestedRepos))}`);
|
|
88
99
|
lines.push('');
|
package/dist/src/routes.d.ts
CHANGED
|
@@ -23,6 +23,11 @@ export declare function matchBracket(code: string, open: number): number;
|
|
|
23
23
|
export declare function urlPathOf(file: string): string | null;
|
|
24
24
|
/** Resolve a local import ("./x", "../lib/ai", "@/lib/ai", "~/lib/ai") to a project file. */
|
|
25
25
|
export declare function resolveImport(ctx: ProjectContext, from: SourceFile, spec: string): SourceFile | null;
|
|
26
|
+
/** Local modules reachable from `text` (a handler body) through imports of `from`, up to MAX_DEPTH hops. */
|
|
27
|
+
export declare function reach(ctx: ProjectContext, from: SourceFile, text: string): {
|
|
28
|
+
files: SourceFile[];
|
|
29
|
+
unresolved: boolean;
|
|
30
|
+
};
|
|
26
31
|
/** Per-handler view of a file, for Next.js-style route files. Empty for files with no exported handlers. */
|
|
27
32
|
export declare function handlersOf(ctx: ProjectContext, file: SourceFile): RouteHandler[];
|
|
28
33
|
export interface HandlerEvidence {
|
|
@@ -34,6 +39,11 @@ export interface HandlerEvidence {
|
|
|
34
39
|
}
|
|
35
40
|
export declare function evidenceOf(ctx: ProjectContext, h: RouteHandler): HandlerEvidence;
|
|
36
41
|
export declare function authIsUsed(authRe: RegExp, ev: HandlerEvidence): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* `code` without its import and require statements (JavaScript and Python). A sign-in helper that is only imported
|
|
44
|
+
* checks nothing, so a sign-in pattern must not count the import line. Every bound is fixed, so this stays linear.
|
|
45
|
+
*/
|
|
46
|
+
export declare function withoutImports(code: string): string;
|
|
37
47
|
/** Turn one Next.js matcher string into a RegExp, or null if we can't understand it. */
|
|
38
48
|
export declare function matcherToRegExp(entry: string): RegExp | null;
|
|
39
49
|
/** Does any matcher entry cover this URL path? `undefined` means "could not tell". */
|
package/dist/src/routes.js
CHANGED
|
@@ -69,11 +69,14 @@ export function urlPathOf(file) {
|
|
|
69
69
|
}
|
|
70
70
|
function importsOf(code) {
|
|
71
71
|
const out = [];
|
|
72
|
-
|
|
72
|
+
// Each clause starts and ends with a non-space character and is at most 2,000 characters long. Otherwise the
|
|
73
|
+
// spaces around it could be split between \s+, the clause and \s* in every possible way (quadratic: a long run
|
|
74
|
+
// of spaces after "import" hung the scan). Real import clauses are far shorter.
|
|
75
|
+
const re = /\bimport\s+(?!type\b)([^'"\s](?:[^'"]{0,2000}?[^'"\s])?)\s*from\s*["']([^"']+)["']|\b(?:const|let|var)\s+([\w${},:](?:[\w${},\s:]{0,2000}?[\w${},:])?)\s*=\s*require\(\s*["']([^"']+)["']\s*\)/g;
|
|
73
76
|
for (let m = re.exec(code); m; m = re.exec(code)) {
|
|
74
77
|
const clause = m[1] ?? m[3] ?? '';
|
|
75
78
|
const spec = m[2] ?? m[4] ?? '';
|
|
76
|
-
const names = [...clause.matchAll(/(?:\bas\s+)?([A-Za-z_$][\w$]*)/g)]
|
|
79
|
+
const names = [...clause.matchAll(/(?:\bas\s+)?(?<![\w$])([A-Za-z_$][\w$]*)/g)]
|
|
77
80
|
.map((x) => x[1])
|
|
78
81
|
.filter((n) => n !== 'as' && n !== 'type');
|
|
79
82
|
out.push({ spec, names });
|
|
@@ -99,7 +102,7 @@ export function resolveImport(ctx, from, spec) {
|
|
|
99
102
|
return null;
|
|
100
103
|
}
|
|
101
104
|
/** Local modules reachable from `text` (a handler body) through imports of `from`, up to MAX_DEPTH hops. */
|
|
102
|
-
function reach(ctx, from, text) {
|
|
105
|
+
export function reach(ctx, from, text) {
|
|
103
106
|
const seen = new Set([from.path]);
|
|
104
107
|
const files = [];
|
|
105
108
|
let unresolved = false;
|
|
@@ -192,7 +195,21 @@ export function evidenceOf(ctx, h) {
|
|
|
192
195
|
export function authIsUsed(authRe, ev) {
|
|
193
196
|
if (ev.wrapped)
|
|
194
197
|
return true;
|
|
195
|
-
|
|
198
|
+
// An imported sign-in helper that is never called checks nothing, so import lines don't count.
|
|
199
|
+
const scope = withoutImports(ev.scope);
|
|
200
|
+
return authRe.test(scope) && REJECTS.test(scope);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* `code` without its import and require statements (JavaScript and Python). A sign-in helper that is only imported
|
|
204
|
+
* checks nothing, so a sign-in pattern must not count the import line. Every bound is fixed, so this stays linear.
|
|
205
|
+
*/
|
|
206
|
+
export function withoutImports(code) {
|
|
207
|
+
return code
|
|
208
|
+
.replace(/^[ \t]*import\b[^;'"]{0,2000}?\bfrom\s*["'][^"'\n]*["'];?/gm, '')
|
|
209
|
+
.replace(/^[ \t]*import\s*["'][^"'\n]*["'];?/gm, '')
|
|
210
|
+
.replace(/^[ \t]*export\s*(?:\*(?:\s+as\s+\w+)?|\{[^}]{0,2000}\})\s*from\s*["'][^"'\n]*["'];?/gm, '')
|
|
211
|
+
.replace(/^[ \t]*(?:const|let|var)\s+[\w${},\s:]{1,500}?=\s*require\(\s*["'][^"'\n]*["']\s*\)[\w.]*;?/gm, '')
|
|
212
|
+
.replace(/^[ \t]*(?:from[ \t]+[\w.]+[ \t]+)?import[ \t]+[^\n'"]*$/gm, '');
|
|
196
213
|
}
|
|
197
214
|
// ---------- middleware matcher against a concrete URL path ----------
|
|
198
215
|
/** Turn one Next.js matcher string into a RegExp, or null if we can't understand it. */
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading Launch Doctor's own files (.launchdoctorignore, the baseline, and the config file planned for R7) from a
|
|
3
|
+
* project that may be hostile.
|
|
4
|
+
*
|
|
5
|
+
* Only a regular file is read. A symlink (which could point outside the project, or at /dev/zero), a FIFO (opening
|
|
6
|
+
* one blocks until someone writes to it), a device or a folder counts as no file at all. The file is checked
|
|
7
|
+
* before it is opened, opened without following links or blocking where the OS supports that, and checked again
|
|
8
|
+
* once open, so swapping it in between doesn't help.
|
|
9
|
+
*/
|
|
10
|
+
export declare function readRegularFile(path: string, maxBytes: number): string | null;
|
|
11
|
+
/**
|
|
12
|
+
* Write `content` to `path`, which must be a regular file or not exist yet. A symlink planted in a project (say,
|
|
13
|
+
* LAUNCH_FIXES.md pointing at ~/.bashrc) would otherwise make a scan overwrite a file outside it, so a link, FIFO,
|
|
14
|
+
* device or folder there is refused with an error instead. Where the OS supports it, the file is also opened
|
|
15
|
+
* without following links or blocking.
|
|
16
|
+
*/
|
|
17
|
+
export declare function writeRegularFile(path: string, content: string): void;
|
|
18
|
+
/** What kind of entry `path` is, without following a link or opening it. */
|
|
19
|
+
export declare function entryKind(path: string): 'file' | 'dir' | 'other' | 'missing';
|