@hardkeel/quantum-scan 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/README.md +151 -3
- package/action.yml +44 -0
- package/dist/badge.js +60 -0
- package/dist/cbom.js +59 -0
- package/dist/cli.js +305 -0
- package/dist/community-rules.js +30 -0
- package/dist/community.js +28 -0
- package/dist/ignore.js +75 -0
- package/dist/infra.js +557 -0
- package/dist/map-activity.js +22 -0
- package/dist/map-areas.js +41 -0
- package/dist/map-host.js +333 -0
- package/dist/map-rank.js +48 -0
- package/dist/map-report.js +138 -0
- package/dist/map-run.js +205 -0
- package/dist/map-types.js +16 -0
- package/dist/map-web.js +640 -0
- package/dist/pqc.js +32 -0
- package/dist/report.js +179 -0
- package/dist/rules.js +133 -0
- package/dist/sarif.js +43 -0
- package/dist/scan.js +214 -0
- package/dist/score.js +122 -0
- package/dist/secrets.js +97 -0
- package/dist/share-config.js +111 -0
- package/dist/share.js +112 -0
- package/dist/types.js +2 -0
- package/dist/walk.js +63 -0
- package/package.json +52 -6
package/dist/score.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// Hard Keel quantum readiness score, method version 1.
|
|
2
|
+
// Turns a full-repository scan into a 0-100 score and a grade from A+ to F.
|
|
3
|
+
// The method is public: docs/readiness-score.md and hardkeel.com/learn/quantum-readiness-score/.
|
|
4
|
+
// Change a weight, cap or threshold only by bumping METHOD_VERSION and the docs together.
|
|
5
|
+
export const METHOD_VERSION = "1";
|
|
6
|
+
const GRADES = ["A+", "A", "B", "C", "D", "F"];
|
|
7
|
+
/** Points taken off for one kind of problem (one algorithm or setting), before the use multiplier. */
|
|
8
|
+
const BASE = {
|
|
9
|
+
secret: () => 25,
|
|
10
|
+
weak: (s) => (s === "critical" || s === "high" ? 10 : 6),
|
|
11
|
+
exposed: (s) => ({ critical: 12, high: 8, medium: 5, low: 2 })[s],
|
|
12
|
+
"quantum-key-exchange": () => 5,
|
|
13
|
+
"quantum-signature": () => 2,
|
|
14
|
+
};
|
|
15
|
+
/** Most a category can take off in total, so one area can't hide every other. */
|
|
16
|
+
const CAP = {
|
|
17
|
+
secret: 50,
|
|
18
|
+
weak: 30,
|
|
19
|
+
exposed: 30,
|
|
20
|
+
"quantum-key-exchange": 20,
|
|
21
|
+
"quantum-signature": 8,
|
|
22
|
+
};
|
|
23
|
+
/** Best grade a repository can get while it has any problem in this category. */
|
|
24
|
+
const GRADE_CAP = {
|
|
25
|
+
secret: "D",
|
|
26
|
+
weak: "C",
|
|
27
|
+
exposed: "C",
|
|
28
|
+
"quantum-key-exchange": "B",
|
|
29
|
+
"quantum-signature": "A",
|
|
30
|
+
};
|
|
31
|
+
const THRESHOLDS = [
|
|
32
|
+
[90, "A"],
|
|
33
|
+
[75, "B"],
|
|
34
|
+
[60, "C"],
|
|
35
|
+
[40, "D"],
|
|
36
|
+
[0, "F"],
|
|
37
|
+
];
|
|
38
|
+
export function categoryOf(f) {
|
|
39
|
+
if (f.risk === "secret")
|
|
40
|
+
return "secret";
|
|
41
|
+
if (f.risk === "weak-now")
|
|
42
|
+
return "weak";
|
|
43
|
+
if (f.risk === "exposed")
|
|
44
|
+
return "exposed";
|
|
45
|
+
return f.primitive === "signature" ? "quantum-signature" : "quantum-key-exchange";
|
|
46
|
+
}
|
|
47
|
+
/** 1 use counts once; more uses count more, up to double (16 or more uses). */
|
|
48
|
+
export function useMultiplier(uses) {
|
|
49
|
+
return Math.min(2, 1 + 0.25 * Math.log2(Math.max(1, uses)));
|
|
50
|
+
}
|
|
51
|
+
function worse(a, b) {
|
|
52
|
+
return GRADES[Math.max(GRADES.indexOf(a), GRADES.indexOf(b))];
|
|
53
|
+
}
|
|
54
|
+
export function readinessScore(findings, pqc = []) {
|
|
55
|
+
const pqcKex = pqc.some((p) => p.use === "key-establishment");
|
|
56
|
+
const pqcSig = pqc.some((p) => p.use === "signature");
|
|
57
|
+
// One deduction per category and algorithm (or setting), however many files use it.
|
|
58
|
+
const groups = new Map();
|
|
59
|
+
for (const f of findings) {
|
|
60
|
+
const k = `${categoryOf(f)}|${f.algorithm}`;
|
|
61
|
+
groups.set(k, [...(groups.get(k) ?? []), f]);
|
|
62
|
+
}
|
|
63
|
+
const deductions = [];
|
|
64
|
+
for (const g of groups.values()) {
|
|
65
|
+
const category = categoryOf(g[0]);
|
|
66
|
+
// A group is as serious as its most serious use.
|
|
67
|
+
const sev = g.map((f) => f.severity).sort((a, b) => sevRank(a) - sevRank(b))[0];
|
|
68
|
+
let points = BASE[category](sev) * useMultiplier(g.length);
|
|
69
|
+
// Credit for migration under way: half off where post-quantum methods are already in use.
|
|
70
|
+
if ((category === "quantum-key-exchange" && pqcKex) || (category === "quantum-signature" && pqcSig))
|
|
71
|
+
points /= 2;
|
|
72
|
+
deductions.push({ category, algorithm: g[0].algorithm, uses: g.length, points: round1(points) });
|
|
73
|
+
}
|
|
74
|
+
const counts = { secret: 0, weak: 0, exposed: 0, "quantum-key-exchange": 0, "quantum-signature": 0 };
|
|
75
|
+
const byCategory = { ...counts };
|
|
76
|
+
for (const d of deductions) {
|
|
77
|
+
counts[d.category] += d.uses;
|
|
78
|
+
byCategory[d.category] += d.points;
|
|
79
|
+
}
|
|
80
|
+
let total = 0;
|
|
81
|
+
for (const c of Object.keys(CAP))
|
|
82
|
+
total += Math.min(CAP[c], byCategory[c]);
|
|
83
|
+
const score = Math.max(0, Math.round(100 - total));
|
|
84
|
+
let grade = THRESHOLDS.find(([min]) => score >= min)[1];
|
|
85
|
+
for (const c of Object.keys(GRADE_CAP))
|
|
86
|
+
if (counts[c])
|
|
87
|
+
grade = worse(grade, GRADE_CAP[c]);
|
|
88
|
+
// A+ is for repositories with nothing left to fix that already use post-quantum methods.
|
|
89
|
+
if (grade === "A" && !findings.length && (pqcKex || pqcSig))
|
|
90
|
+
grade = "A+";
|
|
91
|
+
const due = counts.secret || counts.weak || counts.exposed ? "now" : counts["quantum-key-exchange"] ? "2030" : counts["quantum-signature"] ? "2035" : null;
|
|
92
|
+
return {
|
|
93
|
+
method: METHOD_VERSION,
|
|
94
|
+
score,
|
|
95
|
+
grade,
|
|
96
|
+
counts,
|
|
97
|
+
pqc: { keyExchange: pqcKex, signatures: pqcSig, algorithms: [...new Set(pqc.map((p) => p.algorithm))] },
|
|
98
|
+
due,
|
|
99
|
+
deductions: deductions.sort((a, b) => b.points - a.points),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function sevRank(s) {
|
|
103
|
+
return { critical: 0, high: 1, medium: 2, low: 3 }[s];
|
|
104
|
+
}
|
|
105
|
+
function round1(n) {
|
|
106
|
+
return Math.round(n * 10) / 10;
|
|
107
|
+
}
|
|
108
|
+
/** One plain sentence about the grade, for reports and the command line. */
|
|
109
|
+
export function gradeSentence(s) {
|
|
110
|
+
const lead = `Quantum readiness: ${s.grade} (${s.score}/100, Hard Keel method v${s.method}).`;
|
|
111
|
+
if (s.grade === "A+")
|
|
112
|
+
return `${lead} Nothing to fix, and post-quantum encryption is already in use.`;
|
|
113
|
+
if (s.due === null)
|
|
114
|
+
return `${lead} Nothing to fix.`;
|
|
115
|
+
if (s.counts.secret)
|
|
116
|
+
return `${lead} A leaked key or password holds the grade at D or lower until it is revoked and removed.`;
|
|
117
|
+
if (s.due === "now")
|
|
118
|
+
return `${lead} Weak encryption or exposed settings hold the grade at C or lower until they are fixed.`;
|
|
119
|
+
if (s.due === "2030")
|
|
120
|
+
return `${lead} Key exchange or RSA keys that quantum computers will break hold the grade at B until they move to post-quantum methods.`;
|
|
121
|
+
return `${lead} Only signatures are left to move to post-quantum methods, before 2035.`;
|
|
122
|
+
}
|
package/dist/secrets.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Leaked-credential detection. A bonus check that rides along with the crypto scan.
|
|
2
|
+
export const SECRET_RULES = [
|
|
3
|
+
{ id: "private-key", name: "Private key", pattern: /-----BEGIN (?:RSA |EC |DSA |OPENSSH |ENCRYPTED |PGP )?PRIVATE KEY(?: BLOCK)?-----/ },
|
|
4
|
+
{ id: "aws-access-key-id", name: "AWS access key", pattern: /\b((?:AKIA|ASIA|AGPA|AIDA|AROA)[0-9A-Z]{16})\b/, group: 1 },
|
|
5
|
+
{ id: "github-token", name: "GitHub token", pattern: /\b(gh[pousr]_[A-Za-z0-9]{36,255})\b/, group: 1 },
|
|
6
|
+
{ id: "github-fine-grained-pat", name: "GitHub token", pattern: /\b(github_pat_[A-Za-z0-9_]{60,255})\b/, group: 1 },
|
|
7
|
+
{ id: "gitlab-pat", name: "GitLab token", pattern: /\b(glpat-[A-Za-z0-9_-]{20,})\b/, group: 1 },
|
|
8
|
+
{ id: "slack-token", name: "Slack token", pattern: /\b(xox[baprs]-[A-Za-z0-9-]{10,})\b/, group: 1 },
|
|
9
|
+
{ id: "slack-webhook", name: "Slack webhook", pattern: /(https:\/\/hooks\.slack\.com\/services\/T[A-Z0-9]+\/B[A-Z0-9]+\/[A-Za-z0-9]+)/, group: 1 },
|
|
10
|
+
{ id: "stripe-secret", name: "Stripe secret key", pattern: /\b((?:sk|rk)_live_[A-Za-z0-9]{20,})\b/, group: 1 },
|
|
11
|
+
{ id: "google-api-key", name: "Google API key", pattern: /\b(AIza[0-9A-Za-z_-]{35})\b/, group: 1 },
|
|
12
|
+
{ id: "anthropic-key", name: "Anthropic API key", pattern: /\b(sk-ant-[A-Za-z0-9_-]{32,})\b/, group: 1 },
|
|
13
|
+
{ id: "openai-key", name: "OpenAI API key", pattern: /\b(sk-(?:proj|svcacct|admin)-[A-Za-z0-9_-]{32,})\b/, group: 1 },
|
|
14
|
+
{ id: "sendgrid-key", name: "SendGrid key", pattern: /\b(SG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43})\b/, group: 1 },
|
|
15
|
+
{ id: "npm-token", name: "npm token", pattern: /\b(npm_[A-Za-z0-9]{36})\b/, group: 1 },
|
|
16
|
+
{ id: "db-url-password", name: "Database password in URL", pattern: /\b(?:postgres(?:ql)?|mysql|mongodb(?:\+srv)?|redis|amqp):\/\/[^:\s/'"]+:([^@\s'"]{6,})@/, group: 1, minEntropy: 2.5 },
|
|
17
|
+
{
|
|
18
|
+
id: "generic-assignment",
|
|
19
|
+
name: "Hardcoded secret",
|
|
20
|
+
pattern: /\b[A-Za-z0-9_]*(?:api[_-]?key|secret|token|passwd|password|private[_-]?key)[A-Za-z0-9_]*["']?\s*[:=]\s*["']([^"'\s]{16,})["']/i,
|
|
21
|
+
group: 1,
|
|
22
|
+
minEntropy: 3.5,
|
|
23
|
+
},
|
|
24
|
+
];
|
|
25
|
+
const PLACEHOLDER = /(example|sample|dummy|changeme|change_me|placeholder|your[_-]|^your|xxxx|\*\*\*\*|<[^>]+>|\$\{|\{\{|process\.env|os\.environ|getenv|redacted|test)/i;
|
|
26
|
+
// Documentation tokens like "xoxb-1234567890-abcdefghij".
|
|
27
|
+
const FAKE_SEQUENCE = /1234567890|0123456789|abcdefghij/;
|
|
28
|
+
// Names that hold something about a secret, not the secret itself (SECRET_KEY_PREFIX, password_template).
|
|
29
|
+
const NOT_A_SECRET_NAME = /(template|prefix|suffix|header|_name|name_|field|path|url|label|message|_msg|_type|type_|format|pattern|regex)/i;
|
|
30
|
+
/** The generic "name = value" rule is noisy; drop the shapes that are almost never real secrets. */
|
|
31
|
+
function looksLikeSecretAssignment(match, value, line) {
|
|
32
|
+
const name = match.split(/["']?\s*[:=]/)[0];
|
|
33
|
+
if (NOT_A_SECRET_NAME.test(name))
|
|
34
|
+
return false;
|
|
35
|
+
if (/^\s*(>>>|\.\.\.)\s/.test(line))
|
|
36
|
+
return false; // doctest example
|
|
37
|
+
if (/^[a-z_-]+$/.test(value))
|
|
38
|
+
return false; // an identifier, like "userauth-gssapi-token"
|
|
39
|
+
if (/^\$?[a-z0-9_]+\$/.test(value))
|
|
40
|
+
return false; // a stored password hash, like "pbkdf2_sha256$..." or "$argon2id$..."
|
|
41
|
+
if (/\$\{?[A-Za-z_]/.test(value))
|
|
42
|
+
return false; // string interpolation, like "$user:$password"
|
|
43
|
+
if (/^[([^]/.test(value))
|
|
44
|
+
return false; // a regular expression
|
|
45
|
+
if (/\\(x[0-9a-f]{2}|u[0-9a-f]{4}|0)/i.test(value))
|
|
46
|
+
return false; // a binary magic string, like "openssh-key-v1\x00"
|
|
47
|
+
if (/^[a-z]+(?:[A-Z][a-z]*)+$/.test(value))
|
|
48
|
+
return false; // a camelCase identifier, like "challengePassword"
|
|
49
|
+
if (/\{\{/.test(line))
|
|
50
|
+
return false; // a Helm or Go template line
|
|
51
|
+
if (/^[a-z][A-Za-z0-9_-]*(\.[A-Za-z0-9_-]+)+$/.test(value))
|
|
52
|
+
return false; // a settings key, like "webhook.github.secret"
|
|
53
|
+
if (/^[A-Z][A-Z0-9_]{5,}$/.test(value))
|
|
54
|
+
return false; // the name of an environment variable, like "ARGOCD_AUTH_TOKEN"
|
|
55
|
+
if (/^(\/|\.{1,2}\/|~\/|[A-Za-z]:\\)/.test(value))
|
|
56
|
+
return false; // a file path, like "/var/run/secrets/token"
|
|
57
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(value))
|
|
58
|
+
return false; // a URL, like "https://oauth2.googleapis.com/token"
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
export function entropy(s) {
|
|
62
|
+
const counts = new Map();
|
|
63
|
+
for (const ch of s)
|
|
64
|
+
counts.set(ch, (counts.get(ch) ?? 0) + 1);
|
|
65
|
+
let h = 0;
|
|
66
|
+
for (const c of counts.values()) {
|
|
67
|
+
const p = c / s.length;
|
|
68
|
+
h -= p * Math.log2(p);
|
|
69
|
+
}
|
|
70
|
+
return h;
|
|
71
|
+
}
|
|
72
|
+
export function mask(value) {
|
|
73
|
+
if (value.length <= 8)
|
|
74
|
+
return "****";
|
|
75
|
+
return `${value.slice(0, 4)}${"*".repeat(Math.min(12, value.length - 4))}`;
|
|
76
|
+
}
|
|
77
|
+
export function findSecrets(line) {
|
|
78
|
+
const hits = [];
|
|
79
|
+
for (const rule of SECRET_RULES) {
|
|
80
|
+
const m = line.match(rule.pattern);
|
|
81
|
+
if (!m)
|
|
82
|
+
continue;
|
|
83
|
+
const value = m[rule.group ?? 0] ?? m[0];
|
|
84
|
+
if (rule.id !== "private-key" && (PLACEHOLDER.test(value) || FAKE_SEQUENCE.test(value)))
|
|
85
|
+
continue;
|
|
86
|
+
if (rule.minEntropy !== undefined && entropy(value) < rule.minEntropy)
|
|
87
|
+
continue;
|
|
88
|
+
if (rule.id === "generic-assignment" && !looksLikeSecretAssignment(m[0], value, line))
|
|
89
|
+
continue;
|
|
90
|
+
if (rule.id === "db-url-password" && /^(pass(word)?|passwd|pwd|secret)$/i.test(value))
|
|
91
|
+
continue;
|
|
92
|
+
hits.push({ rule, value });
|
|
93
|
+
}
|
|
94
|
+
// The generic rule only counts when nothing specific matched the line.
|
|
95
|
+
const specific = hits.filter((h) => h.rule.id !== "generic-assignment");
|
|
96
|
+
return specific.length ? specific : hits;
|
|
97
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Local settings for the command-line version: the random install ID, whether anonymized sharing is on,
|
|
2
|
+
// offline mode, the optional industry and size, and what the last run would have sent.
|
|
3
|
+
// Kept in one JSON file in the user's settings folder. Nothing here leaves the machine except
|
|
4
|
+
// through share.ts, and only the fields that file builds.
|
|
5
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
6
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { homedir } from "node:os";
|
|
8
|
+
import { dirname, join, resolve } from "node:path";
|
|
9
|
+
export const INDUSTRIES = [
|
|
10
|
+
"finance",
|
|
11
|
+
"healthcare",
|
|
12
|
+
"government",
|
|
13
|
+
"defense",
|
|
14
|
+
"technology",
|
|
15
|
+
"telecom",
|
|
16
|
+
"energy-utilities",
|
|
17
|
+
"manufacturing",
|
|
18
|
+
"retail",
|
|
19
|
+
"education",
|
|
20
|
+
"professional-services",
|
|
21
|
+
"other",
|
|
22
|
+
];
|
|
23
|
+
export const SIZES = ["1-50", "51-250", "251-1000", "1000+"];
|
|
24
|
+
export function settingsPath(env = process.env, platform = process.platform) {
|
|
25
|
+
if (env.QUANTUM_SCAN_CONFIG)
|
|
26
|
+
return resolve(env.QUANTUM_SCAN_CONFIG);
|
|
27
|
+
if (platform === "win32" && env.APPDATA)
|
|
28
|
+
return join(env.APPDATA, "Hard Keel", "quantum-scan.json");
|
|
29
|
+
if (platform === "darwin")
|
|
30
|
+
return join(homedir(), "Library", "Application Support", "Hard Keel", "quantum-scan.json");
|
|
31
|
+
return join(env.XDG_CONFIG_HOME || join(homedir(), ".config"), "hardkeel", "quantum-scan.json");
|
|
32
|
+
}
|
|
33
|
+
function fresh() {
|
|
34
|
+
return {
|
|
35
|
+
id: randomBytes(16).toString("hex"),
|
|
36
|
+
sharing: "on",
|
|
37
|
+
offline: false,
|
|
38
|
+
noticeShown: false,
|
|
39
|
+
ownershipToken: randomBytes(12).toString("hex"),
|
|
40
|
+
state: {},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export function loadSettings(path = settingsPath()) {
|
|
44
|
+
const base = fresh();
|
|
45
|
+
if (!existsSync(path))
|
|
46
|
+
return base;
|
|
47
|
+
try {
|
|
48
|
+
const raw = JSON.parse(readFileSync(path, "utf8"));
|
|
49
|
+
return {
|
|
50
|
+
...base,
|
|
51
|
+
...raw,
|
|
52
|
+
id: typeof raw.id === "string" && /^[a-f0-9]{32}$/.test(raw.id) ? raw.id : base.id,
|
|
53
|
+
ownershipToken: typeof raw.ownershipToken === "string" && /^[a-f0-9]{24}$/.test(raw.ownershipToken) ? raw.ownershipToken : base.ownershipToken,
|
|
54
|
+
industry: INDUSTRIES.includes(raw.industry) ? raw.industry : undefined,
|
|
55
|
+
size: SIZES.includes(raw.size) ? raw.size : undefined,
|
|
56
|
+
state: raw.state && typeof raw.state === "object" ? raw.state : {},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return base;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/** Saves settings. Never throws: a read-only home folder must not break a scan. */
|
|
64
|
+
export function saveSettings(s, path = settingsPath()) {
|
|
65
|
+
try {
|
|
66
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
67
|
+
const tmp = `${path}.${process.pid}.tmp`;
|
|
68
|
+
writeFileSync(tmp, JSON.stringify(s, null, 2) + "\n", { mode: 0o600 });
|
|
69
|
+
renameSync(tmp, path);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
// ignore
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export function folderKey(folder) {
|
|
76
|
+
return createHash("sha256").update(resolve(folder)).digest("hex").slice(0, 16);
|
|
77
|
+
}
|
|
78
|
+
/** Whether this run may send its summary, and why not. */
|
|
79
|
+
export function sharingState(s, opts, env = process.env) {
|
|
80
|
+
if (opts.offline || s.offline || /^(1|true|yes|on)$/i.test(env.QUANTUM_SCAN_OFFLINE ?? ""))
|
|
81
|
+
return "offline";
|
|
82
|
+
if (/^(0|false|no|off)$/i.test(env.QUANTUM_SCAN_SHARING ?? ""))
|
|
83
|
+
return "env-off";
|
|
84
|
+
if (env.DO_NOT_TRACK && env.DO_NOT_TRACK !== "0")
|
|
85
|
+
return "do-not-track";
|
|
86
|
+
return s.sharing === "off" ? "off" : "on";
|
|
87
|
+
}
|
|
88
|
+
export const NOTICE = `Hard Keel Quantum Scan collects anonymized, aggregated information about how its
|
|
89
|
+
checks perform and what kinds of encryption they find. It never includes your code,
|
|
90
|
+
keys, server names, IP addresses or company name. We use it to improve and build our
|
|
91
|
+
products and to understand encryption trends across the industry.
|
|
92
|
+
|
|
93
|
+
See what's sent: quantum-scan config show-shared
|
|
94
|
+
Turn it off: quantum-scan config sharing off (or QUANTUM_SCAN_SHARING=off)
|
|
95
|
+
Send nothing at all: --offline (or QUANTUM_SCAN_OFFLINE=1)
|
|
96
|
+
Details: https://hardkeel.com/privacy/
|
|
97
|
+
`;
|
|
98
|
+
/**
|
|
99
|
+
* Prints the notice the first time, before anything is sent. In automated pipelines,
|
|
100
|
+
* where settings may not survive between runs, it prints every run, at the top of the log.
|
|
101
|
+
*/
|
|
102
|
+
export function showNoticeIfNeeded(s, state, env = process.env, write = (t) => process.stderr.write(t)) {
|
|
103
|
+
if (state !== "on")
|
|
104
|
+
return false;
|
|
105
|
+
const ci = Boolean(env.CI || env.GITHUB_ACTIONS);
|
|
106
|
+
if (s.noticeShown && !ci)
|
|
107
|
+
return false;
|
|
108
|
+
write(`\n${NOTICE}\n`);
|
|
109
|
+
s.noticeShown = true;
|
|
110
|
+
return true;
|
|
111
|
+
}
|
package/dist/share.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// Builds and sends the anonymized summary of a run. Counts are rounded into bands and every name is
|
|
2
|
+
// checked against a fixed list, so nothing taken from the scanned code or systems can reach us.
|
|
3
|
+
// What is sent is exactly what buildPayload returns; `quantum-scan config show-shared` prints it.
|
|
4
|
+
import { ALGORITHMS } from "./rules.js";
|
|
5
|
+
export const SHARE_URL = "https://scan.hardkeel.com/v1/share";
|
|
6
|
+
export const PAYLOAD_VERSION = 1;
|
|
7
|
+
export const BANDS = ["1", "2-5", "6-20", "21-100", "100+"];
|
|
8
|
+
export function band(n) {
|
|
9
|
+
if (!Number.isFinite(n) || n < 1)
|
|
10
|
+
return null;
|
|
11
|
+
if (n === 1)
|
|
12
|
+
return "1";
|
|
13
|
+
if (n <= 5)
|
|
14
|
+
return "2-5";
|
|
15
|
+
if (n <= 20)
|
|
16
|
+
return "6-20";
|
|
17
|
+
if (n <= 100)
|
|
18
|
+
return "21-100";
|
|
19
|
+
return "100+";
|
|
20
|
+
}
|
|
21
|
+
export const AREAS = ["code", "web", "host", "network", "vpn", "cloud"];
|
|
22
|
+
/** Error kinds a run may report. Messages are never sent, only these labels. */
|
|
23
|
+
export const ERROR_KINDS = ["timeout", "refused", "dns", "tls", "ssh", "read", "parse", "permission", "other"];
|
|
24
|
+
/** Post-quantum algorithms we credit. Anything else is dropped. */
|
|
25
|
+
export const PQC_NAMES = ["ML-KEM", "Hybrid ML-KEM key exchange", "ML-DSA", "SLH-DSA", "FN-DSA", "Hybrid SSH key exchange"];
|
|
26
|
+
/** Extra algorithm names the encryption map uses beyond the code rules. */
|
|
27
|
+
export const MAP_ALGORITHMS = ["TLS key exchange", "SSH key exchange", "SSH host key", "TLS certificate", "Static RSA key exchange", "SSH weak settings", "Expired certificate", "Certificate name mismatch", "Untrusted certificate", "Self-signed certificate"];
|
|
28
|
+
const KNOWN_ALGORITHMS = new Set([...Object.keys(ALGORITHMS), ...MAP_ALGORITHMS, ...PQC_NAMES]);
|
|
29
|
+
const RULE_ID = /^[a-z0-9][a-z0-9-]{0,59}$/;
|
|
30
|
+
function bandMap(src, keyOk) {
|
|
31
|
+
const out = {};
|
|
32
|
+
for (const [k, n] of Object.entries(src)) {
|
|
33
|
+
const b = band(n);
|
|
34
|
+
if (b && keyOk(k))
|
|
35
|
+
out[k] = b;
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
export function buildPayload(facts, s, toolVersion, env = process.env, platform = process.platform) {
|
|
40
|
+
const ruleOk = (k) => RULE_ID.test(k);
|
|
41
|
+
const algorithms = {};
|
|
42
|
+
const other = { count: 0, quantumVulnerable: 0 };
|
|
43
|
+
for (const [name, v] of Object.entries(facts.algorithms)) {
|
|
44
|
+
const target = KNOWN_ALGORITHMS.has(name) ? name : null;
|
|
45
|
+
if (!target) {
|
|
46
|
+
other.count += v.count;
|
|
47
|
+
other.quantumVulnerable += v.quantumVulnerable;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const n = band(v.count);
|
|
51
|
+
if (n)
|
|
52
|
+
algorithms[target] = { n, qv: band(v.quantumVulnerable) ?? "0" };
|
|
53
|
+
}
|
|
54
|
+
const on = band(other.count);
|
|
55
|
+
if (on)
|
|
56
|
+
algorithms.other = { n: on, qv: band(other.quantumVulnerable) ?? "0" };
|
|
57
|
+
const pqcOk = (k) => PQC_NAMES.includes(k);
|
|
58
|
+
const payload = {
|
|
59
|
+
v: PAYLOAD_VERSION,
|
|
60
|
+
id: s.id,
|
|
61
|
+
tool: /^\d+\.\d+\.\d+$/.test(toolVersion) ? toolVersion : "0.0.0",
|
|
62
|
+
os: platform === "linux" || platform === "darwin" || platform === "win32" ? platform : "other",
|
|
63
|
+
ci: Boolean(env.CI || env.GITHUB_ACTIONS),
|
|
64
|
+
command: facts.command,
|
|
65
|
+
areas: AREAS.filter((a) => facts.areas.includes(a)),
|
|
66
|
+
rules: bandMap(facts.rules, ruleOk),
|
|
67
|
+
fixed: bandMap(facts.fixed, ruleOk),
|
|
68
|
+
ignored: bandMap(facts.ignored, ruleOk),
|
|
69
|
+
algorithms,
|
|
70
|
+
quantumSafe: bandMap(facts.quantumSafe, pqcOk),
|
|
71
|
+
replacements: bandMap(facts.replacements, pqcOk),
|
|
72
|
+
errors: ERROR_KINDS.filter((e) => facts.errors.includes(e)),
|
|
73
|
+
};
|
|
74
|
+
if (s.industry)
|
|
75
|
+
payload.industry = s.industry;
|
|
76
|
+
if (s.size)
|
|
77
|
+
payload.size = s.size;
|
|
78
|
+
return payload;
|
|
79
|
+
}
|
|
80
|
+
/** Sends the summary. Never throws and never waits more than a few seconds. */
|
|
81
|
+
export async function sendPayload(p, url = SHARE_URL, timeoutMs = 3000) {
|
|
82
|
+
try {
|
|
83
|
+
const res = await fetch(url, {
|
|
84
|
+
method: "POST",
|
|
85
|
+
headers: { "content-type": "application/json", "user-agent": `hardkeel-quantum-scan/${p.tool}` },
|
|
86
|
+
body: JSON.stringify(p),
|
|
87
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
88
|
+
});
|
|
89
|
+
return res.ok;
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** Compares this run with the last one in the same folder: what went away, and what post-quantum use is new. */
|
|
96
|
+
export function diffRuns(prev, now) {
|
|
97
|
+
const fixed = {};
|
|
98
|
+
const replacements = {};
|
|
99
|
+
if (!prev)
|
|
100
|
+
return { fixed, replacements };
|
|
101
|
+
for (const [rule, n] of Object.entries(prev.rules)) {
|
|
102
|
+
const gone = n - (now.rules[rule] ?? 0);
|
|
103
|
+
if (gone > 0)
|
|
104
|
+
fixed[rule] = gone;
|
|
105
|
+
}
|
|
106
|
+
for (const [alg, n] of Object.entries(now.pqc)) {
|
|
107
|
+
const added = n - (prev.pqc[alg] ?? 0);
|
|
108
|
+
if (added > 0)
|
|
109
|
+
replacements[alg] = added;
|
|
110
|
+
}
|
|
111
|
+
return { fixed, replacements };
|
|
112
|
+
}
|
package/dist/types.js
ADDED
package/dist/walk.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Reads the files in a folder that the scanner looks at.
|
|
2
|
+
// A folder or file that can't be read (no permission, removed mid-scan) is noted and skipped,
|
|
3
|
+
// so one locked folder can't stop the whole scan.
|
|
4
|
+
import { existsSync, lstatSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
5
|
+
import { join, relative } from "node:path";
|
|
6
|
+
import { shouldScan } from "./scan.js";
|
|
7
|
+
function reasonOf(e) {
|
|
8
|
+
const code = String(e?.code ?? "");
|
|
9
|
+
if (code === "EACCES" || code === "EPERM")
|
|
10
|
+
return "permission denied";
|
|
11
|
+
if (code === "ENOENT")
|
|
12
|
+
return "removed during the scan";
|
|
13
|
+
return code || (e instanceof Error ? e.message : String(e));
|
|
14
|
+
}
|
|
15
|
+
/** Throws a plain-English error when the folder itself is missing or isn't a folder. */
|
|
16
|
+
export function walkFolder(root) {
|
|
17
|
+
if (!existsSync(root))
|
|
18
|
+
throw new Error(`Can't scan ${root}: that folder doesn't exist.`);
|
|
19
|
+
if (!statSync(root).isDirectory())
|
|
20
|
+
throw new Error(`Can't scan ${root}: that's a file, not a folder.`);
|
|
21
|
+
const res = { files: [], unreadable: [] };
|
|
22
|
+
const visit = (dir) => {
|
|
23
|
+
let names;
|
|
24
|
+
try {
|
|
25
|
+
names = readdirSync(dir);
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
if (dir === root)
|
|
29
|
+
throw new Error(`Can't scan ${root}: ${reasonOf(e)}.`);
|
|
30
|
+
res.unreadable.push({ path: relative(root, dir).split("\\").join("/") + "/", reason: reasonOf(e) });
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
for (const name of names) {
|
|
34
|
+
const full = join(dir, name);
|
|
35
|
+
const rel = relative(root, full).split("\\").join("/");
|
|
36
|
+
// lstat: symbolic links are skipped, so a broken or looping link can't stop the scan.
|
|
37
|
+
let st;
|
|
38
|
+
try {
|
|
39
|
+
st = lstatSync(full);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (st.isDirectory()) {
|
|
45
|
+
if (name !== ".git" && shouldScan(`${rel}/x`))
|
|
46
|
+
visit(full);
|
|
47
|
+
}
|
|
48
|
+
else if (st.isFile() && st.size <= 1_000_000 && shouldScan(rel)) {
|
|
49
|
+
try {
|
|
50
|
+
res.files.push({ path: rel, content: readFileSync(full, "utf8") });
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
res.unreadable.push({ path: rel, reason: reasonOf(e) });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
visit(root);
|
|
59
|
+
return res;
|
|
60
|
+
}
|
|
61
|
+
export function walk(root) {
|
|
62
|
+
return walkFolder(root).files;
|
|
63
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@hardkeel/quantum-scan",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@hardkeel/quantum-scan",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Finds encryption quantum computers will break, weak encryption, exposed infrastructure settings and leaked keys in code and infrastructure config. Writes an encryption inventory (CycloneDX CBOM) and a migration plan.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "Hard Keel (https://hardkeel.com)",
|
|
7
|
+
"homepage": "https://hardkeel.com",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"post-quantum",
|
|
10
|
+
"pqc",
|
|
11
|
+
"cbom",
|
|
12
|
+
"cyclonedx",
|
|
13
|
+
"encryption",
|
|
14
|
+
"secrets",
|
|
15
|
+
"sarif",
|
|
16
|
+
"security",
|
|
17
|
+
"terraform",
|
|
18
|
+
"kubernetes",
|
|
19
|
+
"cloudformation",
|
|
20
|
+
"tls"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"bin": {
|
|
24
|
+
"quantum-scan": "dist/cli.js"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"action.yml",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=18"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "rm -rf dist && tsc -p tsconfig.json && chmod +x dist/cli.js",
|
|
37
|
+
"rules": "node scripts/build-rules.mjs",
|
|
38
|
+
"test": "node --test test/*.test.ts",
|
|
39
|
+
"typecheck": "tsc --noEmit -p tsconfig.json"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/Hard-Keel/quantum-scan-cli.git"
|
|
44
|
+
},
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/Hard-Keel/quantum-scan-cli/issues"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/node": "^22.10.0",
|
|
50
|
+
"typescript": "^5.9.0"
|
|
51
|
+
}
|
|
52
|
+
}
|