@hardkeel/benthic 0.0.0-stage → 1.0.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 +121 -0
- package/LICENSE +92 -0
- package/NOTICE +16 -0
- package/README.md +424 -2
- package/action.yml +54 -0
- package/dist/agility.js +80 -0
- package/dist/badge.js +62 -0
- package/dist/baseline.js +81 -0
- package/dist/cbom-diff.js +273 -0
- package/dist/cbom.js +418 -0
- package/dist/cli.js +834 -0
- package/dist/cloud-aws-services.js +375 -0
- package/dist/cloud-aws.js +354 -0
- package/dist/cloud-azure-services.js +73 -0
- package/dist/cloud-azure.js +144 -0
- package/dist/cloud-common.js +242 -0
- package/dist/cloud-gcp-services.js +90 -0
- package/dist/cloud-gcp.js +150 -0
- package/dist/comments.js +138 -0
- package/dist/community-rules.js +37 -0
- package/dist/community.js +30 -0
- package/dist/deps.js +406 -0
- package/dist/der.js +571 -0
- package/dist/evidence-cli.js +235 -0
- package/dist/evidence-live.js +109 -0
- package/dist/evidence.js +512 -0
- package/dist/ignore.js +90 -0
- package/dist/image.js +1224 -0
- package/dist/infra.js +635 -0
- package/dist/inventory.js +277 -0
- package/dist/map-activity.js +24 -0
- package/dist/map-areas.js +60 -0
- package/dist/map-cloud-keys.js +110 -0
- package/dist/map-cloud.js +34 -0
- package/dist/map-ct.js +107 -0
- package/dist/map-host.js +335 -0
- package/dist/map-network.js +520 -0
- package/dist/map-people.js +79 -0
- package/dist/map-rank.js +70 -0
- package/dist/map-report.js +304 -0
- package/dist/map-run.js +327 -0
- package/dist/map-sarif.js +61 -0
- package/dist/map-signin.js +247 -0
- package/dist/map-types.js +19 -0
- package/dist/map-web.js +666 -0
- package/dist/native.js +168 -0
- package/dist/policy.js +221 -0
- package/dist/pqc.js +46 -0
- package/dist/profile.js +65 -0
- package/dist/project.js +928 -0
- package/dist/prose.js +159 -0
- package/dist/protocols.js +207 -0
- package/dist/registry.js +326 -0
- package/dist/report.js +190 -0
- package/dist/rules.js +230 -0
- package/dist/sarif-common.js +30 -0
- package/dist/sarif.js +114 -0
- package/dist/scan.js +601 -0
- package/dist/score.js +124 -0
- package/dist/secrets.js +192 -0
- package/dist/share-config.js +129 -0
- package/dist/share-lists.js +19 -0
- package/dist/share.js +125 -0
- package/dist/sonar.js +83 -0
- package/dist/structure.js +218 -0
- package/dist/suites.js +402 -0
- package/dist/tls-check.js +122 -0
- package/dist/types.js +4 -0
- package/dist/walk.js +78 -0
- package/dist/yaml.js +173 -0
- package/package.json +73 -4
package/dist/native.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Encryption built into compiled programs and Java keystores, read from their bytes. Nothing is run.
|
|
4
|
+
//
|
|
5
|
+
// - Programs and shared libraries (ELF, PE, Mach-O) that carry their own copy of an encryption
|
|
6
|
+
// library: statically linked OpenSSL, BoringSSL, AWS-LC, LibreSSL, Mbed TLS, wolfSSL, Libgcrypt,
|
|
7
|
+
// libsodium, liboqs, and Rust's ring. Found by the version text each library compiles in.
|
|
8
|
+
// - Java keystores (JKS, JCEKS, PKCS #12): which entries hold private keys, and the certificates
|
|
9
|
+
// stored in the clear next to them, so their key type and size can be judged.
|
|
10
|
+
import { parseCertificate } from "./der.js";
|
|
11
|
+
/** Whether the bytes start like a compiled program or shared library. */
|
|
12
|
+
export function binaryFormat(b) {
|
|
13
|
+
if (b.length < 4)
|
|
14
|
+
return null;
|
|
15
|
+
if (b[0] === 0x7f && b[1] === 0x45 && b[2] === 0x4c && b[3] === 0x46)
|
|
16
|
+
return "elf";
|
|
17
|
+
if (b[0] === 0x4d && b[1] === 0x5a)
|
|
18
|
+
return "pe";
|
|
19
|
+
const m = b.readUInt32BE(0);
|
|
20
|
+
if (m === 0xfeedface || m === 0xfeedfacf || m === 0xcefaedfe || m === 0xcffaedfe || m === 0xcafebabe)
|
|
21
|
+
return m === 0xcafebabe && b.readUInt32BE(4) > 30 ? null : "mach-o"; // 0xcafebabe is also a Java class
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
const LIBSSL = /^lib(crypto|ssl)\.so/;
|
|
25
|
+
// Order matters: forks report themselves before the OpenSSL text some of them also carry.
|
|
26
|
+
const MARKERS = [
|
|
27
|
+
{ package: "aws-lc", product: "AWS-LC", anchor: "AWS-LC ", pattern: /^AWS-LC (\d+\.\d+\.\d+)/, version: (m) => m[1], shared: LIBSSL },
|
|
28
|
+
{ package: "boringssl", product: "BoringSSL", anchor: "BoringSSL", pattern: /^BoringSSL/, shared: LIBSSL },
|
|
29
|
+
{ package: "libressl", product: "LibreSSL", anchor: "LibreSSL ", pattern: /^LibreSSL (\d+\.\d+\.\d+)/, version: (m) => m[1], shared: LIBSSL, requires: "OPENSSLDIR: " },
|
|
30
|
+
{ package: "openssl", product: "OpenSSL", anchor: "OpenSSL ", pattern: /^OpenSSL (\d+\.\d+\.\d+[a-z]{0,2})(?:-[\w.]+)?\s+\d{1,2} [A-Z][a-z]{2} \d{4}/, version: (m) => m[1], shared: LIBSSL, requires: "OPENSSLDIR: " },
|
|
31
|
+
{ package: "mbedtls", product: "Mbed TLS", anchor: "bed TLS ", pattern: /^bed TLS (\d+\.\d+\.\d+)/, version: (m) => m[1], shared: /^libmbed(tls|crypto|x509)\.so/ },
|
|
32
|
+
{ package: "wolfssl", product: "wolfSSL", anchor: "wolfSSL_CTX_new", pattern: /^wolfSSL_CTX_new/, shared: /^libwolfssl\.so/ },
|
|
33
|
+
{ package: "libgcrypt", product: "Libgcrypt", anchor: "This is Libgcrypt ", pattern: /^This is Libgcrypt (\d+\.\d+\.\d+)/, version: (m) => m[1] },
|
|
34
|
+
{ package: "libsodium", product: "libsodium", anchor: "sodium_init", pattern: /^sodium_init/, shared: /^libsodium\.so/ },
|
|
35
|
+
{ package: "liboqs", product: "liboqs", anchor: "OQS_KEM_new", pattern: /^OQS_KEM_new/, shared: /^liboqs\.so/ },
|
|
36
|
+
{ package: "ring", product: "ring (Rust)", anchor: "ring_core_", pattern: /^ring_core_(\d+)_(\d+)_(\d+)_/, version: (m) => `${m[1]}.${m[2]}.${m[3]}` },
|
|
37
|
+
];
|
|
38
|
+
const WINDOW = 96;
|
|
39
|
+
const MAX_TRIES = 4096; // places an anchor is tried, per library
|
|
40
|
+
/** The shared libraries a program names, which is how an ELF or Mach-O program asks for them. */
|
|
41
|
+
function namedLibraries(b) {
|
|
42
|
+
const names = new Set();
|
|
43
|
+
for (const stem of ["libcrypto.so", "libssl.so", "libmbedtls.so", "libmbedcrypto.so", "libmbedx509.so", "libwolfssl.so", "libsodium.so", "liboqs.so", "libcrypto.", "libssl."]) {
|
|
44
|
+
if (b.indexOf(stem) >= 0)
|
|
45
|
+
names.add(stem.endsWith(".") ? stem + "so" : stem); // libcrypto.3.dylib counts as libcrypto
|
|
46
|
+
}
|
|
47
|
+
return names;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Encryption libraries compiled into a program or shared library, not linked from the system. Each
|
|
51
|
+
* library is reported once. fileName is the program's own name, so libcrypto.so itself counts.
|
|
52
|
+
*/
|
|
53
|
+
export function embeddedLibraries(b, fileName = "") {
|
|
54
|
+
const out = [];
|
|
55
|
+
const named = namedLibraries(b);
|
|
56
|
+
const self = fileName.split("/").pop() ?? "";
|
|
57
|
+
let fork = false;
|
|
58
|
+
for (const m of MARKERS) {
|
|
59
|
+
if (m.package === "openssl" && fork)
|
|
60
|
+
continue; // a fork that also prints an OpenSSL-style version
|
|
61
|
+
if (m.shared && !m.shared.test(self) && [...named].some((n) => m.shared.test(n)))
|
|
62
|
+
continue;
|
|
63
|
+
if (m.requires && b.indexOf(m.requires) < 0)
|
|
64
|
+
continue;
|
|
65
|
+
const anchor = Buffer.from(m.anchor, "latin1");
|
|
66
|
+
let hit = null;
|
|
67
|
+
for (let at = b.indexOf(anchor), tries = 0; at >= 0 && tries < MAX_TRIES && !hit; at = b.indexOf(anchor, at + 1), tries++) {
|
|
68
|
+
hit = m.pattern.exec(b.toString("latin1", at, Math.min(b.length, at + WINDOW)));
|
|
69
|
+
}
|
|
70
|
+
if (!hit)
|
|
71
|
+
continue;
|
|
72
|
+
if (m.package === "aws-lc" || m.package === "boringssl" || m.package === "libressl")
|
|
73
|
+
fork = true;
|
|
74
|
+
const version = m.version?.(hit);
|
|
75
|
+
const evidence = (m.package === "mbedtls" ? "M" : "") + hit[0].replace(/\s+/g, " ");
|
|
76
|
+
out.push({ package: m.package, product: m.product, ...(version ? { version } : {}), evidence });
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
const JKS = 0xfeedfeed;
|
|
81
|
+
const JCEKS = 0xcececece;
|
|
82
|
+
/** Read a JKS or JCEKS keystore's structure, or recognize a PKCS #12 one. Passwords are never tried. */
|
|
83
|
+
export function readKeystore(b) {
|
|
84
|
+
if (b.length >= 12 && (b.readUInt32BE(0) === JKS || b.readUInt32BE(0) === JCEKS))
|
|
85
|
+
return readJks(b);
|
|
86
|
+
if (looksLikePkcs12(b))
|
|
87
|
+
return { format: "PKCS12", entries: [], sealed: true };
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
function readJks(b) {
|
|
91
|
+
const format = b.readUInt32BE(0) === JKS ? "JKS" : "JCEKS";
|
|
92
|
+
const entries = [];
|
|
93
|
+
let at = 8;
|
|
94
|
+
const count = b.readUInt32BE(at);
|
|
95
|
+
at += 4;
|
|
96
|
+
const need = (n) => {
|
|
97
|
+
if (at + n > b.length)
|
|
98
|
+
throw new RangeError("short keystore");
|
|
99
|
+
};
|
|
100
|
+
const utf = () => {
|
|
101
|
+
need(2);
|
|
102
|
+
const n = b.readUInt16BE(at);
|
|
103
|
+
need(2 + n);
|
|
104
|
+
const s = b.toString("utf8", at + 2, at + 2 + n);
|
|
105
|
+
at += 2 + n;
|
|
106
|
+
return s;
|
|
107
|
+
};
|
|
108
|
+
const u32 = () => {
|
|
109
|
+
need(4);
|
|
110
|
+
const v = b.readUInt32BE(at);
|
|
111
|
+
at += 4;
|
|
112
|
+
return v;
|
|
113
|
+
};
|
|
114
|
+
const cert = () => {
|
|
115
|
+
utf(); // "X.509"
|
|
116
|
+
const n = u32();
|
|
117
|
+
need(n);
|
|
118
|
+
const p = parseCertificate(b.subarray(at, at + n));
|
|
119
|
+
at += n;
|
|
120
|
+
return p;
|
|
121
|
+
};
|
|
122
|
+
try {
|
|
123
|
+
for (let i = 0; i < Math.min(count, 10_000); i++) {
|
|
124
|
+
const tag = u32();
|
|
125
|
+
const alias = utf();
|
|
126
|
+
need(8);
|
|
127
|
+
at += 8; // when it was added
|
|
128
|
+
if (tag === 1) {
|
|
129
|
+
const keyLen = u32();
|
|
130
|
+
need(keyLen);
|
|
131
|
+
at += keyLen; // the private key, encrypted with the keystore's password: not read
|
|
132
|
+
const chain = u32();
|
|
133
|
+
const certificates = [];
|
|
134
|
+
for (let c = 0; c < Math.min(chain, 32); c++) {
|
|
135
|
+
const p = cert();
|
|
136
|
+
if (p)
|
|
137
|
+
certificates.push(p);
|
|
138
|
+
}
|
|
139
|
+
entries.push({ alias, kind: "private-key", certificates });
|
|
140
|
+
}
|
|
141
|
+
else if (tag === 2) {
|
|
142
|
+
const p = cert();
|
|
143
|
+
entries.push({ alias, kind: "trusted-certificate", certificates: p ? [p] : [] });
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
// A JCEKS secret key is a serialized Java object with no length in front; stop here.
|
|
147
|
+
entries.push({ alias, kind: "secret-key", certificates: [] });
|
|
148
|
+
return { format, entries, sealed: i + 1 < count };
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return { format, entries, sealed: true };
|
|
154
|
+
}
|
|
155
|
+
return { format, entries, sealed: false };
|
|
156
|
+
}
|
|
157
|
+
/** PKCS #12: SEQUENCE { INTEGER 3, ContentInfo { OID pkcs7-data ... } ... }. */
|
|
158
|
+
function looksLikePkcs12(b) {
|
|
159
|
+
if (b.length < 32 || b[0] !== 0x30)
|
|
160
|
+
return false;
|
|
161
|
+
let at = 1;
|
|
162
|
+
const len = b[at];
|
|
163
|
+
at += len & 0x80 ? 1 + (len & 0x7f) : 1;
|
|
164
|
+
if (b[at] !== 0x02 || b[at + 1] !== 0x01 || b[at + 2] !== 0x03)
|
|
165
|
+
return false;
|
|
166
|
+
// pkcs7-data, 1.2.840.113549.1.7.1, within the next few bytes
|
|
167
|
+
return b.subarray(at + 3, at + 40).includes(Buffer.from([0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01]));
|
|
168
|
+
}
|
package/dist/policy.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Your own encryption policy, in YAML: what isn't allowed, the smallest key sizes, the exceptions you
|
|
4
|
+
// have agreed to and until when, and severities that fit your project. Read from .benthic.yml at
|
|
5
|
+
// the top of the scanned folder, or from --policy <file>.
|
|
6
|
+
//
|
|
7
|
+
// name: Acme encryption policy
|
|
8
|
+
// deny: [SHA-1, 3DES, RSA-1024] # nothing here, anywhere: findings and the inventory
|
|
9
|
+
// min-key-size:
|
|
10
|
+
// RSA: 3072
|
|
11
|
+
// AES: 256
|
|
12
|
+
// allow: # agreed exceptions, each with a reason
|
|
13
|
+
// - path: legacy/**
|
|
14
|
+
// algorithm: RSA
|
|
15
|
+
// reason: The payments partner only takes RSA until its 2027 upgrade
|
|
16
|
+
// expires: 2027-06-30
|
|
17
|
+
// severity: # by algorithm or rule id
|
|
18
|
+
// MD5: critical
|
|
19
|
+
// fail: true # same as --fail-on policy
|
|
20
|
+
//
|
|
21
|
+
// A policy never changes the grade, which is the same for everyone. Severity changes show in the
|
|
22
|
+
// report, SARIF and JSON. To hide a finding altogether, use .benthic-ignore.
|
|
23
|
+
import { parseYaml, YamlError } from "./yaml.js";
|
|
24
|
+
export const POLICY_FILES = [".benthic.yml", ".benthic.yaml", ".quantum-scan.yml", ".quantum-scan.yaml"]; // the last two: names from before Benthic
|
|
25
|
+
const SEVERITIES = ["critical", "high", "medium", "low"];
|
|
26
|
+
const KEYS = new Set(["name", "deny", "min-key-size", "allow", "severity", "fail"]);
|
|
27
|
+
export class PolicyError extends Error {
|
|
28
|
+
}
|
|
29
|
+
/** Read a policy file. Throws a PolicyError that says what to fix. */
|
|
30
|
+
export function parsePolicy(text) {
|
|
31
|
+
let doc;
|
|
32
|
+
try {
|
|
33
|
+
doc = parseYaml(text);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
throw new PolicyError(e instanceof YamlError ? e.message : String(e));
|
|
37
|
+
}
|
|
38
|
+
if (doc === null)
|
|
39
|
+
doc = {};
|
|
40
|
+
if (typeof doc !== "object" || Array.isArray(doc))
|
|
41
|
+
throw new PolicyError("the file should be a map of settings (name, deny, min-key-size, allow, severity, fail)");
|
|
42
|
+
const d = doc;
|
|
43
|
+
for (const k of Object.keys(d))
|
|
44
|
+
if (!KEYS.has(k))
|
|
45
|
+
throw new PolicyError(`unknown setting "${k}". Use name, deny, min-key-size, allow, severity or fail.`);
|
|
46
|
+
const list = (v, what) => {
|
|
47
|
+
if (v === undefined || v === null)
|
|
48
|
+
return [];
|
|
49
|
+
if (typeof v === "string")
|
|
50
|
+
return [v];
|
|
51
|
+
if (!Array.isArray(v) || v.some((x) => typeof x !== "string" && typeof x !== "number"))
|
|
52
|
+
throw new PolicyError(`${what} should be a list of algorithm names`);
|
|
53
|
+
return v.map(String);
|
|
54
|
+
};
|
|
55
|
+
const minKeySize = {};
|
|
56
|
+
if (d["min-key-size"] !== undefined && d["min-key-size"] !== null) {
|
|
57
|
+
const m = d["min-key-size"];
|
|
58
|
+
if (typeof m !== "object" || Array.isArray(m))
|
|
59
|
+
throw new PolicyError("min-key-size should be a map, like RSA: 3072");
|
|
60
|
+
for (const [k, v] of Object.entries(m)) {
|
|
61
|
+
if (typeof v !== "number" || !Number.isInteger(v) || v <= 0)
|
|
62
|
+
throw new PolicyError(`min-key-size for ${k} should be a whole number of bits`);
|
|
63
|
+
minKeySize[k] = v;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const allow = [];
|
|
67
|
+
const a = d.allow;
|
|
68
|
+
if (a !== undefined && a !== null) {
|
|
69
|
+
if (!Array.isArray(a))
|
|
70
|
+
throw new PolicyError("allow should be a list of exceptions");
|
|
71
|
+
a.forEach((e, i) => {
|
|
72
|
+
if (!e || typeof e !== "object" || Array.isArray(e))
|
|
73
|
+
throw new PolicyError(`allow item ${i + 1} should have path, algorithm or rule, and a reason`);
|
|
74
|
+
const x = e;
|
|
75
|
+
for (const k of Object.keys(x))
|
|
76
|
+
if (!["path", "algorithm", "rule", "reason", "expires"].includes(k))
|
|
77
|
+
throw new PolicyError(`allow item ${i + 1}: unknown setting "${k}"`);
|
|
78
|
+
if (typeof x.reason !== "string" || !x.reason.trim())
|
|
79
|
+
throw new PolicyError(`allow item ${i + 1} needs a reason`);
|
|
80
|
+
if (x.path === undefined && x.algorithm === undefined && x.rule === undefined)
|
|
81
|
+
throw new PolicyError(`allow item ${i + 1} needs a path, an algorithm or a rule`);
|
|
82
|
+
if (x.expires !== undefined && !(typeof x.expires === "string" && /^\d{4}-\d{2}-\d{2}$/.test(x.expires) && !Number.isNaN(Date.parse(x.expires))))
|
|
83
|
+
throw new PolicyError(`allow item ${i + 1}: expires should be a date, like 2027-06-30`);
|
|
84
|
+
allow.push({ ...(x.path !== undefined ? { path: String(x.path) } : {}), ...(x.algorithm !== undefined ? { algorithm: String(x.algorithm) } : {}), ...(x.rule !== undefined ? { rule: String(x.rule) } : {}), reason: x.reason.trim(), ...(x.expires ? { expires: x.expires } : {}) });
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const severity = {};
|
|
88
|
+
if (d.severity !== undefined && d.severity !== null) {
|
|
89
|
+
if (typeof d.severity !== "object" || Array.isArray(d.severity))
|
|
90
|
+
throw new PolicyError("severity should be a map, like MD5: critical");
|
|
91
|
+
for (const [k, v] of Object.entries(d.severity)) {
|
|
92
|
+
if (!SEVERITIES.includes(v))
|
|
93
|
+
throw new PolicyError(`severity for ${k} should be critical, high, medium or low`);
|
|
94
|
+
severity[k] = v;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (d.fail !== undefined && typeof d.fail !== "boolean")
|
|
98
|
+
throw new PolicyError("fail should be true or false");
|
|
99
|
+
if (d.name !== undefined && typeof d.name !== "string")
|
|
100
|
+
throw new PolicyError("name should be text");
|
|
101
|
+
return { name: d.name ?? "Your policy", deny: list(d.deny, "deny"), minKeySize, allow, severity, fail: d.fail === true };
|
|
102
|
+
}
|
|
103
|
+
/** One spelling per algorithm: SHA1 and SHA-1, DESede and 3DES, EC and ECDSA. */
|
|
104
|
+
function norm(name) {
|
|
105
|
+
let n = name.toUpperCase().replace(/[\s_]+/g, "-");
|
|
106
|
+
n = n.replace(/^SHA(\d)/, "SHA-$1").replace(/^(DESEDE|TRIPLEDES|TRIPLE-DES|TDEA)/, "3DES").replace(/^ARCFOUR/, "RC4");
|
|
107
|
+
if (n === "EC")
|
|
108
|
+
n = "ECDSA";
|
|
109
|
+
return n;
|
|
110
|
+
}
|
|
111
|
+
/** Whether a policy name covers an algorithm: RSA covers RSA, RSA signature and RSA encryption; AES covers AES-128-GCM. */
|
|
112
|
+
export function covers(entry, algorithm, keySize) {
|
|
113
|
+
const e = norm(entry);
|
|
114
|
+
const a = norm(algorithm);
|
|
115
|
+
if (a === e || a.startsWith(`${e}-`) || a.startsWith(`${e}/`))
|
|
116
|
+
return true;
|
|
117
|
+
// RSA-1024 covers an RSA finding with a 1024-bit key; AES-128 an AES item with a 128-bit key.
|
|
118
|
+
const sized = e.match(/^(.+)-(\d{3,5})$/);
|
|
119
|
+
return !!sized && keySize === Number(sized[2]) && (a === sized[1] || a.startsWith(`${sized[1]}-`));
|
|
120
|
+
}
|
|
121
|
+
function glob(pattern) {
|
|
122
|
+
let p = pattern.split("\\").join("/").replace(/^\.\//, "");
|
|
123
|
+
if (p.endsWith("/"))
|
|
124
|
+
p += "**";
|
|
125
|
+
let re = "";
|
|
126
|
+
for (let i = 0; i < p.length; i++) {
|
|
127
|
+
const c = p[i];
|
|
128
|
+
if (c === "*" && p[i + 1] === "*") {
|
|
129
|
+
re += p[i + 2] === "/" ? "(?:.*/)?" : ".*";
|
|
130
|
+
i += p[i + 2] === "/" ? 2 : 1;
|
|
131
|
+
}
|
|
132
|
+
else if (c === "*")
|
|
133
|
+
re += "[^/]*";
|
|
134
|
+
else if (c === "?")
|
|
135
|
+
re += "[^/]";
|
|
136
|
+
else
|
|
137
|
+
re += c.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
138
|
+
}
|
|
139
|
+
return new RegExp(`^${re}$`);
|
|
140
|
+
}
|
|
141
|
+
function allowMatches(e, v) {
|
|
142
|
+
if (e.path !== undefined && !glob(e.path).test(v.file) && !v.file.startsWith(`${e.path.replace(/\/$/, "")}/`) && v.file !== e.path)
|
|
143
|
+
return false;
|
|
144
|
+
if (e.algorithm !== undefined && !covers(e.algorithm, v.algorithm, v.keySize))
|
|
145
|
+
return false;
|
|
146
|
+
if (e.rule !== undefined && e.rule !== v.ruleId)
|
|
147
|
+
return false;
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
/** Change severities the policy sets, by rule id first, then by algorithm. Call after the grade is worked out. */
|
|
151
|
+
export function applySeverity(policy, findings) {
|
|
152
|
+
const entries = Object.entries(policy.severity);
|
|
153
|
+
if (!entries.length)
|
|
154
|
+
return;
|
|
155
|
+
for (const f of findings) {
|
|
156
|
+
const byRule = policy.severity[f.ruleId];
|
|
157
|
+
const byAlg = entries.find(([k]) => covers(k, f.algorithm, f.keySize))?.[1];
|
|
158
|
+
const s = byRule ?? byAlg;
|
|
159
|
+
if (s)
|
|
160
|
+
f.severity = s;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/** What the scan found that the policy doesn't allow. */
|
|
164
|
+
export function checkPolicy(policy, findings, inventory, today = new Date().toISOString().slice(0, 10)) {
|
|
165
|
+
const out = [];
|
|
166
|
+
const used = new Set();
|
|
167
|
+
const consider = (v) => {
|
|
168
|
+
const hit = policy.allow.filter((e) => allowMatches(e, v));
|
|
169
|
+
for (const h of hit)
|
|
170
|
+
used.add(h);
|
|
171
|
+
const live = hit.find((e) => !e.expires || e.expires >= today);
|
|
172
|
+
const expired = hit.find((e) => e.expires && e.expires < today);
|
|
173
|
+
out.push({ ...v, ...(live ? { allowed: { reason: live.reason, ...(live.expires ? { expires: live.expires } : {}) } } : expired ? { expired: expired.expires } : {}) });
|
|
174
|
+
};
|
|
175
|
+
const items = [
|
|
176
|
+
...findings.filter((f) => f.kind === "crypto" || (f.kind === "config" && f.risk !== "exposed")).map((f) => ({ file: f.file, line: f.line, algorithm: f.algorithm, ...(f.keySize !== undefined ? { keySize: f.keySize } : {}), finding: true, ruleId: f.ruleId })),
|
|
177
|
+
...inventory.map((i) => ({ file: i.file, line: i.line, algorithm: i.algorithm, family: i.family, ...(i.keySize !== undefined ? { keySize: i.keySize } : {}), finding: false })),
|
|
178
|
+
];
|
|
179
|
+
for (const { family, ...it } of items) {
|
|
180
|
+
const is = (name, size) => covers(name, it.algorithm, size) || (family !== undefined && covers(name, family, size));
|
|
181
|
+
const denied = policy.deny.find((d) => is(d, it.keySize));
|
|
182
|
+
if (denied) {
|
|
183
|
+
consider({ ...it, why: `${denied} isn't allowed by ${policy.name}.` });
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
for (const [alg, min] of Object.entries(policy.minKeySize)) {
|
|
187
|
+
if (it.keySize !== undefined && it.keySize < min && is(alg)) {
|
|
188
|
+
consider({ ...it, why: `${policy.name} needs ${alg} keys of at least ${min} bits; this one is ${it.keySize}.` });
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return { name: policy.name, violations: out, unusedAllows: policy.allow.filter((e) => !used.has(e)) };
|
|
194
|
+
}
|
|
195
|
+
/** RSA-1024, or AES-128-GCM as it is (its size is in the name already). */
|
|
196
|
+
export function violationLabel(v) {
|
|
197
|
+
return v.keySize && !v.algorithm.includes(String(v.keySize)) ? `${v.algorithm}-${v.keySize}` : v.algorithm;
|
|
198
|
+
}
|
|
199
|
+
/** Violations that count: not covered by an exception that is still in date. */
|
|
200
|
+
export function policyFailures(r) {
|
|
201
|
+
return r.violations.filter((v) => !v.allowed);
|
|
202
|
+
}
|
|
203
|
+
export function policyText(r) {
|
|
204
|
+
const failing = policyFailures(r);
|
|
205
|
+
const allowed = r.violations.filter((v) => v.allowed);
|
|
206
|
+
const out = [`${r.name} (doesn't change the grade)`];
|
|
207
|
+
if (!r.violations.length)
|
|
208
|
+
out.push(" Nothing found that the policy rules out.");
|
|
209
|
+
for (const v of failing)
|
|
210
|
+
out.push(` ${v.file}${v.line > 0 ? `:${v.line}` : ""} ${violationLabel(v)}`, ` ${v.why}${v.expired ? ` The exception for it ended on ${v.expired}.` : ""}`);
|
|
211
|
+
if (allowed.length) {
|
|
212
|
+
out.push(` Allowed by an exception: ${allowed.length}`);
|
|
213
|
+
for (const v of allowed.slice(0, 10))
|
|
214
|
+
out.push(` ${v.file}${v.line > 0 ? `:${v.line}` : ""} ${v.algorithm}: ${v.allowed.reason}${v.allowed.expires ? ` (until ${v.allowed.expires})` : ""}`);
|
|
215
|
+
if (allowed.length > 10)
|
|
216
|
+
out.push(` and ${allowed.length - 10} more`);
|
|
217
|
+
}
|
|
218
|
+
if (r.unusedAllows.length)
|
|
219
|
+
out.push(` ${r.unusedAllows.length} exception${r.unusedAllows.length === 1 ? "" : "s"} matched nothing and can be removed.`);
|
|
220
|
+
return out.join("\n");
|
|
221
|
+
}
|
package/dist/pqc.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Detects post-quantum encryption already in use (ML-KEM, ML-DSA, SLH-DSA, FN-DSA, hybrids).
|
|
4
|
+
// Used only by the readiness score, to credit migration work. Never reported as a problem.
|
|
5
|
+
import { COMMENT_LINE, stripTrailingComment } from "./comments.js";
|
|
6
|
+
import { inDetectionTable, inProse, isTranslationFile } from "./prose.js";
|
|
7
|
+
// Order matters: the first rule that matches a line wins, so hybrids come before plain ML-KEM.
|
|
8
|
+
const PQC_RULES = [
|
|
9
|
+
{ pattern: /\b(?:X25519|SecP256r1|SecP384r1)[-_]?MLKEM[-_]?(?:768|1024)\b|\bX25519Kyber768\b/i, algorithm: "Hybrid ML-KEM key exchange", use: "key-establishment" },
|
|
10
|
+
{ pattern: /\b(?:ML[-_]?KEM|MLKEM)(?:[-_]?(?:512|768|1024))?\b|\bcrypto\/mlkem\b/i, algorithm: "ML-KEM", use: "key-establishment" },
|
|
11
|
+
{ pattern: /\bKyber(?:512|768|1024)\b/, algorithm: "ML-KEM", use: "key-establishment" },
|
|
12
|
+
{ pattern: /\b(?:ML[-_]?DSA|MLDSA)(?:[-_]?(?:44|65|87))?\b/i, algorithm: "ML-DSA", use: "signature" },
|
|
13
|
+
{ pattern: /\bDilithium[2-5]\b/, algorithm: "ML-DSA", use: "signature" },
|
|
14
|
+
{ pattern: /\bSLH[-_]?DSA\b|\bSPHINCS\+/i, algorithm: "SLH-DSA", use: "signature" },
|
|
15
|
+
{ pattern: /\bFN[-_]?DSA\b|\bFalcon[-_]?(?:512|1024)\b/, algorithm: "FN-DSA", use: "signature" },
|
|
16
|
+
];
|
|
17
|
+
const CODE_OR_CONFIG = /\.(py|pyw|js|mjs|cjs|jsx|ts|mts|cts|tsx|go|java|kt|kts|scala|rs|c|cc|cpp|h|hpp|cs|rb|php|swift|conf|cfg|ini|ya?ml|toml|tf|hcl|json|properties)$|(^|\/)(nginx|httpd)[^/]*$/i;
|
|
18
|
+
/** Post-quantum algorithms used in one source or config file. Documentation is ignored. */
|
|
19
|
+
export function findPqc(path, content) {
|
|
20
|
+
if (!CODE_OR_CONFIG.test(path) || content.includes("\u0000"))
|
|
21
|
+
return [];
|
|
22
|
+
// Most files name no post-quantum algorithm at all. A line can only match where the whole file does.
|
|
23
|
+
if (!PQC_RULES.some((r) => r.pattern.test(content)))
|
|
24
|
+
return [];
|
|
25
|
+
const out = [];
|
|
26
|
+
const lines = content.split(/\r?\n/);
|
|
27
|
+
// A translation catalog or a detection table names ML-KEM; it doesn't use it.
|
|
28
|
+
if (isTranslationFile(path, lines))
|
|
29
|
+
return [];
|
|
30
|
+
for (let i = 0; i < lines.length; i++) {
|
|
31
|
+
// A comment that names ML-KEM is a plan, not use.
|
|
32
|
+
if (lines[i].length > 2000 || COMMENT_LINE.test(lines[i]))
|
|
33
|
+
continue;
|
|
34
|
+
if (inDetectionTable(lines, i))
|
|
35
|
+
continue;
|
|
36
|
+
const text = stripTrailingComment(lines[i], path);
|
|
37
|
+
for (const r of PQC_RULES) {
|
|
38
|
+
const m = text.match(r.pattern);
|
|
39
|
+
if (m && !inProse(text, m.index ?? 0, (m.index ?? 0) + m[0].length, path)) {
|
|
40
|
+
out.push({ algorithm: r.algorithm, use: r.use, file: path, line: i + 1 });
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
package/dist/profile.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Compliance profiles: what a scan found that a named algorithm suite doesn't allow.
|
|
4
|
+
// CNSA 2.0 (NSA's suite for National Security Systems) allows AES-256, SHA-384 and SHA-512,
|
|
5
|
+
// ML-KEM-1024, ML-DSA-87, and LMS and XMSS from NIST SP 800-208 (single-tree only: HSS and XMSS^MT
|
|
6
|
+
// are not allowed). Everything quantum-breakable is already a finding, so it is counted, not repeated.
|
|
7
|
+
// A profile never changes the grade.
|
|
8
|
+
export const PROFILES = ["cnsa2"];
|
|
9
|
+
function cnsa2Why(i) {
|
|
10
|
+
const a = i.algorithm.toUpperCase();
|
|
11
|
+
if (i.family === "AES") {
|
|
12
|
+
if (i.keySize === 256)
|
|
13
|
+
return null;
|
|
14
|
+
return i.keySize ? "CNSA 2.0 uses AES-256 only." : "CNSA 2.0 uses AES-256 only; the key size isn't stated here.";
|
|
15
|
+
}
|
|
16
|
+
if (/^(SHA-?224|SHA-?256|SHA-?512\/2(24|56)|SHA3-(224|256))$/.test(a))
|
|
17
|
+
return "CNSA 2.0 hashes with SHA-384 or SHA-512.";
|
|
18
|
+
if (i.family === "HMAC" && /SHA-?(224|256|1)$|MD5$/.test(a))
|
|
19
|
+
return "CNSA 2.0 hashes with SHA-384 or SHA-512.";
|
|
20
|
+
if (i.family === "ML-KEM" && /^ML-KEM-(512|768)$/.test(a))
|
|
21
|
+
return "CNSA 2.0 key establishment is ML-KEM-1024.";
|
|
22
|
+
if (/MLKEM/.test(a))
|
|
23
|
+
return "CNSA 2.0 key establishment is ML-KEM-1024 on its own.";
|
|
24
|
+
if (i.family === "ML-DSA" && /^ML-DSA-(44|65)$/.test(a))
|
|
25
|
+
return "CNSA 2.0 signatures are ML-DSA-87.";
|
|
26
|
+
if (i.family === "SLH-DSA")
|
|
27
|
+
return "SLH-DSA isn't in CNSA 2.0; use ML-DSA-87, or LMS or XMSS for firmware and software signing.";
|
|
28
|
+
if (i.family === "XMSS^MT")
|
|
29
|
+
return "CNSA 2.0 allows single-tree XMSS, not XMSS^MT.";
|
|
30
|
+
if (/^(CHACHA20|XCHACHA20)/.test(a))
|
|
31
|
+
return "ChaCha20 isn't in CNSA 2.0; use AES-256-GCM.";
|
|
32
|
+
if (/^(BLAKE2|BLAKE3)/.test(a))
|
|
33
|
+
return "BLAKE hashes aren't in CNSA 2.0; use SHA-384 or SHA-512.";
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
/** What one scan has that the profile doesn't allow. */
|
|
37
|
+
export function checkProfile(name, findings, inventory) {
|
|
38
|
+
const gaps = [];
|
|
39
|
+
for (const i of inventory) {
|
|
40
|
+
if (i.risk === "check" && i.family === "ECDSA")
|
|
41
|
+
continue; // account addresses are already listed to check
|
|
42
|
+
const why = cnsa2Why(i);
|
|
43
|
+
if (why)
|
|
44
|
+
gaps.push({ algorithm: i.algorithm, file: i.file, line: i.line, why });
|
|
45
|
+
}
|
|
46
|
+
const counted = findings.filter((f) => f.kind === "crypto" || (f.kind === "config" && f.risk !== "exposed")).length;
|
|
47
|
+
return { profile: name, title: "CNSA 2.0", gaps, findings: counted };
|
|
48
|
+
}
|
|
49
|
+
export function profileText(r) {
|
|
50
|
+
const out = [`${r.title} (doesn't change the grade)`];
|
|
51
|
+
if (!r.gaps.length && !r.findings) {
|
|
52
|
+
out.push(" Nothing found that CNSA 2.0 rules out.");
|
|
53
|
+
return out.join("\n");
|
|
54
|
+
}
|
|
55
|
+
if (r.findings)
|
|
56
|
+
out.push(` The ${r.findings} encryption finding${r.findings === 1 ? "" : "s"} above ${r.findings === 1 ? "is" : "are"} also outside CNSA 2.0.`);
|
|
57
|
+
const by = new Map();
|
|
58
|
+
for (const g of r.gaps)
|
|
59
|
+
by.set(`${g.algorithm}\u0000${g.why}`, [...(by.get(`${g.algorithm}\u0000${g.why}`) ?? []), g]);
|
|
60
|
+
for (const gs of by.values()) {
|
|
61
|
+
const g = gs[0];
|
|
62
|
+
out.push(` ${g.file}:${g.line} ${g.algorithm}${gs.length > 1 ? ` (${gs.length} places)` : ""}`, ` ${g.why}`);
|
|
63
|
+
}
|
|
64
|
+
return out.join("\n");
|
|
65
|
+
}
|