summaryrefslogtreecommitdiff
path: root/plugins/claude-security/scripts/views/term/scan.mjs
blob: ea61d8e6a70f4bce36f448e8e40915aadfe7919e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { sum, whole } from './numbers.mjs';
import { Widths } from './text.mjs';
export const LENSES = ['reachability', 'impact', 'defenses'];
export const SEVERITIES = ['CRITICAL', 'HIGH', 'MEDIUM', 'LOW'];
export const STAGES = ['Setup', 'Inventory', 'Sweep', 'Research', 'Panel', 'Report'];
const STATES = ['waiting', 'running', 'done', 'failed', 'skipped'];
const VOTES = ['pending', 'confirm', 'refute', 'abstain'];
const OUTCOMES = ['testing', 'confirmed', 'refuted', 'unsettled'];
const RECENT_TOUCHES = 600;
const RECENT_NOTES = 400;
const RANK = { running: 3, waiting: 2, failed: 1, done: 0, skipped: 0 };
export const fields = (json) => typeof json === 'object' && json !== null && !Array.isArray(json) ? json : {};
export const text = (json) => typeof json === 'string' ? json.replace(/[\ud800-\udfff]/gu, '\ufffd') : '';
const items = (json) => (Array.isArray(json) ? json : []);
const texts = (json) => items(json).map(text);
const number = (json) => typeof json === 'number' && Number.isFinite(json) ? json : undefined;
const count = (json) => whole(number(json) ?? 0);
export const index = (json, length) => typeof json === 'number' && json >= 0 && json < length ? json | 0 : undefined;
const oneOf = (names, json) => names.find(name => name === json) ?? names[0];
const pair = (json) => Array.isArray(json) && json.length === 2 ? [count(json[0]), count(json[1])] : undefined;
const readTarget = (json) => ({
	name: text(json.name),
	mode: text(json.mode),
	effort: text(json.effort),
	scope: texts(json.scope),
	branch: text(json.branch),
	commit: text(json.commit),
	dirty: json.dirty === true,
	model: text(json.model),
	version: text(json.version),
	sandboxed: json.sandboxed === true,
	hosts: texts(json.hosts),
	writable: texts(json.writable),
});
const readEnding = (json) => ({
	state: oneOf(STATES, json.state),
	message: text(json.message),
	reportDir: text(json.report_dir),
	files: texts(json.files),
	verification: text(json.verification),
});
const readPhase = (json) => ({
	stage: oneOf(STAGES, json.stage),
	state: oneOf(STATES, json.state),
	started: number(json.started),
	ended: number(json.ended),
});
const readTotals = (json) => ({
	agents: count(json.agents),
	running: count(json.running),
	done: count(json.done),
	failed: count(json.failed),
	tools: count(json.tools),
	tokens: count(json.tokens),
	files: count(json.files),
	filesRead: count(json.files_read),
	lines: count(json.lines),
	linesRead: count(json.lines_read),
	leads: count(json.leads),
	candidates: count(json.candidates),
	confirmed: count(json.confirmed),
	refuted: count(json.refuted),
});
const readComponent = (json, plots) => ({
	name: text(json.name),
	language: text(json.language),
	plots: items(json.plots).flatMap(plot => index(plot, plots) ?? []),
});
const readAgent = (json, plots) => ({
	id: count(json.id),
	label: text(json.label),
	name: text(json.name),
	role: text(json.role),
	component: text(json.component),
	lens: text(json.lens),
	state: oneOf(STATES, json.state),
	started: number(json.started),
	ended: number(json.ended),
	tools: count(json.tools),
	tokens: count(json.tokens),
	plot: index(json.plot, plots),
	activity: text(json.activity),
});
const readSpot = (json, plots) => ({
	plot: index(json.plot, plots),
	plotLine: count(json.plot_line),
});
const readCandidate = (json, plots) => ({
	id: text(json.id),
	path: text(json.path),
	line: count(json.line),
	...readSpot(json, plots),
	votes: LENSES.map(lens => oneOf(VOTES, fields(json.votes)[lens])),
	outcome: oneOf(OUTCOMES, json.outcome),
	settled: number(json.settled),
});
const readFinding = (json, plots) => ({
	id: text(json.id),
	title: text(json.title),
	severity: text(json.severity),
	confidence: text(json.confidence),
	file: text(json.file),
	line: count(json.line),
	...readSpot(json, plots),
	category: text(json.category),
	cwe: text(json.cwe),
	symbol: text(json.symbol),
	snippet: text(json.snippet),
	description: text(json.description),
	impact: text(json.impact),
	scenario: text(json.exploit_scenario),
	preconditions: texts(json.preconditions),
	recommendation: text(json.recommendation),
});
const readVisit = (json) => ({
	reads: count(json.reads),
	spans: items(json.spans).flatMap(held => {
		const span = pair(held);
		return span && span[0] <= span[1] ? [span] : [];
	}),
	rev: count(json.rev),
});
function readTouch(json, plots) {
	const [from, to] = pair(json.plots) ?? [0, 0];
	const end = Math.min(to, plots);
	const start = Math.min(from, end);
	return {
		rev: count(json.rev),
		at: number(json.at) ?? 0,
		agent: count(json.agent),
		start,
		end,
		file: json.kind === 'read' && start < end ? start : undefined,
		first: count(json.first),
		last: count(json.last),
	};
}
const readNote = (json) => ({ at: number(json.at) ?? 0, text: text(json.text) });
const column = (agent) => agent.lens || agent.role;
export const crossing = (component, heading) => `${component}\0${heading}`;
const outranks = (agent, other) => RANK[agent.state] === RANK[other.state] ? agent.label < other.label : RANK[agent.state] > RANK[other.state];
function renew(held, fresh) {
	const at = held.findIndex(old => old.id === fresh.id);
	if (at < 0)
		held.push(fresh);
	else
		held[at] = fresh;
}
export class Scan {
	plots;
	widths = new Widths();
	rev = 0;
	now = 0;
	target = readTarget({});
	result = readEnding({ state: 'running' });
	phases = STAGES.map(stage => readPhase({ stage }));
	totals = readTotals({});
	components = [];
	divisions = 0;
	agents = [];
	leads = [];
	candidates = [];
	findings = [];
	visits = [];
	touches = [];
	notes = [];
	verifiers = new Map();
	constructor(plots) {
		this.plots = plots;
	}
	get running() {
		return this.result.state === 'running';
	}
	get latest() {
		return this.phases.reduce((last, phase) => (phase.started === undefined ? last : phase));
	}
	took({ started, ended }) {
		return (ended ?? this.now) - (started ?? 0);
	}
	atWork() {
		return this.agents.filter(agent => agent.state === 'running');
	}
	verifier(id, lens) {
		return this.verifiers.get(crossing(id, lens));
	}
	team() {
		return this.agents.filter(agent => agent.role === 'research');
	}
	columns() {
		return [...new Set(this.team().map(column))];
	}
	cover() {
		const place = (agent) => crossing(agent.component, column(agent));
		const attempts = new Map(this.team().map(agent => [crossing(place(agent), agent.label), agent]));
		const cover = new Map();
		for (const agent of attempts.values()) {
			const best = cover.get(place(agent));
			if (!best || outranks(agent, best))
				cover.set(place(agent), agent);
		}
		return cover;
	}
	readOf(part) {
		const spans = part.plots.flatMap(plot => this.visits[plot]?.spans ?? []);
		return [sum(spans.map(([first, last]) => last - first + 1)), sum(part.plots.map(plot => this.plots[plot].lines))];
	}
	apply(delta) {
		const plots = this.plots.length;
		const each = (list, read) => items(list).map(item => read(fields(item), plots));
		this.widths.learn(text(delta.wide), text(delta.marks));
		this.rev = count(delta.rev);
		this.now = number(delta.now) ?? this.now;
		this.target = readTarget(fields(delta.target));
		this.result = readEnding(fields(delta.result));
		this.totals = readTotals(fields(delta.totals));
		for (const phase of each(delta.phases, readPhase))
			this.phases[STAGES.indexOf(phase.stage)] = phase;
		if (Array.isArray(delta.components)) {
			this.components = each(delta.components, readComponent);
			this.divisions++;
		}
		if (Array.isArray(delta.findings))
			this.findings = each(delta.findings, readFinding);
		for (const agent of each(delta.agents, readAgent))
			renew(this.agents, agent);
		this.verifiers = new Map(this.atWork().map(agent => [crossing(agent.component, agent.lens), agent]));
		for (const one of each(delta.candidates, readCandidate))
			renew(this.candidates, one);
		for (const lead of each(delta.leads, readSpot))
			this.leads.push(lead);
		for (const visit of items(delta.visits).map(fields)) {
			const plot = index(visit.plot, plots);
			if (plot !== undefined)
				this.visits[plot] = readVisit(visit);
		}
		this.touches = this.touches.concat(each(delta.touches, readTouch)).slice(-RECENT_TOUCHES);
		this.notes = this.notes.concat(each(delta.notes, readNote)).slice(-RECENT_NOTES);
	}
}