Assessor_2.0/server/cve/cve-enrichment-service.ts

23 lines
1.1 KiB
TypeScript

import type { CveEnrichmentRecord } from '../../src/domain/cve-enrichment';
import { normalizeCveId } from '../../src/domain/cve-enrichment';
import type { AssessorRepository } from '../../src/persistence/assessor-repository';
import type { NvdCveClient } from './nvd-cve-client';
export class CveEnrichmentService {
constructor(private readonly repository: AssessorRepository, private readonly nvd: Pick<NvdCveClient, 'fetchCve'>) {}
async enrich(value: string): Promise<CveEnrichmentRecord> {
const cveId = normalizeCveId(value);
if (!cveId) throw new Error('A valid CVE identifier is required.');
const occurrences = await this.repository.findOccurrences({
toolId: 'defender-for-iot-vulnerabilities', search: cveId, limit: 10_000
});
const referenced = occurrences.items.some(item =>
normalizeCveId(item.sourceIdentity.canonicalRuleId ?? item.sourceIdentity.originalRuleId ?? '') === cveId);
if (!referenced) throw new Error(`${cveId} is not referenced by a stored Defender for IoT finding.`);
const record = await this.nvd.fetchCve(cveId);
await this.repository.saveCveEnrichment(record);
return record;
}
}