-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextension.js
More file actions
281 lines (245 loc) · 12 KB
/
Copy pathextension.js
File metadata and controls
281 lines (245 loc) · 12 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
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
/**
* Extension Security Auditor for Cursor
*
* @fileoverview A security extension that scans and monitors installed extensions
* for known malicious patterns and provides real-time security alerts.
*
* @author v0ldemort5545, unsafe_call
* @version 0.0.1
* @since 2025-08-19
*
* @description This extension provides comprehensive security auditing for Cursor extensions:
* - Automatically scans all installed extensions on startup
* - Monitors for extension changes and re-runs security audits on extension changes
* - Provides uninstall options for malicious extensions
*
* @usage The extension activates automatically on startup and can be manually triggered
* via the command palette: "Extension Security Auditor: Check Installed Extensions"
*
*/
// The module 'vscode' contains the Cursor extensibility API (Cursor is built on VS Code)
const vscode = require('vscode');
const path = require('path');
const fs = require('fs');
/**
* This function is called when your extension is activated.
* The 'activationEvents' in package.json determine when this runs.
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log('Extension Security Auditor is now active in Cursor!');
// Initial check on startup
checkAndReportMaliciousExtensions(context).catch(error => {
console.error('Error during initial security audit:', error);
vscode.window.showErrorMessage('Error during security audit. Check the console for details.');
});
// Subscribe to the onDidChange event
context.subscriptions.push(vscode.extensions.onDidChange(() => {
console.log('Extension list changed. Re-running security audit...');
checkAndReportMaliciousExtensions(context).catch(error => {
console.error('Error during security audit after extension change:', error);
vscode.window.showErrorMessage('Error during security audit. Check the console for details.');
});
}));
let disposable = vscode.commands.registerCommand('extension-security-auditor.checkExtensions', () => {
vscode.window.showInformationMessage('Starting security audit of installed extensions...');
checkAndReportMaliciousExtensions(context).catch(error => {
console.error('Error during manual security audit:', error);
vscode.window.showErrorMessage('Error during security audit. Check the console for details.');
});
});
context.subscriptions.push(disposable);
}
/**
* Fetches the latest malicious extensions list from GitHub
* @returns {Promise<string[]>} Array of malicious extension IDs
*/
async function fetchLatestMaliciousList() {
try {
// Correct raw GitHub URL to the JSON file on the main branch
const url = 'https://raw.githubusercontent.com/janbro/openvsx-registry-guard/refs/heads/main/malicious.json';
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch malicious list: ${response.status} ${response.statusText}`);
}
const data = await response.json();
if (!Array.isArray(data)) {
throw new Error('Unexpected format: malicious.json is not an array');
}
return data;
} catch (error) {
console.error('Unexpected error fetching from GitHub:', error);
throw error;
}
}
/**
* Updates the local malicious.json file with the latest data from GitHub
* @param {vscode.ExtensionContext} context - Extension context
* @param {string[]} maliciousList - Array of malicious extension IDs
*/
function updateLocalMaliciousFile(context, maliciousList) {
try {
const maliciousFilePath = path.join(context.extensionPath, 'malicious.json');
const content = JSON.stringify(maliciousList, null, 2);
fs.writeFileSync(maliciousFilePath, content, 'utf8');
console.log('Successfully updated local malicious.json file');
} catch (error) {
console.error('Error updating local malicious.json file:', error);
throw error;
}
}
/**
* Reads the list of known malicious extension IDs from a local JSON file.
* Fetches the latest version from GitHub if the cache is expired (10 minutes).
* @param {vscode.ExtensionContext} context
* @returns {Promise<string[]>} An array of malicious extension IDs.
*/
async function getMaliciousExtensionsList(context) {
const maliciousFilePath = path.join(context.extensionPath, 'malicious.json');
const cacheFilePath = path.join(context.extensionPath, '.malicious-cache');
try {
// Check if we have a cache file and if it's still valid (10 minutes)
let shouldFetchFromGitHub = true;
let cachedData = null;
if (fs.existsSync(cacheFilePath)) {
try {
const cacheContent = fs.readFileSync(cacheFilePath, 'utf8');
const cache = JSON.parse(cacheContent);
const now = Date.now();
const tenMinutes = 10 * 60 * 1000; // 10 minutes in milliseconds
if (cache.timestamp && (now - cache.timestamp) < tenMinutes) {
shouldFetchFromGitHub = false;
cachedData = cache.data;
console.log('Using cached malicious extensions list (cache is still valid)');
} else {
console.log('Cache expired, fetching fresh data from GitHub');
}
} catch (cacheError) {
console.warn('Error reading cache file, will fetch from GitHub:', cacheError);
}
}
if (shouldFetchFromGitHub) {
try {
console.log('Fetching latest malicious extensions list from GitHub...');
const latestList = await fetchLatestMaliciousList();
// Update the local file
updateLocalMaliciousFile(context, latestList);
// Update the cache
const cacheData = {
timestamp: Date.now(),
data: latestList
};
fs.writeFileSync(cacheFilePath, JSON.stringify(cacheData, null, 2), 'utf8');
return latestList;
} catch (githubError) {
console.warn('Failed to fetch from GitHub, falling back to local file:', githubError);
// Fall back to local file if GitHub fetch fails
shouldFetchFromGitHub = false;
}
}
// Use cached data or fall back to local file
if (cachedData) {
return cachedData;
}
// Read from local file as fallback
if (fs.existsSync(maliciousFilePath)) {
const fileContent = fs.readFileSync(maliciousFilePath, 'utf8');
const localList = JSON.parse(fileContent);
console.log(`Loaded ${localList.length} malicious extensions from local file`);
return localList;
} else {
console.warn('No local malicious.json file found, returning empty list');
return [];
}
} catch (error) {
console.error("Could not read or parse malicious extensions list:", error);
vscode.window.showErrorMessage("Failed to load malicious extensions list. Check the extension's files and network connection.");
return [];
}
}
/**
* Checks all installed extensions against a malicious list and alerts the user.
* @param {vscode.ExtensionContext} context
*/
async function checkAndReportMaliciousExtensions(context) {
const allExtensions = vscode.extensions.all;
try {
const maliciousList = await getMaliciousExtensionsList(context);
// If the malicious list is empty, there's nothing to do.
if (maliciousList.length === 0) {
return;
}
const foundMaliciousExtensions = [];
let processedCount = 0; // Track how many extensions have been processed (uninstalled or dismissed)
let uninstalledCount = 0; // Track how many extensions have been uninstalled
// Loop through all installed extensions.
for (const extension of allExtensions) {
// Check if the extension's ID is in our malicious list.
if (maliciousList.includes(extension.id)) {
foundMaliciousExtensions.push(extension);
}
}
// Report findings to the user.
if (foundMaliciousExtensions.length > 0) {
// Create an output channel to show detailed information.
const outputChannel = vscode.window.createOutputChannel("Extension Security Auditor");
outputChannel.show(true);
outputChannel.appendLine("--- SECURITY AUDIT REPORT ---");
outputChannel.appendLine(`Scan completed on ${new Date().toLocaleString()}`);
outputChannel.appendLine("--- WARNING: Malicious Extensions Found ---");
foundMaliciousExtensions.forEach(extension => {
const extensionName = extension.packageJSON.displayName || extension.id;
outputChannel.appendLine(`- Name: ${extensionName}`);
outputChannel.appendLine(` ID: ${extension.id}`);
outputChannel.appendLine(" Reason: Found on the known malicious list. This extension may pose a security risk.");
outputChannel.appendLine("-------------------");
// Show a warning message with an "Uninstall" button for each malicious extension.
vscode.window.showWarningMessage(
`Malicious extension found: '${extensionName}'`,
"Uninstall"
).then(selection => {
// Increment the processed counter regardless of user choice
processedCount++;
if (selection === "Uninstall") {
// This command triggers the built-in Cursor uninstaller.
// It requires user confirmation.
vscode.commands.executeCommand('workbench.extensions.uninstallExtension', extension.id);
uninstalledCount++;
}
// Check if this was the last extension to be processed (uninstalled or dismissed)
if (processedCount === foundMaliciousExtensions.length && uninstalledCount > 0) {
// Show restart prompt after all extensions have been processed
setTimeout(() => {
vscode.window.showInformationMessage(
'Security audit completed. Cursor needs to restart to complete uninstallations.',
'Restart Cursor'
).then(restartSelection => {
if (restartSelection === 'Restart Cursor') {
// Execute the restart command
vscode.commands.executeCommand('workbench.action.reloadWindow');
}
});
}, 1000); // Small delay to ensure all commands have been processed
}
});
});
outputChannel.appendLine("--- IMPORTANT ---");
outputChannel.appendLine("The detected extensions were found on a local malicious list. Please verify the extensions and consider uninstalling them immediately.");
outputChannel.appendLine("-------------------");
} else {
vscode.window.showInformationMessage('No known malicious extensions found. Your Cursor environment is looking good!');
}
} catch (error) {
console.error('Error during security audit:', error);
vscode.window.showErrorMessage('Error during security audit. Check the console for details.');
}
}
/**
* This function is called when your extension is deactivated.
*/
function deactivate() {}
// Module exports
module.exports = {
activate,
deactivate
}