@secureport/core 1.0.0 → 2.2.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 +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/issue.d.ts +1 -1
- package/dist/issue.d.ts.map +1 -1
- package/dist/reconcile.d.ts +71 -1
- package/dist/reconcile.d.ts.map +1 -1
- package/dist/reconcile.js +64 -4
- 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/dist/report/anchors.d.ts +39 -0
- package/dist/report/anchors.d.ts.map +1 -0
- package/dist/report/anchors.js +73 -0
- package/dist/report/anchors.js.map +1 -0
- package/dist/report/html.d.ts.map +1 -1
- package/dist/report/html.js +126 -22
- package/dist/report/html.js.map +1 -1
- package/dist/report/markdown.d.ts.map +1 -1
- package/dist/report/markdown.js +112 -26
- package/dist/report/markdown.js.map +1 -1
- package/dist/report/model.d.ts +227 -1
- package/dist/report/model.d.ts.map +1 -1
- package/dist/report/model.js +87 -8
- package/dist/report/model.js.map +1 -1
- 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 +14 -2
- package/src/issue.ts +1 -0
- package/src/reconcile.ts +121 -5
- package/src/refingerprint.ts +282 -0
- package/src/report/anchors.ts +96 -0
- package/src/report/html.ts +146 -26
- package/src/report/markdown.ts +125 -25
- package/src/report/model.ts +344 -7
package/src/import/nessus.ts
CHANGED
|
@@ -28,12 +28,15 @@ const NESSUS_SEVERITY: Readonly<Record<string, Severity>> = Object.freeze({
|
|
|
28
28
|
* fingerprints distinctly per service.
|
|
29
29
|
*
|
|
30
30
|
* The port is also recorded on {@link Finding.port} (**B42**), which is a
|
|
31
|
-
* record and nothing more. The
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
31
|
+
* record and nothing more. **The double-count is gone in `fp_v2`** (6.9c): the
|
|
32
|
+
* fingerprint's last component was `parameter ?? port ?? ''`, so a Nessus port
|
|
33
|
+
* was counted twice — once inside the location and once as that component —
|
|
34
|
+
* and this comment said it had to stay because removing it would force a
|
|
35
|
+
* re-fingerprint migration. 6.9b built that migration, so it went. The tail is
|
|
36
|
+
* now `parameter ?? ''`, and the port is carried by the location alone, which
|
|
37
|
+
* `normaliseLocation` canonicalises: `app.example.com:443` and
|
|
38
|
+
* `https://app.example.com/` are one endpoint (B102), and a non-default port
|
|
39
|
+
* stays in the location where it belongs.
|
|
37
40
|
*
|
|
38
41
|
* Severity comes from the CVSS v3 base score where Nessus supplies one, and from
|
|
39
42
|
* its own numeric rating otherwise.
|
package/src/index.ts
CHANGED
|
@@ -16,6 +16,15 @@
|
|
|
16
16
|
|
|
17
17
|
export type { Finding } from './finding.js';
|
|
18
18
|
export type { FingerprintInput, VulnKeyInput } from './fingerprint.js';
|
|
19
|
+
export type {
|
|
20
|
+
FingerprintAlgorithm,
|
|
21
|
+
IssuePlan,
|
|
22
|
+
IssueWithEvidence,
|
|
23
|
+
RefingerprintOutcome,
|
|
24
|
+
RefingerprintPlan,
|
|
25
|
+
} from './refingerprint.js';
|
|
26
|
+
export { currentAlgorithm, planRefingerprint } from './refingerprint.js';
|
|
27
|
+
export { exposureOf } from './reconcile.js';
|
|
19
28
|
export {
|
|
20
29
|
FINGERPRINT_VERSION,
|
|
21
30
|
PATH_PLACEHOLDER,
|
|
@@ -44,6 +53,9 @@ export { importGeneric } from './import/generic.js';
|
|
|
44
53
|
export type { BuildSnapshotInput } from './snapshot-builder.js';
|
|
45
54
|
export { buildSnapshot, parseSnapshot } from './snapshot-builder.js';
|
|
46
55
|
export type {
|
|
56
|
+
Branding,
|
|
57
|
+
EvidenceVerbosity,
|
|
58
|
+
OmittedIssues,
|
|
47
59
|
ReportKind,
|
|
48
60
|
ReportModel,
|
|
49
61
|
ReportOptions,
|
|
@@ -53,12 +65,12 @@ export type {
|
|
|
53
65
|
RetestVerdict,
|
|
54
66
|
TestingBasis,
|
|
55
67
|
} from './report/model.js';
|
|
56
|
-
export { buildReportModel } from './report/model.js';
|
|
68
|
+
export { buildReportModel, VERDICT_LABELS } from './report/model.js';
|
|
57
69
|
export { renderMarkdown } from './report/markdown.js';
|
|
58
70
|
export { renderHtml } from './report/html.js';
|
|
59
71
|
export type { JsonReport } from './report/json.js';
|
|
60
72
|
export { renderJson } from './report/json.js';
|
|
61
|
-
export type { ReconcileInput, ReconcileResult } from './reconcile.js';
|
|
73
|
+
export type { ReconcileInput, ReconcileResult, SuppressionDecision } from './reconcile.js';
|
|
62
74
|
export { reconcile } from './reconcile.js';
|
|
63
75
|
export type {
|
|
64
76
|
Coverage,
|
package/src/issue.ts
CHANGED
package/src/reconcile.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Finding } from './finding.js';
|
|
2
2
|
import { FINGERPRINT_VERSION } from './fingerprint.js';
|
|
3
|
-
import type { Issue, IssueEvent } from './issue.js';
|
|
3
|
+
import type { IgnoreReason, IgnoreScope, Issue, IssueEvent } from './issue.js';
|
|
4
4
|
import type { Run, RunKind, RunSummary, SeverityCounts } from './run.js';
|
|
5
5
|
import {
|
|
6
6
|
DEFAULT_SLA_POLICY,
|
|
@@ -73,6 +73,56 @@ export interface ReconcileInput {
|
|
|
73
73
|
|
|
74
74
|
/** How long the run took, for the summary. */
|
|
75
75
|
readonly durationMs?: number;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Consulted for each issue this run would newly open, to suppress it at
|
|
79
|
+
* birth — a standing rule rather than a decision about one issue.
|
|
80
|
+
*
|
|
81
|
+
* **It has to live here, and B89 records why the obvious alternative fails.**
|
|
82
|
+
* `reconcile()` computes the run summary, so an issue it opens is counted
|
|
83
|
+
* `new`. A caller that suppressed that issue after the fact would produce a
|
|
84
|
+
* report whose opening line said `new` while its own appendix said
|
|
85
|
+
* `suppressed`, about the same issue, in the same document. Filtering the
|
|
86
|
+
* findings before reconciling is no better: the issue then vanishes from the
|
|
87
|
+
* summary entirely rather than being suppressed, and invariant 7 says a
|
|
88
|
+
* suppressed issue is never absent from the appendix.
|
|
89
|
+
*
|
|
90
|
+
* Return `undefined` to open the issue normally. The suppressed issue is
|
|
91
|
+
* counted `ignored` in the same pass that counts everything else, which the
|
|
92
|
+
* summary already does correctly because it tests `status` before it tests
|
|
93
|
+
* whether the run created the issue.
|
|
94
|
+
*
|
|
95
|
+
* **Consulted only for new issues**, never for one that already exists: an
|
|
96
|
+
* issue a person has looked at is theirs, and a rule written afterwards does
|
|
97
|
+
* not get to reach back and hide it.
|
|
98
|
+
*/
|
|
99
|
+
readonly suppress?: (issue: Issue) => SuppressionDecision | undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Why a standing rule suppressed an issue at the moment it was opened.
|
|
104
|
+
*
|
|
105
|
+
* @see {@link ReconcileInput.suppress}
|
|
106
|
+
*/
|
|
107
|
+
export interface SuppressionDecision {
|
|
108
|
+
/** Which of the domain's ignore reasons applies. */
|
|
109
|
+
readonly reason: IgnoreReason;
|
|
110
|
+
|
|
111
|
+
/** The justification an auditor reads in the suppressed appendix. */
|
|
112
|
+
readonly comment?: string;
|
|
113
|
+
|
|
114
|
+
/** How widely the suppression applies. */
|
|
115
|
+
readonly scope?: IgnoreScope;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Who accepted the risk. Defaults to {@link ReconcileInput.actor}.
|
|
119
|
+
*
|
|
120
|
+
* Worth passing. The appendix is an accepted-risk register, and a register
|
|
121
|
+
* whose every row says `reconciler` answers "who accepted this?" with "the
|
|
122
|
+
* clock did" — which `00-DOMAIN.md` §3 is explicit is not an identity. The
|
|
123
|
+
* rule that matched has an author; this is where to name them.
|
|
124
|
+
*/
|
|
125
|
+
readonly by?: string;
|
|
76
126
|
}
|
|
77
127
|
|
|
78
128
|
/**
|
|
@@ -146,6 +196,40 @@ function defaultThreshold(kind: RunKind): number {
|
|
|
146
196
|
return kind === 'upload' ? 1 : 2;
|
|
147
197
|
}
|
|
148
198
|
|
|
199
|
+
/**
|
|
200
|
+
* How much a single issue contributes to an exposure score.
|
|
201
|
+
*
|
|
202
|
+
* **Severity times age in days, capped at ninety.** The cap is the load-bearing
|
|
203
|
+
* part: without it a two-year-old advisory eventually outweighs a critical
|
|
204
|
+
* found this morning, and the number stops being about risk and starts being
|
|
205
|
+
* about how long you have had the account. Ninety days is where an unfixed
|
|
206
|
+
* issue stops getting worse and simply *is* bad.
|
|
207
|
+
*
|
|
208
|
+
* **Only `open` and `regressed` count.** A resolved issue is not exposure, and
|
|
209
|
+
* an ignored one is excluded from exposure exactly as it is excluded from
|
|
210
|
+
* every count but its own (invariant 7) — suppressing an issue is a statement
|
|
211
|
+
* that it is not currently risk, and a score that disagreed with that would
|
|
212
|
+
* make the suppression pointless.
|
|
213
|
+
*
|
|
214
|
+
* Exported because `reconcile()` is not the only thing that needs it: the
|
|
215
|
+
* nightly rollup scores a whole organisation across every target, and two
|
|
216
|
+
* implementations of this would be two definitions of exposure.
|
|
217
|
+
*
|
|
218
|
+
* @param issue - The issue to score.
|
|
219
|
+
* @param now - When the score is being taken. An argument, so the same issue
|
|
220
|
+
* and the same clock always give the same number.
|
|
221
|
+
* @returns The contribution, which is zero for anything not currently open.
|
|
222
|
+
*/
|
|
223
|
+
export function exposureOf(
|
|
224
|
+
issue: Pick<Issue, 'status' | 'effectiveSeverity' | 'firstSeen'>,
|
|
225
|
+
now: Date,
|
|
226
|
+
): number {
|
|
227
|
+
if (issue.status !== 'open' && issue.status !== 'regressed') return 0;
|
|
228
|
+
return (
|
|
229
|
+
SEVERITY_WEIGHTS[issue.effectiveSeverity] * Math.min(daysBetween(issue.firstSeen, now), 90)
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
149
233
|
/** An all-zero count, so a report never has to decide what a missing key means. */
|
|
150
234
|
function emptyCounts(): Record<string, number> {
|
|
151
235
|
return Object.fromEntries(SEVERITY_ORDER.map((s) => [s, 0]));
|
|
@@ -285,8 +369,42 @@ export function reconcile(input: ReconcileInput): ReconcileResult {
|
|
|
285
369
|
origin: run.kind,
|
|
286
370
|
slaDueAt: slaDueAt(worst.detectedSeverity, now, slaPolicy),
|
|
287
371
|
};
|
|
288
|
-
|
|
372
|
+
const decision = input.suppress?.(issue);
|
|
373
|
+
if (decision === undefined) {
|
|
374
|
+
updated.set(key, issue);
|
|
375
|
+
emit(id, 'created', { findingIds, severity: worst.detectedSeverity });
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Both events, in order. The issue was opened and then suppressed; a
|
|
380
|
+
// history that recorded only the suppression would have an issue whose
|
|
381
|
+
// first event is `ignored`, and invariant 2 wants every state change
|
|
382
|
+
// accounted for rather than the net result.
|
|
383
|
+
//
|
|
384
|
+
// `severityAtIgnore` is set deliberately, which is what makes a rule
|
|
385
|
+
// liftable: the existing branch above un-ignores an issue whose detected
|
|
386
|
+
// severity rises past what was accepted, and a standing rule should not
|
|
387
|
+
// be more permanent than a person's own decision. Accepting the risk of
|
|
388
|
+
// a medium is not accepting the risk of a critical, however the
|
|
389
|
+
// acceptance was expressed. **A merge survivor is the deliberate
|
|
390
|
+
// exception and omits it** (P6, B98) — that is a statement about
|
|
391
|
+
// identity rather than risk, and it has to survive a severity rise.
|
|
392
|
+
const suppressed: Issue = {
|
|
393
|
+
...issue,
|
|
394
|
+
status: 'ignored',
|
|
395
|
+
ignoreReason: decision.reason,
|
|
396
|
+
...(decision.comment === undefined ? {} : { ignoreComment: decision.comment }),
|
|
397
|
+
...(decision.scope === undefined ? {} : { ignoreScope: decision.scope }),
|
|
398
|
+
ignoredBy: decision.by ?? actor,
|
|
399
|
+
severityAtIgnore: worst.detectedSeverity,
|
|
400
|
+
};
|
|
401
|
+
updated.set(key, suppressed);
|
|
289
402
|
emit(id, 'created', { findingIds, severity: worst.detectedSeverity });
|
|
403
|
+
emit(id, 'ignored', {
|
|
404
|
+
reason: decision.reason,
|
|
405
|
+
...(decision.comment === undefined ? {} : { comment: decision.comment }),
|
|
406
|
+
bornSuppressed: true,
|
|
407
|
+
});
|
|
290
408
|
continue;
|
|
291
409
|
}
|
|
292
410
|
|
|
@@ -446,9 +564,7 @@ export function reconcile(input: ReconcileInput): ReconcileResult {
|
|
|
446
564
|
else if (regressedNow.has(key)) counts.regressed[severity]++;
|
|
447
565
|
else if (updated.has(key)) counts.stillOpen[severity]++;
|
|
448
566
|
|
|
449
|
-
|
|
450
|
-
exposureScore += SEVERITY_WEIGHTS[severity] * Math.min(daysBetween(issue.firstSeen, now), 90);
|
|
451
|
-
}
|
|
567
|
+
exposureScore += exposureOf(issue, now);
|
|
452
568
|
}
|
|
453
569
|
|
|
454
570
|
const summary: RunSummary = {
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Heading anchors and the contents list, produced together.
|
|
3
|
+
*
|
|
4
|
+
* **One pass over the rendered body, not two walks over the document.** A
|
|
5
|
+
* contents list built separately from the ids it links to is a list that can
|
|
6
|
+
* point at nothing — the failure is silent, it only shows up when somebody
|
|
7
|
+
* clicks, and it would appear the moment a heading's wording changed in one
|
|
8
|
+
* place and not the other. Here the ids and the entries come out of the same
|
|
9
|
+
* scan, so a heading that exists has an anchor and an entry, and one that does
|
|
10
|
+
* not exist has neither.
|
|
11
|
+
*
|
|
12
|
+
* This post-processes the renderer's **own** output, inside the same function
|
|
13
|
+
* that produced it. That is a different thing from the string surgery P7.1
|
|
14
|
+
* rules out: the rule there is against forking a published template, and this
|
|
15
|
+
* never reads one — every heading it matches was emitted a few lines earlier by
|
|
16
|
+
* the renderer calling it.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** One line of the contents list. */
|
|
20
|
+
export interface TocEntry {
|
|
21
|
+
/** Heading level, 2 or 3. */
|
|
22
|
+
readonly level: number;
|
|
23
|
+
|
|
24
|
+
/** The anchor it links to, without the `#`. */
|
|
25
|
+
readonly id: string;
|
|
26
|
+
|
|
27
|
+
/** The heading as printed. */
|
|
28
|
+
readonly text: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A heading's anchor: lowercase, punctuation dropped, spaces hyphenated.
|
|
33
|
+
*
|
|
34
|
+
* The same rule GitHub and most Markdown renderers use, so a Markdown
|
|
35
|
+
* report's contents links resolve in the places Markdown is usually read —
|
|
36
|
+
* which is the only way this can work at all, because Markdown headings carry
|
|
37
|
+
* no explicit id to point at.
|
|
38
|
+
*/
|
|
39
|
+
function slug(text: string): string {
|
|
40
|
+
return (
|
|
41
|
+
text
|
|
42
|
+
.toLowerCase()
|
|
43
|
+
// Entities first: a heading rendered as `AT&T` should anchor on the
|
|
44
|
+
// text a reader sees, not on the escape that produced it.
|
|
45
|
+
.replace(/&[a-z]+;/gu, ' ')
|
|
46
|
+
.replace(/[^a-z0-9]+/gu, '-')
|
|
47
|
+
.replace(/^-+|-+$/gu, '') || 'section'
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Makes `id` unique within a document, the way a duplicate heading is usually handled. */
|
|
52
|
+
function unique(id: string, taken: Map<string, number>): string {
|
|
53
|
+
const seen = taken.get(id) ?? 0;
|
|
54
|
+
taken.set(id, seen + 1);
|
|
55
|
+
return seen === 0 ? id : `${id}-${String(seen)}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Gives every `<h2>` and `<h3>` an id, and reports what it found.
|
|
60
|
+
*
|
|
61
|
+
* `<h4>` is deliberately left alone: it is an individual issue, and a report
|
|
62
|
+
* with sixty findings would have a contents list longer than its summary.
|
|
63
|
+
*/
|
|
64
|
+
export function anchorHtmlHeadings(html: string): { html: string; toc: TocEntry[] } {
|
|
65
|
+
const toc: TocEntry[] = [];
|
|
66
|
+
const taken = new Map<string, number>();
|
|
67
|
+
|
|
68
|
+
// Safe over this input because it is this package's own markup rather than
|
|
69
|
+
// anything from outside: h2 and h3 are emitted with plain escaped text and
|
|
70
|
+
// never with nested elements.
|
|
71
|
+
const anchored = html.replace(
|
|
72
|
+
/<h([23])([^>]*)>([\s\S]*?)<\/h\1>/gu,
|
|
73
|
+
(_match, level: string, attrs: string, text: string) => {
|
|
74
|
+
const id = unique(slug(text), taken);
|
|
75
|
+
toc.push({ level: Number(level), id, text });
|
|
76
|
+
return `<h${level}${attrs} id="${id}">${text}</h${level}>`;
|
|
77
|
+
},
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
return { html: anchored, toc };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The same, for Markdown, where the anchor is implied by the heading text. */
|
|
84
|
+
export function markdownHeadings(lines: readonly string[]): TocEntry[] {
|
|
85
|
+
const toc: TocEntry[] = [];
|
|
86
|
+
const taken = new Map<string, number>();
|
|
87
|
+
|
|
88
|
+
for (const line of lines) {
|
|
89
|
+
const match = /^(#{2,3})\s+(.*)$/u.exec(line);
|
|
90
|
+
if (!match) continue;
|
|
91
|
+
const text = match[2];
|
|
92
|
+
toc.push({ level: match[1].length, id: unique(slug(text), taken), text });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return toc;
|
|
96
|
+
}
|