vigie-cli 0.0.0-stage → 1.0.1

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/src/scan.js ADDED
@@ -0,0 +1,103 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { CATEGORIES, RULES, SEVERITIES, score, severityRank } from './catalog.js';
3
+ import { Client, normalizeTarget } from './net.js';
4
+ import { transportChecks } from './checks/transport.js';
5
+ import { cookieChecks, headerChecks } from './checks/headers.js';
6
+ import { exposureChecks } from './checks/exposure.js';
7
+ import { contentChecks, disclosureChecks } from './checks/content.js';
8
+
9
+ export const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
10
+
11
+ const SHOWN_HEADERS = [
12
+ 'server', 'content-type', 'strict-transport-security', 'content-security-policy', 'x-frame-options', 'x-content-type-options',
13
+ 'referrer-policy', 'permissions-policy', 'cross-origin-opener-policy', 'access-control-allow-origin', 'x-powered-by',
14
+ ];
15
+
16
+ function variant(target, protocol, port) {
17
+ const url = new URL(target.href);
18
+ url.protocol = protocol;
19
+ url.port = port ?? '';
20
+ return url;
21
+ }
22
+
23
+ export async function scanTarget(input, options = {}) {
24
+ const started = Date.now();
25
+ const target = normalizeTarget(input);
26
+ const client = new Client({
27
+ timeout: options.timeout ?? 10000,
28
+ userAgent: `vigie/${pkg.version} (+https://github.com/FreshlapeufDev/vigie)`,
29
+ ca: options.ca,
30
+ });
31
+
32
+ const port = target.port;
33
+ let securePort = '';
34
+ let plainPort = '';
35
+ if (port && target.protocol === 'https:') {
36
+ securePort = port;
37
+ plainPort = options.httpPort ?? null;
38
+ } else if (port) {
39
+ plainPort = port;
40
+ securePort = options.httpsPort ?? port;
41
+ } else {
42
+ securePort = options.httpsPort ?? '';
43
+ plainPort = options.httpPort ?? '';
44
+ }
45
+ const secureUrl = variant(target, 'https:', String(securePort));
46
+ const plainUrl = plainPort === null ? null : variant(target, 'http:', String(plainPort));
47
+
48
+ const https = await client.follow(secureUrl.href);
49
+ const http = plainUrl ? await client.follow(plainUrl.href) : [];
50
+
51
+ const secureOk = https[0] && !https[0].error;
52
+ const chain = secureOk ? https : http;
53
+ const main = chain.length ? chain[chain.length - 1] : null;
54
+ const reachable = Boolean(main && !main.error);
55
+ const html = reachable && /html|xml/i.test(String(main.headers?.['content-type'] || 'text/html')) ? main.text : '';
56
+
57
+ const findings = [];
58
+ const add = (rule, params = {}, evidence = [], severity) => {
59
+ const spec = RULES[rule];
60
+ findings.push({ rule, severity: severity ?? spec.severity, category: spec.category, cwe: spec.cwe, params, evidence });
61
+ };
62
+
63
+ const ctx = { add, client, target, https, http, main: reachable ? main : null, html, origin: reachable ? new URL(main.url).origin : null };
64
+
65
+ await transportChecks(ctx);
66
+ if (reachable) {
67
+ await headerChecks(ctx);
68
+ cookieChecks(ctx);
69
+ contentChecks(ctx);
70
+ disclosureChecks(ctx);
71
+ await exposureChecks(ctx, { probe: options.probe !== false });
72
+ }
73
+
74
+ const order = (a, b) => severityRank(b.severity) - severityRank(a.severity) || CATEGORIES.indexOf(a.category) - CATEGORIES.indexOf(b.category);
75
+ findings.sort(order);
76
+ const rating = score(findings);
77
+
78
+ return {
79
+ input: String(input),
80
+ url: target.href,
81
+ host: target.host,
82
+ finalUrl: main?.url ?? null,
83
+ status: main?.status ?? null,
84
+ reachable,
85
+ error: reachable ? null : (main?.error ?? https[0]?.error ?? 'UNREACHABLE'),
86
+ tls: https[0]?.tls ?? null,
87
+ headers: reachable ? Object.fromEntries(SHOWN_HEADERS.filter((name) => main.headers[name] !== undefined).map((name) => [name, [].concat(main.headers[name]).join(', ')])) : {},
88
+ redirects: chain.map((hop) => ({ url: hop.url, status: hop.status, error: hop.error })),
89
+ requests: client.log.length,
90
+ startedAt: new Date(started).toISOString(),
91
+ durationMs: Date.now() - started,
92
+ findings,
93
+ counts: Object.fromEntries([...SEVERITIES].reverse().map((severity) => [severity, findings.filter((finding) => finding.severity === severity).length])),
94
+ score: rating.value,
95
+ grade: rating.grade,
96
+ };
97
+ }
98
+
99
+ export async function scanAll(inputs, options = {}) {
100
+ const results = [];
101
+ for (const input of inputs) results.push(await scanTarget(input, options));
102
+ return results;
103
+ }
package/src/ui.js ADDED
@@ -0,0 +1,36 @@
1
+ const enabled = (Boolean(process.stdout.isTTY) || Boolean(process.env.FORCE_COLOR)) && !process.env.NO_COLOR && !process.argv.includes('--no-color');
2
+
3
+ const wrap = (code) => (text) => (enabled ? `\x1b[${code}m${text}\x1b[0m` : String(text));
4
+
5
+ export const c = {
6
+ bold: wrap('1'),
7
+ dim: wrap('2'),
8
+ red: wrap('31'),
9
+ green: wrap('32'),
10
+ yellow: wrap('33'),
11
+ blue: wrap('34'),
12
+ magenta: wrap('35'),
13
+ cyan: wrap('36'),
14
+ gray: wrap('90'),
15
+ inverseRed: wrap('1;97;41'),
16
+ inverseOrange: wrap('1;30;48;5;208'),
17
+ inverseYellow: wrap('1;30;43'),
18
+ inverseBlue: wrap('1;97;44'),
19
+ inverseGray: wrap('1;97;100'),
20
+ };
21
+
22
+ export const BADGES = {
23
+ critical: (s) => c.inverseRed(s),
24
+ high: (s) => c.inverseOrange(s),
25
+ medium: (s) => c.inverseYellow(s),
26
+ low: (s) => c.inverseBlue(s),
27
+ info: (s) => c.inverseGray(s),
28
+ };
29
+
30
+ export const TONES = {
31
+ critical: c.red,
32
+ high: wrap('38;5;208'),
33
+ medium: c.yellow,
34
+ low: c.blue,
35
+ info: c.gray,
36
+ };