supamend
Version:
Pluggable DevSecOps Security Scanner with 10+ scanners and multiple reporting channels
192 lines • 7.16 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginManager = void 0;
const fsExtra = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
class PluginManager {
constructor() {
this.scanners = new Map();
this.reporters = new Map();
this.pluginsLoaded = false;
this.loadPlugins();
}
/**
* Load all available plugins (only once)
*/
loadPlugins() {
if (this.pluginsLoaded)
return;
this.loadScanners();
this.loadReporters();
this.pluginsLoaded = true;
}
/**
* Load scanner plugins from src/scanners directory
*/
loadScanners() {
const scannersDir = path.join(__dirname, '../scanners');
if (fsExtra.pathExistsSync(scannersDir)) {
const files = fsExtra.readdirSync(scannersDir);
for (const file of files) {
const shouldLoad = file.endsWith('.js') && !file.endsWith('.test.js') && !file.endsWith('.d.ts.map') && !file.endsWith('.js.map');
if (shouldLoad) {
try {
// Validate file path to prevent path traversal
if (file.includes('..') || file.includes('/') || file.includes('\\')) {
continue;
}
const scannerPath = path.join(scannersDir, file);
const scannerModule = require(scannerPath);
const moduleKeys = Object.keys(scannerModule);
const firstKey = moduleKeys.length > 0 ? moduleKeys[0] : null;
const scanner = scannerModule.default || (firstKey ? scannerModule[firstKey] : undefined);
if (scanner && typeof scanner === 'object' && 'name' in scanner && scanner.name) {
this.scanners.set(scanner.name, scanner);
}
}
catch (error) {
console.warn(`Failed to load scanner from ${file}:`, error);
}
}
}
}
}
/**
* Load reporter plugins from src/reporters directory
*/
loadReporters() {
const reportersDir = path.join(__dirname, '../reporters');
if (fsExtra.pathExistsSync(reportersDir)) {
const files = fsExtra.readdirSync(reportersDir);
for (const file of files) {
const shouldLoad = file.endsWith('.js') && !file.endsWith('.test.js') && !file.endsWith('.d.ts.map') && !file.endsWith('.js.map');
if (shouldLoad) {
try {
// Validate file path to prevent path traversal
if (file.includes('..') || file.includes('/') || file.includes('\\')) {
continue;
}
const reporterPath = path.join(reportersDir, file);
const reporterModule = require(reporterPath);
const moduleKeys = Object.keys(reporterModule);
const firstKey = moduleKeys.length > 0 ? moduleKeys[0] : null;
const reporter = reporterModule.default || (firstKey ? reporterModule[firstKey] : undefined);
if (reporter && typeof reporter === 'object' && 'name' in reporter && reporter.name) {
this.reporters.set(reporter.name, reporter);
}
}
catch (error) {
console.warn(`Failed to load reporter from ${file}:`, error);
}
}
}
}
}
/**
* Get a scanner by name
*/
async getScanner(name) {
this.loadPlugins();
const scanner = this.scanners.get(name);
if (scanner) {
try {
const isAvailable = await this.withTimeout(scanner.isAvailable(), 5000);
return isAvailable ? scanner : undefined;
}
catch (error) {
return undefined;
}
}
return undefined;
}
/**
* Get a reporter by name
*/
async getReporter(name) {
this.loadPlugins();
const reporter = this.reporters.get(name);
if (reporter) {
try {
const isAvailable = await this.withTimeout(reporter.isAvailable(), 5000);
return isAvailable ? reporter : undefined;
}
catch (error) {
return undefined;
}
}
return undefined;
}
/**
* Check if a reporter exists (regardless of availability)
*/
hasReporter(name) {
this.loadPlugins();
return this.reporters.has(name);
}
/**
* Check if a scanner exists (regardless of availability)
*/
hasScanner(name) {
this.loadPlugins();
return this.scanners.has(name);
}
/**
* List all available scanners
*/
listScanners() {
this.loadPlugins();
return Array.from(this.scanners.keys());
}
/**
* List all available reporters
*/
listReporters() {
this.loadPlugins();
return Array.from(this.reporters.keys());
}
/**
* Add timeout to async operations
*/
withTimeout(promise, timeoutMs) {
return Promise.race([
promise,
new Promise((_, reject) => setTimeout(() => reject(new Error('Operation timed out')), timeoutMs))
]);
}
}
exports.PluginManager = PluginManager;
exports.default = PluginManager;
//# sourceMappingURL=plugin-manager.js.map