@hardkeel/quantum-scan 0.2.0 → 0.3.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 +18 -0
- package/LICENSE +202 -202
- package/README.md +162 -151
- package/action.yml +44 -44
- package/dist/badge.js +60 -60
- package/dist/cbom.js +59 -59
- package/dist/cli.js +305 -305
- package/dist/community-rules.js +30 -30
- package/dist/community.js +28 -28
- package/dist/ignore.js +75 -75
- package/dist/infra.js +557 -557
- package/dist/map-activity.js +22 -22
- package/dist/map-areas.js +50 -41
- package/dist/map-cloud-keys.js +108 -0
- package/dist/map-host.js +333 -333
- package/dist/map-network.js +518 -0
- package/dist/map-people.js +77 -0
- package/dist/map-rank.js +48 -48
- package/dist/map-report.js +178 -138
- package/dist/map-run.js +271 -205
- package/dist/map-types.js +16 -16
- package/dist/map-web.js +640 -640
- package/dist/pqc.js +32 -32
- package/dist/report.js +179 -179
- package/dist/rules.js +135 -133
- package/dist/sarif.js +43 -43
- package/dist/scan.js +220 -214
- package/dist/score.js +122 -122
- package/dist/secrets.js +108 -97
- package/dist/share-config.js +111 -111
- package/dist/share.js +114 -112
- package/dist/types.js +2 -2
- package/dist/walk.js +63 -63
- package/package.json +53 -52
package/dist/cli.js
CHANGED
|
@@ -1,305 +1,305 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Hard Keel Quantum Scan, command line.
|
|
3
|
-
// Finds encryption quantum computers will break, weak encryption, exposed infrastructure settings
|
|
4
|
-
// and leaked keys in a folder,
|
|
5
|
-
// and writes an encryption inventory (CycloneDX CBOM) and a migration report.
|
|
6
|
-
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
7
|
-
import { join } from "node:path";
|
|
8
|
-
import { walkFolder } from "./walk.js";
|
|
9
|
-
import { buildCbom } from "./cbom.js";
|
|
10
|
-
import { migrationReportMarkdown, prSummaryLine, shortLabel, sortFindings } from "./report.js";
|
|
11
|
-
import { buildSarif } from "./sarif.js";
|
|
12
|
-
import { scanFiles } from "./scan.js";
|
|
13
|
-
import { badgeSvg } from "./badge.js";
|
|
14
|
-
import { gradeSentence, readinessScore } from "./score.js";
|
|
15
|
-
import { applyIgnore, IGNORE_FILE, parseIgnore } from "./ignore.js";
|
|
16
|
-
import { ActivityLog } from "./map-activity.js";
|
|
17
|
-
import { parseScope, runMap, SCOPE_FILE } from "./map-run.js";
|
|
18
|
-
import { checkProof, parseTarget, proofRecord, publicLookup } from "./map-web.js";
|
|
19
|
-
import { buildPayload, diffRuns, sendPayload } from "./share.js";
|
|
20
|
-
import { folderKey, INDUSTRIES, loadSettings, saveSettings, settingsPath, sharingState, showNoticeIfNeeded, SIZES } from "./share-config.js";
|
|
21
|
-
export const VERSION = "0.
|
|
22
|
-
const APP_URL = "https://github.com/apps/hard-keel-quantum-scan";
|
|
23
|
-
const HELP = `Hard Keel Quantum Scan ${VERSION}
|
|
24
|
-
|
|
25
|
-
Finds encryption quantum computers will break, weak encryption, exposed settings and
|
|
26
|
-
leaked keys or passwords in your code and infrastructure config (Terraform, CloudFormation,
|
|
27
|
-
Kubernetes, Dockerfiles, nginx and Apache). Writes an encryption inventory and a
|
|
28
|
-
migration report.
|
|
29
|
-
|
|
30
|
-
Usage:
|
|
31
|
-
quantum-scan [folder] [options] Scan code and infrastructure config
|
|
32
|
-
quantum-scan map [options]
|
|
33
|
-
asking before each area
|
|
34
|
-
quantum-scan prove <domain> Show the DNS record that proves a domain is yours
|
|
35
|
-
quantum-scan config <setting> Sharing, offline mode, industry and size
|
|
36
|
-
|
|
37
|
-
Options:
|
|
38
|
-
--format <text|json|sarif> What to print (default: text)
|
|
39
|
-
--output <file> Write the --format output to a file instead
|
|
40
|
-
--cbom <file> Write the encryption inventory (CycloneDX 1.6 CBOM)
|
|
41
|
-
--report <file> Write the migration report (Markdown)
|
|
42
|
-
--badge <file> Write a quantum readiness badge (SVG) for your README
|
|
43
|
-
--name <name> Repository name to use in the inventory and report
|
|
44
|
-
--fail-on <list> Exit with 1 on these: secret, weak-now, exposed,
|
|
45
|
-
quantum-vulnerable, or none (default: secret,weak-now,exposed)
|
|
46
|
-
--quiet Leave out the closing line about installing on GitHub
|
|
47
|
-
--offline Send nothing to Hard Keel
|
|
48
|
-
--version Print the version
|
|
49
|
-
--help Print this help
|
|
50
|
-
|
|
51
|
-
Map options:
|
|
52
|
-
--scope <file> Use saved answers (quantum-scan.scope.json) instead of asking
|
|
53
|
-
--out <folder> Where to write the map (default: quantum-scan-map)
|
|
54
|
-
--fail-on broken Exit with 1 when anything is broken now
|
|
55
|
-
|
|
56
|
-
Config:
|
|
57
|
-
quantum-scan config show Current settings
|
|
58
|
-
quantum-scan config show-shared Exactly what the last run sent, or would have sent
|
|
59
|
-
quantum-scan config sharing on|off Anonymized sharing (on by default)
|
|
60
|
-
quantum-scan config offline on|off Send nothing at all, ever
|
|
61
|
-
quantum-scan config industry <name> Optional. ${INDUSTRIES.join(", ")}, or none
|
|
62
|
-
quantum-scan config size <band> Optional. ${SIZES.join(", ")}, or none
|
|
63
|
-
|
|
64
|
-
Sharing: unless you turn it off, each run sends an anonymized summary: rounded counts of
|
|
65
|
-
which checks fired and which encryption types were found. Never code, keys, server
|
|
66
|
-
names, IP addresses or company name. Details: https://hardkeel.com/privacy/
|
|
67
|
-
`;
|
|
68
|
-
function parseArgs(argv) {
|
|
69
|
-
const flags = new Set(["quiet", "help", "version", "offline"]);
|
|
70
|
-
const opts = {};
|
|
71
|
-
const rest = [];
|
|
72
|
-
for (let i = 0; i < argv.length; i++) {
|
|
73
|
-
const a = argv[i];
|
|
74
|
-
if (!a.startsWith("--")) {
|
|
75
|
-
rest.push(a);
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
const [k, v] = a.slice(2).split("=", 2);
|
|
79
|
-
if (flags.has(k))
|
|
80
|
-
opts[k] = true;
|
|
81
|
-
else if (v !== undefined)
|
|
82
|
-
opts[k] = v;
|
|
83
|
-
else
|
|
84
|
-
opts[k] = argv[++i] ?? "";
|
|
85
|
-
}
|
|
86
|
-
return { opts, dir: rest[0] ?? "." };
|
|
87
|
-
}
|
|
88
|
-
function textReport(findings, filesScanned, quiet, score) {
|
|
89
|
-
const out = [`Hard Keel Quantum Scan · ${filesScanned} file${filesScanned === 1 ? "" : "s"} scanned`, "", gradeSentence(score), ""];
|
|
90
|
-
if (!findings.length) {
|
|
91
|
-
out.push("Nothing found. No leaked keys, weak or quantum-breakable encryption, or exposed settings.");
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
out.push(prSummaryLine(findings).replace("This change adds", "Found"));
|
|
95
|
-
const sorted = sortFindings(findings);
|
|
96
|
-
const section = (title, list, note) => {
|
|
97
|
-
if (!list.length)
|
|
98
|
-
return;
|
|
99
|
-
out.push("", title);
|
|
100
|
-
if (note)
|
|
101
|
-
out.push(` ${note}`);
|
|
102
|
-
for (const f of list)
|
|
103
|
-
out.push(` ${f.file}:${f.line} ${shortLabel(f)}`, ` ${f.fix}`);
|
|
104
|
-
};
|
|
105
|
-
section("Fix now", sorted.filter((f) => f.risk !== "quantum-vulnerable"));
|
|
106
|
-
section("Plan for later", sorted.filter((f) => f.risk === "quantum-vulnerable"), "Safe today, but quantum computers will be able to break these. NIST plans to phase them out between 2030 and 2035.");
|
|
107
|
-
}
|
|
108
|
-
if (!quiet)
|
|
109
|
-
out.push("", `Check every pull request automatically: ${APP_URL}`);
|
|
110
|
-
return out.join("\n");
|
|
111
|
-
}
|
|
112
|
-
export function main(argv) {
|
|
113
|
-
return scanCommand(argv).code;
|
|
114
|
-
}
|
|
115
|
-
/** The folder scan. Returns its exit code and the counts the anonymized summary may use. */
|
|
116
|
-
export function scanCommand(argv) {
|
|
117
|
-
const { opts, dir } = parseArgs(argv);
|
|
118
|
-
if (opts.help) {
|
|
119
|
-
process.stdout.write(HELP);
|
|
120
|
-
return { code: 0 };
|
|
121
|
-
}
|
|
122
|
-
if (opts.version) {
|
|
123
|
-
console.log(VERSION);
|
|
124
|
-
return { code: 0 };
|
|
125
|
-
}
|
|
126
|
-
const str = (k) => (typeof opts[k] === "string" ? opts[k] : undefined);
|
|
127
|
-
let walked;
|
|
128
|
-
try {
|
|
129
|
-
walked = walkFolder(dir);
|
|
130
|
-
}
|
|
131
|
-
catch (e) {
|
|
132
|
-
console.error(e instanceof Error ? e.message : String(e));
|
|
133
|
-
return { code: 2 };
|
|
134
|
-
}
|
|
135
|
-
if (walked.unreadable.length) {
|
|
136
|
-
const list = walked.unreadable.slice(0, 5).map((u) => `${u.path} (${u.reason})`).join(", ");
|
|
137
|
-
console.error(`Skipped ${walked.unreadable.length} path${walked.unreadable.length === 1 ? "" : "s"} that couldn't be read: ${list}${walked.unreadable.length > 5 ? ", ..." : ""}`);
|
|
138
|
-
}
|
|
139
|
-
const scanned = scanFiles(walked.files);
|
|
140
|
-
const { filesScanned, pqc } = scanned;
|
|
141
|
-
const ignorePath = join(dir, IGNORE_FILE);
|
|
142
|
-
const { kept: findings, ignored } = applyIgnore(scanned.findings, existsSync(ignorePath) ? parseIgnore(readFileSync(ignorePath, "utf8")) : []);
|
|
143
|
-
const score = readinessScore(findings, pqc);
|
|
144
|
-
const repo = str("name") ?? dir;
|
|
145
|
-
const badgePath = str("badge");
|
|
146
|
-
if (badgePath)
|
|
147
|
-
writeFileSync(badgePath, badgeSvg(score) + "\n");
|
|
148
|
-
const cbomPath = str("cbom");
|
|
149
|
-
if (cbomPath)
|
|
150
|
-
writeFileSync(cbomPath, JSON.stringify(buildCbom(findings, { repo, toolVersion: VERSION }), null, 2) + "\n");
|
|
151
|
-
const reportPath = str("report");
|
|
152
|
-
if (reportPath)
|
|
153
|
-
writeFileSync(reportPath, migrationReportMarkdown(repo, findings, new Date().toISOString().slice(0, 10), score) + "\n");
|
|
154
|
-
const format = str("format") ?? "text";
|
|
155
|
-
let body;
|
|
156
|
-
if (format === "json")
|
|
157
|
-
body = JSON.stringify({ tool: "hard-keel-quantum-scan", version: VERSION, filesScanned, score, findings }, null, 2);
|
|
158
|
-
else if (format === "sarif")
|
|
159
|
-
body = JSON.stringify(buildSarif(findings, VERSION), null, 2);
|
|
160
|
-
else if (format === "text")
|
|
161
|
-
body = textReport(findings, filesScanned, opts.quiet === true, score);
|
|
162
|
-
else {
|
|
163
|
-
console.error(`Unknown --format "${format}". Use text, json or sarif.`);
|
|
164
|
-
return { code: 2 };
|
|
165
|
-
}
|
|
166
|
-
const outPath = str("output");
|
|
167
|
-
if (outPath) {
|
|
168
|
-
writeFileSync(outPath, body + "\n");
|
|
169
|
-
console.log(`${filesScanned} files scanned, ${findings.length} finding${findings.length === 1 ? "" : "s"}. Wrote ${outPath}.`);
|
|
170
|
-
}
|
|
171
|
-
else
|
|
172
|
-
console.log(body);
|
|
173
|
-
const failOn = (str("fail-on") ?? "secret,weak-now,exposed").split(",").map((s) => s.trim());
|
|
174
|
-
const code = findings.some((f) => failOn.includes(f.risk)) ? 1 : 0;
|
|
175
|
-
const rules = {};
|
|
176
|
-
const algorithms = {};
|
|
177
|
-
for (const f of findings) {
|
|
178
|
-
rules[f.ruleId] = (rules[f.ruleId] ?? 0) + 1;
|
|
179
|
-
if (f.kind === "secret")
|
|
180
|
-
continue; // what kind of secret is a rule id already; no algorithm entry
|
|
181
|
-
const a = (algorithms[f.algorithm] ??= { count: 0, quantumVulnerable: 0 });
|
|
182
|
-
a.count++;
|
|
183
|
-
if (f.risk === "quantum-vulnerable")
|
|
184
|
-
a.quantumVulnerable++;
|
|
185
|
-
}
|
|
186
|
-
const pqcCounts = {};
|
|
187
|
-
for (const p of pqc)
|
|
188
|
-
pqcCounts[p.algorithm] = (pqcCounts[p.algorithm] ?? 0) + 1;
|
|
189
|
-
const facts = { command: "scan", areas: ["code"], rules, algorithms, quantumSafe: pqcCounts, fixed: {}, ignored, replacements: {}, errors: walked.unreadable.some((u) => u.reason === "permission denied") ? ["permission"] : walked.unreadable.length ? ["read"] : [] };
|
|
190
|
-
return { code, facts, stateKey: folderKey(dir), state: { rules, pqc: pqcCounts } };
|
|
191
|
-
}
|
|
192
|
-
/** Records this run, works out what was fixed since the last one, and sends the summary unless sharing is off. */
|
|
193
|
-
async function finishRun(facts, stateKey, state, s, sharing, send) {
|
|
194
|
-
if (!facts || !stateKey || !state)
|
|
195
|
-
return;
|
|
196
|
-
const key = stateKey.startsWith("map:") ? folderKey(stateKey) : stateKey;
|
|
197
|
-
const diff = diffRuns(s.state[key], state);
|
|
198
|
-
facts.fixed = diff.fixed;
|
|
199
|
-
facts.replacements = diff.replacements;
|
|
200
|
-
s.state[key] = state;
|
|
201
|
-
const payload = buildPayload(facts, s, VERSION);
|
|
202
|
-
s.lastShared = sharing === "on" ? payload : { notSent: sharing, wouldHaveSent: payload };
|
|
203
|
-
// The notice comes before the first summary is sent, and only on runs that send one.
|
|
204
|
-
showNoticeIfNeeded(s, sharing);
|
|
205
|
-
saveSettings(s);
|
|
206
|
-
if (sharing === "on")
|
|
207
|
-
await send(payload);
|
|
208
|
-
}
|
|
209
|
-
function configCommand(args, s) {
|
|
210
|
-
const [what, value] = args;
|
|
211
|
-
const onOff = (v) => (v === "on" || v === "off" ? v : null);
|
|
212
|
-
if (!what || what === "show") {
|
|
213
|
-
console.log([
|
|
214
|
-
`Settings file: ${settingsPath()}`,
|
|
215
|
-
`Sharing: ${s.sharing}${sharingState(s, {}) !== s.sharing ? ` (overridden here: ${sharingState(s, {})})` : ""}`,
|
|
216
|
-
`Offline mode: ${s.offline ? "on" : "off"}`,
|
|
217
|
-
`Industry: ${s.industry ?? "not set"}`,
|
|
218
|
-
`Size: ${s.size ?? "not set"}`,
|
|
219
|
-
`Random ID: ${s.id} (linked to nothing; quote it to ask us to delete your data)`,
|
|
220
|
-
].join("\n"));
|
|
221
|
-
return 0;
|
|
222
|
-
}
|
|
223
|
-
if (what === "show-shared") {
|
|
224
|
-
console.log(s.lastShared ? JSON.stringify(s.lastShared, null, 2) : "Nothing yet. Run a scan first.");
|
|
225
|
-
return 0;
|
|
226
|
-
}
|
|
227
|
-
if (what === "sharing" && onOff(value))
|
|
228
|
-
s.sharing = onOff(value);
|
|
229
|
-
else if (what === "offline" && onOff(value))
|
|
230
|
-
s.offline = value === "on";
|
|
231
|
-
else if (what === "industry" && value && (value === "none" || INDUSTRIES.includes(value)))
|
|
232
|
-
s.industry = value === "none" ? undefined : value;
|
|
233
|
-
else if (what === "size" && value && (value === "none" || SIZES.includes(value)))
|
|
234
|
-
s.size = value === "none" ? undefined : value;
|
|
235
|
-
else {
|
|
236
|
-
console.error("Unknown setting. Run quantum-scan --help for the list.");
|
|
237
|
-
return 2;
|
|
238
|
-
}
|
|
239
|
-
s.profileAsked = true;
|
|
240
|
-
saveSettings(s);
|
|
241
|
-
console.log(`Saved: ${what} ${value}.`);
|
|
242
|
-
return 0;
|
|
243
|
-
}
|
|
244
|
-
async function proveCommand(domain, s) {
|
|
245
|
-
const p = domain ? parseTarget(domain) : { error: "missing domain" };
|
|
246
|
-
if ("error" in p) {
|
|
247
|
-
console.error("Usage: quantum-scan prove <domain>, for example quantum-scan prove example.com");
|
|
248
|
-
return 2;
|
|
249
|
-
}
|
|
250
|
-
const scope = existsSync(SCOPE_FILE) ? parseScope(readFileSync(SCOPE_FILE, "utf8")) : null;
|
|
251
|
-
const token = scope && !("error" in scope) && scope.ownershipToken ? scope.ownershipToken : s.ownershipToken;
|
|
252
|
-
const r = proofRecord(p.host, token);
|
|
253
|
-
console.log(`To prove you own ${p.host}, add this DNS record:\n\n Name: ${r.name}\n Type: TXT\n Value: ${r.value}\n\nIt covers ${p.host} and everything under it. It only allows look-only checks from this machine${scope && !("error" in scope) && scope.ownershipToken ? " and anyone using this scope file" : ""}.`);
|
|
254
|
-
const r2 = await checkProof(p.host, token, new ActivityLog(), undefined, publicLookup());
|
|
255
|
-
if (r2.status === "found")
|
|
256
|
-
console.log(`\nFound it. ${p.host} is proved.`);
|
|
257
|
-
else if (r2.status === "missing")
|
|
258
|
-
console.log("\nNot found yet. DNS changes can take a few minutes; run this again to check.");
|
|
259
|
-
else
|
|
260
|
-
console.log(`\nCouldn't check: ${r2.detail}. Check this machine's DNS settings, then run this again.`);
|
|
261
|
-
return r2.status === "found" ? 0 : 1;
|
|
262
|
-
}
|
|
263
|
-
/** Entry point: subcommands, the first-run notice and the anonymized summary. */
|
|
264
|
-
export async function run(argv, deps = {}) {
|
|
265
|
-
const send = deps.send ?? ((p) => sendPayload(p));
|
|
266
|
-
const s = loadSettings();
|
|
267
|
-
const [cmd, ...rest] = argv;
|
|
268
|
-
if (cmd === "config")
|
|
269
|
-
return configCommand(rest, s);
|
|
270
|
-
if (cmd === "prove")
|
|
271
|
-
return proveCommand(rest[0], s);
|
|
272
|
-
const offline = argv.includes("--offline");
|
|
273
|
-
const sharing = sharingState(s, { offline });
|
|
274
|
-
if (cmd === "map") {
|
|
275
|
-
const { opts } = parseArgs(rest);
|
|
276
|
-
const str = (k) => (typeof opts[k] === "string" ? opts[k] : undefined);
|
|
277
|
-
if (opts.help) {
|
|
278
|
-
process.stdout.write(HELP);
|
|
279
|
-
return 0;
|
|
280
|
-
}
|
|
281
|
-
const out = await runMap({
|
|
282
|
-
scopePath: str("scope"),
|
|
283
|
-
outDir: str("out") ?? "quantum-scan-map",
|
|
284
|
-
cwd: process.cwd(),
|
|
285
|
-
version: VERSION,
|
|
286
|
-
settings: s,
|
|
287
|
-
sharingOn: sharing === "on",
|
|
288
|
-
interactive: deps.interactive ?? Boolean(process.stdin.isTTY),
|
|
289
|
-
});
|
|
290
|
-
await finishRun(out.facts, out.stateKey, out.state, s, sharing, send);
|
|
291
|
-
if (str("fail-on") === "broken" && out.report && out.report.counts[1] > 0)
|
|
292
|
-
return 1;
|
|
293
|
-
return out.code;
|
|
294
|
-
}
|
|
295
|
-
const r = scanCommand(argv);
|
|
296
|
-
await finishRun(r.facts, r.stateKey, r.state, s, sharing, send);
|
|
297
|
-
return r.code;
|
|
298
|
-
}
|
|
299
|
-
const invoked = process.argv[1] ?? "";
|
|
300
|
-
if (/cli\.(ts|js)$/.test(invoked) || invoked.endsWith("quantum-scan")) {
|
|
301
|
-
run(process.argv.slice(2)).then((code) => (process.exitCode = code), (e) => {
|
|
302
|
-
console.error(e instanceof Error ? e.message : e);
|
|
303
|
-
process.exitCode = 2;
|
|
304
|
-
});
|
|
305
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Hard Keel Quantum Scan, command line.
|
|
3
|
+
// Finds encryption quantum computers will break, weak encryption, exposed infrastructure settings
|
|
4
|
+
// and leaked keys in a folder,
|
|
5
|
+
// and writes an encryption inventory (CycloneDX CBOM) and a migration report.
|
|
6
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { walkFolder } from "./walk.js";
|
|
9
|
+
import { buildCbom } from "./cbom.js";
|
|
10
|
+
import { migrationReportMarkdown, prSummaryLine, shortLabel, sortFindings } from "./report.js";
|
|
11
|
+
import { buildSarif } from "./sarif.js";
|
|
12
|
+
import { scanFiles } from "./scan.js";
|
|
13
|
+
import { badgeSvg } from "./badge.js";
|
|
14
|
+
import { gradeSentence, readinessScore } from "./score.js";
|
|
15
|
+
import { applyIgnore, IGNORE_FILE, parseIgnore } from "./ignore.js";
|
|
16
|
+
import { ActivityLog } from "./map-activity.js";
|
|
17
|
+
import { parseScope, runMap, SCOPE_FILE } from "./map-run.js";
|
|
18
|
+
import { checkProof, parseTarget, proofRecord, publicLookup } from "./map-web.js";
|
|
19
|
+
import { buildPayload, diffRuns, sendPayload } from "./share.js";
|
|
20
|
+
import { folderKey, INDUSTRIES, loadSettings, saveSettings, settingsPath, sharingState, showNoticeIfNeeded, SIZES } from "./share-config.js";
|
|
21
|
+
export const VERSION = "0.3.0";
|
|
22
|
+
const APP_URL = "https://github.com/apps/hard-keel-quantum-scan";
|
|
23
|
+
const HELP = `Hard Keel Quantum Scan ${VERSION}
|
|
24
|
+
|
|
25
|
+
Finds encryption quantum computers will break, weak encryption, exposed settings and
|
|
26
|
+
leaked keys or passwords in your code and infrastructure config (Terraform, CloudFormation,
|
|
27
|
+
Kubernetes, Dockerfiles, nginx and Apache). Writes an encryption inventory and a
|
|
28
|
+
migration report.
|
|
29
|
+
|
|
30
|
+
Usage:
|
|
31
|
+
quantum-scan [folder] [options] Scan code and infrastructure config
|
|
32
|
+
quantum-scan map [options] Security map: code, websites, your network and
|
|
33
|
+
equipment, and this machine, asking before each area
|
|
34
|
+
quantum-scan prove <domain> Show the DNS record that proves a domain is yours
|
|
35
|
+
quantum-scan config <setting> Sharing, offline mode, industry and size
|
|
36
|
+
|
|
37
|
+
Options:
|
|
38
|
+
--format <text|json|sarif> What to print (default: text)
|
|
39
|
+
--output <file> Write the --format output to a file instead
|
|
40
|
+
--cbom <file> Write the encryption inventory (CycloneDX 1.6 CBOM)
|
|
41
|
+
--report <file> Write the migration report (Markdown)
|
|
42
|
+
--badge <file> Write a quantum readiness badge (SVG) for your README
|
|
43
|
+
--name <name> Repository name to use in the inventory and report
|
|
44
|
+
--fail-on <list> Exit with 1 on these: secret, weak-now, exposed,
|
|
45
|
+
quantum-vulnerable, or none (default: secret,weak-now,exposed)
|
|
46
|
+
--quiet Leave out the closing line about installing on GitHub
|
|
47
|
+
--offline Send nothing to Hard Keel
|
|
48
|
+
--version Print the version
|
|
49
|
+
--help Print this help
|
|
50
|
+
|
|
51
|
+
Map options:
|
|
52
|
+
--scope <file> Use saved answers (quantum-scan.scope.json) instead of asking
|
|
53
|
+
--out <folder> Where to write the map (default: quantum-scan-map)
|
|
54
|
+
--fail-on broken Exit with 1 when anything is broken now
|
|
55
|
+
|
|
56
|
+
Config:
|
|
57
|
+
quantum-scan config show Current settings
|
|
58
|
+
quantum-scan config show-shared Exactly what the last run sent, or would have sent
|
|
59
|
+
quantum-scan config sharing on|off Anonymized sharing (on by default)
|
|
60
|
+
quantum-scan config offline on|off Send nothing at all, ever
|
|
61
|
+
quantum-scan config industry <name> Optional. ${INDUSTRIES.join(", ")}, or none
|
|
62
|
+
quantum-scan config size <band> Optional. ${SIZES.join(", ")}, or none
|
|
63
|
+
|
|
64
|
+
Sharing: unless you turn it off, each run sends an anonymized summary: rounded counts of
|
|
65
|
+
which checks fired and which encryption types were found. Never code, keys, server
|
|
66
|
+
names, IP addresses or company name. Details: https://hardkeel.com/privacy/
|
|
67
|
+
`;
|
|
68
|
+
function parseArgs(argv) {
|
|
69
|
+
const flags = new Set(["quiet", "help", "version", "offline"]);
|
|
70
|
+
const opts = {};
|
|
71
|
+
const rest = [];
|
|
72
|
+
for (let i = 0; i < argv.length; i++) {
|
|
73
|
+
const a = argv[i];
|
|
74
|
+
if (!a.startsWith("--")) {
|
|
75
|
+
rest.push(a);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const [k, v] = a.slice(2).split("=", 2);
|
|
79
|
+
if (flags.has(k))
|
|
80
|
+
opts[k] = true;
|
|
81
|
+
else if (v !== undefined)
|
|
82
|
+
opts[k] = v;
|
|
83
|
+
else
|
|
84
|
+
opts[k] = argv[++i] ?? "";
|
|
85
|
+
}
|
|
86
|
+
return { opts, dir: rest[0] ?? "." };
|
|
87
|
+
}
|
|
88
|
+
function textReport(findings, filesScanned, quiet, score) {
|
|
89
|
+
const out = [`Hard Keel Quantum Scan · ${filesScanned} file${filesScanned === 1 ? "" : "s"} scanned`, "", gradeSentence(score), ""];
|
|
90
|
+
if (!findings.length) {
|
|
91
|
+
out.push("Nothing found. No leaked keys, weak or quantum-breakable encryption, or exposed settings.");
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
out.push(prSummaryLine(findings).replace("This change adds", "Found"));
|
|
95
|
+
const sorted = sortFindings(findings);
|
|
96
|
+
const section = (title, list, note) => {
|
|
97
|
+
if (!list.length)
|
|
98
|
+
return;
|
|
99
|
+
out.push("", title);
|
|
100
|
+
if (note)
|
|
101
|
+
out.push(` ${note}`);
|
|
102
|
+
for (const f of list)
|
|
103
|
+
out.push(` ${f.file}:${f.line} ${shortLabel(f)}`, ` ${f.fix}`);
|
|
104
|
+
};
|
|
105
|
+
section("Fix now", sorted.filter((f) => f.risk !== "quantum-vulnerable"));
|
|
106
|
+
section("Plan for later", sorted.filter((f) => f.risk === "quantum-vulnerable"), "Safe today, but quantum computers will be able to break these. NIST plans to phase them out between 2030 and 2035.");
|
|
107
|
+
}
|
|
108
|
+
if (!quiet)
|
|
109
|
+
out.push("", `Check every pull request automatically: ${APP_URL}`);
|
|
110
|
+
return out.join("\n");
|
|
111
|
+
}
|
|
112
|
+
export function main(argv) {
|
|
113
|
+
return scanCommand(argv).code;
|
|
114
|
+
}
|
|
115
|
+
/** The folder scan. Returns its exit code and the counts the anonymized summary may use. */
|
|
116
|
+
export function scanCommand(argv) {
|
|
117
|
+
const { opts, dir } = parseArgs(argv);
|
|
118
|
+
if (opts.help) {
|
|
119
|
+
process.stdout.write(HELP);
|
|
120
|
+
return { code: 0 };
|
|
121
|
+
}
|
|
122
|
+
if (opts.version) {
|
|
123
|
+
console.log(VERSION);
|
|
124
|
+
return { code: 0 };
|
|
125
|
+
}
|
|
126
|
+
const str = (k) => (typeof opts[k] === "string" ? opts[k] : undefined);
|
|
127
|
+
let walked;
|
|
128
|
+
try {
|
|
129
|
+
walked = walkFolder(dir);
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
console.error(e instanceof Error ? e.message : String(e));
|
|
133
|
+
return { code: 2 };
|
|
134
|
+
}
|
|
135
|
+
if (walked.unreadable.length) {
|
|
136
|
+
const list = walked.unreadable.slice(0, 5).map((u) => `${u.path} (${u.reason})`).join(", ");
|
|
137
|
+
console.error(`Skipped ${walked.unreadable.length} path${walked.unreadable.length === 1 ? "" : "s"} that couldn't be read: ${list}${walked.unreadable.length > 5 ? ", ..." : ""}`);
|
|
138
|
+
}
|
|
139
|
+
const scanned = scanFiles(walked.files);
|
|
140
|
+
const { filesScanned, pqc } = scanned;
|
|
141
|
+
const ignorePath = join(dir, IGNORE_FILE);
|
|
142
|
+
const { kept: findings, ignored } = applyIgnore(scanned.findings, existsSync(ignorePath) ? parseIgnore(readFileSync(ignorePath, "utf8")) : []);
|
|
143
|
+
const score = readinessScore(findings, pqc);
|
|
144
|
+
const repo = str("name") ?? dir;
|
|
145
|
+
const badgePath = str("badge");
|
|
146
|
+
if (badgePath)
|
|
147
|
+
writeFileSync(badgePath, badgeSvg(score) + "\n");
|
|
148
|
+
const cbomPath = str("cbom");
|
|
149
|
+
if (cbomPath)
|
|
150
|
+
writeFileSync(cbomPath, JSON.stringify(buildCbom(findings, { repo, toolVersion: VERSION }), null, 2) + "\n");
|
|
151
|
+
const reportPath = str("report");
|
|
152
|
+
if (reportPath)
|
|
153
|
+
writeFileSync(reportPath, migrationReportMarkdown(repo, findings, new Date().toISOString().slice(0, 10), score) + "\n");
|
|
154
|
+
const format = str("format") ?? "text";
|
|
155
|
+
let body;
|
|
156
|
+
if (format === "json")
|
|
157
|
+
body = JSON.stringify({ tool: "hard-keel-quantum-scan", version: VERSION, filesScanned, score, findings }, null, 2);
|
|
158
|
+
else if (format === "sarif")
|
|
159
|
+
body = JSON.stringify(buildSarif(findings, VERSION), null, 2);
|
|
160
|
+
else if (format === "text")
|
|
161
|
+
body = textReport(findings, filesScanned, opts.quiet === true, score);
|
|
162
|
+
else {
|
|
163
|
+
console.error(`Unknown --format "${format}". Use text, json or sarif.`);
|
|
164
|
+
return { code: 2 };
|
|
165
|
+
}
|
|
166
|
+
const outPath = str("output");
|
|
167
|
+
if (outPath) {
|
|
168
|
+
writeFileSync(outPath, body + "\n");
|
|
169
|
+
console.log(`${filesScanned} files scanned, ${findings.length} finding${findings.length === 1 ? "" : "s"}. Wrote ${outPath}.`);
|
|
170
|
+
}
|
|
171
|
+
else
|
|
172
|
+
console.log(body);
|
|
173
|
+
const failOn = (str("fail-on") ?? "secret,weak-now,exposed").split(",").map((s) => s.trim());
|
|
174
|
+
const code = findings.some((f) => failOn.includes(f.risk)) ? 1 : 0;
|
|
175
|
+
const rules = {};
|
|
176
|
+
const algorithms = {};
|
|
177
|
+
for (const f of findings) {
|
|
178
|
+
rules[f.ruleId] = (rules[f.ruleId] ?? 0) + 1;
|
|
179
|
+
if (f.kind === "secret")
|
|
180
|
+
continue; // what kind of secret is a rule id already; no algorithm entry
|
|
181
|
+
const a = (algorithms[f.algorithm] ??= { count: 0, quantumVulnerable: 0 });
|
|
182
|
+
a.count++;
|
|
183
|
+
if (f.risk === "quantum-vulnerable")
|
|
184
|
+
a.quantumVulnerable++;
|
|
185
|
+
}
|
|
186
|
+
const pqcCounts = {};
|
|
187
|
+
for (const p of pqc)
|
|
188
|
+
pqcCounts[p.algorithm] = (pqcCounts[p.algorithm] ?? 0) + 1;
|
|
189
|
+
const facts = { command: "scan", areas: ["code"], rules, algorithms, quantumSafe: pqcCounts, fixed: {}, ignored, replacements: {}, errors: walked.unreadable.some((u) => u.reason === "permission denied") ? ["permission"] : walked.unreadable.length ? ["read"] : [] };
|
|
190
|
+
return { code, facts, stateKey: folderKey(dir), state: { rules, pqc: pqcCounts } };
|
|
191
|
+
}
|
|
192
|
+
/** Records this run, works out what was fixed since the last one, and sends the summary unless sharing is off. */
|
|
193
|
+
async function finishRun(facts, stateKey, state, s, sharing, send) {
|
|
194
|
+
if (!facts || !stateKey || !state)
|
|
195
|
+
return;
|
|
196
|
+
const key = stateKey.startsWith("map:") ? folderKey(stateKey) : stateKey;
|
|
197
|
+
const diff = diffRuns(s.state[key], state);
|
|
198
|
+
facts.fixed = diff.fixed;
|
|
199
|
+
facts.replacements = diff.replacements;
|
|
200
|
+
s.state[key] = state;
|
|
201
|
+
const payload = buildPayload(facts, s, VERSION);
|
|
202
|
+
s.lastShared = sharing === "on" ? payload : { notSent: sharing, wouldHaveSent: payload };
|
|
203
|
+
// The notice comes before the first summary is sent, and only on runs that send one.
|
|
204
|
+
showNoticeIfNeeded(s, sharing);
|
|
205
|
+
saveSettings(s);
|
|
206
|
+
if (sharing === "on")
|
|
207
|
+
await send(payload);
|
|
208
|
+
}
|
|
209
|
+
function configCommand(args, s) {
|
|
210
|
+
const [what, value] = args;
|
|
211
|
+
const onOff = (v) => (v === "on" || v === "off" ? v : null);
|
|
212
|
+
if (!what || what === "show") {
|
|
213
|
+
console.log([
|
|
214
|
+
`Settings file: ${settingsPath()}`,
|
|
215
|
+
`Sharing: ${s.sharing}${sharingState(s, {}) !== s.sharing ? ` (overridden here: ${sharingState(s, {})})` : ""}`,
|
|
216
|
+
`Offline mode: ${s.offline ? "on" : "off"}`,
|
|
217
|
+
`Industry: ${s.industry ?? "not set"}`,
|
|
218
|
+
`Size: ${s.size ?? "not set"}`,
|
|
219
|
+
`Random ID: ${s.id} (linked to nothing; quote it to ask us to delete your data)`,
|
|
220
|
+
].join("\n"));
|
|
221
|
+
return 0;
|
|
222
|
+
}
|
|
223
|
+
if (what === "show-shared") {
|
|
224
|
+
console.log(s.lastShared ? JSON.stringify(s.lastShared, null, 2) : "Nothing yet. Run a scan first.");
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
if (what === "sharing" && onOff(value))
|
|
228
|
+
s.sharing = onOff(value);
|
|
229
|
+
else if (what === "offline" && onOff(value))
|
|
230
|
+
s.offline = value === "on";
|
|
231
|
+
else if (what === "industry" && value && (value === "none" || INDUSTRIES.includes(value)))
|
|
232
|
+
s.industry = value === "none" ? undefined : value;
|
|
233
|
+
else if (what === "size" && value && (value === "none" || SIZES.includes(value)))
|
|
234
|
+
s.size = value === "none" ? undefined : value;
|
|
235
|
+
else {
|
|
236
|
+
console.error("Unknown setting. Run quantum-scan --help for the list.");
|
|
237
|
+
return 2;
|
|
238
|
+
}
|
|
239
|
+
s.profileAsked = true;
|
|
240
|
+
saveSettings(s);
|
|
241
|
+
console.log(`Saved: ${what} ${value}.`);
|
|
242
|
+
return 0;
|
|
243
|
+
}
|
|
244
|
+
async function proveCommand(domain, s) {
|
|
245
|
+
const p = domain ? parseTarget(domain) : { error: "missing domain" };
|
|
246
|
+
if ("error" in p) {
|
|
247
|
+
console.error("Usage: quantum-scan prove <domain>, for example quantum-scan prove example.com");
|
|
248
|
+
return 2;
|
|
249
|
+
}
|
|
250
|
+
const scope = existsSync(SCOPE_FILE) ? parseScope(readFileSync(SCOPE_FILE, "utf8")) : null;
|
|
251
|
+
const token = scope && !("error" in scope) && scope.ownershipToken ? scope.ownershipToken : s.ownershipToken;
|
|
252
|
+
const r = proofRecord(p.host, token);
|
|
253
|
+
console.log(`To prove you own ${p.host}, add this DNS record:\n\n Name: ${r.name}\n Type: TXT\n Value: ${r.value}\n\nIt covers ${p.host} and everything under it. It only allows look-only checks from this machine${scope && !("error" in scope) && scope.ownershipToken ? " and anyone using this scope file" : ""}.`);
|
|
254
|
+
const r2 = await checkProof(p.host, token, new ActivityLog(), undefined, publicLookup());
|
|
255
|
+
if (r2.status === "found")
|
|
256
|
+
console.log(`\nFound it. ${p.host} is proved.`);
|
|
257
|
+
else if (r2.status === "missing")
|
|
258
|
+
console.log("\nNot found yet. DNS changes can take a few minutes; run this again to check.");
|
|
259
|
+
else
|
|
260
|
+
console.log(`\nCouldn't check: ${r2.detail}. Check this machine's DNS settings, then run this again.`);
|
|
261
|
+
return r2.status === "found" ? 0 : 1;
|
|
262
|
+
}
|
|
263
|
+
/** Entry point: subcommands, the first-run notice and the anonymized summary. */
|
|
264
|
+
export async function run(argv, deps = {}) {
|
|
265
|
+
const send = deps.send ?? ((p) => sendPayload(p));
|
|
266
|
+
const s = loadSettings();
|
|
267
|
+
const [cmd, ...rest] = argv;
|
|
268
|
+
if (cmd === "config")
|
|
269
|
+
return configCommand(rest, s);
|
|
270
|
+
if (cmd === "prove")
|
|
271
|
+
return proveCommand(rest[0], s);
|
|
272
|
+
const offline = argv.includes("--offline");
|
|
273
|
+
const sharing = sharingState(s, { offline });
|
|
274
|
+
if (cmd === "map") {
|
|
275
|
+
const { opts } = parseArgs(rest);
|
|
276
|
+
const str = (k) => (typeof opts[k] === "string" ? opts[k] : undefined);
|
|
277
|
+
if (opts.help) {
|
|
278
|
+
process.stdout.write(HELP);
|
|
279
|
+
return 0;
|
|
280
|
+
}
|
|
281
|
+
const out = await runMap({
|
|
282
|
+
scopePath: str("scope"),
|
|
283
|
+
outDir: str("out") ?? "quantum-scan-map",
|
|
284
|
+
cwd: process.cwd(),
|
|
285
|
+
version: VERSION,
|
|
286
|
+
settings: s,
|
|
287
|
+
sharingOn: sharing === "on",
|
|
288
|
+
interactive: deps.interactive ?? Boolean(process.stdin.isTTY),
|
|
289
|
+
});
|
|
290
|
+
await finishRun(out.facts, out.stateKey, out.state, s, sharing, send);
|
|
291
|
+
if (str("fail-on") === "broken" && out.report && out.report.counts[1] > 0)
|
|
292
|
+
return 1;
|
|
293
|
+
return out.code;
|
|
294
|
+
}
|
|
295
|
+
const r = scanCommand(argv);
|
|
296
|
+
await finishRun(r.facts, r.stateKey, r.state, s, sharing, send);
|
|
297
|
+
return r.code;
|
|
298
|
+
}
|
|
299
|
+
const invoked = process.argv[1] ?? "";
|
|
300
|
+
if (/cli\.(ts|js)$/.test(invoked) || invoked.endsWith("quantum-scan")) {
|
|
301
|
+
run(process.argv.slice(2)).then((code) => (process.exitCode = code), (e) => {
|
|
302
|
+
console.error(e instanceof Error ? e.message : e);
|
|
303
|
+
process.exitCode = 2;
|
|
304
|
+
});
|
|
305
|
+
}
|