File size: 8,585 Bytes
13fb9f7
 
 
 
 
 
 
 
f8e3ab3
 
13fb9f7
f8e3ab3
 
 
13fb9f7
 
 
 
 
 
 
 
f8e3ab3
 
13fb9f7
 
 
 
 
 
 
f8e3ab3
88ae80a
 
 
f8e3ab3
 
 
13fb9f7
 
 
 
 
 
 
 
 
 
 
 
f8e3ab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13fb9f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8e3ab3
 
 
 
 
13fb9f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8e3ab3
 
 
 
 
 
 
 
13fb9f7
f8e3ab3
13fb9f7
 
 
 
 
 
 
f8e3ab3
 
 
 
13fb9f7
 
 
 
 
 
 
 
 
 
 
f8e3ab3
 
 
 
 
 
 
 
 
 
 
 
 
 
13fb9f7
 
 
f8e3ab3
 
 
 
13fb9f7
 
 
 
 
 
 
 
f8e3ab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13fb9f7
f8e3ab3
 
 
 
13fb9f7
 
f8e3ab3
 
 
13fb9f7
f8e3ab3
 
 
 
13fb9f7
 
f8e3ab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13fb9f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import * as vscode from 'vscode';

export interface Vulnerability {
	file: string;
	status: string;
	error?: string;
	result?: string;
	line?: number;
	endLine?: number;
	type?: string;
	severity?: string;
	description?: string;
	originalCode?: string;
	fixedCode?: string;
}

export class VulnerabilityItem extends vscode.TreeItem {
	constructor(
		public readonly label: string,
		public readonly collapsibleState: vscode.TreeItemCollapsibleState,
		public readonly vulnerability?: Vulnerability,
		public readonly command?: vscode.Command,
		contextValue?: string,
		public readonly index?: number
	) {
		super(label, collapsibleState);

		if (contextValue) {
			this.contextValue = contextValue;
		}

		if (vulnerability?.status === 'analyzed' && vulnerability?.type) {
			// Show vulnerability type and severity with fix indicator
			const fixAvailable = vulnerability.fixedCode ? ' ― Apply Fix β†’' : '';
			this.description = this.getSeverityLabel(vulnerability.severity) + fixAvailable;
			this.iconPath = this.getSeverityIcon(vulnerability.severity);
			this.tooltip = this.buildTooltip(vulnerability);
		} else if (vulnerability?.status === 'analyzed' && vulnerability?.result) {
			this.description = 'Click to apply fix';
			this.iconPath = new vscode.ThemeIcon('error', new vscode.ThemeColor('errorForeground'));
			this.tooltip = vulnerability.result.substring(0, 500);
		} else if (vulnerability?.status === 'error') {
			this.description = 'Analysis error';
			this.iconPath = new vscode.ThemeIcon('warning', new vscode.ThemeColor('warningForeground'));
			this.tooltip = vulnerability.error || 'Unknown error';
		} else {
			this.iconPath = new vscode.ThemeIcon('file-code');
			this.tooltip = 'Click to open file';
		}
	}

	private getSeverityLabel(severity?: string): string {
		switch (severity) {
			case 'critical': return 'πŸ”΄ Critical';
			case 'high': return '🟠 High';
			case 'medium': return '🟑 Medium';
			case 'low': return '🟒 Low';
			default: return 'Issue';
		}
	}

	private getSeverityIcon(severity?: string): vscode.ThemeIcon {
		switch (severity) {
			case 'critical':
				return new vscode.ThemeIcon('error', new vscode.ThemeColor('errorForeground'));
			case 'high':
				return new vscode.ThemeIcon('warning', new vscode.ThemeColor('problemsWarningIcon.foreground'));
			case 'medium':
				return new vscode.ThemeIcon('info', new vscode.ThemeColor('notificationsInfoIcon.foreground'));
			case 'low':
				return new vscode.ThemeIcon('pass', new vscode.ThemeColor('testing.iconPassed'));
			default:
				return new vscode.ThemeIcon('circle-outline');
		}
	}

	private buildTooltip(vuln: Vulnerability): string {
		const lines = [
			`Type: ${vuln.type || 'Unknown'}`,
			`Severity: ${vuln.severity || 'Unknown'}`,
			`Location: Line ${vuln.line}${vuln.endLine && vuln.endLine !== vuln.line ? `-${vuln.endLine}` : ''}`,
			'',
			'Original Code:',
			vuln.originalCode || '(not available)',
			'',
			'Fixed Code:',
			vuln.fixedCode || '(not available)'
		];
		return lines.join('\n');
	}
}

export class VulnerabilityTreeDataProvider implements vscode.TreeDataProvider<VulnerabilityItem> {
	private _onDidChangeTreeData: vscode.EventEmitter<VulnerabilityItem | undefined | null | void> =
		new vscode.EventEmitter<VulnerabilityItem | undefined | null | void>();
	readonly onDidChangeTreeData: vscode.Event<VulnerabilityItem | undefined | null | void> =
		this._onDidChangeTreeData.event;

	private vulnerabilities: Vulnerability[] = [];

	setVulnerabilities(vulnerabilities: Vulnerability[]): void {
		this.vulnerabilities = vulnerabilities;
		this._onDidChangeTreeData.fire(null);
	}

