@secureport/core 1.0.0 → 2.1.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/README.md +2 -1
- package/dist/finding.d.ts +9 -8
- package/dist/finding.d.ts.map +1 -1
- package/dist/fingerprint.d.ts +12 -5
- package/dist/fingerprint.d.ts.map +1 -1
- package/dist/fingerprint.js +74 -8
- package/dist/fingerprint.js.map +1 -1
- package/dist/import/nessus.d.ts +9 -6
- package/dist/import/nessus.d.ts.map +1 -1
- package/dist/import/nessus.js +9 -6
- package/dist/import/nessus.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/reconcile.d.ts +25 -0
- package/dist/reconcile.d.ts.map +1 -1
- package/dist/reconcile.js +30 -3
- package/dist/reconcile.js.map +1 -1
- package/dist/refingerprint.d.ts +116 -0
- package/dist/refingerprint.d.ts.map +1 -0
- package/dist/refingerprint.js +190 -0
- package/dist/refingerprint.js.map +1 -0
- package/package.json +1 -1
- package/src/finding.ts +9 -8
- package/src/fingerprint.ts +76 -10
- package/src/import/nessus.ts +9 -6
- package/src/index.ts +9 -0
- package/src/reconcile.ts +35 -3
- package/src/refingerprint.ts +282 -0
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import type { Finding } from './finding.js';
|
|
2
|
+
import type { FingerprintInput, VulnKeyInput } from './fingerprint.js';
|
|
3
|
+
import { FINGERPRINT_VERSION, fingerprint, vulnKey } from './fingerprint.js';
|
|
4
|
+
import type { Issue } from './issue.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* How to compute a fingerprint. Injected so the dry run can be pointed at a
|
|
8
|
+
* version that does not exist yet.
|
|
9
|
+
*
|
|
10
|
+
* **`@secureport/core` only ever contains one algorithm** — the current one,
|
|
11
|
+
* named by `FINGERPRINT_VERSION`. So on the day a version bump is being
|
|
12
|
+
* considered, the way to find out what it would do is to hand this planner
|
|
13
|
+
* the candidate implementation. Against the shipped algorithm the plan is a
|
|
14
|
+
* no-op by construction, which is exactly what makes it safe to run at any
|
|
15
|
+
* time and what lets the tests drive real merges and splits without a
|
|
16
|
+
* released `fp_v2`.
|
|
17
|
+
*/
|
|
18
|
+
export interface FingerprintAlgorithm {
|
|
19
|
+
readonly version: string;
|
|
20
|
+
fingerprintOf(input: FingerprintInput): string;
|
|
21
|
+
vulnKeyOf(input: VulnKeyInput): string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The algorithm this build ships. */
|
|
25
|
+
export const currentAlgorithm: FingerprintAlgorithm = {
|
|
26
|
+
version: FINGERPRINT_VERSION,
|
|
27
|
+
fingerprintOf: fingerprint,
|
|
28
|
+
vulnKeyOf: vulnKey,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** An issue and the evidence a re-fingerprint would recompute it from. */
|
|
32
|
+
export interface IssueWithEvidence {
|
|
33
|
+
readonly issue: Issue;
|
|
34
|
+
/**
|
|
35
|
+
* The evidence that **defines** this issue, which is not always every
|
|
36
|
+
* finding linked to it: `gatherForOrg` excludes what a merge attached from
|
|
37
|
+
* another issue, because that evidence describes a different weakness and
|
|
38
|
+
* re-deriving a key from it would split the survivor apart.
|
|
39
|
+
*/
|
|
40
|
+
readonly findings: readonly Finding[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* What would happen to one issue.
|
|
45
|
+
*
|
|
46
|
+
* - `unchanged` — recomputes to the fingerprint it already has.
|
|
47
|
+
* - `moved` — one new fingerprint, different, and nothing else lands on it.
|
|
48
|
+
* The issue keeps its identity and its history; only the key changes.
|
|
49
|
+
* - `merged` — its new fingerprint is shared with at least one other issue.
|
|
50
|
+
* They become one, which is the outcome B39 and B41 are chasing.
|
|
51
|
+
* - `split` — its findings no longer agree on a fingerprint, so the issue
|
|
52
|
+
* would become several. The most disruptive outcome and the one that most
|
|
53
|
+
* needs reading before an execution.
|
|
54
|
+
* - `undecidable` — at least one of its findings cannot be re-keyed at all,
|
|
55
|
+
* so no honest answer exists for it. See {@link planRefingerprint}.
|
|
56
|
+
*/
|
|
57
|
+
export type RefingerprintOutcome = 'unchanged' | 'moved' | 'merged' | 'split' | 'undecidable';
|
|
58
|
+
|
|
59
|
+
export interface IssuePlan {
|
|
60
|
+
readonly issueId: string;
|
|
61
|
+
readonly targetId: string;
|
|
62
|
+
readonly title: string;
|
|
63
|
+
readonly from: string;
|
|
64
|
+
readonly fromVersion: string;
|
|
65
|
+
/** Distinct fingerprints this issue's evidence produces at the new version. */
|
|
66
|
+
readonly to: readonly string[];
|
|
67
|
+
readonly outcome: RefingerprintOutcome;
|
|
68
|
+
/** Other issues that land on one of `to`. Empty unless `outcome` involves a merge. */
|
|
69
|
+
readonly mergesWith: readonly string[];
|
|
70
|
+
/**
|
|
71
|
+
* Findings linked to the issue — **not necessarily the number the recompute
|
|
72
|
+
* read**. A manual issue is keyed from its own columns however much evidence
|
|
73
|
+
* is attached to it, and a scanner issue with zero findings falls back to
|
|
74
|
+
* the same path. Both are reported here as they stand so the count stays a
|
|
75
|
+
* fact about the data rather than about this function.
|
|
76
|
+
*/
|
|
77
|
+
readonly evidence: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface RefingerprintPlan {
|
|
81
|
+
readonly fromVersion: string;
|
|
82
|
+
readonly toVersion: string;
|
|
83
|
+
readonly issues: readonly IssuePlan[];
|
|
84
|
+
readonly counts: Readonly<Record<RefingerprintOutcome, number>>;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Works out what re-fingerprinting every issue would do, and writes nothing.
|
|
89
|
+
*
|
|
90
|
+
* **Recomputed from inputs, never by comparing stored strings.** A stored
|
|
91
|
+
* `fp_v1` fingerprint and a stored `fp_v2` one are not comparable — that is
|
|
92
|
+
* the whole reason `reconcile()` refuses to run across versions (B45) — so
|
|
93
|
+
* the only honest question is "what does the new algorithm say about the same
|
|
94
|
+
* evidence?". That means re-deriving `vulnKey` too: B39's change was to the
|
|
95
|
+
* `vulnKey` fallback, and reading the stored value would miss precisely the
|
|
96
|
+
* class of change this migration exists for.
|
|
97
|
+
*
|
|
98
|
+
* **A manual issue is keyed from its own columns, evidence or not.** Findings
|
|
99
|
+
* can be attached to a human-raised issue after the fact; 24 of the 404 manual
|
|
100
|
+
* issues in the development database are. Re-deriving those from the evidence
|
|
101
|
+
* would replace the human's `vulnKey` with the scanner's and merge the issue
|
|
102
|
+
* into whichever upload-origin issue already holds that key — losing the
|
|
103
|
+
* issue a person raised, in a migration nobody expected to lose anything.
|
|
104
|
+
*
|
|
105
|
+
* **An issue an earlier merge retired keeps the key it has**, and this is what
|
|
106
|
+
* makes the plan reach a fixed point. A merge copies the duplicate's evidence
|
|
107
|
+
* to the survivor without removing it from the duplicate (B98), so re-deriving
|
|
108
|
+
* a retired issue from its evidence produces the survivor's key and proposes
|
|
109
|
+
* the merge that already happened — every run, forever. Checked before the
|
|
110
|
+
* manual rule, because an issue can be both and retirement is the later
|
|
111
|
+
* statement about its identity.
|
|
112
|
+
*
|
|
113
|
+
* **Findings are never touched, and this never needs them to be.** A finding
|
|
114
|
+
* is evidence (invariant 1) and `findings` rejects `UPDATE` at the database
|
|
115
|
+
* (B66); its `fingerprint_version` records which algorithm produced it and
|
|
116
|
+
* that stays true forever. Only `issues` are re-keyed. B69, 8 September.
|
|
117
|
+
*
|
|
118
|
+
* **A finding whose key cannot be re-derived makes its issue `undecidable`,
|
|
119
|
+
* and does not stop the report.** `vulnKey()` throws when a finding carries
|
|
120
|
+
* no mapped rule, CWE, category or engine rule id — so a row that was never
|
|
121
|
+
* produced by an importer at this version (or was produced by a different
|
|
122
|
+
* one) has no answer. Reporting that is the useful behaviour: a migration
|
|
123
|
+
* plan that dies on one row tells an operator nothing about the other
|
|
124
|
+
* thousand, and guessing a key from the stored value would defeat the whole
|
|
125
|
+
* purpose, since re-deriving `vulnKey` is the point.
|
|
126
|
+
*/
|
|
127
|
+
export function planRefingerprint(
|
|
128
|
+
input: readonly IssueWithEvidence[],
|
|
129
|
+
algorithm: FingerprintAlgorithm = currentAlgorithm,
|
|
130
|
+
): RefingerprintPlan {
|
|
131
|
+
const recomputed = input.map(({ issue, findings }) => {
|
|
132
|
+
// Retirement is checked first, and the order is load-bearing. An issue an
|
|
133
|
+
// earlier merge retired is not re-keyed at all: its identity moved to the
|
|
134
|
+
// survivor, and its evidence — which the merge copied to that survivor —
|
|
135
|
+
// would re-derive the survivor's key and propose merging the two all over
|
|
136
|
+
// again, so the plan would never reach a fixed point. A *manual* issue
|
|
137
|
+
// that was later merged away is both things at once, and retirement is
|
|
138
|
+
// the later statement about its identity, so it wins. Measured: with the
|
|
139
|
+
// branches the other way round, 95 issues reported `moved` on every run
|
|
140
|
+
// while the executor left them alone on every run, forever.
|
|
141
|
+
if (issue.status === 'ignored' && issue.ignoreReason === 'duplicate') {
|
|
142
|
+
return { issue, evidence: findings.length, undecidable: false, to: [issue.fingerprint] };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// A manual issue is keyed from its own columns: its `vulnKey` and location
|
|
146
|
+
// were declared by a person, and evidence linked afterwards corroborates
|
|
147
|
+
// that issue rather than redefining it. Re-deriving would replace the
|
|
148
|
+
// human's key with the scanner's and dissolve the issue into whichever
|
|
149
|
+
// upload-origin issue already holds it.
|
|
150
|
+
//
|
|
151
|
+
// A *merge survivor* is deliberately not handled here, though it is also
|
|
152
|
+
// an identity a person asserted. Keying one from its own columns carries a
|
|
153
|
+
// stale `vulnKey` — written before the mapping table knew the engine's
|
|
154
|
+
// rule — so the migrated issue stops matching what the importer now
|
|
155
|
+
// produces, auto-resolves and comes back as new. `gatherForOrg` excludes
|
|
156
|
+
// the absorbed evidence instead, so a survivor re-derives correctly from
|
|
157
|
+
// what was always its own.
|
|
158
|
+
if (issue.origin === 'manual') {
|
|
159
|
+
return {
|
|
160
|
+
issue,
|
|
161
|
+
evidence: findings.length,
|
|
162
|
+
undecidable: false,
|
|
163
|
+
to: [fromOwnColumns(issue, algorithm)],
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const keys = findings.map((f) => fingerprintOfFinding(issue, f, algorithm));
|
|
168
|
+
return {
|
|
169
|
+
issue,
|
|
170
|
+
evidence: findings.length,
|
|
171
|
+
undecidable: keys.some((k) => k === undefined),
|
|
172
|
+
to: [...new Set(keys.filter((k): k is string => k !== undefined))].sort(),
|
|
173
|
+
};
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// A scanner-origin issue whose evidence has since been detached has nothing
|
|
177
|
+
// left to re-derive from, so it falls back to its own columns rather than
|
|
178
|
+
// vanishing from the report.
|
|
179
|
+
for (const row of recomputed) {
|
|
180
|
+
if (row.to.length === 0 && !row.undecidable) {
|
|
181
|
+
row.to = [fromOwnColumns(row.issue, algorithm)];
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const owners = new Map<string, string[]>();
|
|
186
|
+
for (const row of recomputed) {
|
|
187
|
+
if (row.undecidable) continue;
|
|
188
|
+
for (const fp of row.to) {
|
|
189
|
+
const list = owners.get(fp);
|
|
190
|
+
if (list) list.push(row.issue.id);
|
|
191
|
+
else owners.set(fp, [row.issue.id]);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const counts: Record<RefingerprintOutcome, number> = {
|
|
196
|
+
unchanged: 0,
|
|
197
|
+
moved: 0,
|
|
198
|
+
merged: 0,
|
|
199
|
+
split: 0,
|
|
200
|
+
undecidable: 0,
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const issues = recomputed.map(({ issue, evidence, to, undecidable }): IssuePlan => {
|
|
204
|
+
const mergesWith = [
|
|
205
|
+
...new Set(to.flatMap((fp) => owners.get(fp) ?? []).filter((id) => id !== issue.id)),
|
|
206
|
+
].sort();
|
|
207
|
+
|
|
208
|
+
// Split first: an issue that fragments is the most disruptive outcome
|
|
209
|
+
// and stays visible even when one of its fragments also collides.
|
|
210
|
+
const outcome: RefingerprintOutcome = undecidable
|
|
211
|
+
? 'undecidable'
|
|
212
|
+
: to.length > 1
|
|
213
|
+
? 'split'
|
|
214
|
+
: mergesWith.length > 0
|
|
215
|
+
? 'merged'
|
|
216
|
+
: to[0] === issue.fingerprint
|
|
217
|
+
? 'unchanged'
|
|
218
|
+
: 'moved';
|
|
219
|
+
|
|
220
|
+
counts[outcome] += 1;
|
|
221
|
+
return {
|
|
222
|
+
issueId: issue.id,
|
|
223
|
+
targetId: issue.targetId,
|
|
224
|
+
title: issue.title,
|
|
225
|
+
from: issue.fingerprint,
|
|
226
|
+
fromVersion: issue.fingerprintVersion,
|
|
227
|
+
to,
|
|
228
|
+
outcome,
|
|
229
|
+
mergesWith,
|
|
230
|
+
evidence,
|
|
231
|
+
};
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const fromVersions = [...new Set(input.map(({ issue }) => issue.fingerprintVersion))].sort();
|
|
235
|
+
return {
|
|
236
|
+
fromVersion: fromVersions.join(', ') || algorithm.version,
|
|
237
|
+
toVersion: algorithm.version,
|
|
238
|
+
issues,
|
|
239
|
+
counts,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** An issue's fingerprint at the new version, from the issue's own columns. */
|
|
244
|
+
function fromOwnColumns(issue: Issue, algorithm: FingerprintAlgorithm): string {
|
|
245
|
+
return algorithm.fingerprintOf({
|
|
246
|
+
targetId: issue.targetId,
|
|
247
|
+
vulnKey: issue.vulnKey,
|
|
248
|
+
location: issue.location,
|
|
249
|
+
...(issue.parameter === undefined ? {} : { parameter: issue.parameter }),
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* One finding's fingerprint at the new version, `vulnKey` re-derived and all,
|
|
255
|
+
* or `undefined` when the key cannot be derived from what the finding holds.
|
|
256
|
+
*/
|
|
257
|
+
function fingerprintOfFinding(
|
|
258
|
+
issue: Issue,
|
|
259
|
+
finding: Finding,
|
|
260
|
+
algorithm: FingerprintAlgorithm,
|
|
261
|
+
): string | undefined {
|
|
262
|
+
let key;
|
|
263
|
+
try {
|
|
264
|
+
key = algorithm.vulnKeyOf({
|
|
265
|
+
sourceEngine: finding.sourceEngine,
|
|
266
|
+
...(finding.sourceRuleId === undefined ? {} : { sourceRuleId: finding.sourceRuleId }),
|
|
267
|
+
...(finding.cwe === undefined ? {} : { cwe: finding.cwe }),
|
|
268
|
+
...(finding.category === undefined ? {} : { category: finding.category }),
|
|
269
|
+
});
|
|
270
|
+
} catch {
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
return algorithm.fingerprintOf({
|
|
274
|
+
// The target is the issue's: a finding records the run it came from, and
|
|
275
|
+
// every run of an issue's evidence is against the same target anyway.
|
|
276
|
+
targetId: issue.targetId,
|
|
277
|
+
vulnKey: key,
|
|
278
|
+
location: finding.location,
|
|
279
|
+
...(finding.parameter === undefined ? {} : { parameter: finding.parameter }),
|
|
280
|
+
...(finding.port === undefined ? {} : { port: finding.port }),
|
|
281
|
+
});
|
|
282
|
+
}
|