launch-doctor 0.2.1 → 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.
Files changed (66) hide show
  1. package/CHANGELOG.md +105 -4
  2. package/LICENSE +1 -0
  3. package/README.md +239 -39
  4. package/dist/src/access.d.ts +52 -0
  5. package/dist/src/access.js +482 -0
  6. package/dist/src/baseline.d.ts +6 -3
  7. package/dist/src/baseline.js +15 -8
  8. package/dist/src/changed.d.ts +12 -0
  9. package/dist/src/changed.js +34 -0
  10. package/dist/src/checks/access.d.ts +7 -0
  11. package/dist/src/checks/access.js +868 -0
  12. package/dist/src/checks/costs.d.ts +8 -0
  13. package/dist/src/checks/costs.js +54 -11
  14. package/dist/src/checks/database.js +201 -6
  15. package/dist/src/checks/first-impressions.js +247 -117
  16. package/dist/src/checks/legal.js +29 -13
  17. package/dist/src/checks/monitoring.js +47 -15
  18. package/dist/src/checks/payments.js +141 -6
  19. package/dist/src/checks/secrets.d.ts +8 -0
  20. package/dist/src/checks/secrets.js +185 -16
  21. package/dist/src/checks/supabase-storage.d.ts +31 -0
  22. package/dist/src/checks/supabase-storage.js +216 -0
  23. package/dist/src/cli.js +201 -12
  24. package/dist/src/demo.d.ts +13 -0
  25. package/dist/src/demo.js +59 -2
  26. package/dist/src/detect.d.ts +1 -1
  27. package/dist/src/detect.js +5 -2
  28. package/dist/src/fixes.js +10 -7
  29. package/dist/src/git.d.ts +25 -0
  30. package/dist/src/git.js +121 -0
  31. package/dist/src/github.d.ts +90 -0
  32. package/dist/src/github.js +330 -0
  33. package/dist/src/history.js +17 -3
  34. package/dist/src/hook.d.ts +13 -0
  35. package/dist/src/hook.js +96 -0
  36. package/dist/src/hooks-setup.d.ts +24 -0
  37. package/dist/src/hooks-setup.js +187 -0
  38. package/dist/src/ignore.d.ts +36 -0
  39. package/dist/src/ignore.js +164 -0
  40. package/dist/src/index.d.ts +1 -0
  41. package/dist/src/index.js +30 -3
  42. package/dist/src/init.d.ts +1 -1
  43. package/dist/src/init.js +2 -2
  44. package/dist/src/live.d.ts +9 -0
  45. package/dist/src/live.js +73 -22
  46. package/dist/src/mcp.d.ts +140 -0
  47. package/dist/src/mcp.js +386 -0
  48. package/dist/src/net-guard.d.ts +34 -0
  49. package/dist/src/net-guard.js +144 -0
  50. package/dist/src/project.d.ts +42 -5
  51. package/dist/src/project.js +237 -107
  52. package/dist/src/report.d.ts +4 -0
  53. package/dist/src/report.js +23 -1
  54. package/dist/src/routes.d.ts +10 -0
  55. package/dist/src/routes.js +21 -4
  56. package/dist/src/safe-fs.d.ts +19 -0
  57. package/dist/src/safe-fs.js +77 -0
  58. package/dist/src/sanitize.d.ts +17 -0
  59. package/dist/src/sanitize.js +90 -0
  60. package/dist/src/sarif.js +1 -1
  61. package/dist/src/summary.d.ts +7 -0
  62. package/dist/src/summary.js +26 -17
  63. package/dist/src/types.d.ts +26 -0
  64. package/dist/src/util.d.ts +2 -0
  65. package/dist/src/util.js +2 -0
  66. package/package.json +9 -5
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Which addresses a live scan may connect to.
3
+ *
4
+ * A deployed site decides which URLs the scanner requests next (its script tags, source maps and redirects), so a
5
+ * public site could otherwise point the scanner at the cloud metadata address, a router or a service on your own
6
+ * machine. Every request is checked first: its host name is resolved, and the request is refused unless every
7
+ * address it resolves to is public.
8
+ *
9
+ * Alternate spellings of an IPv4 address (2130706433, 0x7f000001, 0177.0.0.1, 127.1) need no extra handling here:
10
+ * the URL parser turns them all into "127.0.0.1" before this code sees the host.
11
+ *
12
+ * Known gap (see SECURITY.md): fetch resolves the name again when it connects, so a DNS server that answers with a
13
+ * public address to the check and a private one to the connection (DNS rebinding) can get past it.
14
+ */
15
+ export type Lookup = (host: string) => Promise<{
16
+ address: string;
17
+ family: number;
18
+ }[]>;
19
+ export declare const defaultLookup: Lookup;
20
+ /** Is this literal IP address one anyone on the internet could reach? Null when `text` isn't an IP address. */
21
+ export declare function isPublicAddress(text: string): boolean | null;
22
+ export type HostCheck = {
23
+ kind: 'public';
24
+ } | {
25
+ kind: 'private';
26
+ reason: string;
27
+ }
28
+ /** The name doesn't resolve, so no request can reach it. */
29
+ | {
30
+ kind: 'unresolved';
31
+ reason: string;
32
+ };
33
+ /** Is `url`'s host public (every address it resolves to is public), private, or not resolvable? Never throws. */
34
+ export declare function checkHost(url: URL, lookup?: Lookup): Promise<HostCheck>;
@@ -0,0 +1,144 @@
1
+ import { lookup as dnsLookup } from 'node:dns/promises';
2
+ export const defaultLookup = (host) => dnsLookup(host, { all: true, verbatim: true });
3
+ function parseIPv4(text) {
4
+ const parts = text.split('.');
5
+ if (parts.length !== 4)
6
+ return null;
7
+ const out = [];
8
+ for (const p of parts) {
9
+ if (!/^\d{1,3}$/.test(p))
10
+ return null;
11
+ const n = Number(p);
12
+ if (n > 255)
13
+ return null;
14
+ out.push(n);
15
+ }
16
+ return out;
17
+ }
18
+ /** The 8 groups of an IPv6 address (a trailing dotted IPv4 part included), or null if it isn't one. */
19
+ function parseIPv6(text) {
20
+ let s = text.replace(/^\[|\]$/g, '').toLowerCase();
21
+ const zone = s.indexOf('%');
22
+ if (zone >= 0)
23
+ s = s.slice(0, zone);
24
+ if (!s.includes(':'))
25
+ return null;
26
+ let tail = [];
27
+ const lastColon = s.lastIndexOf(':');
28
+ if (s.slice(lastColon + 1).includes('.')) {
29
+ const v4 = parseIPv4(s.slice(lastColon + 1));
30
+ if (!v4)
31
+ return null;
32
+ tail = [(v4[0] << 8) | v4[1], (v4[2] << 8) | v4[3]];
33
+ s = `${s.slice(0, lastColon + 1)}0:0`;
34
+ }
35
+ const halves = s.split('::');
36
+ if (halves.length > 2)
37
+ return null;
38
+ const group = (h) => (h ? h.split(':') : []);
39
+ const head = group(halves[0]);
40
+ const rest = halves.length === 2 ? group(halves[1]) : [];
41
+ const missing = 8 - head.length - rest.length;
42
+ if (halves.length === 2 ? missing < 1 : missing !== 0)
43
+ return null;
44
+ const all = [...head, ...Array(halves.length === 2 ? missing : 0).fill('0'), ...rest];
45
+ const out = [];
46
+ for (const g of all) {
47
+ if (!/^[0-9a-f]{1,4}$/.test(g))
48
+ return null;
49
+ out.push(Number.parseInt(g, 16));
50
+ }
51
+ if (tail.length)
52
+ out.splice(6, 2, ...tail);
53
+ return out;
54
+ }
55
+ function isPublicIPv4([a, b, c]) {
56
+ if (a === 0 || a === 10 || a === 127)
57
+ return false; // "this network", private, loopback
58
+ if (a === 100 && b >= 64 && b <= 127)
59
+ return false; // carrier-grade NAT (100.64.0.0/10)
60
+ if (a === 169 && b === 254)
61
+ return false; // link-local, including the cloud metadata address
62
+ if (a === 172 && b >= 16 && b <= 31)
63
+ return false; // private
64
+ if (a === 192 && b === 168)
65
+ return false; // private
66
+ if (a === 192 && b === 0 && (c === 0 || c === 2))
67
+ return false; // IETF protocol assignments, documentation
68
+ if (a === 198 && (b === 18 || b === 19))
69
+ return false; // benchmarking
70
+ if (a === 198 && b === 51 && c === 100)
71
+ return false; // documentation
72
+ if (a === 203 && b === 0 && c === 113)
73
+ return false; // documentation
74
+ if (a >= 224)
75
+ return false; // multicast, reserved, broadcast
76
+ return true;
77
+ }
78
+ function isPublicIPv6(g) {
79
+ const v4 = (hi, lo) => [hi >> 8, hi & 255, lo >> 8, lo & 255];
80
+ if (g.every((x) => x === 0))
81
+ return false; // ::
82
+ if (g.slice(0, 7).every((x) => x === 0) && g[7] === 1)
83
+ return false; // ::1
84
+ if (g.slice(0, 5).every((x) => x === 0) && g[5] === 0xffff)
85
+ return isPublicIPv4(v4(g[6], g[7])); // ::ffff:a.b.c.d
86
+ if (g.slice(0, 6).every((x) => x === 0))
87
+ return isPublicIPv4(v4(g[6], g[7])); // ::a.b.c.d (deprecated)
88
+ if (g[0] === 0x64 && g[1] === 0xff9b && g.slice(2, 6).every((x) => x === 0))
89
+ return isPublicIPv4(v4(g[6], g[7])); // NAT64
90
+ if (g[0] === 0x2002)
91
+ return isPublicIPv4(v4(g[1], g[2])); // 6to4
92
+ if ((g[0] & 0xfe00) === 0xfc00)
93
+ return false; // unique local (fc00::/7)
94
+ if ((g[0] & 0xffc0) === 0xfe80)
95
+ return false; // link-local
96
+ if ((g[0] & 0xffc0) === 0xfec0)
97
+ return false; // site-local (deprecated)
98
+ if ((g[0] & 0xff00) === 0xff00)
99
+ return false; // multicast
100
+ if (g[0] === 0x2001 && g[1] === 0xdb8)
101
+ return false; // documentation
102
+ if (g[0] === 0x100 && g.slice(1, 4).every((x) => x === 0))
103
+ return false; // discard-only
104
+ return true;
105
+ }
106
+ /** Is this literal IP address one anyone on the internet could reach? Null when `text` isn't an IP address. */
107
+ export function isPublicAddress(text) {
108
+ const v4 = parseIPv4(text);
109
+ if (v4)
110
+ return isPublicIPv4(v4);
111
+ const v6 = parseIPv6(text);
112
+ if (v6)
113
+ return isPublicIPv6(v6);
114
+ return null;
115
+ }
116
+ /** Names that always mean this machine, whatever DNS says. */
117
+ function isLocalName(host) {
118
+ const h = host.toLowerCase().replace(/\.$/, '');
119
+ return h === 'localhost' || h.endsWith('.localhost') || h === 'localhost.localdomain';
120
+ }
121
+ /** Is `url`'s host public (every address it resolves to is public), private, or not resolvable? Never throws. */
122
+ export async function checkHost(url, lookup = defaultLookup) {
123
+ const host = url.hostname;
124
+ if (isLocalName(host))
125
+ return { kind: 'private', reason: `${host} is this computer` };
126
+ const literal = isPublicAddress(host);
127
+ if (literal === false)
128
+ return { kind: 'private', reason: `${host} is a private or local network address` };
129
+ if (literal === true)
130
+ return { kind: 'public' };
131
+ let addresses;
132
+ try {
133
+ addresses = await lookup(host);
134
+ }
135
+ catch {
136
+ return { kind: 'unresolved', reason: `${host} could not be resolved` };
137
+ }
138
+ if (!addresses.length)
139
+ return { kind: 'unresolved', reason: `${host} could not be resolved` };
140
+ const bad = addresses.find((a) => isPublicAddress(a.address) !== true);
141
+ return bad
142
+ ? { kind: 'private', reason: `${host} resolves to ${bad.address}, a private or local network address` }
143
+ : { kind: 'public' };
144
+ }
@@ -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,11 @@ 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;
42
+ /** Folders left out because they are separate git repositories (they have their own .git folder). */
43
+ readonly nestedRepos: string[];
32
44
  readonly deps: Set<string>;
