Elffuss BraidHub

kikocis/osiris public

master · 4.3 KiBRawPermalink
1import { NextResponse } from 'next/server';
2import { isRateLimited, getClientIp } from '@/lib/ssrf-guard';
3
4// CVE Intelligence — fetches vulnerability details from CIRCL CVE API (free, no key)
5export async function GET(req: Request) {
6 const { searchParams } = new URL(req.url);
7 const cve = searchParams.get('cve');
8 if (!cve) return NextResponse.json({ error: 'Missing cve parameter' }, { status: 400 });
9
10 const clientIp = getClientIp(req);
11 if (isRateLimited(clientIp, 30, 60_000)) {
12 return NextResponse.json({ error: 'Rate limit exceeded' }, { status: 429 });
13 }
14
15 // Validate CVE format (CVE-YYYY-NNNNN)
16 if (!/^CVE-\d{4}-\d{4,}$/i.test(cve)) {
17 return NextResponse.json({ error: 'Invalid CVE format. Expected: CVE-YYYY-NNNNN' }, { status: 400 });
18 }
19
20 try {
21 const res = await fetch(`https://cveawg.mitre.org/api/cve/${encodeURIComponent(cve.toUpperCase())}`, {
22 signal: AbortSignal.timeout(8000),
23 headers: { 'Accept': 'application/json' },
24 });
25
26 if (!res.ok) {
27 // Fallback to CIRCL
28 try {
29 const circlRes = await fetch(`https://cve.circl.lu/api/cve/${encodeURIComponent(cve.toUpperCase())}`, {
30 signal: AbortSignal.timeout(8000),
31 headers: { 'Accept': 'application/json' },
32 });
33 if (circlRes.ok) {
34 const data = await circlRes.json();
35 return NextResponse.json({
36 id: data.id || cve.toUpperCase(),
37 description: data.summary || 'No description available.',
38 cvss: data.cvss ?? null,
39 cvss_vector: data.cvss_vector || null,
40 references: (data.references || []).slice(0, 5),
41 published: data.Published || null,
42 modified: data.Modified || null,
43 cwe: data.cwe || null,
44 source: 'circl',
45 });
46 }
47 } catch { /* fall through */ }
48
49 return NextResponse.json({
50 id: cve.toUpperCase(),
51 description: 'CVE details could not be retrieved at this time.',
52 cvss: null,
53 references: [],
54 source: 'unavailable',
55 });
56 }
57
58 const data = await res.json();
59
60 // Parse the CVE 5.0 JSON format from MITRE
61 const cna = data.containers?.cna;
62 const description = cna?.descriptions?.find((d: any) => d.lang === 'en')?.value
63 || cna?.descriptions?.[0]?.value
64 || 'No description available.';
65
66 // Extract CVSS from metrics
67 let cvss: number | null = null;
68 let cvss_vector: string | null = null;
69 let severity: string | null = null;
70
71 const metrics = cna?.metrics;
72 if (metrics) {
73 for (const m of metrics) {
74 const v31 = m.cvssV3_1 || m.cvssV3_0 || m.cvssV31;
75 if (v31) {
76 cvss = v31.baseScore ?? null;
77 cvss_vector = v31.vectorString ?? null;
78 severity = v31.baseSeverity ?? null;
79 break;
80 }
81 const v2 = m.cvssV2_0 || m.cvssV2;
82 if (v2) {
83 cvss = v2.baseScore ?? null;
84 cvss_vector = v2.vectorString ?? null;
85 break;
86 }
87 }
88 }
89
90 // Extract CWE
91 const problemTypes = cna?.problemTypes;
92 let cwe: string | null = null;
93 if (problemTypes?.[0]?.descriptions?.[0]) {
94 cwe = problemTypes[0].descriptions[0].cweId || problemTypes[0].descriptions[0].description || null;
95 }
96
97 // Extract references
98 const references = (cna?.references || []).slice(0, 5).map((r: any) => r.url);
99
100 // Extract affected products
101 const affected = (cna?.affected || []).slice(0, 5).map((a: any) => ({
102 vendor: a.vendor || 'Unknown',
103 product: a.product || 'Unknown',
104 versions: (a.versions || []).slice(0, 3).map((v: any) => v.version).filter(Boolean),
105 }));
106
107 return NextResponse.json({
108 id: data.cveMetadata?.cveId || cve.toUpperCase(),
109 description,
110 cvss,
111 cvss_vector,
112 severity: severity || (cvss !== null ? (cvss >= 9 ? 'CRITICAL' : cvss >= 7 ? 'HIGH' : cvss >= 4 ? 'MEDIUM' : 'LOW') : null),
113 cwe,
114 affected,
115 references,
116 published: data.cveMetadata?.datePublished || null,
117 modified: data.cveMetadata?.dateUpdated || null,
118 source: 'mitre',
119 });
120 } catch {
121 return NextResponse.json({ error: 'CVE lookup failed' }, { status: 500 });
122 }
123}