@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/registry.js
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Pull an image straight from a registry to scan it: read-only, with the credentials already set up
|
|
4
|
+
// for docker or podman on this machine. Nothing is pushed, tagged or deleted, and no docker or podman
|
|
5
|
+
// program needs to run. Every manifest, config and layer is checked against its digest.
|
|
6
|
+
//
|
|
7
|
+
// Credentials come from, in order: podman's auth file (REGISTRY_AUTH_FILE, then the usual places),
|
|
8
|
+
// then docker's config (DOCKER_CONFIG or ~/.docker/config.json), including its credential helpers
|
|
9
|
+
// (docker-credential-<name>), the same way docker itself reads them. Without any, the pull is anonymous.
|
|
10
|
+
import { execFileSync } from "node:child_process";
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
13
|
+
import { homedir } from "node:os";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { Readable } from "node:stream";
|
|
16
|
+
import { configOf, ImageFS, readLayer } from "./image.js";
|
|
17
|
+
const COMPONENT = "[a-z0-9]+(?:(?:[._]|__|-+)[a-z0-9]+)*";
|
|
18
|
+
const REPO = new RegExp(`^${COMPONENT}(?:/${COMPONENT})*$`);
|
|
19
|
+
/** Read an image name like nginx:1.27, ghcr.io/org/app@sha256:..., or docker://localhost:5000/app. */
|
|
20
|
+
export function parseImageRef(input) {
|
|
21
|
+
let s = input.trim().replace(/^docker:\/\//, "");
|
|
22
|
+
let digest;
|
|
23
|
+
const at = s.indexOf("@");
|
|
24
|
+
if (at >= 0) {
|
|
25
|
+
digest = s.slice(at + 1);
|
|
26
|
+
if (!/^sha256:[0-9a-f]{64}$/.test(digest))
|
|
27
|
+
return null;
|
|
28
|
+
s = s.slice(0, at);
|
|
29
|
+
}
|
|
30
|
+
let tag;
|
|
31
|
+
const lastSlash = s.lastIndexOf("/");
|
|
32
|
+
const colon = s.lastIndexOf(":");
|
|
33
|
+
if (colon > lastSlash) {
|
|
34
|
+
tag = s.slice(colon + 1);
|
|
35
|
+
if (!/^[\w][\w.-]{0,127}$/.test(tag))
|
|
36
|
+
return null;
|
|
37
|
+
s = s.slice(0, colon);
|
|
38
|
+
}
|
|
39
|
+
let registry = "docker.io";
|
|
40
|
+
const first = s.split("/")[0];
|
|
41
|
+
if (s.includes("/") && (first.includes(".") || first.includes(":") || first === "localhost")) {
|
|
42
|
+
if (!/^[a-z0-9.-]+(:\d{1,5})?$/i.test(first))
|
|
43
|
+
return null;
|
|
44
|
+
registry = first.toLowerCase();
|
|
45
|
+
s = s.slice(first.length + 1);
|
|
46
|
+
}
|
|
47
|
+
if (registry === "docker.io" || registry === "index.docker.io") {
|
|
48
|
+
registry = "docker.io";
|
|
49
|
+
if (!s.includes("/"))
|
|
50
|
+
s = `library/${s}`;
|
|
51
|
+
}
|
|
52
|
+
if (!REPO.test(s))
|
|
53
|
+
return null;
|
|
54
|
+
if (!tag && !digest)
|
|
55
|
+
tag = "latest";
|
|
56
|
+
return { display: input.trim().replace(/^docker:\/\//, ""), registry, host: registry === "docker.io" ? "registry-1.docker.io" : registry, repository: s, ...(tag ? { tag } : {}), ...(digest ? { digest } : {}) };
|
|
57
|
+
}
|
|
58
|
+
/** Whether an argument names an image in a registry rather than a file or folder. */
|
|
59
|
+
export function looksLikeImageRef(input) {
|
|
60
|
+
if (input.startsWith("docker://"))
|
|
61
|
+
return true;
|
|
62
|
+
if (existsSync(input) || /^(\.|\/|~|[A-Za-z]:\\)/.test(input) || /\.(tar|tgz|tar\.gz|oci)$/i.test(input))
|
|
63
|
+
return false;
|
|
64
|
+
return parseImageRef(input) !== null;
|
|
65
|
+
}
|
|
66
|
+
function readAuthFile(path) {
|
|
67
|
+
try {
|
|
68
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** The names a registry's credentials may be filed under, most specific first. */
|
|
75
|
+
function authKeys(ref) {
|
|
76
|
+
const keys = [];
|
|
77
|
+
const parts = ref.repository.split("/");
|
|
78
|
+
for (let i = parts.length; i > 0; i--)
|
|
79
|
+
keys.push(`${ref.registry}/${parts.slice(0, i).join("/")}`); // podman allows per-repository entries
|
|
80
|
+
if (ref.registry === "docker.io")
|
|
81
|
+
keys.push("https://index.docker.io/v1/", "index.docker.io", "docker.io", "registry-1.docker.io", "https://registry-1.docker.io");
|
|
82
|
+
else
|
|
83
|
+
keys.push(ref.registry, `https://${ref.registry}`, `http://${ref.registry}`, `https://${ref.registry}/v1/`, `https://${ref.registry}/v2/`);
|
|
84
|
+
return keys;
|
|
85
|
+
}
|
|
86
|
+
function fromEntry(e, source) {
|
|
87
|
+
if (e.identitytoken)
|
|
88
|
+
return { identityToken: e.identitytoken, source };
|
|
89
|
+
if (e.auth) {
|
|
90
|
+
const plain = Buffer.from(e.auth, "base64").toString("utf8");
|
|
91
|
+
const i = plain.indexOf(":");
|
|
92
|
+
if (i > 0)
|
|
93
|
+
return { username: plain.slice(0, i), password: plain.slice(i + 1), source };
|
|
94
|
+
}
|
|
95
|
+
if (e.username && e.password)
|
|
96
|
+
return { username: e.username, password: e.password, source };
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
const runHelper = (helper, server) => {
|
|
100
|
+
try {
|
|
101
|
+
return execFileSync(`docker-credential-${helper}`, ["get"], { input: server, timeout: 30_000, stdio: ["pipe", "pipe", "ignore"] }).toString("utf8");
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
/** The credentials docker or podman would use for this image, if any. */
|
|
108
|
+
export function findCredential(ref, places = {}) {
|
|
109
|
+
const env = places.env ?? process.env;
|
|
110
|
+
const home = places.home ?? homedir();
|
|
111
|
+
const keys = authKeys(ref);
|
|
112
|
+
const podman = [env.REGISTRY_AUTH_FILE, env.XDG_RUNTIME_DIR && join(env.XDG_RUNTIME_DIR, "containers", "auth.json"), join(home, ".config", "containers", "auth.json")].filter((p) => Boolean(p));
|
|
113
|
+
const docker = join(env.DOCKER_CONFIG ?? join(home, ".docker"), "config.json");
|
|
114
|
+
for (const file of [...podman, docker]) {
|
|
115
|
+
const f = readAuthFile(file);
|
|
116
|
+
if (!f)
|
|
117
|
+
continue;
|
|
118
|
+
const label = file === docker ? "docker's config" : "podman's auth file";
|
|
119
|
+
for (const k of keys) {
|
|
120
|
+
const e = f.auths?.[k];
|
|
121
|
+
const c = e && fromEntry(e, label);
|
|
122
|
+
if (c)
|
|
123
|
+
return c;
|
|
124
|
+
}
|
|
125
|
+
// A credential helper: one for this registry, or the store for all of them.
|
|
126
|
+
const helper = keys.map((k) => f.credHelpers?.[k]).find(Boolean) ?? f.credsStore;
|
|
127
|
+
if (helper && /^[A-Za-z0-9._-]+$/.test(helper)) {
|
|
128
|
+
const server = ref.registry === "docker.io" ? "https://index.docker.io/v1/" : ref.registry;
|
|
129
|
+
const out = (places.helper ?? runHelper)(helper, server);
|
|
130
|
+
if (out) {
|
|
131
|
+
try {
|
|
132
|
+
const j = JSON.parse(out);
|
|
133
|
+
if (j.Secret)
|
|
134
|
+
return j.Username === "<token>" ? { identityToken: j.Secret, source: `docker-credential-${helper}` } : { username: j.Username, password: j.Secret, source: `docker-credential-${helper}` };
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
// the helper had nothing for this registry
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
// ---------- the registry, read-only ----------
|
|
145
|
+
const MANIFEST_TYPES = [
|
|
146
|
+
"application/vnd.oci.image.index.v1+json",
|
|
147
|
+
"application/vnd.oci.image.manifest.v1+json",
|
|
148
|
+
"application/vnd.docker.distribution.manifest.list.v2+json",
|
|
149
|
+
"application/vnd.docker.distribution.manifest.v2+json",
|
|
150
|
+
].join(", ");
|
|
151
|
+
const LOCAL = /^(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/;
|
|
152
|
+
export class RegistryClient {
|
|
153
|
+
token = null;
|
|
154
|
+
basic = false;
|
|
155
|
+
ref;
|
|
156
|
+
cred;
|
|
157
|
+
fetcher;
|
|
158
|
+
constructor(ref, cred, fetcher = fetch) {
|
|
159
|
+
this.ref = ref;
|
|
160
|
+
this.cred = cred;
|
|
161
|
+
this.fetcher = fetcher;
|
|
162
|
+
}
|
|
163
|
+
/** Registries on this machine may answer over plain HTTP, as docker allows. */
|
|
164
|
+
get base() {
|
|
165
|
+
return `${LOCAL.test(this.ref.host) ? "http" : "https"}://${this.ref.host}/v2/${this.ref.repository}/`;
|
|
166
|
+
}
|
|
167
|
+
headers(extra = {}) {
|
|
168
|
+
const h = { "user-agent": "hard-keel-benthic", ...extra };
|
|
169
|
+
if (this.token)
|
|
170
|
+
h.authorization = this.basic ? `Basic ${this.token}` : `Bearer ${this.token}`;
|
|
171
|
+
return h;
|
|
172
|
+
}
|
|
173
|
+
async get(path, accept) {
|
|
174
|
+
const url = this.base + path;
|
|
175
|
+
let res = await this.fetcher(url, { headers: this.headers(accept ? { accept } : {}), redirect: "follow" });
|
|
176
|
+
if (res.status === 401) {
|
|
177
|
+
const challenge = res.headers.get("www-authenticate") ?? "";
|
|
178
|
+
await res.body?.cancel();
|
|
179
|
+
await this.authenticate(challenge);
|
|
180
|
+
res = await this.fetcher(url, { headers: this.headers(accept ? { accept } : {}), redirect: "follow" });
|
|
181
|
+
}
|
|
182
|
+
if (res.status === 401 || res.status === 403) {
|
|
183
|
+
await res.body?.cancel();
|
|
184
|
+
throw new Error(`${this.ref.registry} refused to let this scan read ${this.ref.repository}${this.cred ? ` with the credentials from ${this.cred.source}` : " without credentials"}. Log in first (docker login ${this.ref.registry} or podman login ${this.ref.registry}), then run the scan again.`);
|
|
185
|
+
}
|
|
186
|
+
if (res.status === 404) {
|
|
187
|
+
await res.body?.cancel();
|
|
188
|
+
throw new Error(`${this.ref.registry} has no ${path.startsWith("manifests/") ? `image ${this.ref.repository}:${path.slice(10)}` : `${path} for ${this.ref.repository}`}.`);
|
|
189
|
+
}
|
|
190
|
+
if (!res.ok) {
|
|
191
|
+
await res.body?.cancel();
|
|
192
|
+
throw new Error(`${this.ref.registry} answered ${res.status} for ${this.ref.repository}.`);
|
|
193
|
+
}
|
|
194
|
+
return res;
|
|
195
|
+
}
|
|
196
|
+
async authenticate(challenge) {
|
|
197
|
+
const scheme = challenge.trim().split(/\s+/)[0]?.toLowerCase();
|
|
198
|
+
const params = {};
|
|
199
|
+
for (const m of challenge.matchAll(/(\w+)="([^"]*)"/g))
|
|
200
|
+
params[m[1].toLowerCase()] = m[2];
|
|
201
|
+
if (scheme === "basic") {
|
|
202
|
+
if (!this.cred?.username || this.cred.password === undefined)
|
|
203
|
+
return;
|
|
204
|
+
this.token = Buffer.from(`${this.cred.username}:${this.cred.password}`).toString("base64");
|
|
205
|
+
this.basic = true;
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (scheme !== "bearer" || !params.realm)
|
|
209
|
+
return;
|
|
210
|
+
let realm;
|
|
211
|
+
try {
|
|
212
|
+
realm = new URL(params.realm);
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
// Credentials only go to a token service over HTTPS (or one on this machine).
|
|
218
|
+
const safe = realm.protocol === "https:" || LOCAL.test(realm.host);
|
|
219
|
+
const scope = `repository:${this.ref.repository}:pull`;
|
|
220
|
+
let res;
|
|
221
|
+
if (this.cred?.identityToken && safe) {
|
|
222
|
+
const form = new URLSearchParams({ grant_type: "refresh_token", refresh_token: this.cred.identityToken, service: params.service ?? "", scope, client_id: "hard-keel-benthic" });
|
|
223
|
+
res = await this.fetcher(realm.toString(), { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded", "user-agent": "hard-keel-benthic" }, body: form.toString() });
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
if (params.service)
|
|
227
|
+
realm.searchParams.set("service", params.service);
|
|
228
|
+
realm.searchParams.set("scope", scope);
|
|
229
|
+
const h = { "user-agent": "hard-keel-benthic" };
|
|
230
|
+
if (this.cred?.username && this.cred.password !== undefined && safe)
|
|
231
|
+
h.authorization = `Basic ${Buffer.from(`${this.cred.username}:${this.cred.password}`).toString("base64")}`;
|
|
232
|
+
res = await this.fetcher(realm.toString(), { headers: h });
|
|
233
|
+
}
|
|
234
|
+
if (!res.ok) {
|
|
235
|
+
await res.body?.cancel();
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const j = (await res.json());
|
|
239
|
+
this.token = j.token ?? j.access_token ?? null;
|
|
240
|
+
this.basic = false;
|
|
241
|
+
}
|
|
242
|
+
/** A manifest or index, checked against its digest when it was asked for by digest. */
|
|
243
|
+
async manifest(reference) {
|
|
244
|
+
const res = await this.get(`manifests/${reference}`, MANIFEST_TYPES);
|
|
245
|
+
const body = Buffer.from(await res.arrayBuffer());
|
|
246
|
+
const digest = `sha256:${createHash("sha256").update(body).digest("hex")}`;
|
|
247
|
+
if (reference.startsWith("sha256:") && digest !== reference)
|
|
248
|
+
throw new Error(`The manifest ${this.ref.registry} sent for ${this.ref.repository} doesn't match its digest.`);
|
|
249
|
+
return { doc: JSON.parse(body.toString("utf8")), digest };
|
|
250
|
+
}
|
|
251
|
+
async blobJson(digest) {
|
|
252
|
+
const body = Buffer.from(await (await this.get(`blobs/${digest}`)).arrayBuffer());
|
|
253
|
+
if (`sha256:${createHash("sha256").update(body).digest("hex")}` !== digest)
|
|
254
|
+
throw new Error(`A file ${this.ref.registry} sent for ${this.ref.repository} doesn't match its digest.`);
|
|
255
|
+
return JSON.parse(body.toString("utf8"));
|
|
256
|
+
}
|
|
257
|
+
async blobStream(digest) {
|
|
258
|
+
const res = await this.get(`blobs/${digest}`);
|
|
259
|
+
if (!res.body)
|
|
260
|
+
throw new Error(`${this.ref.registry} sent an empty layer for ${this.ref.repository}.`);
|
|
261
|
+
return Readable.fromWeb(res.body);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function platformName(p) {
|
|
265
|
+
return p ? [p.os, p.architecture, p.variant].filter(Boolean).join("/") : "unknown";
|
|
266
|
+
}
|
|
267
|
+
function kindOf(mediaType) {
|
|
268
|
+
if (!mediaType || /gzip/.test(mediaType))
|
|
269
|
+
return "gzip"; // docker's own layers are always gzip
|
|
270
|
+
if (/zstd/.test(mediaType))
|
|
271
|
+
return "zstd";
|
|
272
|
+
return "tar";
|
|
273
|
+
}
|
|
274
|
+
/** Pull an image's manifest, config and layers from its registry and read them. */
|
|
275
|
+
export async function loadFromRegistry(input, opts = {}) {
|
|
276
|
+
const ref = parseImageRef(input);
|
|
277
|
+
if (!ref)
|
|
278
|
+
throw new Error(`${input} isn't an image name this scan can read (for example nginx:1.27 or ghcr.io/org/app:tag).`);
|
|
279
|
+
const cred = findCredential(ref, opts.credentials);
|
|
280
|
+
const client = new RegistryClient(ref, cred, opts.fetcher);
|
|
281
|
+
const notes = [];
|
|
282
|
+
const [wantOs, wantArch, wantVariant] = (opts.platform ?? "linux/amd64").split("/");
|
|
283
|
+
let { doc, digest } = await client.manifest(ref.digest ?? ref.tag ?? "latest");
|
|
284
|
+
if (doc.schemaVersion === 1)
|
|
285
|
+
throw new Error(`${ref.display} uses Docker's old image format (schema 1), which this scan doesn't read.`);
|
|
286
|
+
if (Array.isArray(doc.manifests)) {
|
|
287
|
+
const list = doc.manifests.filter((m) => m.platform?.architecture !== "unknown"); // build attestations
|
|
288
|
+
const pick = list.find((m) => m.platform?.os === wantOs && m.platform?.architecture === wantArch && (!wantVariant || m.platform?.variant === wantVariant));
|
|
289
|
+
if (!pick)
|
|
290
|
+
throw new Error(`${ref.display} has no ${opts.platform ?? "linux/amd64"} image. It has: ${list.map((m) => platformName(m.platform)).join(", ")}. Choose one with --platform.`);
|
|
291
|
+
if (list.length > 1)
|
|
292
|
+
notes.push(`The image holds ${list.length} platforms; this scan read ${platformName(pick.platform)}.`);
|
|
293
|
+
({ doc, digest } = await client.manifest(pick.digest));
|
|
294
|
+
}
|
|
295
|
+
const config = doc.config;
|
|
296
|
+
const layers = doc.layers ?? [];
|
|
297
|
+
if (!config || !layers.length)
|
|
298
|
+
throw new Error(`The manifest for ${ref.display} lists no layers.`);
|
|
299
|
+
const cfg = configOf(await client.blobJson(config.digest));
|
|
300
|
+
const fs = new ImageFS();
|
|
301
|
+
const stats = { cached: 0 };
|
|
302
|
+
let read = 0;
|
|
303
|
+
for (const l of layers) {
|
|
304
|
+
if (/foreign|nondistributable/.test(l.mediaType ?? "")) {
|
|
305
|
+
notes.push("A layer the registry doesn't serve (a Windows base layer) was skipped.");
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (!/^sha256:[0-9a-f]{64}$/.test(l.digest)) {
|
|
309
|
+
notes.push(`A layer with an unsupported digest (${l.digest.split(":")[0]}) was skipped.`);
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
if (await readLayer(fs, l.digest, async () => ({ raw: await client.blobStream(l.digest), kind: kindOf(l.mediaType) }), opts.cache, notes, stats))
|
|
313
|
+
read++;
|
|
314
|
+
}
|
|
315
|
+
const config2 = cfg.config;
|
|
316
|
+
return {
|
|
317
|
+
fs,
|
|
318
|
+
format: "registry",
|
|
319
|
+
layers: read,
|
|
320
|
+
...(stats.cached ? { cachedLayers: stats.cached } : {}),
|
|
321
|
+
platform: cfg.platform,
|
|
322
|
+
tags: [ref.display.includes("@") ? ref.display : `${ref.display}${ref.tag && !ref.display.endsWith(`:${ref.tag}`) ? `:${ref.tag}` : ""}`],
|
|
323
|
+
config: config2,
|
|
324
|
+
notes: [`Pulled from ${ref.registry}${cred ? ` with the credentials from ${cred.source}` : " without credentials"}; image digest ${digest}.`, ...notes],
|
|
325
|
+
};
|
|
326
|
+
}
|
package/dist/report.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Human-readable output: pull request comment, check-run annotations, migration report.
|
|
4
|
+
import { gradeSentence } from "./score.js";
|
|
5
|
+
export const COMMENT_MARKER = "<!-- hardkeel-quantum-scan -->";
|
|
6
|
+
const SEV_ORDER = { critical: 0, high: 1, medium: 2, low: 3 };
|
|
7
|
+
export function sortFindings(fs) {
|
|
8
|
+
return [...fs].sort((a, b) => SEV_ORDER[a.severity] - SEV_ORDER[b.severity] || a.file.localeCompare(b.file) || a.line - b.line);
|
|
9
|
+
}
|
|
10
|
+
function counts(fs) {
|
|
11
|
+
return {
|
|
12
|
+
secrets: fs.filter((f) => f.risk === "secret").length,
|
|
13
|
+
weak: fs.filter((f) => f.risk === "weak-now").length,
|
|
14
|
+
quantum: fs.filter((f) => f.risk === "quantum-vulnerable").length,
|
|
15
|
+
exposed: fs.filter((f) => f.risk === "exposed").length,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function riskLabel(f) {
|
|
19
|
+
if (f.risk === "secret")
|
|
20
|
+
return "Leaked key or password";
|
|
21
|
+
if (f.risk === "weak-now")
|
|
22
|
+
return "Weak today";
|
|
23
|
+
if (f.risk === "exposed")
|
|
24
|
+
return "Exposed setting";
|
|
25
|
+
return "Quantum-vulnerable";
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Text for a Markdown table cell or code span. File names come from the scanned repository and can hold a
|
|
29
|
+
* line break, which would end the table and let the name add words of its own to a report or PR comment.
|
|
30
|
+
*/
|
|
31
|
+
export function esc(s) {
|
|
32
|
+
return s.replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g, "?").replace(/\|/g, "\\|").replace(/`/g, "'");
|
|
33
|
+
}
|
|
34
|
+
/** Text outside a code span, escaped so it can't become a link, a heading, a mention or an image. */
|
|
35
|
+
export function escText(s) {
|
|
36
|
+
return esc(s).replace(/[\\[\]<>*_#@!~]/g, (c) => `\\${c}`).replace(/\\\\\|/g, "\\|");
|
|
37
|
+
}
|
|
38
|
+
/** Conclusion for the GitHub check run. Secrets and weak crypto fail; quantum-only is neutral. */
|
|
39
|
+
export function checkConclusion(fs, blockOnQuantum = false) {
|
|
40
|
+
const c = counts(fs);
|
|
41
|
+
if (c.secrets || c.weak || c.exposed)
|
|
42
|
+
return "failure";
|
|
43
|
+
if (c.quantum)
|
|
44
|
+
return blockOnQuantum ? "failure" : "neutral";
|
|
45
|
+
return "success";
|
|
46
|
+
}
|
|
47
|
+
export function prSummaryLine(fs) {
|
|
48
|
+
const c = counts(fs);
|
|
49
|
+
if (!fs.length)
|
|
50
|
+
return "No leaked keys or passwords, weak encryption, exposed infrastructure settings, or encryption quantum computers will break in this change.";
|
|
51
|
+
const parts = [];
|
|
52
|
+
if (c.secrets)
|
|
53
|
+
parts.push(`${c.secrets} leaked key${c.secrets > 1 ? "s or passwords" : " or password"}`);
|
|
54
|
+
if (c.weak)
|
|
55
|
+
parts.push(`${c.weak} use${c.weak > 1 ? "s" : ""} of weak encryption`);
|
|
56
|
+
if (c.exposed)
|
|
57
|
+
parts.push(`${c.exposed} exposed infrastructure setting${c.exposed > 1 ? "s" : ""}`);
|
|
58
|
+
if (c.quantum)
|
|
59
|
+
parts.push(`${c.quantum} use${c.quantum > 1 ? "s" : ""} of encryption quantum computers will break`);
|
|
60
|
+
return `This change adds ${parts.length > 1 ? parts.slice(0, -1).join(", ") + " and " + parts[parts.length - 1] : parts[0]}.`;
|
|
61
|
+
}
|
|
62
|
+
const LOGO = "https://avatars.githubusercontent.com/in/5838578?s=64";
|
|
63
|
+
/** Short name for a finding, like "MD5 hash" or "RSA-1024 key". */
|
|
64
|
+
export function shortLabel(f) {
|
|
65
|
+
const a = f.algorithm;
|
|
66
|
+
if (f.kind === "config" && a === "Open to the internet")
|
|
67
|
+
return f.title; // names the ports, like "SSH (22) open to the whole internet"
|
|
68
|
+
if (f.risk === "secret" || f.kind === "config")
|
|
69
|
+
return a;
|
|
70
|
+
if (a.startsWith("RSA") && f.keySize !== undefined && f.keySize < 2048)
|
|
71
|
+
return `RSA-${f.keySize} key`;
|
|
72
|
+
if (a === "RSA")
|
|
73
|
+
return "RSA key";
|
|
74
|
+
if (a === "EC")
|
|
75
|
+
return "Elliptic-curve key";
|
|
76
|
+
if (/(mode|signature|encryption|exchange)$/.test(a) || a.startsWith("TLS"))
|
|
77
|
+
return a;
|
|
78
|
+
const noun = {
|
|
79
|
+
hash: "hash",
|
|
80
|
+
"block-cipher": "cipher",
|
|
81
|
+
"stream-cipher": "cipher",
|
|
82
|
+
signature: "signature",
|
|
83
|
+
"key-agreement": "key exchange",
|
|
84
|
+
"public-key-encryption": "encryption",
|
|
85
|
+
};
|
|
86
|
+
return noun[f.primitive] ? `${a} ${noun[f.primitive]}` : a;
|
|
87
|
+
}
|
|
88
|
+
function plural(n, one, many) {
|
|
89
|
+
return `${n} ${n === 1 ? one : many}`;
|
|
90
|
+
}
|
|
91
|
+
/** The comment's bold first line. */
|
|
92
|
+
export function prHeadline(fs) {
|
|
93
|
+
const now = fs.filter((f) => f.risk !== "quantum-vulnerable").length;
|
|
94
|
+
const later = fs.length - now;
|
|
95
|
+
if (!fs.length)
|
|
96
|
+
return "**Nothing to fix.** No leaked keys, weak or quantum-breakable encryption, or exposed infrastructure settings in this change.";
|
|
97
|
+
if (!now)
|
|
98
|
+
return `**Nothing blocks this merge.** ${plural(later, "item", "items")} to plan for, below.`;
|
|
99
|
+
return `**${plural(now, "problem", "problems")} to fix before merging.**` + (later ? ` ${plural(later, "more item", "more items")} to plan for.` : "");
|
|
100
|
+
}
|
|
101
|
+
export function prComment(fs, opts = {}) {
|
|
102
|
+
const max = opts.maxRows ?? 25;
|
|
103
|
+
const sorted = sortFindings(fs).slice(0, max);
|
|
104
|
+
const where = (f) => {
|
|
105
|
+
const label = `\`${esc(f.file)}:${f.line}\``;
|
|
106
|
+
return opts.repo && opts.sha ? `[${label}](https://github.com/${opts.repo}/blob/${opts.sha}/${f.file.split("/").map(encodeURIComponent).join("/")}#L${f.line})` : label;
|
|
107
|
+
};
|
|
108
|
+
const table = (rows) => [
|
|
109
|
+
"| Where | Issue | Fix |",
|
|
110
|
+
"| --- | --- | --- |",
|
|
111
|
+
...rows.map((f) => `| ${where(f)} | ${esc(shortLabel(f))} | ${esc(f.fix)} |`),
|
|
112
|
+
];
|
|
113
|
+
const now = sorted.filter((f) => f.risk !== "quantum-vulnerable");
|
|
114
|
+
const later = sorted.filter((f) => f.risk === "quantum-vulnerable");
|
|
115
|
+
const lines = [
|
|
116
|
+
COMMENT_MARKER,
|
|
117
|
+
`### <img src="${LOGO}" width="20" height="20" alt=""> Hard Keel Benthic`,
|
|
118
|
+
"",
|
|
119
|
+
prHeadline(fs),
|
|
120
|
+
];
|
|
121
|
+
if (now.length)
|
|
122
|
+
lines.push("", "#### Fix before merging", "", ...table(now));
|
|
123
|
+
if (later.length) {
|
|
124
|
+
lines.push("", "#### Plan for later", "", "Safe today, but quantum computers will be able to break these. NIST plans to phase them out between 2030 and 2035.", "", ...table(later));
|
|
125
|
+
}
|
|
126
|
+
if (fs.length > max)
|
|
127
|
+
lines.push("", `${fs.length - max} more are listed in the check results.`);
|
|
128
|
+
if (opts.reportUrl) {
|
|
129
|
+
lines.push("", `**[Open the full report](${opts.reportUrl})** to see every use of encryption in this repository and the order to fix them in.`);
|
|
130
|
+
}
|
|
131
|
+
lines.push("", "<sub>Hard Keel · hardkeel.com · Questions: contact@hardkeel.com</sub>");
|
|
132
|
+
return lines.join("\n");
|
|
133
|
+
}
|
|
134
|
+
export function annotations(fs) {
|
|
135
|
+
return sortFindings(fs).map((f) => ({
|
|
136
|
+
path: f.file,
|
|
137
|
+
start_line: f.line,
|
|
138
|
+
end_line: f.line,
|
|
139
|
+
annotation_level: f.risk === "quantum-vulnerable" ? "warning" : "failure",
|
|
140
|
+
title: `${riskLabel(f)}: ${f.title}`,
|
|
141
|
+
message: f.fix,
|
|
142
|
+
}));
|
|
143
|
+
}
|
|
144
|
+
/** Group a full-repo scan by algorithm and rank by what to migrate first. */
|
|
145
|
+
export function migrationPlan(fs) {
|
|
146
|
+
const map = new Map();
|
|
147
|
+
for (const f of fs)
|
|
148
|
+
map.set(`${f.risk}|${f.algorithm}`, [...(map.get(`${f.risk}|${f.algorithm}`) ?? []), f]);
|
|
149
|
+
const groups = [...map.values()].map((g) => {
|
|
150
|
+
const f = g[0];
|
|
151
|
+
let priority = 4;
|
|
152
|
+
let when = "Before 2035";
|
|
153
|
+
if (f.risk === "secret")
|
|
154
|
+
[priority, when] = [0, "Now"];
|
|
155
|
+
else if (f.risk === "exposed")
|
|
156
|
+
[priority, when] = [1, "Now"];
|
|
157
|
+
else if (f.risk === "weak-now")
|
|
158
|
+
[priority, when] = [1, "Now"];
|
|
159
|
+
else if (f.primitive === "key-agreement" || f.primitive === "public-key-encryption")
|
|
160
|
+
[priority, when] = [2, "By 2030"];
|
|
161
|
+
else
|
|
162
|
+
[priority, when] = [3, "By 2030 to 2035"];
|
|
163
|
+
return { algorithm: f.algorithm, risk: f.risk, count: g.length, files: [...new Set(g.map((x) => x.file))], fix: f.fix, priority, when };
|
|
164
|
+
});
|
|
165
|
+
return groups.sort((a, b) => a.priority - b.priority || b.count - a.count);
|
|
166
|
+
}
|
|
167
|
+
export function migrationReportMarkdown(repo, fs, scannedAt, score, coverage, note) {
|
|
168
|
+
const plan = migrationPlan(fs);
|
|
169
|
+
const c = counts(fs);
|
|
170
|
+
const out = [
|
|
171
|
+
`# Quantum readiness report: ${escText(repo)}`,
|
|
172
|
+
"",
|
|
173
|
+
...(score ? [`**${gradeSentence(score)}** How the score works: https://hardkeel.com/learn/quantum-readiness-score/`, ""] : []),
|
|
174
|
+
...(note ? [`**${escText(note)}**`, ""] : []),
|
|
175
|
+
`Scanned ${scannedAt}. ${c.quantum} use${c.quantum === 1 ? "" : "s"} of encryption quantum computers will break, ${c.weak} of weak encryption, ${c.exposed} exposed infrastructure setting${c.exposed === 1 ? "" : "s"}, and ${c.secrets} leaked key${c.secrets === 1 ? "" : "s"} or password${c.secrets === 1 ? "" : "s"}.`,
|
|
176
|
+
"",
|
|
177
|
+
"NIST's final transition plan (IR 8547, October 2026) deprecates today's public-key algorithms (RSA, elliptic curve, Diffie-Hellman) after 2030 and disallows them after 2035. Key exchange comes first, because encrypted traffic recorded today can be decrypted once a large quantum computer exists.",
|
|
178
|
+
"",
|
|
179
|
+
"| Order | What | Uses | Files | When | Move to |",
|
|
180
|
+
"| --- | --- | --- | --- | --- | --- |",
|
|
181
|
+
];
|
|
182
|
+
plan.forEach((g, i) => {
|
|
183
|
+
const files = g.files.slice(0, 3).map((f) => `\`${esc(f)}\``).join(", ") + (g.files.length > 3 ? ` +${g.files.length - 3}` : "");
|
|
184
|
+
out.push(`| ${i + 1} | ${esc(g.algorithm)} | ${g.count} | ${files} | ${g.when} | ${esc(g.fix)} |`);
|
|
185
|
+
});
|
|
186
|
+
if (!plan.length)
|
|
187
|
+
out.push("| | Nothing found | | | | |");
|
|
188
|
+
out.push("", coverage ?? "This report lists what the scanner found in source code and infrastructure config in the repository. It does not cover compiled dependencies, running services or certificates in use. benthic image checks container images, and benthic map checks websites, networks and machines.");
|
|
189
|
+
return out.join("\n");
|
|
190
|
+
}
|