@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/scan.js
CHANGED
|
@@ -1,214 +1,220 @@
|
|
|
1
|
-
// Core scanner: runs crypto and secret rules over file contents.
|
|
2
|
-
import { ALGORITHMS, CRYPTO_RULES, langFor } from "./rules.js";
|
|
3
|
-
import { scanInfra } from "./infra.js";
|
|
4
|
-
import { findSecrets, mask } from "./secrets.js";
|
|
5
|
-
import { findPqc } from "./pqc.js";
|
|
6
|
-
import { communityRulesFor } from "./community.js";
|
|
7
|
-
const MAX_LINE = 2000;
|
|
8
|
-
const DOC_FILE = /\.(md|mdx|rst|txt|html?|adoc)$/i;
|
|
9
|
-
// Test code is full of throwaway keys and passwords
|
|
10
|
-
export const TEST_PATH = /(^|\/)(tests?|__tests__|testdata|test-data|testing|fixtures?|spec|specs|mocks?|e2e|integration(?:[-_]?tests?)?|vectors|test[-_]?vectors|[a-z0-9_]+_vectors)\/|(_test\.go|_test\.py|\.(test|spec)\.[cm]?[jt]sx?)$|(^|\/)
|
|
11
|
-
// Example and tutorial code, which is documentation in code form.
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (
|
|
63
|
-
continue;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (!
|
|
88
|
-
continue;
|
|
89
|
-
|
|
90
|
-
if (
|
|
91
|
-
continue;
|
|
92
|
-
|
|
93
|
-
if (info
|
|
94
|
-
continue;
|
|
95
|
-
//
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
let
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (HASH_NOT_SECURITY.test(
|
|
151
|
-
return false;
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
line++;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
1
|
+
// Core scanner: runs crypto and secret rules over file contents.
|
|
2
|
+
import { ALGORITHMS, CRYPTO_RULES, langFor } from "./rules.js";
|
|
3
|
+
import { scanInfra } from "./infra.js";
|
|
4
|
+
import { findSecrets, mask } from "./secrets.js";
|
|
5
|
+
import { findPqc } from "./pqc.js";
|
|
6
|
+
import { communityRulesFor } from "./community.js";
|
|
7
|
+
const MAX_LINE = 2000;
|
|
8
|
+
const DOC_FILE = /\.(md|mdx|rst|txt|html?|adoc)$/i;
|
|
9
|
+
// Test code is full of throwaway keys and passwords, including fakes in real token formats, so no secret counts there.
|
|
10
|
+
export const TEST_PATH = /(^|\/)(tests?|__tests__|__snapshots__|testdata|test-data|testing|test[-_]?(?:utils?|helpers?|support|env|suite|fixtures?)|e2e[-_][a-z0-9_-]+|[a-z0-9_-]+[-_]tests?|(?!(?:la|con|at|pro|de)test\/)[a-z0-9]+test|fixtures?|spec|specs|mocks?|e2e|integration(?:[-_]?tests?)?|vectors|test[-_]?vectors|[a-z0-9_]+_vectors)\/|(_test\.go|_test\.py|\.(test|spec)\.[cm]?[jt]sx?)$|(^|\/)test[-_][^/]*$|(^|\/)testing[-_]?utils?\.[a-z]+$|(^|\/)fixtures?\.[cm]?[jt]sx?$/i;
|
|
11
|
+
// Example and tutorial code, which is documentation in code form.
|
|
12
|
+
// Translation files: "password" and "token" there are labels shown to people.
|
|
13
|
+
export const LOCALE_PATH = /(^|\/)(locales?|i18n|l10n|translations?|nls)\/|\.(po|pot|xliff|xlf|ftl|strings)$/i;
|
|
14
|
+
// Example settings files (.env.example), API specs and docs-site configs (public search keys).
|
|
15
|
+
export const EXAMPLE_FILE = /\.(example|sample|template|tmpl|dist)$|(^|\/)\.env\.(example|sample|template)|(^|\/)(openapi|swagger)[^/]*\.(ya?ml|json)$|(^|\/)docusaurus\.config\.[cm]?[jt]s$|\.stories\.[cm]?[jt]sx?$|(^|\/)\.secrets\.baseline$/i;
|
|
16
|
+
export const EXAMPLE_PATH = /(^|\/)(docs?_src|docs?|examples?|samples?|tutorials?)\//i;
|
|
17
|
+
const LOOSE_SECRET_RULES = new Set(["private-key", "generic-assignment", "db-url-password"]);
|
|
18
|
+
const SKIP_PATH = /(^|\/)(node_modules|vendor|dist|build|\.git|__pycache__|\.venv|venv|target)\//;
|
|
19
|
+
const SKIP_FILE = /(\.min\.js|\.map|package-lock\.json|yarn\.lock|pnpm-lock\.yaml|go\.sum|\.lock)$/;
|
|
20
|
+
export function shouldScan(path) {
|
|
21
|
+
if (SKIP_PATH.test(path) || SKIP_FILE.test(path))
|
|
22
|
+
return false;
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
function trimSnippet(line) {
|
|
26
|
+
const t = line.trim();
|
|
27
|
+
return t.length > 160 ? `${t.slice(0, 157)}...` : t;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Scan one file.
|
|
31
|
+
* @param onlyLines if given, only report findings on these 1-based line numbers
|
|
32
|
+
* (used for pull requests: only what the change added).
|
|
33
|
+
*/
|
|
34
|
+
export function scanFile(path, content, onlyLines) {
|
|
35
|
+
if (content.includes("\u0000"))
|
|
36
|
+
return []; // binary
|
|
37
|
+
const lines = content.split(/\r?\n/).map((text, i) => ({ no: i + 1, text }));
|
|
38
|
+
// Scan the whole file so settings see their surrounding block, then keep only the lines asked for.
|
|
39
|
+
const findings = scanLines(path, lines);
|
|
40
|
+
return onlyLines ? findings.filter((f) => onlyLines.has(f.line)) : findings;
|
|
41
|
+
}
|
|
42
|
+
/** Scan the lines a pull request added, straight from GitHub's patch text. No file download needed. */
|
|
43
|
+
export function scanPatch(path, patch) {
|
|
44
|
+
return scanLines(path, patchAddedLines(patch));
|
|
45
|
+
}
|
|
46
|
+
export function scanLines(path, lines) {
|
|
47
|
+
if (!shouldScan(path))
|
|
48
|
+
return [];
|
|
49
|
+
const lang = langFor(path);
|
|
50
|
+
const rules = [...CRYPTO_RULES.filter((r) => r.langs.includes("any") || (lang !== null && r.langs.includes(lang))), ...communityRulesFor(path)];
|
|
51
|
+
const findings = [];
|
|
52
|
+
for (let idx = 0; idx < lines.length; idx++) {
|
|
53
|
+
const { no: lineNo, text: raw } = lines[idx];
|
|
54
|
+
if (raw.length > MAX_LINE)
|
|
55
|
+
continue;
|
|
56
|
+
// Secrets: every text file.
|
|
57
|
+
for (const hit of findSecrets(raw)) {
|
|
58
|
+
// A private-key header only counts when real key material follows it (docs often show "XXXX").
|
|
59
|
+
if (hit.rule.id === "private-key" && !hasKeyMaterial(raw, lines[idx + 1]))
|
|
60
|
+
continue;
|
|
61
|
+
// Docs are full of example passwords; only specific token formats count there.
|
|
62
|
+
if (hit.rule.id === "generic-assignment" && (DOC_FILE.test(path) || LOCALE_PATH.test(path)))
|
|
63
|
+
continue;
|
|
64
|
+
if (hit.rule.id === "db-url-password" && DOC_FILE.test(path))
|
|
65
|
+
continue;
|
|
66
|
+
if (TEST_PATH.test(path))
|
|
67
|
+
continue;
|
|
68
|
+
if (LOOSE_SECRET_RULES.has(hit.rule.id) && (EXAMPLE_PATH.test(path) || EXAMPLE_FILE.test(path) || DOC_FILE.test(path)))
|
|
69
|
+
continue;
|
|
70
|
+
findings.push({
|
|
71
|
+
kind: "secret",
|
|
72
|
+
ruleId: hit.rule.id,
|
|
73
|
+
title: `${hit.rule.name} committed to the repository`,
|
|
74
|
+
algorithm: hit.rule.name,
|
|
75
|
+
primitive: "credential",
|
|
76
|
+
risk: "secret",
|
|
77
|
+
severity: "critical",
|
|
78
|
+
file: path,
|
|
79
|
+
line: lineNo,
|
|
80
|
+
snippet: trimSnippet(raw.split(hit.value).join(mask(hit.value))),
|
|
81
|
+
fix: "Revoke it and issue a new one, then load it from a secret store. Deleting it from the code leaves it in git history.",
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
// Crypto: language-scoped rules.
|
|
85
|
+
for (const rule of rules) {
|
|
86
|
+
const m = raw.match(rule.pattern);
|
|
87
|
+
if (!m)
|
|
88
|
+
continue;
|
|
89
|
+
let algorithm = typeof rule.algorithm === "string" ? rule.algorithm : rule.algorithm(m);
|
|
90
|
+
if (!algorithm)
|
|
91
|
+
continue;
|
|
92
|
+
let info = ALGORITHMS[algorithm];
|
|
93
|
+
if (!info)
|
|
94
|
+
continue;
|
|
95
|
+
// Encryption in test code is not what ships, so it stays out of the inventory and the check.
|
|
96
|
+
if (TEST_PATH.test(path))
|
|
97
|
+
continue;
|
|
98
|
+
// Python's usedforsecurity=False marks a hash that is only a checksum.
|
|
99
|
+
if (info.primitive === "hash" && /usedforsecurity\s*=\s*False/.test(raw))
|
|
100
|
+
continue;
|
|
101
|
+
// MD5 and SHA-1 are mostly used for checksums, cache keys and ETags, which are fine.
|
|
102
|
+
// Only flag them where the code around them is about passwords, signing, tokens and the like.
|
|
103
|
+
if ((algorithm === "MD5" || algorithm === "SHA-1") && !weakHashForSecurity(raw, lines, idx))
|
|
104
|
+
continue;
|
|
105
|
+
let keySize;
|
|
106
|
+
if (rule.keySize) {
|
|
107
|
+
const k = raw.match(rule.keySize);
|
|
108
|
+
if (k)
|
|
109
|
+
keySize = Number(k[1]);
|
|
110
|
+
}
|
|
111
|
+
let title = `${algorithm} is ${info.risk === "weak-now" ? "weak today" : "breakable by quantum computers"}`;
|
|
112
|
+
let severity = info.severity;
|
|
113
|
+
let risk = info.risk;
|
|
114
|
+
let fix = info.fix;
|
|
115
|
+
if (keySize !== undefined && algorithm.startsWith("RSA") && keySize < 2048) {
|
|
116
|
+
title = `RSA-${keySize} is weak today and breakable by quantum computers`;
|
|
117
|
+
severity = "high";
|
|
118
|
+
risk = "weak-now";
|
|
119
|
+
fix = "Use 3072-bit keys or larger now, then plan the move to post-quantum methods.";
|
|
120
|
+
}
|
|
121
|
+
findings.push({
|
|
122
|
+
kind: "crypto",
|
|
123
|
+
ruleId: rule.id,
|
|
124
|
+
title,
|
|
125
|
+
algorithm,
|
|
126
|
+
primitive: info.primitive,
|
|
127
|
+
risk,
|
|
128
|
+
severity,
|
|
129
|
+
file: path,
|
|
130
|
+
line: lineNo,
|
|
131
|
+
snippet: trimSnippet(raw),
|
|
132
|
+
fix,
|
|
133
|
+
keySize,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// Infrastructure config: test fixtures and examples are written to be wrong or to show options.
|
|
138
|
+
if (!TEST_PATH.test(path) && !EXAMPLE_PATH.test(path))
|
|
139
|
+
findings.push(...scanInfra(path, lines));
|
|
140
|
+
return dedupe(findings);
|
|
141
|
+
}
|
|
142
|
+
// Words that say a hash is not protecting anything.
|
|
143
|
+
const HASH_NOT_SECURITY = /nosec|NOSONAR|noqa|usedforsecurity|e-?tag|cache|checksum|\bcrc|dedup|uuid|gravatar|content[-_]?md5|shard|partition|consistent.?hash|file.?hash|hash.?file|filename|file.?name|\bgit\b|identicon|colou?r/i;
|
|
144
|
+
// Words that say it is.
|
|
145
|
+
const HASH_SECURITY = /passw|passwd|\bpwd|signature|\bsign(ed|er|ing)?\b|verif|hmac|authenti|\bauth\b|token|secret|credential|certificat|\bcert\b|salt|nonce|session|encrypt|decrypt|login|\bjwt\b|\botp\b|api.?key|private|\bkdf\b|pbkdf|derive|card|\bssn\b|integrity/i;
|
|
146
|
+
/** Whether an MD5 or SHA-1 call at lines[idx] looks like it protects something. */
|
|
147
|
+
function weakHashForSecurity(raw, lines, idx) {
|
|
148
|
+
if (/hmac/i.test(raw))
|
|
149
|
+
return true;
|
|
150
|
+
if (HASH_NOT_SECURITY.test(raw))
|
|
151
|
+
return false;
|
|
152
|
+
if (HASH_SECURITY.test(raw))
|
|
153
|
+
return true;
|
|
154
|
+
// The function or block around it: a few lines up (often the function name) and a couple down.
|
|
155
|
+
const around = lines.slice(Math.max(0, idx - 6), idx + 3).map((l) => l.text).join("\n");
|
|
156
|
+
if (HASH_NOT_SECURITY.test(around))
|
|
157
|
+
return false;
|
|
158
|
+
return HASH_SECURITY.test(around);
|
|
159
|
+
}
|
|
160
|
+
const KEY_BODY = /^[A-Za-z0-9+/=]{40,}$/;
|
|
161
|
+
function hasKeyMaterial(line, next) {
|
|
162
|
+
const after = line.split(/PRIVATE KEY(?: BLOCK)?-----/)[1] ?? "";
|
|
163
|
+
if (after.split(/\\n|\s+/).some((part) => KEY_BODY.test(part)))
|
|
164
|
+
return true;
|
|
165
|
+
return next !== undefined && KEY_BODY.test(next.text.trim().replace(/^["']|["',]$/g, ""));
|
|
166
|
+
}
|
|
167
|
+
// Two rules can hit the same line for the same algorithm; keep one.
|
|
168
|
+
function dedupe(findings) {
|
|
169
|
+
const seen = new Set();
|
|
170
|
+
return findings.filter((f) => {
|
|
171
|
+
const k = `${f.file}:${f.line}:${f.kind}:${f.algorithm}`;
|
|
172
|
+
if (seen.has(k))
|
|
173
|
+
return false;
|
|
174
|
+
seen.add(k);
|
|
175
|
+
return true;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
export function scanFiles(files) {
|
|
179
|
+
const findings = [];
|
|
180
|
+
const pqc = [];
|
|
181
|
+
let filesScanned = 0;
|
|
182
|
+
for (const f of files) {
|
|
183
|
+
if (!shouldScan(f.path))
|
|
184
|
+
continue;
|
|
185
|
+
filesScanned++;
|
|
186
|
+
findings.push(...scanFile(f.path, f.content, f.onlyLines));
|
|
187
|
+
// Post-quantum use only counts in code that ships, like the problems do.
|
|
188
|
+
if (!TEST_PATH.test(f.path) && !EXAMPLE_PATH.test(f.path))
|
|
189
|
+
pqc.push(...findPqc(f.path, f.content));
|
|
190
|
+
}
|
|
191
|
+
return { findings, filesScanned, pqc };
|
|
192
|
+
}
|
|
193
|
+
/** Parse a unified diff patch (GitHub's `patch` field) into the added lines of the new file. */
|
|
194
|
+
export function patchAddedLines(patch) {
|
|
195
|
+
const out = [];
|
|
196
|
+
if (!patch)
|
|
197
|
+
return out;
|
|
198
|
+
let line = 0;
|
|
199
|
+
for (const l of patch.split("\n")) {
|
|
200
|
+
const h = l.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
|
|
201
|
+
if (h) {
|
|
202
|
+
line = Number(h[1]);
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
if (l.startsWith("+")) {
|
|
206
|
+
out.push({ no: line, text: l.slice(1) });
|
|
207
|
+
line++;
|
|
208
|
+
}
|
|
209
|
+
else if (l.startsWith("-")) {
|
|
210
|
+
// removed line: new-file counter does not move
|
|
211
|
+
}
|
|
212
|
+
else if (!l.startsWith("\\")) {
|
|
213
|
+
line++;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return out;
|
|
217
|
+
}
|
|
218
|
+
export function addedLines(patch) {
|
|
219
|
+
return new Set(patchAddedLines(patch).map((l) => l.no));
|
|
220
|
+
}
|