	getVulnerabilitiesForFile(fileName: string): Vulnerability[] {
		return this.vulnerabilities.filter((v: Vulnerability) => {
			const vulnFileName = v.file.split('\\').pop()?.split('/').pop() || v.file;
			return vulnFileName === fileName;
		});
	}

	getVulnerabilityByIndex(fileName: string, index: number): Vulnerability | undefined {
		const fileVulns = this.getVulnerabilitiesForFile(fileName);
		return fileVulns[index];
	}

	getTreeItem(element: VulnerabilityItem): vscode.TreeItem {
		return element;
	}

	getChildren(element?: VulnerabilityItem): Thenable<VulnerabilityItem[]> {
		if (!element) {
			// Root level - show list of files with vulnerabilities
			const fileMap = new Map<string, Vulnerability[]>();

			this.vulnerabilities.forEach((vuln: Vulnerability) => {
				if (!fileMap.has(vuln.file)) {
					fileMap.set(vuln.file, []);
				}
				fileMap.get(vuln.file)!.push(vuln);
			});

			const items = Array.from(fileMap.entries()).map(
				([file, vulns]) => {
					const errorCount = vulns.filter(v => v.status === 'error').length;
					const issueCount = vulns.filter(v => v.status === 'analyzed').length;
					const description = errorCount > 0
						? `${issueCount} issues, ${errorCount} errors`
						: `${issueCount} issue${issueCount !== 1 ? 's' : ''}`;

					const item = new VulnerabilityItem(
						this.getFileName(file),
						vscode.TreeItemCollapsibleState.Expanded,
						undefined,
						{
							command: 'vscode.open',
							title: 'Open file',
							arguments: [vscode.Uri.file(file)],
						},
						'file'
					);
					item.description = description;
					return item;
				}
			);

			return Promise.resolve(items);
		} else if (element.vulnerability === undefined) {
			// File level - show vulnerabilities in this file
			const fileName = element.label;
			const fileVulns = this.vulnerabilities.filter((v: Vulnerability) =>
				this.getFileName(v.file) === fileName
			);

			const items = fileVulns.map(
				(vuln, index) => {
					// Build a descriptive label
					let label: string;
					if (vuln.type) {
						label = vuln.type;
						if (vuln.line) {
							label += ` (Line ${vuln.line})`;
						}
					} else {
						label = `Issue ${index + 1}${vuln.status === 'error' ? ' (Error)' : ''}`;
					}

					return new VulnerabilityItem(
						label,
						vscode.TreeItemCollapsibleState.Collapsed,
						vuln,
						undefined,
						vuln.status === 'analyzed' ? 'vulnerability' : undefined,
						index
					);
				}
			);

			return Promise.resolve(items);
		} else {
			// Vulnerability level - show details
			const vuln = element.vulnerability;
			const items: VulnerabilityItem[] = [];

			if (vuln.status === 'analyzed') {
				// Show vulnerability details
				if (vuln.description) {
					const descItem = new VulnerabilityItem(
						`πŸ“‹ ${vuln.description}`,
						vscode.TreeItemCollapsibleState.None,
						undefined
					);
					items.push(descItem);
				}

				// Show original code
				if (vuln.originalCode) {
					const origHeader = new VulnerabilityItem(
						'❌ Vulnerable Code:',
						vscode.TreeItemCollapsibleState.None,
						undefined
					);
					items.push(origHeader);

					const origLines = vuln.originalCode.split('\n').slice(0, 5);
					const origPreview = origLines.map(l => `  ${l}`).join('\n');
					const origItem = new VulnerabilityItem(
						origPreview + (vuln.originalCode.split('\n').length > 5 ? '\n  ...' : ''),
						vscode.TreeItemCollapsibleState.None,
						undefined
					);
					items.push(origItem);
				}

				// Show fixed code
				if (vuln.fixedCode) {
					const fixHeader = new VulnerabilityItem(
						'βœ… Fixed Code:',
						vscode.TreeItemCollapsibleState.None,
						undefined
					);
					items.push(fixHeader);

					const fixLines = vuln.fixedCode.split('\n').slice(0, 5);
					const fixPreview = fixLines.map(l => `  ${l}`).join('\n');
					const fixItem = new VulnerabilityItem(
						fixPreview + (vuln.fixedCode.split('\n').length > 5 ? '\n  ...' : ''),
						vscode.TreeItemCollapsibleState.None,
						undefined
					);
					items.push(fixItem);
				} else if (vuln.result) {
					// Fallback to old format
					const lines = vuln.result.split('\n');
					const previewLines = lines.slice(0, 10);
					const preview = previewLines.join('\n') + (lines.length > 10 ? '\n...' : '');

					items.push(
						new VulnerabilityItem(
							'Fixed Code Preview:',
							vscode.TreeItemCollapsibleState.None,
							undefined
						)
					);

					items.push(
						new VulnerabilityItem(
							preview,
							vscode.TreeItemCollapsibleState.None,
							undefined
						)
					);
				}
			} else if (vuln.error) {
				items.push(
					new VulnerabilityItem(
						`Error: ${vuln.error}`,
						vscode.TreeItemCollapsibleState.None,
						undefined
					)
				);
			}

			return Promise.resolve(items);
		}
	}

	private getFileName(filePath: string): string {
		return filePath.split('\\').pop()?.split('/').pop() || filePath;
	}
}