Elffuss BraidHub

kikocis/osiris public

master · 2.4 KiBRawPermalink
1import { NextResponse } from 'next/server';
2import { isRateLimited, getClientIp } from '@/lib/ssrf-guard';
3
4// Certificate Transparency lookup via crt.sh (free, no key)
5export async function GET(req: Request) {
6 const { searchParams } = new URL(req.url);
7 const domain = searchParams.get('domain');
8 if (!domain) return NextResponse.json({ error: 'Missing domain parameter' }, { status: 400 });
9
10 const clientIp = getClientIp(req);
11 if (isRateLimited(clientIp, 20, 60_000)) {
12 return NextResponse.json({ error: 'Rate limit exceeded' }, { status: 429 });
13 }
14
15 if (!/^[a-zA-Z0-9][a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(domain)) {
16 return NextResponse.json({ error: 'Invalid domain format' }, { status: 400 });
17 }
18
19 try {
20 const res = await fetch(`https://crt.sh/?q=%25.${encodeURIComponent(domain)}&output=json`, {
21 signal: AbortSignal.timeout(10000),
22 headers: { 'User-Agent': 'Osiris-OSINT/3.0' },
23 });
24
25 if (!res.ok) {
26 return NextResponse.json({ domain, certificates: [], error: 'crt.sh unavailable' });
27 }
28
29 const certs = await res.json();
30
31 // Deduplicate by common name and extract subdomains
32 const seen = new Set<string>();
33 const subdomains = new Set<string>();
34 const uniqueCerts = [];
35
36 for (const cert of certs.slice(0, 200)) {
37 const key = `${cert.common_name}-${cert.serial_number}`;
38 if (seen.has(key)) continue;
39 seen.add(key);
40
41 // Extract subdomains
42 const name = cert.name_value || '';
43 name.split('\n').forEach((n: string) => {
44 const clean = n.trim().replace(/^\*\./, '');
45 if (clean.endsWith(domain)) subdomains.add(clean);
46 });
47
48 uniqueCerts.push({
49 id: cert.id,
50 issuer: cert.issuer_name,
51 common_name: cert.common_name,
52 name_value: cert.name_value,
53 not_before: cert.not_before,
54 not_after: cert.not_after,
55 serial: cert.serial_number,
56 });
57 }
58
59 return NextResponse.json({
60 domain,
61 certificates: uniqueCerts.slice(0, 50),
62 subdomains: Array.from(subdomains).sort(),
63 total_certs: certs.length,
64 unique_subdomains: subdomains.size,
65 timestamp: new Date().toISOString(),
66 });
67 } catch {
68 return NextResponse.json({ domain, certificates: [], subdomains: [], error: 'Lookup failed' }, { status: 500 });
69 }
70}