@secureport/core 0.2.1 → 0.4.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 +146 -33
- package/dist/coverage.d.ts +21 -0
- package/dist/coverage.d.ts.map +1 -0
- package/dist/coverage.js +65 -0
- package/dist/coverage.js.map +1 -0
- package/dist/finding.d.ts +89 -0
- package/dist/finding.d.ts.map +1 -0
- package/dist/finding.js +2 -0
- package/dist/finding.js.map +1 -0
- package/dist/fingerprint.d.ts +185 -0
- package/dist/fingerprint.d.ts.map +1 -0
- package/dist/fingerprint.js +247 -0
- package/dist/fingerprint.js.map +1 -0
- package/dist/import/burp.d.ts +19 -0
- package/dist/import/burp.d.ts.map +1 -0
- package/dist/import/burp.js +114 -0
- package/dist/import/burp.js.map +1 -0
- package/dist/import/generic.d.ts +90 -0
- package/dist/import/generic.d.ts.map +1 -0
- package/dist/import/generic.js +159 -0
- package/dist/import/generic.js.map +1 -0
- package/dist/import/nessus.d.ts +32 -0
- package/dist/import/nessus.d.ts.map +1 -0
- package/dist/import/nessus.js +125 -0
- package/dist/import/nessus.js.map +1 -0
- package/dist/import/nuclei.d.ts +39 -0
- package/dist/import/nuclei.d.ts.map +1 -0
- package/dist/import/nuclei.js +115 -0
- package/dist/import/nuclei.js.map +1 -0
- package/dist/import/xml.d.ts +47 -0
- package/dist/import/xml.d.ts.map +1 -0
- package/dist/import/xml.js +157 -0
- package/dist/import/xml.js.map +1 -0
- package/dist/import/zap.d.ts +26 -0
- package/dist/import/zap.d.ts.map +1 -0
- package/dist/import/zap.js +119 -0
- package/dist/import/zap.js.map +1 -0
- package/dist/index.d.ts +41 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +29 -2
- package/dist/index.js.map +1 -1
- package/dist/issue.d.ts +190 -11
- package/dist/issue.d.ts.map +1 -1
- package/dist/reconcile.d.ts +115 -10
- package/dist/reconcile.d.ts.map +1 -1
- package/dist/reconcile.js +306 -12
- package/dist/reconcile.js.map +1 -1
- package/dist/report/html.d.ts +21 -0
- package/dist/report/html.d.ts.map +1 -0
- package/dist/report/html.js +324 -0
- package/dist/report/html.js.map +1 -0
- package/dist/report/json.d.ts +81 -0
- package/dist/report/json.d.ts.map +1 -0
- package/dist/report/json.js +47 -0
- package/dist/report/json.js.map +1 -0
- package/dist/report/markdown.d.ts +24 -0
- package/dist/report/markdown.d.ts.map +1 -0
- package/dist/report/markdown.js +304 -0
- package/dist/report/markdown.js.map +1 -0
- package/dist/report/model.d.ts +215 -0
- package/dist/report/model.d.ts.map +1 -0
- package/dist/report/model.js +197 -0
- package/dist/report/model.js.map +1 -0
- package/dist/run.d.ts +134 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +2 -0
- package/dist/run.js.map +1 -0
- package/dist/severity.d.ts +191 -0
- package/dist/severity.d.ts.map +1 -0
- package/dist/severity.js +171 -0
- package/dist/severity.js.map +1 -0
- package/dist/snapshot-builder.d.ts +78 -0
- package/dist/snapshot-builder.d.ts.map +1 -0
- package/dist/snapshot-builder.js +172 -0
- package/dist/snapshot-builder.js.map +1 -0
- package/dist/snapshot.d.ts +125 -0
- package/dist/snapshot.d.ts.map +1 -0
- package/dist/snapshot.js +2 -0
- package/dist/snapshot.js.map +1 -0
- package/package.json +5 -4
- package/src/coverage.ts +65 -0
- package/src/finding.ts +112 -0
- package/src/fingerprint.ts +315 -0
- package/src/import/burp.ts +126 -0
- package/src/import/generic.ts +258 -0
- package/src/import/nessus.ts +136 -0
- package/src/import/nuclei.ts +173 -0
- package/src/import/xml.ts +187 -0
- package/src/import/zap.ts +161 -0
- package/src/index.ts +75 -2
- package/src/issue.ts +244 -11
- package/src/reconcile.ts +421 -17
- package/src/report/html.ts +449 -0
- package/src/report/json.ts +134 -0
- package/src/report/markdown.ts +435 -0
- package/src/report/model.ts +462 -0
- package/src/run.ts +163 -0
- package/src/severity.ts +250 -0
- package/src/snapshot-builder.ts +225 -0
- package/src/snapshot.ts +146 -0
package/src/severity.ts
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How urgent an issue is.
|
|
3
|
+
*
|
|
4
|
+
* Five levels, ordered most to least urgent. `advisory` is the bottom of the
|
|
5
|
+
* scale rather than a separate "informational" category: it carries a weight of
|
|
6
|
+
* zero in the exposure score and no SLA, but it is still an issue, still
|
|
7
|
+
* tracked across runs, and still appears in reports.
|
|
8
|
+
*
|
|
9
|
+
* @see {@link SEVERITY_ORDER} for comparing two severities.
|
|
10
|
+
* @see {@link SEVERITY_WEIGHTS} for the exposure-score weights.
|
|
11
|
+
*/
|
|
12
|
+
export type Severity = 'critical' | 'high' | 'medium' | 'low' | 'advisory';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Every {@link Severity}, most urgent first.
|
|
16
|
+
*
|
|
17
|
+
* Iteration order is part of the contract: report sections, severity
|
|
18
|
+
* breakdowns and count tables all render in this order, so a reader sees the
|
|
19
|
+
* same shape everywhere.
|
|
20
|
+
*/
|
|
21
|
+
export const SEVERITY_ORDER = ['critical', 'high', 'medium', 'low', 'advisory'] as const;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Rank of a {@link Severity}, where a **higher number is more urgent**.
|
|
25
|
+
*
|
|
26
|
+
* Use this to compare severities rather than comparing the strings, which sort
|
|
27
|
+
* alphabetically and would put `advisory` above `critical`.
|
|
28
|
+
*
|
|
29
|
+
* @param severity - The severity to rank.
|
|
30
|
+
* @returns `4` for `critical` down to `0` for `advisory`.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* severityRank(finding.detectedSeverity) > severityRank(issue.severityAtIgnore);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function severityRank(severity: Severity): number {
|
|
38
|
+
return SEVERITY_ORDER.length - 1 - SEVERITY_ORDER.indexOf(severity);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Exposure-score weight for each {@link Severity}.
|
|
43
|
+
*
|
|
44
|
+
* `exposure_score = Σ over open issues of weight × min(daysOpen, 90)`.
|
|
45
|
+
*
|
|
46
|
+
* `advisory` weighs nothing, so advisories never inflate the number — they are
|
|
47
|
+
* tracked and reported, but they do not represent exposure.
|
|
48
|
+
*
|
|
49
|
+
* These weights are **placeholders** until ten design partners have looked at
|
|
50
|
+
* the number, and are stated as such in `00-DOMAIN.md` §8. Treat a change to
|
|
51
|
+
* them as a change to a published contract.
|
|
52
|
+
*/
|
|
53
|
+
export const SEVERITY_WEIGHTS: Readonly<Record<Severity, number>> = Object.freeze({
|
|
54
|
+
critical: 10,
|
|
55
|
+
high: 5,
|
|
56
|
+
medium: 2,
|
|
57
|
+
low: 1,
|
|
58
|
+
advisory: 0,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Why a finding was given the severity it has.
|
|
63
|
+
*
|
|
64
|
+
* Recorded on every finding because auditors ask, and because a severity with
|
|
65
|
+
* no provenance is not evidence. Listed here in precedence order — an earlier
|
|
66
|
+
* source wins over a later one:
|
|
67
|
+
*
|
|
68
|
+
* 1. `explicit` — stated by the scanner or the operator for this exact finding.
|
|
69
|
+
* 2. `cvss` — derived from a CVSS base score via {@link severityFromCvss}.
|
|
70
|
+
* 3. `engine_default` — the engine's own rating for the rule that fired.
|
|
71
|
+
* 4. `advisory` — taken from a published advisory for the associated CVE.
|
|
72
|
+
*
|
|
73
|
+
* Note the unfortunate collision: `advisory` is both the lowest
|
|
74
|
+
* {@link Severity} and the least-preferred severity *source*. They are
|
|
75
|
+
* unrelated — a finding can be `critical` from an `advisory` source. The names
|
|
76
|
+
* come from `00-DOMAIN.md` §3 and are kept so code and document agree.
|
|
77
|
+
*/
|
|
78
|
+
export type SeveritySource = 'explicit' | 'cvss' | 'engine_default' | 'advisory';
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Every {@link SeveritySource}, most authoritative first.
|
|
82
|
+
*
|
|
83
|
+
* The order is the precedence rule: when two sources offer a severity for the
|
|
84
|
+
* same finding, the one appearing earlier here wins.
|
|
85
|
+
*/
|
|
86
|
+
export const SEVERITY_SOURCE_PRECEDENCE = [
|
|
87
|
+
'explicit',
|
|
88
|
+
'cvss',
|
|
89
|
+
'engine_default',
|
|
90
|
+
'advisory',
|
|
91
|
+
] as const;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Maps a CVSS base score to a {@link Severity}.
|
|
95
|
+
*
|
|
96
|
+
* Uses the CVSS v3.1 qualitative severity rating scale unchanged, so a score
|
|
97
|
+
* rated "High" by any other tool is rated `high` here. The one adaptation is
|
|
98
|
+
* at the bottom: CVSS calls `0.0` "None", and this model has no "none", so it
|
|
99
|
+
* becomes `advisory`.
|
|
100
|
+
*
|
|
101
|
+
* | CVSS score | CVSS rating | {@link Severity} |
|
|
102
|
+
* | ----------- | ----------- | ---------------- |
|
|
103
|
+
* | 9.0 – 10.0 | Critical | `critical` |
|
|
104
|
+
* | 7.0 – 8.9 | High | `high` |
|
|
105
|
+
* | 4.0 – 6.9 | Medium | `medium` |
|
|
106
|
+
* | 0.1 – 3.9 | Low | `low` |
|
|
107
|
+
* | 0.0 | None | `advisory` |
|
|
108
|
+
*
|
|
109
|
+
* @param score - A CVSS base score between 0 and 10.
|
|
110
|
+
* @returns The corresponding severity.
|
|
111
|
+
* @throws RangeError If `score` is outside 0–10 or is not a number. A score
|
|
112
|
+
* that cannot be mapped is a data problem worth surfacing, not something to
|
|
113
|
+
* silently round into `advisory`.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* severityFromCvss(9.8); // 'critical'
|
|
118
|
+
* severityFromCvss(0); // 'advisory'
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
export function severityFromCvss(score: number): Severity {
|
|
122
|
+
if (!Number.isFinite(score) || score < 0 || score > 10) {
|
|
123
|
+
throw new RangeError(`CVSS score must be between 0 and 10, received ${String(score)}`);
|
|
124
|
+
}
|
|
125
|
+
if (score >= 9) return 'critical';
|
|
126
|
+
if (score >= 7) return 'high';
|
|
127
|
+
if (score >= 4) return 'medium';
|
|
128
|
+
if (score > 0) return 'low';
|
|
129
|
+
return 'advisory';
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* How long each severity may stay open before it breaches, and how long before
|
|
134
|
+
* breaching an issue starts warning.
|
|
135
|
+
*
|
|
136
|
+
* A policy rather than a constant because remediation windows are a customer
|
|
137
|
+
* agreement, not a property of the domain: a payments company and a hobby
|
|
138
|
+
* project do not owe the same turnaround on a `high`.
|
|
139
|
+
*
|
|
140
|
+
* @see {@link DEFAULT_SLA_POLICY} for the shipped default.
|
|
141
|
+
*/
|
|
142
|
+
export interface SlaPolicy {
|
|
143
|
+
/**
|
|
144
|
+
* Days allowed to remediate an issue of each severity, from `firstSeen`.
|
|
145
|
+
*
|
|
146
|
+
* `null` means no deadline applies — the issue is tracked and reported, but
|
|
147
|
+
* it can never be "due" or "breached". `advisory` is `null` by default.
|
|
148
|
+
*/
|
|
149
|
+
readonly durationDays: Readonly<Record<Severity, number | null>>;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* How many days before the deadline an issue starts reporting `due_soon`.
|
|
153
|
+
*
|
|
154
|
+
* Exists so a report can distinguish "you have time" from "this is about to
|
|
155
|
+
* breach" without the reader doing arithmetic.
|
|
156
|
+
*/
|
|
157
|
+
readonly dueSoonDays: number;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The default remediation windows.
|
|
162
|
+
*
|
|
163
|
+
* Chosen to be defensible rather than derived: they line up with the windows
|
|
164
|
+
* most vulnerability-management programmes already run, so a customer can
|
|
165
|
+
* adopt them without an argument and override them without a migration.
|
|
166
|
+
*
|
|
167
|
+
* `advisory` has no deadline at all. Giving a zero-weight severity a due date
|
|
168
|
+
* would manufacture breaches out of things nobody agreed to fix.
|
|
169
|
+
*
|
|
170
|
+
* | Severity | Window |
|
|
171
|
+
* | ---------- | -------- |
|
|
172
|
+
* | `critical` | 7 days |
|
|
173
|
+
* | `high` | 30 days |
|
|
174
|
+
* | `medium` | 90 days |
|
|
175
|
+
* | `low` | 180 days |
|
|
176
|
+
* | `advisory` | none |
|
|
177
|
+
*
|
|
178
|
+
* **These are defaults, not the domain.** `00-DOMAIN.md` says only that
|
|
179
|
+
* `sla_due_at` is "derived from effective severity"; the numbers are this
|
|
180
|
+
* package's opinion and are meant to be replaced by an org's own policy.
|
|
181
|
+
*/
|
|
182
|
+
export const DEFAULT_SLA_POLICY: SlaPolicy = Object.freeze({
|
|
183
|
+
durationDays: Object.freeze({
|
|
184
|
+
critical: 7,
|
|
185
|
+
high: 30,
|
|
186
|
+
medium: 90,
|
|
187
|
+
low: 180,
|
|
188
|
+
advisory: null,
|
|
189
|
+
}),
|
|
190
|
+
dueSoonDays: 7,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Where an issue stands against its remediation deadline.
|
|
195
|
+
*
|
|
196
|
+
* - `within` — inside the window, and not close enough to warn about.
|
|
197
|
+
* - `due_soon` — inside the window but within {@link SlaPolicy.dueSoonDays}.
|
|
198
|
+
* - `breached` — past the deadline.
|
|
199
|
+
*
|
|
200
|
+
* An issue with no deadline (see {@link DEFAULT_SLA_POLICY}) is always
|
|
201
|
+
* `within`: it cannot breach something it was never given.
|
|
202
|
+
*/
|
|
203
|
+
export type SlaStatus = 'within' | 'due_soon' | 'breached';
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* When an issue of this severity, first seen at this moment, is due.
|
|
207
|
+
*
|
|
208
|
+
* Pure: the deadline is computed from the arguments alone, so the same inputs
|
|
209
|
+
* always give the same answer and a test needs no control over the clock.
|
|
210
|
+
*
|
|
211
|
+
* @param severity - The issue's **effective** severity, not its detected one —
|
|
212
|
+
* an override is a deliberate statement about how urgent something is, and the
|
|
213
|
+
* deadline should follow it.
|
|
214
|
+
* @param firstSeen - When the issue was first seen. Never the current run:
|
|
215
|
+
* `first_seen` is never reset, so neither is the deadline (invariant 4).
|
|
216
|
+
* @param policy - The remediation windows to apply.
|
|
217
|
+
* @returns The deadline, or `null` where the severity has no window.
|
|
218
|
+
*/
|
|
219
|
+
export function slaDueAt(
|
|
220
|
+
severity: Severity,
|
|
221
|
+
firstSeen: Date,
|
|
222
|
+
policy: SlaPolicy = DEFAULT_SLA_POLICY,
|
|
223
|
+
): Date | null {
|
|
224
|
+
const days = policy.durationDays[severity];
|
|
225
|
+
if (days === null) return null;
|
|
226
|
+
return new Date(firstSeen.getTime() + days * 24 * 60 * 60 * 1000);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Where an issue stands against a deadline, as of a given moment.
|
|
231
|
+
*
|
|
232
|
+
* Pure, and takes `now` explicitly rather than reading the clock, so a report
|
|
233
|
+
* rendered for a past run reports the status *as of that run* rather than as of
|
|
234
|
+
* today.
|
|
235
|
+
*
|
|
236
|
+
* @param dueAt - The deadline from {@link slaDueAt}, or `null` for no deadline.
|
|
237
|
+
* @param now - The moment to evaluate against.
|
|
238
|
+
* @param policy - Supplies the `due_soon` window.
|
|
239
|
+
* @returns The status. Always `within` when `dueAt` is `null`.
|
|
240
|
+
*/
|
|
241
|
+
export function slaStatus(
|
|
242
|
+
dueAt: Date | null,
|
|
243
|
+
now: Date,
|
|
244
|
+
policy: SlaPolicy = DEFAULT_SLA_POLICY,
|
|
245
|
+
): SlaStatus {
|
|
246
|
+
if (dueAt === null) return 'within';
|
|
247
|
+
if (now.getTime() > dueAt.getTime()) return 'breached';
|
|
248
|
+
const warnFrom = dueAt.getTime() - policy.dueSoonDays * 24 * 60 * 60 * 1000;
|
|
249
|
+
return now.getTime() >= warnFrom ? 'due_soon' : 'within';
|
|
250
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import type { Finding } from './finding.js';
|
|
2
|
+
import type { Issue } from './issue.js';
|
|
3
|
+
import type { Run } from './run.js';
|
|
4
|
+
import type { IssueChange, Snapshot, SnapshotIssue, SuppressedIssue, Target } from './snapshot.js';
|
|
5
|
+
import { DEFAULT_SLA_POLICY, slaStatus, type SlaPolicy } from './severity.js';
|
|
6
|
+
import { reconcile } from './reconcile.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Everything needed to turn a run's findings into a report-ready snapshot.
|
|
10
|
+
*/
|
|
11
|
+
export interface BuildSnapshotInput {
|
|
12
|
+
/** The system under test. */
|
|
13
|
+
readonly target: Target;
|
|
14
|
+
|
|
15
|
+
/** The run being reported on. */
|
|
16
|
+
readonly run: Run;
|
|
17
|
+
|
|
18
|
+
/** The findings it produced. */
|
|
19
|
+
readonly findings: readonly Finding[];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The previous snapshot, if there is one.
|
|
23
|
+
*
|
|
24
|
+
* **This is what makes a retest work with no account and no database.** The
|
|
25
|
+
* prior snapshot carries the issues, so a second run can tell what is new,
|
|
26
|
+
* what is still open, what got fixed and what came back — from a JSON file on
|
|
27
|
+
* disk. Parse it with {@link parseSnapshot}.
|
|
28
|
+
*/
|
|
29
|
+
readonly previous?: Snapshot;
|
|
30
|
+
|
|
31
|
+
/** The moment being reported at. */
|
|
32
|
+
readonly now: Date;
|
|
33
|
+
|
|
34
|
+
/** Supplies ids for issues and events. */
|
|
35
|
+
readonly newId: () => string;
|
|
36
|
+
|
|
37
|
+
/** Remediation windows. */
|
|
38
|
+
readonly slaPolicy?: SlaPolicy;
|
|
39
|
+
|
|
40
|
+
/** Consecutive covering misses before an issue resolves. */
|
|
41
|
+
readonly autoResolveThreshold?: number;
|
|
42
|
+
|
|
43
|
+
/** How long the run took. */
|
|
44
|
+
readonly durationMs?: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The key a finding and an issue share when they are the same issue.
|
|
49
|
+
*
|
|
50
|
+
* Mirrors the one reconciliation uses, including the version: a fingerprint
|
|
51
|
+
* means nothing without the algorithm that produced it. The separator is an
|
|
52
|
+
* escape rather than a literal control character — writing it as a raw byte has
|
|
53
|
+
* now put an invisible NUL into two source files, and `scripts/check-text.sh`
|
|
54
|
+
* exists because nothing else noticed.
|
|
55
|
+
*/
|
|
56
|
+
function issueKey(of: {
|
|
57
|
+
readonly fingerprintVersion: string;
|
|
58
|
+
readonly fingerprint: string;
|
|
59
|
+
}): string {
|
|
60
|
+
return `${of.fingerprintVersion}\u0000${of.fingerprint}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Whole days between two moments, floored. */
|
|
64
|
+
function daysBetween(from: Date, to: Date): number {
|
|
65
|
+
return Math.max(0, Math.floor((to.getTime() - from.getTime()) / 86_400_000));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Builds the {@link Snapshot} a report renders from.
|
|
70
|
+
*
|
|
71
|
+
* Runs {@link reconcile} over the previous snapshot's issues and this run's
|
|
72
|
+
* findings, then works out what changed for each issue and precomputes the
|
|
73
|
+
* things a template would otherwise each derive slightly differently —
|
|
74
|
+
* `daysOpen`, `slaStatus`, and which findings are the evidence.
|
|
75
|
+
*
|
|
76
|
+
* **Two runs on disk are enough.** No account, no database: the previous
|
|
77
|
+
* snapshot is the state, and this function is the only thing that has to
|
|
78
|
+
* understand it. That is the whole bring-your-own-findings path.
|
|
79
|
+
*
|
|
80
|
+
* Suppressed issues are separated out into {@link Snapshot.suppressed} and do
|
|
81
|
+
* not appear in {@link Snapshot.issues}, so a template cannot count an accepted
|
|
82
|
+
* risk as an open one (invariant 7).
|
|
83
|
+
*
|
|
84
|
+
* @param input - The run, its findings, and optionally the previous snapshot.
|
|
85
|
+
* @returns A snapshot, ready to render.
|
|
86
|
+
*/
|
|
87
|
+
export function buildSnapshot(input: BuildSnapshotInput): Snapshot {
|
|
88
|
+
const slaPolicy = input.slaPolicy ?? DEFAULT_SLA_POLICY;
|
|
89
|
+
|
|
90
|
+
const priorIssues: Issue[] = [
|
|
91
|
+
...(input.previous?.issues.map((i) => i.issue) ?? []),
|
|
92
|
+
...(input.previous?.suppressed.map((s) => s.issue) ?? []),
|
|
93
|
+
];
|
|
94
|
+
const priorById = new Map(priorIssues.map((i) => [i.id, i]));
|
|
95
|
+
|
|
96
|
+
const { issues, summary } = reconcile({
|
|
97
|
+
issues: priorIssues,
|
|
98
|
+
findings: input.findings,
|
|
99
|
+
run: input.run,
|
|
100
|
+
now: input.now,
|
|
101
|
+
newId: input.newId,
|
|
102
|
+
slaPolicy,
|
|
103
|
+
...(input.autoResolveThreshold === undefined
|
|
104
|
+
? {}
|
|
105
|
+
: { autoResolveThreshold: input.autoResolveThreshold }),
|
|
106
|
+
...(input.durationMs === undefined ? {} : { durationMs: input.durationMs }),
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// Evidence, grouped by the issue it belongs to.
|
|
110
|
+
const byFingerprint = new Map<string, Finding[]>();
|
|
111
|
+
for (const finding of input.findings) {
|
|
112
|
+
const key = issueKey(finding);
|
|
113
|
+
const existing = byFingerprint.get(key);
|
|
114
|
+
if (existing) existing.push(finding);
|
|
115
|
+
else byFingerprint.set(key, [finding]);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const reported: SnapshotIssue[] = [];
|
|
119
|
+
const suppressed: SuppressedIssue[] = [];
|
|
120
|
+
|
|
121
|
+
for (const issue of issues) {
|
|
122
|
+
if (issue.status === 'ignored') {
|
|
123
|
+
suppressed.push({
|
|
124
|
+
issue,
|
|
125
|
+
reason: issue.ignoreReason ?? 'accepted_risk',
|
|
126
|
+
comment: issue.ignoreComment ?? '',
|
|
127
|
+
ignoredBy: issue.ignoredBy ?? 'unknown',
|
|
128
|
+
ignoredAt: issue.lastSeen,
|
|
129
|
+
...(issue.ignoreExpiresAt === undefined ? {} : { expiresAt: issue.ignoreExpiresAt }),
|
|
130
|
+
severityAtIgnore: issue.severityAtIgnore ?? issue.effectiveSeverity,
|
|
131
|
+
});
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const prior = priorById.get(issue.id);
|
|
136
|
+
let change: IssueChange;
|
|
137
|
+
if (prior === undefined) change = 'new';
|
|
138
|
+
else if (issue.status === 'resolved') change = 'resolved';
|
|
139
|
+
else if (issue.status === 'regressed' && prior.status !== 'regressed') change = 'regressed';
|
|
140
|
+
else change = 'still_open';
|
|
141
|
+
|
|
142
|
+
reported.push({
|
|
143
|
+
issue,
|
|
144
|
+
change,
|
|
145
|
+
// From firstSeen, which is never reset, so a regression does not make a
|
|
146
|
+
// year-old problem look new.
|
|
147
|
+
daysOpen: daysBetween(issue.firstSeen, input.now),
|
|
148
|
+
slaStatus: slaStatus(issue.slaDueAt ?? null, input.now, slaPolicy),
|
|
149
|
+
findings: byFingerprint.get(issueKey(issue)) ?? [],
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
run: summary,
|
|
155
|
+
...(input.previous === undefined ? {} : { baseline: input.previous.run }),
|
|
156
|
+
target: input.target,
|
|
157
|
+
issues: reported,
|
|
158
|
+
suppressed,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Every field in the snapshot tree that JSON turns into a string. */
|
|
163
|
+
const DATE_FIELDS = new Set([
|
|
164
|
+
'createdAt',
|
|
165
|
+
'firstSeen',
|
|
166
|
+
'lastSeen',
|
|
167
|
+
'resolvedAt',
|
|
168
|
+
'reopenedAt',
|
|
169
|
+
'slaDueAt',
|
|
170
|
+
'severityOverriddenAt',
|
|
171
|
+
'ignoreExpiresAt',
|
|
172
|
+
'ignoredAt',
|
|
173
|
+
'startedAt',
|
|
174
|
+
'finishedAt',
|
|
175
|
+
]);
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Parses a snapshot written to disk, restoring the `Date` objects JSON lost.
|
|
179
|
+
*
|
|
180
|
+
* `JSON.stringify` turns every date into a string and `JSON.parse` leaves it as
|
|
181
|
+
* one, so a snapshot round-tripped through a file is **not** a `Snapshot` — its
|
|
182
|
+
* dates are strings wearing the type. Everything that then compares or
|
|
183
|
+
* subtracts them silently produces nonsense rather than failing.
|
|
184
|
+
*
|
|
185
|
+
* Use this rather than `JSON.parse` for any snapshot read back from disk.
|
|
186
|
+
*
|
|
187
|
+
* **It checks the shape, because this is where untrusted JSON enters.** A
|
|
188
|
+
* snapshot arrives from a file somebody chose, and the signature promises a
|
|
189
|
+
* `Snapshot` — so returning whatever parsed and letting the caller discover
|
|
190
|
+
* the difference is the function lying about its own return type. The check is
|
|
191
|
+
* deliberately shallow: the top-level shape, which is enough to tell a
|
|
192
|
+
* snapshot from any other JSON document and to make the failure say so.
|
|
193
|
+
*
|
|
194
|
+
* @param json - The contents of a snapshot file.
|
|
195
|
+
* @returns The snapshot, with dates revived.
|
|
196
|
+
* @throws SyntaxError If the text is not valid JSON.
|
|
197
|
+
* @throws TypeError If the JSON parsed but is not a snapshot.
|
|
198
|
+
*/
|
|
199
|
+
export function parseSnapshot(json: string): Snapshot {
|
|
200
|
+
const parsed: unknown = JSON.parse(json, (key, value: unknown) => {
|
|
201
|
+
if (DATE_FIELDS.has(key) && typeof value === 'string') {
|
|
202
|
+
const parsed = new Date(value);
|
|
203
|
+
if (!Number.isNaN(parsed.getTime())) return parsed;
|
|
204
|
+
}
|
|
205
|
+
return value;
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
|
|
209
|
+
throw new TypeError('not a snapshot: expected a JSON object');
|
|
210
|
+
}
|
|
211
|
+
const record = parsed as Record<string, unknown>;
|
|
212
|
+
for (const field of ['run', 'target'] as const) {
|
|
213
|
+
const value = record[field];
|
|
214
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
215
|
+
throw new TypeError(`not a snapshot: \`${field}\` is missing`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
for (const field of ['issues', 'suppressed'] as const) {
|
|
219
|
+
if (!Array.isArray(record[field])) {
|
|
220
|
+
throw new TypeError(`not a snapshot: \`${field}\` is missing`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return parsed as Snapshot;
|
|
225
|
+
}
|
package/src/snapshot.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type { Finding } from './finding.js';
|
|
2
|
+
import type { IgnoreReason, Issue } from './issue.js';
|
|
3
|
+
import type { RunSummary } from './run.js';
|
|
4
|
+
import type { Severity, SlaStatus } from './severity.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A system under test.
|
|
8
|
+
*
|
|
9
|
+
* Targets are the unit of authorisation as well as of organisation: staging,
|
|
10
|
+
* production and dev are separate targets with separate tokens and separate
|
|
11
|
+
* verification states.
|
|
12
|
+
*/
|
|
13
|
+
export interface Target {
|
|
14
|
+
/** Unique id. */
|
|
15
|
+
readonly id: string;
|
|
16
|
+
|
|
17
|
+
/** Organisation it belongs to. */
|
|
18
|
+
readonly orgId: string;
|
|
19
|
+
|
|
20
|
+
/** Human-readable name, as it appears on a report cover. */
|
|
21
|
+
readonly name: string;
|
|
22
|
+
|
|
23
|
+
/** What is being tested — a URL, host or repository. */
|
|
24
|
+
readonly url: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* What happened to an issue between the baseline and this run.
|
|
29
|
+
*
|
|
30
|
+
* This is the axis every report opens on, because it is the question a reader
|
|
31
|
+
* actually has: not "what is wrong" but "what changed since last time".
|
|
32
|
+
*/
|
|
33
|
+
export type IssueChange = 'new' | 'still_open' | 'regressed' | 'resolved' | 'ignored';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* An issue as a report sees it: its own state, plus everything a template
|
|
37
|
+
* would otherwise have to compute.
|
|
38
|
+
*
|
|
39
|
+
* The derived fields are computed once, when the snapshot is built, so that
|
|
40
|
+
* five report templates in three formats cannot each work out `daysOpen`
|
|
41
|
+
* slightly differently.
|
|
42
|
+
*/
|
|
43
|
+
export interface SnapshotIssue {
|
|
44
|
+
/** The issue itself. */
|
|
45
|
+
readonly issue: Issue;
|
|
46
|
+
|
|
47
|
+
/** What happened to it relative to the baseline. */
|
|
48
|
+
readonly change: IssueChange;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Whole days between `firstSeen` and the run this snapshot is for.
|
|
52
|
+
*
|
|
53
|
+
* Measured from `firstSeen`, which is never reset, so a regression does not
|
|
54
|
+
* make a year-old problem look new.
|
|
55
|
+
*/
|
|
56
|
+
readonly daysOpen: number;
|
|
57
|
+
|
|
58
|
+
/** Where it stands against its remediation deadline, as of this run. */
|
|
59
|
+
readonly slaStatus: SlaStatus;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The evidence behind it.
|
|
63
|
+
*
|
|
64
|
+
* Findings from this run, and from earlier ones where the report shows
|
|
65
|
+
* history. This is what makes a report checkable rather than merely
|
|
66
|
+
* assertive.
|
|
67
|
+
*/
|
|
68
|
+
readonly findings: readonly Finding[];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A suppressed issue, as the appendix shows it.
|
|
73
|
+
*
|
|
74
|
+
* Deliberately its own shape rather than a flag on {@link SnapshotIssue}: the
|
|
75
|
+
* suppressed appendix answers a different question — not "what is wrong" but
|
|
76
|
+
* "what did you decide not to fix, and who decided it".
|
|
77
|
+
*
|
|
78
|
+
* **Every report carries this appendix, on by default.** It is an
|
|
79
|
+
* accepted-risk register, which is exactly what an auditor asks for, and
|
|
80
|
+
* hiding it would be both less useful and less honest.
|
|
81
|
+
*/
|
|
82
|
+
export interface SuppressedIssue {
|
|
83
|
+
/** The issue itself. */
|
|
84
|
+
readonly issue: Issue;
|
|
85
|
+
|
|
86
|
+
/** Why it was suppressed. */
|
|
87
|
+
readonly reason: IgnoreReason;
|
|
88
|
+
|
|
89
|
+
/** The justification given at the time. */
|
|
90
|
+
readonly comment: string;
|
|
91
|
+
|
|
92
|
+
/** Who suppressed it. */
|
|
93
|
+
readonly ignoredBy: string;
|
|
94
|
+
|
|
95
|
+
/** When. */
|
|
96
|
+
readonly ignoredAt: Date;
|
|
97
|
+
|
|
98
|
+
/** When the suppression lapses, if it does. */
|
|
99
|
+
readonly expiresAt?: Date;
|
|
100
|
+
|
|
101
|
+
/** Detected severity at the moment it was suppressed. */
|
|
102
|
+
readonly severityAtIgnore: Severity;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Issue state as of a run, plus a baseline to compare against.
|
|
107
|
+
*
|
|
108
|
+
* **The report engine's only input.** Every template consumes a `Snapshot` and
|
|
109
|
+
* nothing else — no database handle, no network, no clock. That is what makes
|
|
110
|
+
* a report reproducible, testable against golden files, and renderable by a
|
|
111
|
+
* third party who has findings on disk and no account.
|
|
112
|
+
*
|
|
113
|
+
* **The snapshot is the artefact; a PDF is a view of it.** Reports are rendered
|
|
114
|
+
* on demand and never stored (invariant 12). Snapshots are small, deterministic
|
|
115
|
+
* JSON, so caching one is cheap where storing a rendered document would buy
|
|
116
|
+
* retention policy, signed-URL expiry, encryption review and a deletion
|
|
117
|
+
* obligation in exchange for saving a render.
|
|
118
|
+
*/
|
|
119
|
+
export interface Snapshot {
|
|
120
|
+
/** The run this snapshot describes. */
|
|
121
|
+
readonly run: RunSummary;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* What to compare against.
|
|
125
|
+
*
|
|
126
|
+
* The previous run for an ordinary report, or a specific named run for a
|
|
127
|
+
* retest. Absent for a first run, where there is nothing to compare to and
|
|
128
|
+
* every issue is `new`.
|
|
129
|
+
*/
|
|
130
|
+
readonly baseline?: RunSummary;
|
|
131
|
+
|
|
132
|
+
/** The system under test. */
|
|
133
|
+
readonly target: Target;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Every issue in scope, with what changed and the evidence for it.
|
|
137
|
+
*
|
|
138
|
+
* Excludes suppressed issues, which appear in {@link Snapshot.suppressed}
|
|
139
|
+
* instead — the two lists are disjoint by construction so a template cannot
|
|
140
|
+
* accidentally count an accepted risk as an open one.
|
|
141
|
+
*/
|
|
142
|
+
readonly issues: readonly SnapshotIssue[];
|
|
143
|
+
|
|
144
|
+
/** The accepted-risk register. */
|
|
145
|
+
readonly suppressed: readonly SuppressedIssue[];
|
|
146
|
+
}
|