@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/sonar.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// SonarQube and SonarCloud: findings in Sonar's generic external issue format (SonarQube Server 10.3
|
|
4
|
+
// and later, SonarQube Cloud), read with sonar.externalIssuesReportPaths. Each kind of problem is a
|
|
5
|
+
// rule, with both the older type and severity and the newer impacts, so it shows in either mode.
|
|
6
|
+
//
|
|
7
|
+
// benthic . --format sonar --output benthic-sonar.json
|
|
8
|
+
// sonar-scanner -Dsonar.externalIssuesReportPaths=benthic-sonar.json
|
|
9
|
+
//
|
|
10
|
+
// Paths are relative to the scanned folder (or to the repository root when that is above it), so run
|
|
11
|
+
// the scan from the folder Sonar uses as sonar.projectBaseDir.
|
|
12
|
+
import { violationLabel } from "./policy.js";
|
|
13
|
+
import { ruleId } from "./sarif.js";
|
|
14
|
+
import { shortLabel } from "./report.js";
|
|
15
|
+
const ENGINE = "hard-keel-benthic";
|
|
16
|
+
const SEVERITY = {
|
|
17
|
+
critical: { old: "BLOCKER", impact: "BLOCKER" },
|
|
18
|
+
high: { old: "CRITICAL", impact: "HIGH" },
|
|
19
|
+
medium: { old: "MAJOR", impact: "MEDIUM" },
|
|
20
|
+
low: { old: "MINOR", impact: "LOW" },
|
|
21
|
+
};
|
|
22
|
+
// Rough minutes to fix: a leaked key takes rotating; a quantum migration takes planning.
|
|
23
|
+
const EFFORT = { secret: 30, "weak-now": 30, exposed: 15, "quantum-vulnerable": 60 };
|
|
24
|
+
function path(prefix, file) {
|
|
25
|
+
const clean = (s) => s.replace(/\\/g, "/").replace(/^(\.\/)+/, "").replace(/\/+$/, "");
|
|
26
|
+
const pre = clean(prefix);
|
|
27
|
+
return pre && pre !== "." ? `${pre}/${clean(file)}` : clean(file);
|
|
28
|
+
}
|
|
29
|
+
const range = (line) => (line > 0 ? { textRange: { startLine: line } } : {});
|
|
30
|
+
export function buildSonar(findings, opts = {}) {
|
|
31
|
+
const pre = opts.pathPrefix ?? "";
|
|
32
|
+
const rules = new Map();
|
|
33
|
+
const rank = (s) => ["low", "medium", "high", "critical"].indexOf(s);
|
|
34
|
+
const worst = new Map();
|
|
35
|
+
for (const f of findings) {
|
|
36
|
+
const id = ruleId(f);
|
|
37
|
+
const w = worst.get(id);
|
|
38
|
+
if (!w || rank(f.severity) > rank(w))
|
|
39
|
+
worst.set(id, f.severity);
|
|
40
|
+
}
|
|
41
|
+
for (const f of findings) {
|
|
42
|
+
const id = ruleId(f);
|
|
43
|
+
if (rules.has(id))
|
|
44
|
+
continue;
|
|
45
|
+
const sev = SEVERITY[worst.get(id)];
|
|
46
|
+
rules.set(id, {
|
|
47
|
+
id,
|
|
48
|
+
name: shortLabel(f),
|
|
49
|
+
description: `${f.title}. Fix: ${f.fix}`,
|
|
50
|
+
engineId: ENGINE,
|
|
51
|
+
cleanCodeAttribute: "TRUSTWORTHY",
|
|
52
|
+
type: "VULNERABILITY",
|
|
53
|
+
severity: sev.old,
|
|
54
|
+
impacts: [{ softwareQuality: "SECURITY", severity: sev.impact }],
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const issues = findings.map((f) => ({
|
|
58
|
+
ruleId: ruleId(f),
|
|
59
|
+
effortMinutes: EFFORT[f.risk],
|
|
60
|
+
primaryLocation: { message: `${f.title}. ${f.fix}`, filePath: path(pre, f.file), ...range(f.line) },
|
|
61
|
+
...(f.via?.length ? { secondaryLocations: f.via.map((v) => ({ message: v.note, filePath: path(pre, v.file), ...range(v.line) })) } : {}),
|
|
62
|
+
}));
|
|
63
|
+
// Your policy: what it rules out that isn't already a finding above, like AES-128 under a 256-bit rule.
|
|
64
|
+
for (const v of opts.policy ?? []) {
|
|
65
|
+
if (v.finding || v.allowed)
|
|
66
|
+
continue;
|
|
67
|
+
const id = "policy/inventory";
|
|
68
|
+
if (!rules.has(id)) {
|
|
69
|
+
rules.set(id, {
|
|
70
|
+
id,
|
|
71
|
+
name: "Not allowed by your encryption policy",
|
|
72
|
+
description: "An algorithm or key size that your .benthic.yml rules out. It doesn't change the grade.",
|
|
73
|
+
engineId: ENGINE,
|
|
74
|
+
cleanCodeAttribute: "LAWFUL",
|
|
75
|
+
type: "VULNERABILITY",
|
|
76
|
+
severity: "MAJOR",
|
|
77
|
+
impacts: [{ softwareQuality: "SECURITY", severity: "MEDIUM" }],
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
issues.push({ ruleId: id, effortMinutes: 30, primaryLocation: { message: `${violationLabel(v)}: ${v.why}`, filePath: path(pre, v.file), ...range(v.line) } });
|
|
81
|
+
}
|
|
82
|
+
return { rules: [...rules.values()], issues };
|
|
83
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// A little code structure, without a parser per language: constants defined in a file, and calls that
|
|
4
|
+
// run over several lines. Both are common in real code and invisible to a line-by-line match:
|
|
5
|
+
//
|
|
6
|
+
// KEY_BITS = 1024 const TRANSFORM = "RSA/ECB/PKCS1Padding";
|
|
7
|
+
// key = rsa.generate_private_key( Cipher c = Cipher.getInstance(TRANSFORM);
|
|
8
|
+
// public_exponent=65537, key_size=KEY_BITS)
|
|
9
|
+
//
|
|
10
|
+
// Constants: names given one literal (a string or a whole number) exactly once in the file, by a
|
|
11
|
+
// plain assignment, are replaced by that literal where they are used. Calls: a line that opens more
|
|
12
|
+
// brackets than it closes is joined with the lines after it until they balance (up to 12 lines).
|
|
13
|
+
// Both work on code with comments and regular expression literals already blanked out.
|
|
14
|
+
const NAME = "[A-Za-z_$][A-Za-z0-9_$]*";
|
|
15
|
+
const LIT = `("(?:[^"\\\\\\n]|\\\\.)*"|'(?:[^'\\\\\\n]|\\\\.)*'|\`[^\`$\\n]*\`|-?\\d+)`;
|
|
16
|
+
const LIT_ONLY = new RegExp(`^${LIT}$`);
|
|
17
|
+
/** Whether a piece of code is one literal: a string or a whole number. */
|
|
18
|
+
export function isLiteral(text) {
|
|
19
|
+
return text.length < 400 && LIT_ONLY.test(text.trim());
|
|
20
|
+
}
|
|
21
|
+
// One pattern per language family; group 1 is the name and group 2 the literal.
|
|
22
|
+
const DEFS = [
|
|
23
|
+
// JavaScript and TypeScript: const/let/var, class fields (static readonly X = ...).
|
|
24
|
+
{ files: /\.(js|mjs|cjs|jsx|ts|mts|cts|tsx)$/i, re: new RegExp(`^\\s*(?:export\\s+)?(?:(?:const|let|var)\\s+|(?:(?:public|private|protected|static|readonly)\\s+)+)(${NAME})\\s*(?::\\s*[A-Za-z_][\\w<>\\[\\]|. ]*)?=\\s*${LIT}\\s*(?:as\\s+const\\s*)?;?\\s*$`) },
|
|
25
|
+
// Python and Ruby: NAME = literal (module, class or function level).
|
|
26
|
+
{ files: /\.(py|pyw|rb)$/i, re: new RegExp(`^\\s*(${NAME})\\s*(?::\\s*[A-Za-z_][\\w\\[\\], .]*)?=\\s*${LIT}\\s*$`) },
|
|
27
|
+
// Go: const X = ..., var X = ..., X := ..., and X = ... inside a const ( ... ) block.
|
|
28
|
+
{ files: /\.go$/i, re: new RegExp(`^\\s*(?:(?:const|var)\\s+)?(${NAME})\\s*(?:[A-Za-z_][\\w.]*\\s*)?:?=\\s*${LIT}\\s*$`) },
|
|
29
|
+
// Java, Kotlin, Scala, C#: typed fields and locals, val/var, const.
|
|
30
|
+
{ files: /\.(java|kt|kts|scala|cs)$/i, re: new RegExp(`^\\s*(?:(?:public|private|protected|internal|static|final|const|readonly|override)\\s+)*(?:val|var|[A-Za-z_][\\w<>\\[\\].]*)\\s+(${NAME})\\s*(?::\\s*[A-Za-z_][\\w<>?]*)?=\\s*${LIT}\\s*;?\\s*$`) },
|
|
31
|
+
// C and C++: #define X literal, and const char * / int X = literal.
|
|
32
|
+
{ files: /\.(c|cc|cpp|cxx|h|hh|hpp|m|mm)$/i, re: new RegExp(`^\\s*(?:#\\s*define\\s+(${NAME})\\s+${LIT}|(?:static\\s+)?(?:const\\s+)?(?:char\\s*\\*\\s*(?:const\\s+)?|int\\s+|unsigned\\s+|size_t\\s+|long\\s+)(${NAME})(?:\\[\\])?\\s*=\\s*${LIT}\\s*;)\\s*$`) },
|
|
33
|
+
// Rust: const/static X: T = literal; let X = literal;
|
|
34
|
+
{ files: /\.rs$/i, re: new RegExp(`^\\s*(?:pub(?:\\([^)]*\\))?\\s+)?(?:const|static|let)\\s+(?:mut\\s+)?(${NAME})\\s*(?::\\s*[^=]+)?=\\s*${LIT}\\s*;\\s*$`) },
|
|
35
|
+
// PHP: const X = ...; define('X', ...); $x = ...;
|
|
36
|
+
{ files: /\.php$/i, re: new RegExp(`^\\s*(?:(?:public|private|protected|static|final)\\s+)*(?:const\\s+(${NAME})|\\$(${NAME}))\\s*=\\s*${LIT}\\s*;\\s*$`) },
|
|
37
|
+
];
|
|
38
|
+
/** Names given one literal exactly once in the file. A name assigned twice is left out, since its value depends on the path taken. */
|
|
39
|
+
export function fileConstants(path, code) {
|
|
40
|
+
const def = DEFS.find((d) => d.files.test(path));
|
|
41
|
+
const out = new Map();
|
|
42
|
+
if (!def)
|
|
43
|
+
return out;
|
|
44
|
+
const seen = new Map();
|
|
45
|
+
code.forEach((text, i) => {
|
|
46
|
+
if (text.length > 400)
|
|
47
|
+
return;
|
|
48
|
+
const m = text.match(def.re);
|
|
49
|
+
if (!m)
|
|
50
|
+
return;
|
|
51
|
+
const groups = m.slice(1).filter((g) => g !== undefined);
|
|
52
|
+
let [name, value] = groups.length >= 2 ? [groups[0], groups[groups.length - 1]] : [undefined, undefined];
|
|
53
|
+
// PHP variables keep their $, which is how they are used.
|
|
54
|
+
if (name && /\.php$/i.test(path) && m[2] === name)
|
|
55
|
+
name = "$" + name;
|
|
56
|
+
if (!name || value === undefined)
|
|
57
|
+
return;
|
|
58
|
+
seen.set(name, (seen.get(name) ?? 0) + 1);
|
|
59
|
+
out.set(name, { value, line: i + 1, name });
|
|
60
|
+
});
|
|
61
|
+
// Any other assignment to the same name also makes it ambiguous: X = something_else().
|
|
62
|
+
const reassigned = new RegExp(`(?:^|[^\\w$.])(${NAME})\\s*(?:[-+*/|&]?=)(?!=)`, "g");
|
|
63
|
+
for (const text of code)
|
|
64
|
+
for (const m of text.matchAll(reassigned))
|
|
65
|
+
if (out.has(m[1]))
|
|
66
|
+
seen.set(m[1], (seen.get(m[1]) ?? 0) + (def.re.test(text) ? 0 : 1));
|
|
67
|
+
for (const [name, n] of seen)
|
|
68
|
+
if (n !== 1)
|
|
69
|
+
out.delete(name);
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The line with each known constant replaced by its literal. Only plain uses are replaced: not a
|
|
74
|
+
* property (obj.X), not a call (X(...)), not the left side of an assignment, and not inside a string.
|
|
75
|
+
* Returns null when nothing changed.
|
|
76
|
+
*/
|
|
77
|
+
export function substitute(line, consts) {
|
|
78
|
+
if (!consts.size)
|
|
79
|
+
return null;
|
|
80
|
+
const used = [];
|
|
81
|
+
let out = "";
|
|
82
|
+
let i = 0;
|
|
83
|
+
let quote = null;
|
|
84
|
+
while (i < line.length) {
|
|
85
|
+
const c = line[i];
|
|
86
|
+
if (quote) {
|
|
87
|
+
out += c;
|
|
88
|
+
if (c === "\\") {
|
|
89
|
+
out += line[i + 1] ?? "";
|
|
90
|
+
i += 2;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (c === quote)
|
|
94
|
+
quote = null;
|
|
95
|
+
i++;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
99
|
+
quote = c;
|
|
100
|
+
out += c;
|
|
101
|
+
i++;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const m = /^[A-Za-z_$][A-Za-z0-9_$]*(?:\.[A-Za-z_$][A-Za-z0-9_$]*){0,3}/.exec(line.slice(i));
|
|
105
|
+
if (m && !/[A-Za-z0-9_$.]/.test(line[i - 1] ?? "")) {
|
|
106
|
+
// A dotted name (module.NAME, Class.CONST) when it is known as a whole, else its first part.
|
|
107
|
+
let name = m[0];
|
|
108
|
+
while (name.includes(".") && !consts.has(name))
|
|
109
|
+
name = name.slice(0, name.lastIndexOf("."));
|
|
110
|
+
const after = line.slice(i + name.length);
|
|
111
|
+
const k = consts.get(name);
|
|
112
|
+
if (k && !/^\s*\(/.test(after) && !/^\s*(?:[-+*/|&]?=)(?!=)/.test(after) && !/^\s*:\s*[A-Za-z_]/.test(after.replace(/^\s*:\s*=/, ""))) {
|
|
113
|
+
out += k.value;
|
|
114
|
+
used.push(k);
|
|
115
|
+
}
|
|
116
|
+
else
|
|
117
|
+
out += name;
|
|
118
|
+
i += name.length;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
out += c;
|
|
122
|
+
i++;
|
|
123
|
+
}
|
|
124
|
+
return used.length ? { text: out, used } : null;
|
|
125
|
+
}
|
|
126
|
+
/** Bracket depth change on a line, ignoring brackets inside strings. */
|
|
127
|
+
export function depth(code) {
|
|
128
|
+
let d = 0;
|
|
129
|
+
let quote = null;
|
|
130
|
+
for (let i = 0; i < code.length; i++) {
|
|
131
|
+
const c = code[i];
|
|
132
|
+
if (quote) {
|
|
133
|
+
if (c === "\\")
|
|
134
|
+
i++;
|
|
135
|
+
else if (c === quote)
|
|
136
|
+
quote = null;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (c === '"' || c === "'" || c === "`")
|
|
140
|
+
quote = c;
|
|
141
|
+
else if (c === "(" || c === "[" || c === "{")
|
|
142
|
+
d++;
|
|
143
|
+
else if (c === ")" || c === "]" || c === "}")
|
|
144
|
+
d--;
|
|
145
|
+
}
|
|
146
|
+
return d;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The statement that starts at code[idx] and runs on over the next lines: joined while brackets stay
|
|
150
|
+
* open, up to 12 lines. Null when the line closes what it opens.
|
|
151
|
+
*/
|
|
152
|
+
export function statementAt(code, idx) {
|
|
153
|
+
let d = depth(code[idx]);
|
|
154
|
+
if (d <= 0)
|
|
155
|
+
return null;
|
|
156
|
+
// Only a call, a list or an object value left open, not a block body: if (x) { class A {
|
|
157
|
+
if (!opensValue(code[idx]))
|
|
158
|
+
return null;
|
|
159
|
+
let text = code[idx];
|
|
160
|
+
let j = idx;
|
|
161
|
+
while (d > 0 && j < idx + 11 && j + 1 < code.length) {
|
|
162
|
+
j++;
|
|
163
|
+
text += " " + code[j].trim();
|
|
164
|
+
d += depth(code[j]);
|
|
165
|
+
if (text.length > 2000)
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
return d <= 0 ? { text, lastIdx: j } : null;
|
|
169
|
+
}
|
|
170
|
+
/** Whether what a line leaves open is a call's arguments, a list, or an object given as a value. */
|
|
171
|
+
function opensValue(code) {
|
|
172
|
+
const stack = [];
|
|
173
|
+
let quote = null;
|
|
174
|
+
for (let i = 0; i < code.length; i++) {
|
|
175
|
+
const c = code[i];
|
|
176
|
+
if (quote) {
|
|
177
|
+
if (c === "\\")
|
|
178
|
+
i++;
|
|
179
|
+
else if (c === quote)
|
|
180
|
+
quote = null;
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (c === '"' || c === "'" || c === "`")
|
|
184
|
+
quote = c;
|
|
185
|
+
else if (c === "(" || c === "[" || c === "{")
|
|
186
|
+
stack.push({ c, at: i });
|
|
187
|
+
else if (c === ")" || c === "]" || c === "}")
|
|
188
|
+
stack.pop();
|
|
189
|
+
}
|
|
190
|
+
if (stack.some((b) => b.c !== "{"))
|
|
191
|
+
return true;
|
|
192
|
+
// Only braces open: an object after = : ( , or return is a value; anything else opens a block.
|
|
193
|
+
const first = stack[0];
|
|
194
|
+
return !!first && /(?:[=:(,]|\breturn)\s*$/.test(code.slice(0, first.at));
|
|
195
|
+
}
|
|
196
|
+
// ---------- Python import aliases ----------
|
|
197
|
+
/**
|
|
198
|
+
* `from Crypto.PublicKey import RSA as PyRSA` lets the code say PyRSA.generate(2048). Rewrites each
|
|
199
|
+
* use of an alias to the name it stands for, so the rules see RSA.generate(2048). Import lines are
|
|
200
|
+
* left as they are.
|
|
201
|
+
*/
|
|
202
|
+
export function unaliasPython(code) {
|
|
203
|
+
const aliases = new Map();
|
|
204
|
+
for (const line of code) {
|
|
205
|
+
const m = line.match(/^\s*from\s+[\w.]+\s+import\s+\(?([^)#]+)\)?\s*$/) ?? line.match(/^\s*import\s+([\w.]+\s+as\s+\w+(?:\s*,\s*[\w.]+\s+as\s+\w+)*)\s*$/);
|
|
206
|
+
if (!m)
|
|
207
|
+
continue;
|
|
208
|
+
for (const part of m[1].split(",")) {
|
|
209
|
+
const a = part.trim().match(/^([\w.]+)\s+as\s+(\w+)$/);
|
|
210
|
+
if (a && a[2] !== a[1] && /^[A-Za-z_]\w*$/.test(a[1].split(".").pop()))
|
|
211
|
+
aliases.set(a[2], a[1].split(".").pop());
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (!aliases.size)
|
|
215
|
+
return code;
|
|
216
|
+
const names = new RegExp(`(?<![\\w.])(${[...aliases.keys()].join("|")})(?=\\s*\\.)`, "g");
|
|
217
|
+
return code.map((line) => (/^\s*(?:from|import)\s/.test(line) ? line : line.replace(names, (n) => aliases.get(n))));
|
|
218
|
+
}
|