Repository navigation
Expand file tree
/
Copy pathdiff.ts
More file actions
189 lines (159 loc) · 5.39 KB
/
Copy pathdiff.ts
File metadata and controls
189 lines (159 loc) · 5.39 KB
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
import * as vscode from 'vscode';
import type { Finding } from './types.ts';
export const FIX_SCHEME = 'lory-fix';
/**
* TextDocumentContentProvider providing virtual documents representing
* proposed code fixes for side-by-side diff previews.
*/
export class LoryDiffProvider implements vscode.TextDocumentContentProvider {
private readonly previews = new Map<string, string>();
private readonly onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>();
readonly onDidChange = this.onDidChangeEmitter.event;
provideTextDocumentContent(uri: vscode.Uri): string {
return this.previews.get(uri.toString()) ?? '';
}
setPreview(uri: vscode.Uri, content: string): void {
this.previews.set(uri.toString(), content);
this.onDidChangeEmitter.fire(uri);
}
clearPreview(uri: vscode.Uri): void {
this.previews.delete(uri.toString());
}
dispose(): void {
this.onDidChangeEmitter.dispose();
this.previews.clear();
}
}
/**
* Generates the full file content with the finding's proposed fix applied.
*/
export function generateFixedContent(
document: vscode.TextDocument,
finding: Finding
): string | null {
if (!finding.fix) {
return null;
}
const startLine = finding.line - 1;
const endLine = (finding.end_line ?? finding.line) - 1;
if (startLine < 0 || startLine >= document.lineCount) {
return null;
}
const clampedEndLine = Math.min(Math.max(startLine, endLine), document.lineCount - 1);
const startOffset = document.offsetAt(new vscode.Position(startLine, 0));
const endOffset = document.offsetAt(document.lineAt(clampedEndLine).range.end);
const fullText = document.getText();
const before = fullText.slice(0, startOffset);
const after = fullText.slice(endOffset);
const cleanFix = finding.fix.replace(/\r?\n$/, '');
return before + cleanFix + after;
}
/**
* Opens a side-by-side diff preview between the real document and the proposed fix,
* prompting the user to accept or reject the fix.
*/
export async function previewFindingFix(
document: vscode.TextDocument,
finding: Finding,
diffProvider: LoryDiffProvider
): Promise<void> {
if (!finding.fix) {
void vscode.window.showInformationMessage(
`No automated quick-fix available for "${finding.title}".`
);
return;
}
const fixedContent = generateFixedContent(document, finding);
if (fixedContent === null) {
void vscode.window.showErrorMessage(
`Unable to anchor fix for "${finding.title}": file has changed.`
);
return;
}
const query = `finding=${encodeURIComponent(finding.title)}&t=${Date.now()}`;
const previewUri = vscode.Uri.from({
scheme: FIX_SCHEME,
path: document.uri.path,
query,
});
diffProvider.setPreview(previewUri, fixedContent);
const title = `Original ↔ Lory Fix: ${finding.title}`;
await vscode.commands.executeCommand('vscode.diff', document.uri, previewUri, title, {
preview: true,
});
const choice = await vscode.window.showInformationMessage(
`Lory suggested fix for "${finding.title}". Apply this fix?`,
'Apply Fix',
'Keep Original'
);
if (choice === 'Apply Fix') {
await applyFixToDocument(document, finding);
diffProvider.clearPreview(previewUri);
}
}
/**
* Applies a finding's fix directly to a text document.
*/
export async function applyFixToDocument(
document: vscode.TextDocument,
finding: Finding
): Promise<boolean> {
if (!finding.fix) {
return false;
}
const startLine = finding.line - 1;
const endLine = Math.min(
(finding.end_line ?? finding.line) - 1,
document.lineCount - 1
);
if (startLine < 0 || startLine >= document.lineCount) {
return false;
}
const replaceRange = new vscode.Range(
new vscode.Position(startLine, 0),
document.lineAt(endLine).range.end
);
const cleanFix = finding.fix.replace(/\r?\n$/, '');
const edit = new vscode.WorkspaceEdit();
edit.replace(document.uri, replaceRange, cleanFix);
const success = await vscode.workspace.applyEdit(edit);
if (success) {
void vscode.window.showInformationMessage(
`Fix applied for "${finding.title}".`
);
}
return success;
}
/**
* Reports a false positive to the Lorikeet PTaaS triage queue.
*/
export async function reportFalsePositive(
document: vscode.TextDocument,
finding: Finding,
baseUrl: string,
log?: vscode.LogOutputChannel
): Promise<void> {
const note = await vscode.window.showInputBox({
title: `Report False Positive: ${finding.title}`,
prompt: 'Optionally explain why this is a false positive (e.g. validated upstream):',
placeHolder: 'e.g. Input is sanitized by regex guard on line 42',
ignoreFocusOut: true,
});
if (note === undefined) {
return; // User cancelled
}
const cleanBase = baseUrl.replace(/\/+$/, '');
const triageUrl =
`${cleanBase}/talon/dashboard/mcp?action=report_fp` +
`&title=${encodeURIComponent(finding.title)}` +
`&cwe=${encodeURIComponent(finding.cwe ?? '')}` +
`&severity=${encodeURIComponent(finding.severity)}` +
`&file=${encodeURIComponent(vscode.workspace.asRelativePath(document.uri))}` +
`&line=${finding.line}` +
`¬e=${encodeURIComponent(note.trim())}`;
log?.info(`Reporting false positive for ${finding.title} in ${document.uri.fsPath}`);
await vscode.env.openExternal(vscode.Uri.parse(triageUrl));
void vscode.window.showInformationMessage(
`Reported false positive for "${finding.title}". Thank you for improving Lory accuracy!`
);
}