mcp-cve-intelligence-server-lite-test
Version:
Lite Model Context Protocol server for comprehensive CVE intelligence gathering with multi-source exploit discovery, designed for security professionals and cybersecurity researchers - Alpha Release
129 lines • 5.01 kB
JavaScript
/**
* Utility functions for CVE data normalization and processing
*/
/**
* Get English description from CVE descriptions array with fallback
* @param descriptions Array of CVE descriptions
* @returns English description string or fallback message
*/
export function getEnglishDescription(descriptions) {
if (!descriptions || !Array.isArray(descriptions) || descriptions.length === 0) {
return 'No description found';
}
// First, try to find English description
const englishDesc = descriptions.find(desc => desc.lang && desc.lang.toLowerCase() === 'en');
if (englishDesc && englishDesc.value && englishDesc.value.trim()) {
return englishDesc.value.trim();
}
// If no English description, try to find description in common English variants
const englishVariants = ['en-us', 'en-gb', 'en-ca', 'en-au'];
const englishVariantDesc = descriptions.find(desc => desc.lang && englishVariants.includes(desc.lang.toLowerCase()));
if (englishVariantDesc && englishVariantDesc.value && englishVariantDesc.value.trim()) {
return englishVariantDesc.value.trim();
}
// If no English description found, return the first available description
const firstDesc = descriptions.find(desc => desc.value && desc.value.trim());
if (firstDesc) {
return firstDesc.value.trim();
}
// Fallback if no valid description found
return 'No description found';
}
/**
* Normalize CVE descriptions array to ensure consistent format
* @param descriptions Raw descriptions data from various sources
* @returns Normalized descriptions array
*/
export function normalizeDescriptions(descriptions) {
if (!descriptions) {
return [{ lang: 'en', value: 'No description found' }];
}
if (Array.isArray(descriptions)) {
const normalized = descriptions.map(desc => ({
lang: desc.lang || desc.language || 'en',
value: desc.value || desc.description || desc.text || '',
})).filter(desc => desc.value && desc.value.trim());
return normalized.length > 0 ? normalized : [{ lang: 'en', value: 'No description found' }];
}
// Handle single description object
if (typeof descriptions === 'object') {
return [{
lang: descriptions.lang || descriptions.language || 'en',
value: descriptions.value || descriptions.description || descriptions.text || 'No description found',
}];
}
// Handle string description
if (typeof descriptions === 'string') {
return [{
lang: 'en',
value: descriptions.trim() || 'No description found',
}];
}
return [{ lang: 'en', value: 'No description found' }];
}
/**
* Transform CVE object to replace descriptions array with single description string
* @param cve CVE object with descriptions array
* @returns CVE object with description as string
*/
export function transformCVEForDisplay(cve) {
if (!cve) {
return cve;
}
// Create a copy of the CVE object
const transformedCVE = { ...cve };
// Replace descriptions array with single description string
if (cve.descriptions) {
transformedCVE.description = getEnglishDescription(cve.descriptions);
delete transformedCVE.descriptions;
}
return transformedCVE;
}
/**
* Transform CVE search result to replace descriptions arrays with description strings
* @param result CVE search result
* @returns Transformed search result with description strings
*/
export function transformCVESearchResultForDisplay(result) {
if (!result) {
return result;
}
// Create a copy of the result object
const transformedResult = { ...result };
// Transform each CVE in the results
if (result.cves && Array.isArray(result.cves)) {
transformedResult.cves = result.cves.map(transformCVEForDisplay);
}
return transformedResult;
}
/**
* Deep transform any object that might contain CVE objects with descriptions
* This handles nested structures like reports, arrays, etc.
* @param obj Object that might contain CVE objects
* @returns Transformed object with description strings
*/
export function deepTransformCVEsForDisplay(obj) {
if (!obj) {
return obj;
}
// Handle arrays
if (Array.isArray(obj)) {
return obj.map(deepTransformCVEsForDisplay);
}
// Handle objects
if (typeof obj === 'object') {
// Check if this object looks like a CVE (has id and descriptions)
if (obj.id && obj.descriptions && Array.isArray(obj.descriptions)) {
return transformCVEForDisplay(obj);
}
// Recursively transform nested objects
const transformed = {};
for (const [key, value] of Object.entries(obj)) {
transformed[key] = deepTransformCVEsForDisplay(value);
}
return transformed;
}
// Return primitive values as-is
return obj;
}
//# sourceMappingURL=cve-utils.js.map