@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/score.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Hard Keel quantum readiness score, method version 1.
|
|
4
|
+
// Turns a full-repository scan into a 0-100 score and a grade from A+ to F.
|
|
5
|
+
// The method is public: docs/readiness-score.md and hardkeel.com/learn/quantum-readiness-score/.
|
|
6
|
+
// Change a weight, cap or threshold only by bumping METHOD_VERSION and the docs together.
|
|
7
|
+
export const METHOD_VERSION = "1";
|
|
8
|
+
const GRADES = ["A+", "A", "B", "C", "D", "F"];
|
|
9
|
+
/** Points taken off for one kind of problem (one algorithm or setting), before the use multiplier. */
|
|
10
|
+
const BASE = {
|
|
11
|
+
secret: () => 25,
|
|
12
|
+
weak: (s) => (s === "critical" || s === "high" ? 10 : 6),
|
|
13
|
+
exposed: (s) => ({ critical: 12, high: 8, medium: 5, low: 2 })[s],
|
|
14
|
+
"quantum-key-exchange": () => 5,
|
|
15
|
+
"quantum-signature": () => 2,
|
|
16
|
+
};
|
|
17
|
+
/** Most a category can take off in total, so one area can't hide every other. */
|
|
18
|
+
const CAP = {
|
|
19
|
+
secret: 50,
|
|
20
|
+
weak: 30,
|
|
21
|
+
exposed: 30,
|
|
22
|
+
"quantum-key-exchange": 20,
|
|
23
|
+
"quantum-signature": 8,
|
|
24
|
+
};
|
|
25
|
+
/** Best grade a repository can get while it has any problem in this category. */
|
|
26
|
+
const GRADE_CAP = {
|
|
27
|
+
secret: "D",
|
|
28
|
+
weak: "C",
|
|
29
|
+
exposed: "C",
|
|
30
|
+
"quantum-key-exchange": "B",
|
|
31
|
+
"quantum-signature": "A",
|
|
32
|
+
};
|
|
33
|
+
const THRESHOLDS = [
|
|
34
|
+
[90, "A"],
|
|
35
|
+
[75, "B"],
|
|
36
|
+
[60, "C"],
|
|
37
|
+
[40, "D"],
|
|
38
|
+
[0, "F"],
|
|
39
|
+
];
|
|
40
|
+
export function categoryOf(f) {
|
|
41
|
+
if (f.risk === "secret")
|
|
42
|
+
return "secret";
|
|
43
|
+
if (f.risk === "weak-now")
|
|
44
|
+
return "weak";
|
|
45
|
+
if (f.risk === "exposed")
|
|
46
|
+
return "exposed";
|
|
47
|
+
return f.primitive === "signature" ? "quantum-signature" : "quantum-key-exchange";
|
|
48
|
+
}
|
|
49
|
+
/** 1 use counts once; more uses count more, up to double (16 or more uses). */
|
|
50
|
+
export function useMultiplier(uses) {
|
|
51
|
+
return Math.min(2, 1 + 0.25 * Math.log2(Math.max(1, uses)));
|
|
52
|
+
}
|
|
53
|
+
function worse(a, b) {
|
|
54
|
+
return GRADES[Math.max(GRADES.indexOf(a), GRADES.indexOf(b))];
|
|
55
|
+
}
|
|
56
|
+
export function readinessScore(findings, pqc = []) {
|
|
57
|
+
const pqcKex = pqc.some((p) => p.use === "key-establishment");
|
|
58
|
+
const pqcSig = pqc.some((p) => p.use === "signature");
|
|
59
|
+
// One deduction per category and algorithm (or setting), however many files use it.
|
|
60
|
+
const groups = new Map();
|
|
61
|
+
for (const f of findings) {
|
|
62
|
+
const k = `${categoryOf(f)}|${f.algorithm}`;
|
|
63
|
+
groups.set(k, [...(groups.get(k) ?? []), f]);
|
|
64
|
+
}
|
|
65
|
+
const deductions = [];
|
|
66
|
+
for (const g of groups.values()) {
|
|
67
|
+
const category = categoryOf(g[0]);
|
|
68
|
+
// A group is as serious as its most serious use.
|
|
69
|
+
const sev = g.map((f) => f.severity).sort((a, b) => sevRank(a) - sevRank(b))[0];
|
|
70
|
+
let points = BASE[category](sev) * useMultiplier(g.length);
|
|
71
|
+
// Credit for migration under way: half off where post-quantum methods are already in use.
|
|
72
|
+
if ((category === "quantum-key-exchange" && pqcKex) || (category === "quantum-signature" && pqcSig))
|
|
73
|
+
points /= 2;
|
|
74
|
+
deductions.push({ category, algorithm: g[0].algorithm, uses: g.length, points: round1(points) });
|
|
75
|
+
}
|
|
76
|
+
const counts = { secret: 0, weak: 0, exposed: 0, "quantum-key-exchange": 0, "quantum-signature": 0 };
|
|
77
|
+
const byCategory = { ...counts };
|
|
78
|
+
for (const d of deductions) {
|
|
79
|
+
counts[d.category] += d.uses;
|
|
80
|
+
byCategory[d.category] += d.points;
|
|
81
|
+
}
|
|
82
|
+
let total = 0;
|
|
83
|
+
for (const c of Object.keys(CAP))
|
|
84
|
+
total += Math.min(CAP[c], byCategory[c]);
|
|
85
|
+
const score = Math.max(0, Math.round(100 - total));
|
|
86
|
+
let grade = THRESHOLDS.find(([min]) => score >= min)[1];
|
|
87
|
+
for (const c of Object.keys(GRADE_CAP))
|
|
88
|
+
if (counts[c])
|
|
89
|
+
grade = worse(grade, GRADE_CAP[c]);
|
|
90
|
+
// A+ is for repositories with nothing left to fix that already use post-quantum methods.
|
|
91
|
+
if (grade === "A" && !findings.length && (pqcKex || pqcSig))
|
|
92
|
+
grade = "A+";
|
|
93
|
+
const due = counts.secret || counts.weak || counts.exposed ? "now" : counts["quantum-key-exchange"] ? "2030" : counts["quantum-signature"] ? "2035" : null;
|
|
94
|
+
return {
|
|
95
|
+
method: METHOD_VERSION,
|
|
96
|
+
score,
|
|
97
|
+
grade,
|
|
98
|
+
counts,
|
|
99
|
+
pqc: { keyExchange: pqcKex, signatures: pqcSig, algorithms: [...new Set(pqc.map((p) => p.algorithm))] },
|
|
100
|
+
due,
|
|
101
|
+
deductions: deductions.sort((a, b) => b.points - a.points),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function sevRank(s) {
|
|
105
|
+
return { critical: 0, high: 1, medium: 2, low: 3 }[s];
|
|
106
|
+
}
|
|
107
|
+
function round1(n) {
|
|
108
|
+
return Math.round(n * 10) / 10;
|
|
109
|
+
}
|
|
110
|
+
/** One plain sentence about the grade, for reports and the command line. */
|
|
111
|
+
export function gradeSentence(s) {
|
|
112
|
+
const lead = `Quantum readiness: ${s.grade} (${s.score}/100, Hard Keel method v${s.method}).`;
|
|
113
|
+
if (s.grade === "A+")
|
|
114
|
+
return `${lead} Nothing to fix, and post-quantum encryption is already in use.`;
|
|
115
|
+
if (s.due === null)
|
|
116
|
+
return `${lead} Nothing to fix.`;
|
|
117
|
+
if (s.counts.secret)
|
|
118
|
+
return `${lead} A leaked key or password holds the grade at D or lower until it is revoked and removed.`;
|
|
119
|
+
if (s.due === "now")
|
|
120
|
+
return `${lead} Weak encryption or exposed settings hold the grade at C or lower until they are fixed.`;
|
|
121
|
+
if (s.due === "2030")
|
|
122
|
+
return `${lead} Key exchange or RSA keys that quantum computers will break hold the grade at B until they move to post-quantum methods.`;
|
|
123
|
+
return `${lead} Only signatures are left to move to post-quantum methods, before 2035.`;
|
|
124
|
+
}
|
package/dist/secrets.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Leaked-credential detection. A bonus check that rides along with the crypto scan.
|
|
4
|
+
// Files where settings are written unquoted, one per line: .env files, shell scripts, ini and properties files.
|
|
5
|
+
// Dockerfile ENV and ARG lines are checked with the rest of the Dockerfile settings.
|
|
6
|
+
export const ENV_STYLE_FILE = /(^|\/)\.env(\.[A-Za-z0-9_-]+)?$|(^|\/)\.envrc$|\.(sh|bash|zsh|ksh|ini|cfg|conf|cnf|properties|env)$/i;
|
|
7
|
+
export const SECRET_RULES = [
|
|
8
|
+
{ id: "private-key", name: "Private key", pattern: /-----BEGIN (?:RSA |EC |DSA |OPENSSH |ENCRYPTED |PGP )?PRIVATE KEY(?: BLOCK)?-----/ },
|
|
9
|
+
{ id: "aws-access-key-id", name: "AWS access key", pattern: /\b((?:AKIA|ASIA)[0-9A-Z]{16})\b/, group: 1 },
|
|
10
|
+
{ id: "aws-secret-access-key", name: "AWS secret access key", pattern: /\b[A-Za-z0-9_]*(?:aws[A-Za-z0-9_]*secret|secret[_-]?access[_-]?key)[A-Za-z0-9_]*["']?\s*[:=]\s*["']?([A-Za-z0-9\/+]{40})(?![A-Za-z0-9\/+=])/i, group: 1, minEntropy: 4 },
|
|
11
|
+
{ id: "github-token", name: "GitHub token", pattern: /\b(gh[pousr]_[A-Za-z0-9]{36,255})\b/, group: 1 },
|
|
12
|
+
{ id: "github-fine-grained-pat", name: "GitHub token", pattern: /\b(github_pat_[A-Za-z0-9_]{60,255})\b/, group: 1 },
|
|
13
|
+
{ id: "gitlab-pat", name: "GitLab token", pattern: /\b(glpat-[A-Za-z0-9_-]{20,})\b/, group: 1 },
|
|
14
|
+
{ id: "slack-token", name: "Slack token", pattern: /\b(xox[baprs]-[A-Za-z0-9-]{10,})\b/, group: 1 },
|
|
15
|
+
{ id: "slack-webhook", name: "Slack webhook", pattern: /(https:\/\/hooks\.slack\.com\/services\/T[A-Z0-9]+\/B[A-Z0-9]+\/[A-Za-z0-9]+)/, group: 1 },
|
|
16
|
+
{ id: "stripe-secret", name: "Stripe secret key", pattern: /\b((?:sk|rk)_live_[A-Za-z0-9]{20,})\b/, group: 1 },
|
|
17
|
+
{ id: "google-api-key", name: "Google API key", pattern: /\b(AIza[0-9A-Za-z_-]{35})\b/, group: 1 },
|
|
18
|
+
{ id: "anthropic-key", name: "Anthropic API key", pattern: /\b(sk-ant-[A-Za-z0-9_-]{32,})\b/, group: 1 },
|
|
19
|
+
{ id: "openai-key", name: "OpenAI API key", pattern: /\b(sk-(?:proj|svcacct|admin)-[A-Za-z0-9_-]{32,})\b/, group: 1 },
|
|
20
|
+
{ id: "sendgrid-key", name: "SendGrid key", pattern: /\b(SG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43})\b/, group: 1 },
|
|
21
|
+
{ id: "npm-token", name: "npm token", pattern: /\b(npm_[A-Za-z0-9]{36})\b/, group: 1 },
|
|
22
|
+
{ id: "db-url-password", name: "Database password in URL", pattern: /\b(?:postgres(?:ql)?|mysql|mongodb(?:\+srv)?|redis|amqp):\/\/[^:\s/'"]+:([^@\s'"]{6,})@/, group: 1, minEntropy: 2.5 },
|
|
23
|
+
{
|
|
24
|
+
id: "generic-assignment",
|
|
25
|
+
name: "Hardcoded secret",
|
|
26
|
+
pattern: /\b[A-Za-z0-9_]*(?:api[_-]?key|secret|token|passwd|password|private[_-]?key)[A-Za-z0-9_]*["']?\s*[:=]\s*["']([^"'\s]{12,})["']/i,
|
|
27
|
+
group: 1,
|
|
28
|
+
minEntropy: 3.5,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
// Unquoted values in settings files and scripts: DB_PASSWORD=Sup3rS3cret99, export API_TOKEN=...
|
|
32
|
+
id: "env-assignment",
|
|
33
|
+
name: "Hardcoded secret",
|
|
34
|
+
pattern: /^\s*(?:export\s+)?([A-Za-z0-9_.-]*(?:api[_-]?key|secret|token|passwd|password|private[_-]?key)[A-Za-z0-9_.-]*)\s*=\s*([^\s"'`$#;|&<>(){}\[\]]{12,})\s*(?:[#;].*)?$/i,
|
|
35
|
+
group: 2,
|
|
36
|
+
minEntropy: 3,
|
|
37
|
+
files: ENV_STYLE_FILE,
|
|
38
|
+
},
|
|
39
|
+
];
|
|
40
|
+
const PLACEHOLDER = /(example|sample|dummy|changeme|change_me|placeholder|your[_-]|^your|xxxx|\*\*\*\*|<[^>]+>|\$\{|\{\{|process\.env|os\.environ|getenv|redacted|test)/i;
|
|
41
|
+
// Documentation tokens like "xoxb-1234567890-abcdefghij".
|
|
42
|
+
const FAKE_SEQUENCE = /1234567890|0123456789|abcdefghij|zyxwvutsrq/;
|
|
43
|
+
// Names that hold something about a secret, not the secret itself (SECRET_KEY_PREFIX, password_template).
|
|
44
|
+
// These words count only as the last word of the name, so DEPLOY_REPO_TOKEN is still a token.
|
|
45
|
+
const NOT_A_SECRET_LAST_WORD = new Set(["template", "prefix", "suffix", "header", "headers", "name", "field", "path", "url", "label", "message", "msg", "type", "format", "pattern", "regex", "uid", "class", "checksum", "param", "params", "repo"]);
|
|
46
|
+
const NOT_A_SECRET_ANYWHERE = /(tokeniz|hashed|token_?(?:roles_?)?key|invalid|not_?found|expir|permission|^\W*has_|req_?(?:query|body)|_type_|_name_|_path_|_url_)/i;
|
|
47
|
+
/** Whether a variable or setting name is about a secret (its name, where it lives) rather than holding one. */
|
|
48
|
+
export function notASecretName(name) {
|
|
49
|
+
const clean = name.replace(/^\W+|\W+$/g, "");
|
|
50
|
+
if (NOT_A_SECRET_ANYWHERE.test(clean) || NOT_A_SECRET_ANYWHERE.test(clean.replace(/([a-z0-9])([A-Z])/g, "$1_$2")))
|
|
51
|
+
return true;
|
|
52
|
+
const words = clean.replace(/([a-z0-9])([A-Z])/g, "$1_$2").split(/[^A-Za-z0-9]+/).filter(Boolean);
|
|
53
|
+
return words.length > 1 && NOT_A_SECRET_LAST_WORD.has(words[words.length - 1].toLowerCase());
|
|
54
|
+
}
|
|
55
|
+
/** The generic "name = value" rule is noisy; drop the shapes that are almost never real secrets. */
|
|
56
|
+
function looksLikeSecretAssignment(match, value, line) {
|
|
57
|
+
const name = match.split(/["']?\s*[:=]/)[0].replace(/^\s*export\s+/, "");
|
|
58
|
+
if (notASecretName(name))
|
|
59
|
+
return false;
|
|
60
|
+
if (/^\s*(>>>|\.\.\.)\s/.test(line))
|
|
61
|
+
return false; // doctest example
|
|
62
|
+
if (/^[a-z_-]+$/.test(value))
|
|
63
|
+
return false; // an identifier, like "userauth-gssapi-token"
|
|
64
|
+
if (/^[a-z][a-z0-9]*(?:_[a-z0-9]+){2,}$/.test(value))
|
|
65
|
+
return false; // a snake_case name, like the table "oauth2_access_token_session"
|
|
66
|
+
if (/\b(?:nosec|nolint:gosec|gitleaks:allow|pragma: allowlist secret|NOSONAR)\b/.test(line))
|
|
67
|
+
return false; // the author marked it as not a secret
|
|
68
|
+
if (value.toLowerCase().startsWith(name.replace(/^\W+/, "").replace(/_/g, "-").toLowerCase()))
|
|
69
|
+
return false; // a constant named for its own value, like a header name
|
|
70
|
+
if (!/^[\x21-\x7e]+$/.test(value))
|
|
71
|
+
return false; // text in another script, like a translated "Password" label
|
|
72
|
+
if (!/\d/.test(value) && value.length < 32)
|
|
73
|
+
return false;
|
|
74
|
+
if (!/\d/.test(value) && /^[A-Za-z]+(?:[:._/-]+[A-Za-z]+)+$/.test(value))
|
|
75
|
+
return false; // a long name made of words, like "urn:ietf:params:oauth:grant-type:token-exchange" or a scope // words, like "excludedRequestDomains:", not a random value
|
|
76
|
+
if (/^\$?[a-z0-9_]+\$/.test(value))
|
|
77
|
+
return false; // a stored password hash, like "pbkdf2_sha256$..." or "$argon2id$..."
|
|
78
|
+
if (/\$\{?[A-Za-z_]|\$\(|\{\}|%[sd]|^var\(/.test(value))
|
|
79
|
+
return false; // interpolation or a format string, like "$user:$password" or "$(Build.Secret)"
|
|
80
|
+
if (/^(bao|vault|secret|op|sm|ref\+[a-z]+|arn):/i.test(value))
|
|
81
|
+
return false; // a reference to a secret store, not the secret
|
|
82
|
+
if (/^(phc_|pk_(live|test)_)/.test(value))
|
|
83
|
+
return false; // a public client key (PostHog, Stripe publishable), safe to ship
|
|
84
|
+
if (/^[([^]/.test(value))
|
|
85
|
+
return false; // a regular expression
|
|
86
|
+
if (/\\(x[0-9a-f]{2}|u[0-9a-f]{4}|0)/i.test(value))
|
|
87
|
+
return false; // a binary magic string, like "openssh-key-v1\x00"
|
|
88
|
+
if (/^[a-z]+(?:[A-Z][a-z]*)+$/.test(value))
|
|
89
|
+
return false; // a camelCase identifier, like "challengePassword"
|
|
90
|
+
if (/\{\{/.test(line))
|
|
91
|
+
return false; // a Helm or Go template line
|
|
92
|
+
if (/^[a-z][A-Za-z0-9_-]*(\.[A-Za-z0-9_-]+)+$/.test(value))
|
|
93
|
+
return false; // a settings key, like "webhook.github.secret"
|
|
94
|
+
if (/^[A-Z][A-Z0-9_]{5,}$/.test(value))
|
|
95
|
+
return false; // the name of an environment variable, like "ARGOCD_AUTH_TOKEN"
|
|
96
|
+
if (/^(\/|\.{1,2}\/|~\/|[A-Za-z]:\\)/.test(value))
|
|
97
|
+
return false; // a file path, like "/var/run/secrets/token"
|
|
98
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(value))
|
|
99
|
+
return false; // a URL, like "https://oauth2.googleapis.com/token"
|
|
100
|
+
if (/^0x[0-9a-fA-F]{40}$/.test(value))
|
|
101
|
+
return false; // a blockchain address, like a token contract's: public by design
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
export function entropy(s) {
|
|
105
|
+
const counts = new Map();
|
|
106
|
+
for (const ch of s)
|
|
107
|
+
counts.set(ch, (counts.get(ch) ?? 0) + 1);
|
|
108
|
+
let h = 0;
|
|
109
|
+
for (const c of counts.values()) {
|
|
110
|
+
const p = c / s.length;
|
|
111
|
+
h -= p * Math.log2(p);
|
|
112
|
+
}
|
|
113
|
+
return h;
|
|
114
|
+
}
|
|
115
|
+
// Token prefixes that are public (they only say which service issued the token), so a masked value can keep them.
|
|
116
|
+
const PUBLIC_PREFIX = /^(AKIA|ASIA|AGPA|AIDA|AROA|gh[pousr]_|github_pat_|glpat-|xox[baprs]-|(?:sk|rk)_live_|AIza|sk-ant-|sk-(?:proj|svcacct|admin)-|SG\.|npm_|https:\/\/hooks\.slack\.com\/services\/)/;
|
|
117
|
+
/** A secret with nothing of its own left: only a public prefix like AKIA or ghp_ stays, so a short password gives away nothing. */
|
|
118
|
+
export function mask(value) {
|
|
119
|
+
const prefix = value.match(PUBLIC_PREFIX)?.[0] ?? "";
|
|
120
|
+
return `${prefix}${"*".repeat(12)}`;
|
|
121
|
+
}
|
|
122
|
+
const PEM_PRIVATE = /-----BEGIN (?:RSA |EC |DSA |OPENSSH |ENCRYPTED |PGP )?PRIVATE KEY(?: BLOCK)?-----/;
|
|
123
|
+
const GLOBAL_RULES = new Map();
|
|
124
|
+
function globalPattern(rule) {
|
|
125
|
+
let g = GLOBAL_RULES.get(rule);
|
|
126
|
+
if (!g)
|
|
127
|
+
GLOBAL_RULES.set(rule, (g = new RegExp(rule.pattern.source, rule.pattern.flags.includes("g") ? rule.pattern.flags : rule.pattern.flags + "g")));
|
|
128
|
+
g.lastIndex = 0;
|
|
129
|
+
return g;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* A line with every secret on it masked, for any output that shows source text. Every match of every rule is
|
|
133
|
+
* masked, whatever the filters decided, and everything after a private key header is cut, since the key's
|
|
134
|
+
* body often follows it on the same line ("-----BEGIN PRIVATE KEY-----\nMIIE...").
|
|
135
|
+
*/
|
|
136
|
+
export function redactSecrets(line) {
|
|
137
|
+
let out = line;
|
|
138
|
+
const pem = out.match(PEM_PRIVATE);
|
|
139
|
+
if (pem)
|
|
140
|
+
out = `${out.slice(0, (pem.index ?? 0) + pem[0].length)} [key material removed]`;
|
|
141
|
+
for (const rule of SECRET_RULES) {
|
|
142
|
+
if (rule.id === "private-key")
|
|
143
|
+
continue;
|
|
144
|
+
const values = new Set();
|
|
145
|
+
for (const m of out.matchAll(globalPattern(rule))) {
|
|
146
|
+
const v = m[rule.group ?? 0];
|
|
147
|
+
if (v && v.length >= 6)
|
|
148
|
+
values.add(v);
|
|
149
|
+
}
|
|
150
|
+
for (const v of [...values].sort((a, b) => b.length - a.length))
|
|
151
|
+
out = out.split(v).join(mask(v));
|
|
152
|
+
}
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
/** Rules that are a service's own token format: they count anywhere, test code included. */
|
|
156
|
+
export const PROVIDER_RULES = new Set(SECRET_RULES.filter((r) => !["private-key", "generic-assignment", "env-assignment", "db-url-password"].includes(r.id)).map((r) => r.id));
|
|
157
|
+
export function findSecrets(line, path = "", only) {
|
|
158
|
+
const hits = [];
|
|
159
|
+
for (const rule of SECRET_RULES) {
|
|
160
|
+
if (only && !only.has(rule.id))
|
|
161
|
+
continue;
|
|
162
|
+
if (rule.files && !rule.files.test(path))
|
|
163
|
+
continue;
|
|
164
|
+
if (!rule.pattern.test(line))
|
|
165
|
+
continue;
|
|
166
|
+
const matches = rule.id === "private-key" ? [line.match(rule.pattern)] : [...line.matchAll(globalPattern(rule))];
|
|
167
|
+
const seen = new Set();
|
|
168
|
+
for (const m of matches) {
|
|
169
|
+
const value = m[rule.group ?? 0] ?? m[0];
|
|
170
|
+
if (seen.has(value))
|
|
171
|
+
continue;
|
|
172
|
+
seen.add(value);
|
|
173
|
+
// A placeholder next to a real key on the same line doesn't hide the real one, so each value is checked on its own.
|
|
174
|
+
if (rule.id !== "private-key" && (PLACEHOLDER.test(value) || FAKE_SEQUENCE.test(value)))
|
|
175
|
+
continue;
|
|
176
|
+
if (rule.minEntropy !== undefined && entropy(value) < rule.minEntropy)
|
|
177
|
+
continue;
|
|
178
|
+
if ((rule.id === "generic-assignment" || rule.id === "env-assignment") && !looksLikeSecretAssignment(m[0], value, line))
|
|
179
|
+
continue;
|
|
180
|
+
if (rule.id === "db-url-password" && /^(pass(word)?|passwd|pwd|secret)$/i.test(value))
|
|
181
|
+
continue;
|
|
182
|
+
// A database on this machine (CI services, local dev) has a throwaway password.
|
|
183
|
+
if (rule.id === "db-url-password" && /@(localhost|127\.0\.0\.1|\[::1\])[:/]/.test(m[0] + line.slice((m.index ?? 0) + m[0].length, (m.index ?? 0) + m[0].length + 12)))
|
|
184
|
+
continue;
|
|
185
|
+
hits.push({ rule, value });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// The generic rules only count when nothing specific matched the line.
|
|
189
|
+
const generic = (h) => h.rule.id === "generic-assignment" || h.rule.id === "env-assignment";
|
|
190
|
+
const specific = hits.filter((h) => !generic(h));
|
|
191
|
+
return specific.length ? specific : hits;
|
|
192
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Local settings for the command-line version: the random install ID, whether anonymized sharing is on,
|
|
4
|
+
// offline mode, the optional industry and size, and what the last run would have sent.
|
|
5
|
+
// Kept in one JSON file in the user's settings folder. Nothing here leaves the machine except
|
|
6
|
+
// through share.ts, and only the fields that file builds.
|
|
7
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
8
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { homedir } from "node:os";
|
|
10
|
+
import { dirname, join, resolve } from "node:path";
|
|
11
|
+
import { inCi } from "./share.js";
|
|
12
|
+
export { inCi };
|
|
13
|
+
// The two lists live in share-lists.ts, which has no Node imports, so the service can load them too.
|
|
14
|
+
import { INDUSTRIES, SIZES } from "./share-lists.js";
|
|
15
|
+
export { INDUSTRIES, SIZES };
|
|
16
|
+
/**
|
|
17
|
+
* A setting from the environment. BENTHIC_<name> first, then QUANTUM_SCAN_<name>, the variable's name
|
|
18
|
+
* before the scanner was called Benthic, so pipelines set up then keep working.
|
|
19
|
+
*/
|
|
20
|
+
export function envSetting(name, env = process.env) {
|
|
21
|
+
return env[`BENTHIC_${name}`] || env[`QUANTUM_SCAN_${name}`] || undefined;
|
|
22
|
+
}
|
|
23
|
+
function settingsFile(name, env, platform) {
|
|
24
|
+
if (platform === "win32" && env.APPDATA)
|
|
25
|
+
return join(env.APPDATA, "Hard Keel", name);
|
|
26
|
+
if (platform === "darwin")
|
|
27
|
+
return join(homedir(), "Library", "Application Support", "Hard Keel", name);
|
|
28
|
+
return join(env.XDG_CONFIG_HOME || join(homedir(), ".config"), "hardkeel", name);
|
|
29
|
+
}
|
|
30
|
+
/** Where settings are saved. */
|
|
31
|
+
export function settingsPath(env = process.env, platform = process.platform) {
|
|
32
|
+
const set = envSetting("CONFIG", env);
|
|
33
|
+
return set ? resolve(set) : settingsFile("benthic.json", env, platform);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Where settings are read from: the saved file, or until one is saved under the new name, the one saved
|
|
37
|
+
* before the scanner was called Benthic, so an opt-out, offline mode and the install id carry over.
|
|
38
|
+
*/
|
|
39
|
+
export function settingsReadPath(env = process.env, platform = process.platform) {
|
|
40
|
+
const path = settingsPath(env, platform);
|
|
41
|
+
if (envSetting("CONFIG", env) || existsSync(path))
|
|
42
|
+
return path;
|
|
43
|
+
const before = settingsFile("quantum-scan.json", env, platform);
|
|
44
|
+
return existsSync(before) ? before : path;
|
|
45
|
+
}
|
|
46
|
+
function fresh() {
|
|
47
|
+
return {
|
|
48
|
+
id: randomBytes(16).toString("hex"),
|
|
49
|
+
sharing: "on",
|
|
50
|
+
offline: false,
|
|
51
|
+
noticeShown: false,
|
|
52
|
+
ownershipToken: randomBytes(12).toString("hex"),
|
|
53
|
+
state: {},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function loadSettings(path = settingsReadPath()) {
|
|
57
|
+
const base = fresh();
|
|
58
|
+
if (!existsSync(path))
|
|
59
|
+
return base;
|
|
60
|
+
try {
|
|
61
|
+
const raw = JSON.parse(readFileSync(path, "utf8"));
|
|
62
|
+
return {
|
|
63
|
+
...base,
|
|
64
|
+
...raw,
|
|
65
|
+
id: typeof raw.id === "string" && /^[a-f0-9]{32}$/.test(raw.id) ? raw.id : base.id,
|
|
66
|
+
ownershipToken: typeof raw.ownershipToken === "string" && /^[a-f0-9]{24}$/.test(raw.ownershipToken) ? raw.ownershipToken : base.ownershipToken,
|
|
67
|
+
industry: INDUSTRIES.includes(raw.industry) ? raw.industry : undefined,
|
|
68
|
+
size: SIZES.includes(raw.size) ? raw.size : undefined,
|
|
69
|
+
state: raw.state && typeof raw.state === "object" ? raw.state : {},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return base;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Saves settings. Never throws: a read-only home folder must not break a scan. */
|
|
77
|
+
export function saveSettings(s, path = settingsPath()) {
|
|
78
|
+
try {
|
|
79
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
80
|
+
const tmp = `${path}.${process.pid}.tmp`;
|
|
81
|
+
writeFileSync(tmp, JSON.stringify(s, null, 2) + "\n", { mode: 0o600 });
|
|
82
|
+
renameSync(tmp, path);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// ignore
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
export function folderKey(folder) {
|
|
89
|
+
return createHash("sha256").update(resolve(folder)).digest("hex").slice(0, 16);
|
|
90
|
+
}
|
|
91
|
+
const FALSE = /^(0|false|no|off)$/i;
|
|
92
|
+
/**
|
|
93
|
+
* Whether this run may send its summary, and why not. In a pipeline nothing is sent unless
|
|
94
|
+
* BENTHIC_SHARING=on turns it on: an outbound call nobody approved can block a build's sign-off.
|
|
95
|
+
*/
|
|
96
|
+
export function sharingState(s, opts, env = process.env) {
|
|
97
|
+
if (opts.offline || s.offline || /^(1|true|yes|on)$/i.test(envSetting("OFFLINE", env) ?? ""))
|
|
98
|
+
return "offline";
|
|
99
|
+
if (FALSE.test(envSetting("SHARING", env) ?? ""))
|
|
100
|
+
return "env-off";
|
|
101
|
+
if (env.DO_NOT_TRACK && env.DO_NOT_TRACK !== "0")
|
|
102
|
+
return "do-not-track";
|
|
103
|
+
if (inCi(env) && !/^(1|true|yes|on)$/i.test(envSetting("SHARING", env) ?? ""))
|
|
104
|
+
return "ci";
|
|
105
|
+
return s.sharing === "off" ? "off" : "on";
|
|
106
|
+
}
|
|
107
|
+
export const NOTICE = `Hard Keel Benthic shares anonymized, aggregated usage stats about how its
|
|
108
|
+
checks perform and what kinds of encryption they find, to improve our products.
|
|
109
|
+
It never includes your code, keys, files, server names, IP addresses or company name.
|
|
110
|
+
|
|
111
|
+
See what's sent: benthic config show-shared
|
|
112
|
+
Turn it off: benthic config sharing off (or BENTHIC_SHARING=off)
|
|
113
|
+
In CI pipelines: off unless BENTHIC_SHARING=on
|
|
114
|
+
Send nothing at all: --offline (or BENTHIC_OFFLINE=1)
|
|
115
|
+
Details: https://hardkeel.com/privacy/
|
|
116
|
+
`;
|
|
117
|
+
/**
|
|
118
|
+
* Prints the notice the first time, before anything is sent. In automated pipelines that turned
|
|
119
|
+
* sharing on, where settings may not survive between runs, it prints every run, at the top of the log.
|
|
120
|
+
*/
|
|
121
|
+
export function showNoticeIfNeeded(s, state, env = process.env, write = (t) => process.stderr.write(t)) {
|
|
122
|
+
if (state !== "on")
|
|
123
|
+
return false;
|
|
124
|
+
if (s.noticeShown && !inCi(env))
|
|
125
|
+
return false;
|
|
126
|
+
write(`\n${NOTICE}\n`);
|
|
127
|
+
s.noticeShown = true;
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// The industry and size choices for anonymized sharing. No imports, so both the command-line version
|
|
4
|
+
// and the service load the same lists.
|
|
5
|
+
export const INDUSTRIES = [
|
|
6
|
+
"finance",
|
|
7
|
+
"healthcare",
|
|
8
|
+
"government",
|
|
9
|
+
"defense",
|
|
10
|
+
"technology",
|
|
11
|
+
"telecom",
|
|
12
|
+
"energy-utilities",
|
|
13
|
+
"manufacturing",
|
|
14
|
+
"retail",
|
|
15
|
+
"education",
|
|
16
|
+
"professional-services",
|
|
17
|
+
"other",
|
|
18
|
+
];
|
|
19
|
+
export const SIZES = ["1-50", "51-250", "251-1000", "1000+"];
|
package/dist/share.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Builds and sends the anonymized summary of a run. Counts are rounded into bands and every name is
|
|
4
|
+
// checked against a fixed list, so nothing taken from the scanned code or systems can reach us.
|
|
5
|
+
// What is sent is exactly what buildPayload returns; `benthic config show-shared` prints it.
|
|
6
|
+
import { ALGORITHMS } from "./rules.js";
|
|
7
|
+
/** Whether this run is in an automated pipeline: GitHub Actions, or any CI system that sets CI. */
|
|
8
|
+
export function inCi(env = process.env) {
|
|
9
|
+
const off = /^(0|false|no|off)$/i;
|
|
10
|
+
return Boolean((env.CI && !off.test(env.CI)) || (env.GITHUB_ACTIONS && !off.test(env.GITHUB_ACTIONS)));
|
|
11
|
+
}
|
|
12
|
+
export const SHARE_URL = "https://scan.hardkeel.com/v1/share";
|
|
13
|
+
export const PAYLOAD_VERSION = 1;
|
|
14
|
+
export const BANDS = ["1", "2-5", "6-20", "21-100", "100+"];
|
|
15
|
+
export function band(n) {
|
|
16
|
+
if (!Number.isFinite(n) || n < 1)
|
|
17
|
+
return null;
|
|
18
|
+
if (n === 1)
|
|
19
|
+
return "1";
|
|
20
|
+
if (n <= 5)
|
|
21
|
+
return "2-5";
|
|
22
|
+
if (n <= 20)
|
|
23
|
+
return "6-20";
|
|
24
|
+
if (n <= 100)
|
|
25
|
+
return "21-100";
|
|
26
|
+
return "100+";
|
|
27
|
+
}
|
|
28
|
+
export const AREAS = ["code", "web", "host", "network", "vpn", "cloud"];
|
|
29
|
+
/** Error kinds a run may report. Messages are never sent, only these labels. */
|
|
30
|
+
export const ERROR_KINDS = ["timeout", "refused", "dns", "tls", "ssh", "read", "parse", "permission", "other"];
|
|
31
|
+
/** Post-quantum algorithms we credit. Anything else is dropped. */
|
|
32
|
+
export const PQC_NAMES = ["ML-KEM", "Hybrid ML-KEM key exchange", "ML-DSA", "SLH-DSA", "FN-DSA", "Hybrid SSH key exchange"];
|
|
33
|
+
/** Extra algorithm names the encryption map uses beyond the code rules. */
|
|
34
|
+
export const MAP_ALGORITHMS = ["TLS key exchange", "SSH key exchange", "SSH host key", "TLS certificate", "Static RSA key exchange", "SSH weak settings", "Expired certificate", "Certificate name mismatch", "Untrusted certificate", "Self-signed certificate", "Unsigned sign-in tokens",
|
|
35
|
+
// Cloud account settings (cloud-*.ts).
|
|
36
|
+
"Expiring certificate", "Weak TLS ciphers", "Unencrypted traffic allowed", "Encryption at rest off",
|
|
37
|
+
// Network and connected equipment (map-network.ts NET_RULES; listed here so the Worker needn't load network code).
|
|
38
|
+
"Telnet", "Plain FTP", "Remote Desktop open", "VNC open", "Remote support tool", "Setup page open", "Admin page over plain HTTP", "Vendor default certificate", "Open print port", "LPD printing", "Camera stream port", "DVR service port", "MQTT without encryption", "Docker API open", "Docker API port open", "Database open on the network", "UPnP on the router", "Router forward from the internet"];
|
|
39
|
+
/** Algorithm names a summary may carry, besides "other". The service checks against the same set. */
|
|
40
|
+
export const KNOWN_ALGORITHMS = new Set([...Object.keys(ALGORITHMS), ...MAP_ALGORITHMS, ...PQC_NAMES]);
|
|
41
|
+
/** The shape of a rule id: lower case, digits and hyphens. No spaces, dots or slashes, so no host names or addresses. */
|
|
42
|
+
export const RULE_ID = /^[a-z0-9][a-z0-9-]{0,59}$/;
|
|
43
|
+
function bandMap(src, keyOk) {
|
|
44
|
+
const out = {};
|
|
45
|
+
for (const [k, n] of Object.entries(src)) {
|
|
46
|
+
const b = band(n);
|
|
47
|
+
if (b && keyOk(k))
|
|
48
|
+
out[k] = b;
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
export function buildPayload(facts, s, toolVersion, env = process.env, platform = process.platform) {
|
|
53
|
+
const ruleOk = (k) => RULE_ID.test(k);
|
|
54
|
+
const algorithms = {};
|
|
55
|
+
const other = { count: 0, quantumVulnerable: 0 };
|
|
56
|
+
for (const [name, v] of Object.entries(facts.algorithms)) {
|
|
57
|
+
const target = KNOWN_ALGORITHMS.has(name) ? name : null;
|
|
58
|
+
if (!target) {
|
|
59
|
+
other.count += v.count;
|
|
60
|
+
other.quantumVulnerable += v.quantumVulnerable;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const n = band(v.count);
|
|
64
|
+
if (n)
|
|
65
|
+
algorithms[target] = { n, qv: band(v.quantumVulnerable) ?? "0" };
|
|
66
|
+
}
|
|
67
|
+
const on = band(other.count);
|
|
68
|
+
if (on)
|
|
69
|
+
algorithms.other = { n: on, qv: band(other.quantumVulnerable) ?? "0" };
|
|
70
|
+
const pqcOk = (k) => PQC_NAMES.includes(k);
|
|
71
|
+
const payload = {
|
|
72
|
+
v: PAYLOAD_VERSION,
|
|
73
|
+
id: s.id,
|
|
74
|
+
tool: /^\d+\.\d+\.\d+$/.test(toolVersion) ? toolVersion : "0.0.0",
|
|
75
|
+
os: platform === "linux" || platform === "darwin" || platform === "win32" ? platform : "other",
|
|
76
|
+
ci: inCi(env),
|
|
77
|
+
command: facts.command,
|
|
78
|
+
areas: AREAS.filter((a) => facts.areas.includes(a)),
|
|
79
|
+
rules: bandMap(facts.rules, ruleOk),
|
|
80
|
+
fixed: bandMap(facts.fixed, ruleOk),
|
|
81
|
+
ignored: bandMap(facts.ignored, ruleOk),
|
|
82
|
+
algorithms,
|
|
83
|
+
quantumSafe: bandMap(facts.quantumSafe, pqcOk),
|
|
84
|
+
replacements: bandMap(facts.replacements, pqcOk),
|
|
85
|
+
errors: ERROR_KINDS.filter((e) => facts.errors.includes(e)),
|
|
86
|
+
};
|
|
87
|
+
if (s.industry)
|
|
88
|
+
payload.industry = s.industry;
|
|
89
|
+
if (s.size)
|
|
90
|
+
payload.size = s.size;
|
|
91
|
+
return payload;
|
|
92
|
+
}
|
|
93
|
+
/** Sends the summary. Never throws and never waits more than a few seconds. */
|
|
94
|
+
export async function sendPayload(p, url = SHARE_URL, timeoutMs = 3000) {
|
|
95
|
+
try {
|
|
96
|
+
const res = await fetch(url, {
|
|
97
|
+
method: "POST",
|
|
98
|
+
headers: { "content-type": "application/json", "user-agent": `hardkeel-benthic/${p.tool}` },
|
|
99
|
+
body: JSON.stringify(p),
|
|
100
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
101
|
+
});
|
|
102
|
+
return res.ok;
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/** Compares this run with the last one in the same folder: what went away, and what post-quantum use is new. */
|
|
109
|
+
export function diffRuns(prev, now) {
|
|
110
|
+
const fixed = {};
|
|
111
|
+
const replacements = {};
|
|
112
|
+
if (!prev)
|
|
113
|
+
return { fixed, replacements };
|
|
114
|
+
for (const [rule, n] of Object.entries(prev.rules)) {
|
|
115
|
+
const gone = n - (now.rules[rule] ?? 0);
|
|
116
|
+
if (gone > 0)
|
|
117
|
+
fixed[rule] = gone;
|
|
118
|
+
}
|
|
119
|
+
for (const [alg, n] of Object.entries(now.pqc)) {
|
|
120
|
+
const added = n - (prev.pqc[alg] ?? 0);
|
|
121
|
+
if (added > 0)
|
|
122
|
+
replacements[alg] = added;
|
|
123
|
+
}
|
|
124
|
+
return { fixed, replacements };
|
|
125
|
+
}
|