33
45
  readonly imports: Set<string>;
34
46
  readonly scripts: string[];
@@ -39,15 +51,25 @@ export declare class ProjectContext {
39
51
  * Monitoring, legal pages and first impressions only matter for web apps.
40
52
  */
41
53
  readonly isWebApp: boolean;
54
+ /** Is this a React Native / Expo app (or does it contain one)? */
55
+ readonly isMobileApp: boolean;
56
+ /** Every package.json folder ("" for the root), and whether it is a mobile app. */
57
+ private readonly packageDirs;
58
+ /** Does the root package.json declare a "bin" (is this a command-line tool)? */
59
+ private rootIsCli;
42
60
  /** Finding / checklist ids silenced in .launchdoctorignore. */
43
61
  readonly ignoredIds: Set<string>;
62
+ /** Lines of .launchdoctorignore past the first MAX_IGNORE_LINES, which were not used. */
63
+ ignoreLinesSkipped: number;
44
64
  private ignoredPathRules;
45
65
  private readonly tracked;
46
66
  private readonly byPath;
47
67
  private readonly clientCache;
48
68
  private readonly lineStarts;
49
69
  private readonly codeCache;
70
+ private readonly addedCache;
50
71
  private readonly maxFiles;
72
+ private readonly maxBytes;
51
73
  constructor(root: string, options?: Options);
52
74
  /**
53
75
  * .launchdoctorignore: one entry per line.
@@ -66,6 +88,13 @@ export declare class ProjectContext {
66
88
  /** Is this package actually imported in code (not just listed in package.json)? "/" suffix matches a scope. */
67
89
  imported(name: string): boolean;
68
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;
69
98
  hasPath(re: RegExp): boolean;
70
99
  /** Find every regex match across files (optionally filtered). */
71
100
  find(re: RegExp, filter?: FileFilter, limit?: number): Match[];
@@ -81,6 +110,8 @@ export declare class ProjectContext {
81
110
  lineOf(file: SourceFile, index: number): number;
82
111
  /** Best guess: does this file end up in the browser? */
83
112
  isClient(file: SourceFile): boolean;
113
+ /** Is this file part of a React Native / Expo app (the closest package.json is a mobile one)? */
114
+ isMobileFile(path: string): boolean;
84
115
  private computeIsClient;
85
116
  isTracked(path: string): boolean;
86
117
  /** Which of these paths would git ignore? */
@@ -88,10 +119,16 @@ export declare class ProjectContext {
88
119
  /** Short list of the main tools this project uses, for the report header. */
89
120
  stack(): string[];
90
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;
91
125
  /**
92
- * Every git call goes through here. core.fsmonitor=false stops a scanned folder's .git/config
93
- * from making git run a command of its choosing (fsmonitor can be set to any program).
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`.
94
128
  */
95
- export declare function gitArgs(args: string[]): string[];
96
- /** Minimal .gitignore matching, used when git isn't available. */
97
- export declare function matchesGitignore(gitignore: string, path: string): boolean;
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 };