@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/baseline.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Compare a scan with an earlier one, so a team can stop new quantum-vulnerable or weak encryption
|
|
4
|
+
// from creeping in while they work through what is already there. The earlier run is a CBOM this
|
|
5
|
+
// tool wrote (--cbom), usually committed to the repository, or its --format json output.
|
|
6
|
+
/** Risks that count: what is weak now, breakable by quantum computers, or worth a look. */
|
|
7
|
+
const COUNTED = new Set(["quantum-vulnerable", "weak-now", "check", "exposed", "secret"]);
|
|
8
|
+
/** Counts per algorithm from an earlier CBOM (--cbom) or --format json run. Throws a plain-English error otherwise. */
|
|
9
|
+
export function readBaseline(text) {
|
|
10
|
+
let doc;
|
|
11
|
+
try {
|
|
12
|
+
doc = JSON.parse(text);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
throw new Error("The baseline isn't JSON. Use a CBOM from --cbom, or --format json output.");
|
|
16
|
+
}
|
|
17
|
+
const out = new Map();
|
|
18
|
+
const d = doc;
|
|
19
|
+
if (d.bomFormat === "CycloneDX" && Array.isArray(d.components)) {
|
|
20
|
+
for (const c of d.components) {
|
|
21
|
+
if (c.cryptoProperties?.assetType !== "algorithm" && c.cryptoProperties?.assetType !== "protocol")
|
|
22
|
+
continue;
|
|
23
|
+
const prop = (n) => c.properties?.find((p) => p.name === n)?.value;
|
|
24
|
+
const risk = prop("hardkeel:risk") ?? "unknown";
|
|
25
|
+
const count = Number(prop("hardkeel:occurrences") ?? "0");
|
|
26
|
+
if (c.name && COUNTED.has(risk) && count > 0)
|
|
27
|
+
out.set(c.name, { name: c.name, count, risk });
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
if (Array.isArray(d.findings)) {
|
|
32
|
+
for (const [name, list] of group(d.findings))
|
|
33
|
+
out.set(name, { name, count: list.length, risk: list[0].risk });
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
throw new Error("The baseline isn't a Benthic CBOM or JSON report.");
|
|
37
|
+
}
|
|
38
|
+
/** The same name the CBOM gives each algorithm: "RSA-2048", "ECDSA P-256", "MD5". */
|
|
39
|
+
export function baselineName(f) {
|
|
40
|
+
return f.kind === "crypto" || f.kind === "config" ? (f.keySize ? `${f.algorithm}-${f.keySize}` : f.curve ? `${f.algorithm} ${f.curve}` : f.algorithm) : f.algorithm;
|
|
41
|
+
}
|
|
42
|
+
function group(findings) {
|
|
43
|
+
const m = new Map();
|
|
44
|
+
for (const f of findings)
|
|
45
|
+
if (COUNTED.has(f.risk))
|
|
46
|
+
m.set(baselineName(f), [...(m.get(baselineName(f)) ?? []), f]);
|
|
47
|
+
return m;
|
|
48
|
+
}
|
|
49
|
+
/** Algorithms used more (or for the first time) and less (or no longer) than in the baseline. */
|
|
50
|
+
export function compareBaseline(base, findings, inventory = []) {
|
|
51
|
+
const now = group(findings.filter((f) => f.kind !== "secret"));
|
|
52
|
+
// Inventory entries marked worth a look (LMS/XMSS parameter sets) are counted too.
|
|
53
|
+
for (const i of inventory)
|
|
54
|
+
if (i.risk === "check")
|
|
55
|
+
now.set(i.algorithm, [...(now.get(i.algorithm) ?? []), i]);
|
|
56
|
+
const up = [];
|
|
57
|
+
const down = [];
|
|
58
|
+
for (const [name, list] of now) {
|
|
59
|
+
const before = base.get(name)?.count ?? 0;
|
|
60
|
+
if (list.length > before)
|
|
61
|
+
up.push({ name, before, after: list.length });
|
|
62
|
+
else if (list.length < before)
|
|
63
|
+
down.push({ name, before, after: list.length });
|
|
64
|
+
}
|
|
65
|
+
for (const [name, b] of base)
|
|
66
|
+
if (!now.has(name) && b.risk !== "secret")
|
|
67
|
+
down.push({ name, before: b.count, after: 0 });
|
|
68
|
+
const byName = (a, b) => a.name.localeCompare(b.name);
|
|
69
|
+
return { up: up.sort(byName), down: down.sort(byName) };
|
|
70
|
+
}
|
|
71
|
+
export function baselineText(d) {
|
|
72
|
+
if (!d.up.length && !d.down.length)
|
|
73
|
+
return "Compared with the baseline: no change.";
|
|
74
|
+
const line = (x) => ` ${x.name}: ${x.before} -> ${x.after}${x.before === 0 ? " (new)" : x.after === 0 ? " (gone)" : ""}`;
|
|
75
|
+
const out = ["Compared with the baseline:"];
|
|
76
|
+
if (d.up.length)
|
|
77
|
+
out.push(" More than before", ...d.up.map(line));
|
|
78
|
+
if (d.down.length)
|
|
79
|
+
out.push(" Fewer than before", ...d.down.map(line));
|
|
80
|
+
return out.join("\n");
|
|
81
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Compare two encryption inventories (CycloneDX 1.6 CBOMs): what was added, what went away, and what
|
|
4
|
+
// changed, like a key size, a parameter set, a risk or the places it is used. Works on CBOMs from this
|
|
5
|
+
// tool and from other tools that write CycloneDX cryptographic assets.
|
|
6
|
+
//
|
|
7
|
+
// benthic diff before.cbom.json after.cbom.json [--format text|json] [--fail-on added,new-risk]
|
|
8
|
+
// Risks that mean something to fix: a new one, or more places using it, is a step back.
|
|
9
|
+
const RISKY = new Set(["weak-now", "quantum-vulnerable", "secret", "exposed", "check"]);
|
|
10
|
+
function flatten(v, prefix, out) {
|
|
11
|
+
if (v === null || v === undefined)
|
|
12
|
+
return;
|
|
13
|
+
if (Array.isArray(v)) {
|
|
14
|
+
const simple = v.every((x) => typeof x !== "object" || x === null);
|
|
15
|
+
if (simple)
|
|
16
|
+
out[prefix] = v.map(String).sort().join(", ");
|
|
17
|
+
else
|
|
18
|
+
v.forEach((x, i) => flatten(x, `${prefix}[${i}]`, out));
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (typeof v === "object") {
|
|
22
|
+
for (const [k, x] of Object.entries(v))
|
|
23
|
+
flatten(x, prefix ? `${prefix}.${k}` : k, out);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
out[prefix] = String(v);
|
|
27
|
+
}
|
|
28
|
+
/** The assets in a CBOM. Throws a plain-English error when the file isn't one. */
|
|
29
|
+
export function readCbom(text, label) {
|
|
30
|
+
let doc;
|
|
31
|
+
try {
|
|
32
|
+
doc = JSON.parse(text);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
throw new Error(`${label} isn't JSON. Use a CycloneDX CBOM, like the one --cbom writes.`);
|
|
36
|
+
}
|
|
37
|
+
if (doc.bomFormat !== "CycloneDX" || !Array.isArray(doc.components))
|
|
38
|
+
throw new Error(`${label} isn't a CycloneDX document with components.`);
|
|
39
|
+
const tool = toolOf(doc.metadata?.tools);
|
|
40
|
+
const out = new Map();
|
|
41
|
+
const walk = (list) => {
|
|
42
|
+
for (const c of list) {
|
|
43
|
+
if (Array.isArray(c.components))
|
|
44
|
+
walk(c.components);
|
|
45
|
+
const crypto = c.type === "cryptographic-asset" || c.cryptoProperties;
|
|
46
|
+
const lib = c.type === "library" || c.type === "framework";
|
|
47
|
+
if (!c.name || (!crypto && !lib))
|
|
48
|
+
continue;
|
|
49
|
+
const assetType = crypto ? (c.cryptoProperties?.assetType ?? "unknown") : "library";
|
|
50
|
+
const key = lib ? `library:${(c.purl ?? c.name).replace(/@[^@/]*$/, "").toLowerCase()}` : `${assetType}:${c.name.toLowerCase()}`;
|
|
51
|
+
const properties = {};
|
|
52
|
+
if (c.cryptoProperties) {
|
|
53
|
+
const { assetType: _a, ...rest } = c.cryptoProperties;
|
|
54
|
+
flatten(rest, "", properties);
|
|
55
|
+
}
|
|
56
|
+
const risk = c.properties?.find((p) => p.name === "hardkeel:risk")?.value;
|
|
57
|
+
const places = (c.evidence?.occurrences ?? []).filter((o) => o.location).map((o) => `${o.location}${o.line ? `:${o.line}` : ""}`);
|
|
58
|
+
const prev = out.get(key);
|
|
59
|
+
if (prev) {
|
|
60
|
+
prev.places.push(...places);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
out.set(key, { key, name: c.name, assetType, ...(risk ? { risk } : {}), ...(c.version ? { version: c.version } : {}), properties, places, ...(tool ? { tool } : {}) });
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
walk(doc.components);
|
|
67
|
+
return [...out.values()];
|
|
68
|
+
}
|
|
69
|
+
/** The name of the tool that wrote a CBOM: CycloneDX 1.5+ (tools.components) or the older list. */
|
|
70
|
+
function toolOf(tools) {
|
|
71
|
+
const list = Array.isArray(tools) ? tools : tools && typeof tools === "object" ? tools.components : undefined;
|
|
72
|
+
const first = Array.isArray(list) ? list[0] : undefined;
|
|
73
|
+
return typeof first?.name === "string" ? first.name : undefined;
|
|
74
|
+
}
|
|
75
|
+
const fileOf = (place) => place.replace(/:\d+$/, "");
|
|
76
|
+
// ---------- Which assets are the same, across tools ----------
|
|
77
|
+
// Two CBOMs from different tools name things their own way ("Ed25519" and "Ed25519 / Ed448 (EdDSA)",
|
|
78
|
+
// "ML-DSA-65" and "ML-DSA (FIPS 204)"). An object identifier plus the parameter set or curve is the
|
|
79
|
+
// same in both, so that decides first. Names are the fallback, only when an asset has no identifier.
|
|
80
|
+
/** OIDs that name a family, where the key size or curve says which one: RSA, elliptic curve, DH, DSA. */
|
|
81
|
+
const FAMILY_OIDS = [
|
|
82
|
+
[/^1\.2\.840\.113549\.1\.1\.(1|5|11|12|13|14|10)$/, "rsa"], // rsaEncryption and the RSA signature OIDs
|
|
83
|
+
[/^1\.2\.840\.10045\.(2\.1|4(\..+)?)$/, "ec"], // id-ecPublicKey and the ecdsa-with-* signature OIDs
|
|
84
|
+
[/^1\.3\.132\.1\.12$/, "ecdh"],
|
|
85
|
+
[/^1\.2\.840\.10046\.2\.1$|^1\.2\.840\.113549\.1\.3\.1$/, "dh"],
|
|
86
|
+
[/^1\.2\.840\.10040\.4\.[13]$|^2\.16\.840\.1\.101\.3\.4\.3\.2$/, "dsa"],
|
|
87
|
+
];
|
|
88
|
+
/** Curve names in their common spellings, so P-256, secp256r1, prime256v1 and nistp256 are one. */
|
|
89
|
+
const CURVES = {
|
|
90
|
+
p256: "p-256", secp256r1: "p-256", prime256v1: "p-256", nistp256: "p-256",
|
|
91
|
+
p384: "p-384", secp384r1: "p-384", nistp384: "p-384",
|
|
92
|
+
p521: "p-521", secp521r1: "p-521", nistp521: "p-521",
|
|
93
|
+
p224: "p-224", secp224r1: "p-224",
|
|
94
|
+
secp256k1: "secp256k1",
|
|
95
|
+
};
|
|
96
|
+
/** The OID's family and the parameter that tells members apart, or null when the asset has no OID. */
|
|
97
|
+
function identity(x) {
|
|
98
|
+
const oid = x.properties.oid?.trim();
|
|
99
|
+
if (!oid)
|
|
100
|
+
return null;
|
|
101
|
+
const fam = FAMILY_OIDS.find(([re]) => re.test(oid));
|
|
102
|
+
if (!fam)
|
|
103
|
+
return { family: oid, param: "" }; // the OID pins the algorithm: ML-KEM-768, Ed25519, SHA-256
|
|
104
|
+
const raw = x.properties["algorithmProperties.curve"] ?? x.properties["algorithmProperties.parameterSetIdentifier"] ?? "";
|
|
105
|
+
const w = raw.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
106
|
+
return { family: fam[1], param: CURVES[w] ?? w };
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Signing or agreeing keys, from the CycloneDX primitive. The same curve does both (ECDSA and ECDH),
|
|
110
|
+
* so a signature never matches a key agreement. Other primitives say less: RSA is signature to one
|
|
111
|
+
* tool and encryption to another.
|
|
112
|
+
*/
|
|
113
|
+
function useOf(x) {
|
|
114
|
+
const p = x.properties["algorithmProperties.primitive"];
|
|
115
|
+
return p === "signature" ? "sign" : p === "key-agree" || p === "kem" ? "agree" : undefined;
|
|
116
|
+
}
|
|
117
|
+
/** A name's spellings: "Ed25519 / Ed448 (EdDSA)" is ed25519, ed448 and eddsa. */
|
|
118
|
+
function aliases(name) {
|
|
119
|
+
const parts = name.split(/\s*[/|,]\s*|\s*\(([^)]*)\)\s*/).filter(Boolean);
|
|
120
|
+
return [...new Set(parts.map((p) => p.toLowerCase().replace(/^fips\s*\d+$/, "").replace(/[^a-z0-9]/g, "")).filter(Boolean))];
|
|
121
|
+
}
|
|
122
|
+
/** Whether two assets can be the same thing at all: same kind, and not one for signing and one for key agreement. */
|
|
123
|
+
function compatible(x, y) {
|
|
124
|
+
if (x.assetType !== y.assetType || x.assetType === "library")
|
|
125
|
+
return false;
|
|
126
|
+
const p = useOf(x);
|
|
127
|
+
const q = useOf(y);
|
|
128
|
+
return !p || !q || p === q;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Pairs assets of the first CBOM with assets of the second: by name, then by OID with the parameter
|
|
132
|
+
* set or curve, then by OID alone where only one of each is left, then by name spellings for assets
|
|
133
|
+
* with no OID. A pair is only made when it is the one candidate on both sides.
|
|
134
|
+
*/
|
|
135
|
+
function pairUp(before, after) {
|
|
136
|
+
const pairs = [];
|
|
137
|
+
const left = new Set(before);
|
|
138
|
+
const right = new Set(after);
|
|
139
|
+
const take = (y, x) => {
|
|
140
|
+
pairs.push([y, x]);
|
|
141
|
+
left.delete(y);
|
|
142
|
+
right.delete(x);
|
|
143
|
+
};
|
|
144
|
+
const byKey = new Map(before.map((y) => [y.key, y]));
|
|
145
|
+
for (const x of after) {
|
|
146
|
+
const y = byKey.get(x.key);
|
|
147
|
+
if (y)
|
|
148
|
+
take(y, x);
|
|
149
|
+
}
|
|
150
|
+
/** Match each remaining asset where exactly one on the other side fits, and it fits only this one. */
|
|
151
|
+
const unique = (fits) => {
|
|
152
|
+
for (const x of [...right]) {
|
|
153
|
+
const ys = [...left].filter((y) => compatible(y, x) && fits(y, x));
|
|
154
|
+
if (ys.length !== 1)
|
|
155
|
+
continue;
|
|
156
|
+
const xs = [...right].filter((x2) => compatible(ys[0], x2) && fits(ys[0], x2));
|
|
157
|
+
if (xs.length === 1)
|
|
158
|
+
take(ys[0], x);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
const ids = new Map([...before, ...after].map((a) => [a, identity(a)]));
|
|
162
|
+
// The OID and the parameter set or curve, both given and equal.
|
|
163
|
+
unique((y, x) => {
|
|
164
|
+
const i = ids.get(y);
|
|
165
|
+
const j = ids.get(x);
|
|
166
|
+
return !!i && !!j && i.family === j.family && i.param === j.param;
|
|
167
|
+
});
|
|
168
|
+
// The same OID, where one side doesn't give the parameter set (or the OID already pins it).
|
|
169
|
+
unique((y, x) => {
|
|
170
|
+
const i = ids.get(y);
|
|
171
|
+
const j = ids.get(x);
|
|
172
|
+
return !!i && !!j && i.family === j.family && (!i.param || !j.param);
|
|
173
|
+
});
|
|
174
|
+
// No OID on one side: the name, in any of its spellings, or a family name for a member of it ("ml-dsa" for "ml-dsa-65").
|
|
175
|
+
unique((y, x) => {
|
|
176
|
+
if (ids.get(y) && ids.get(x))
|
|
177
|
+
return false;
|
|
178
|
+
const a = aliases(y.name);
|
|
179
|
+
const b = aliases(x.name);
|
|
180
|
+
return a.some((n) => b.includes(n)) || a.some((n) => b.some((m) => familyOf(n, m)));
|
|
181
|
+
});
|
|
182
|
+
return pairs;
|
|
183
|
+
}
|
|
184
|
+
/** "mldsa" names the family of "mldsa65": a name plus a parameter set made of digits. */
|
|
185
|
+
const familyOf = (a, b) => (a.length >= 3 && b.startsWith(a) && /^\d+$/.test(b.slice(a.length))) || (b.length >= 3 && a.startsWith(b) && /^\d+$/.test(a.slice(b.length)));
|
|
186
|
+
export function diffCbom(before, after) {
|
|
187
|
+
const pairs = pairUp(before, after);
|
|
188
|
+
const pairedBefore = new Set(pairs.map(([y]) => y));
|
|
189
|
+
const pairedAfter = new Set(pairs.map(([, x]) => x));
|
|
190
|
+
const added = after.filter((x) => !pairedAfter.has(x));
|
|
191
|
+
const removed = before.filter((y) => !pairedBefore.has(y));
|
|
192
|
+
// Two tools write different fields. A field only one of them writes is how it describes things, not a change.
|
|
193
|
+
const toolsBefore = new Set(before.map((y) => y.tool ?? ""));
|
|
194
|
+
const toolsAfter = new Set(after.map((x) => x.tool ?? ""));
|
|
195
|
+
const sameTool = toolsBefore.size <= 1 && toolsAfter.size <= 1 && [...toolsBefore][0] === [...toolsAfter][0];
|
|
196
|
+
const changed = [];
|
|
197
|
+
let unchanged = 0;
|
|
198
|
+
for (const [y, x] of pairs) {
|
|
199
|
+
const changes = [];
|
|
200
|
+
const differ = (p, q) => (sameTool ? (p ?? "") !== (q ?? "") : p !== undefined && q !== undefined && p !== q);
|
|
201
|
+
if (differ(y.risk, x.risk))
|
|
202
|
+
changes.push({ field: "risk", ...(y.risk ? { before: y.risk } : {}), ...(x.risk ? { after: x.risk } : {}) });
|
|
203
|
+
if (differ(y.version, x.version))
|
|
204
|
+
changes.push({ field: "version", ...(y.version ? { before: y.version } : {}), ...(x.version ? { after: x.version } : {}) });
|
|
205
|
+
for (const f of new Set([...Object.keys(y.properties), ...Object.keys(x.properties)])) {
|
|
206
|
+
// An OID inside the same family (a key OID and a signature OID for ECDSA) is a spelling, not a change.
|
|
207
|
+
if (f === "oid" && !sameTool)
|
|
208
|
+
continue;
|
|
209
|
+
if (differ(y.properties[f], x.properties[f]))
|
|
210
|
+
changes.push({ field: f, ...(y.properties[f] !== undefined ? { before: y.properties[f] } : {}), ...(x.properties[f] !== undefined ? { after: x.properties[f] } : {}) });
|
|
211
|
+
}
|
|
212
|
+
// Places: by file, since lines move when anything above them changes.
|
|
213
|
+
const fy = new Set(y.places.map(fileOf));
|
|
214
|
+
const fx = new Set(x.places.map(fileOf));
|
|
215
|
+
const newFiles = [...fx].filter((f) => !fy.has(f)).sort();
|
|
216
|
+
const goneFiles = [...fy].filter((f) => !fx.has(f)).sort();
|
|
217
|
+
const formerName = y.name !== x.name ? { formerName: y.name } : {};
|
|
218
|
+
if (changes.length || newFiles.length || goneFiles.length || x.places.length !== y.places.length)
|
|
219
|
+
changed.push({ name: x.name, ...formerName, assetType: x.assetType, changes, placesBefore: y.places.length, placesAfter: x.places.length, newFiles, goneFiles });
|
|
220
|
+
else
|
|
221
|
+
unchanged++;
|
|
222
|
+
}
|
|
223
|
+
const byName = (p, q) => p.name.localeCompare(q.name);
|
|
224
|
+
return { added: added.sort(byName), removed: removed.sort(byName), changed: changed.sort(byName), unchanged };
|
|
225
|
+
}
|
|
226
|
+
/** Steps back: an asset added with a risk to fix, or one that became risky or is used in more places. */
|
|
227
|
+
export function newRisk(d, after) {
|
|
228
|
+
const risk = new Map(after.map((x) => [`${x.assetType}:${x.name}`, x.risk]));
|
|
229
|
+
const out = [];
|
|
230
|
+
for (const x of d.added)
|
|
231
|
+
if (x.risk && RISKY.has(x.risk))
|
|
232
|
+
out.push(x.name);
|
|
233
|
+
for (const c of d.changed) {
|
|
234
|
+
const r = risk.get(`${c.assetType}:${c.name}`);
|
|
235
|
+
const became = c.changes.find((x) => x.field === "risk" && x.after && RISKY.has(x.after));
|
|
236
|
+
if (became || (r && RISKY.has(r) && c.placesAfter > c.placesBefore))
|
|
237
|
+
out.push(c.name);
|
|
238
|
+
}
|
|
239
|
+
return out;
|
|
240
|
+
}
|
|
241
|
+
export function cbomDiffText(d) {
|
|
242
|
+
if (!d.added.length && !d.removed.length && !d.changed.length)
|
|
243
|
+
return `No change: the same ${d.unchanged} encryption asset${d.unchanged === 1 ? "" : "s"} in both.`;
|
|
244
|
+
const out = [`${d.added.length} added, ${d.removed.length} removed, ${d.changed.length} changed, ${d.unchanged} the same.`];
|
|
245
|
+
const label = (x) => `${x.name} (${x.assetType}${x.risk ? `, ${x.risk}` : ""})`;
|
|
246
|
+
const where = (places) => (places.length ? ` ${places.slice(0, 3).join(", ")}${places.length > 3 ? `, and ${places.length - 3} more` : ""}` : "");
|
|
247
|
+
if (d.added.length) {
|
|
248
|
+
out.push("", "Added");
|
|
249
|
+
for (const x of d.added)
|
|
250
|
+
out.push(` ${label(x)}${where(x.places)}`);
|
|
251
|
+
}
|
|
252
|
+
if (d.removed.length) {
|
|
253
|
+
out.push("", "Removed");
|
|
254
|
+
for (const x of d.removed)
|
|
255
|
+
out.push(` ${label(x)}`);
|
|
256
|
+
}
|
|
257
|
+
if (d.changed.length) {
|
|
258
|
+
out.push("", "Changed");
|
|
259
|
+
for (const c of d.changed) {
|
|
260
|
+
const bits = [];
|
|
261
|
+
if (c.placesAfter !== c.placesBefore)
|
|
262
|
+
bits.push(`${c.placesBefore} -> ${c.placesAfter} places`);
|
|
263
|
+
for (const x of c.changes)
|
|
264
|
+
bits.push(`${x.field} ${x.before ?? "(none)"} -> ${x.after ?? "(none)"}`);
|
|
265
|
+
if (c.newFiles.length)
|
|
266
|
+
bits.push(`now also in ${c.newFiles.slice(0, 3).join(", ")}${c.newFiles.length > 3 ? ` and ${c.newFiles.length - 3} more` : ""}`);
|
|
267
|
+
if (c.goneFiles.length)
|
|
268
|
+
bits.push(`no longer in ${c.goneFiles.slice(0, 3).join(", ")}${c.goneFiles.length > 3 ? ` and ${c.goneFiles.length - 3} more` : ""}`);
|
|
269
|
+
out.push(` ${c.name} (${c.assetType}${c.formerName ? `, was "${c.formerName}"` : ""}): ${bits.join("; ")}`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return out.join("\n");
|
|
273
|
+
}
|