Elffuss BraidHub

kikocis/osiris public

master · 3.9 KiBRawPermalink
1import { NextResponse } from 'next/server';
2import { validateHost, isRateLimited, getClientIp } from '@/lib/ssrf-guard';
3
4/**
5 * OSIRIS — Scanner Proxy (Hardened)
6 * Rate-limited, target-validated, scope-restricted
7 */
8
9const SCANNER_URL = process.env.SCANNER_URL || '';
10const SCANNER_KEY = process.env.SCANNER_KEY || '';
11
12// The string-based regex previously here matched only literal dotted-quad
13// IPv4, missed every IPv6 form, and never resolved hostnames — so an attacker
14// could bypass it with `target=metadata.example.com` (DNS A → 169.254.169.254),
15// `target=2130706433` (decimal 127.0.0.1), or `target=::1`. Validation now
16// canonicalises the input and resolves hostnames before deciding. See
17// `src/lib/ssrf-guard.ts`.
18
19// ── ALLOWED SCAN TYPES (safe subset only) ──
20const ALLOWED_SCANS: Record<string, { endpoint: string; timeout: number }> = {
21 quick: { endpoint: '/scan/quick', timeout: 15000 },
22 ssl: { endpoint: '/scan/ssl', timeout: 10000 },
23 headers: { endpoint: '/scan/headers', timeout: 10000 },
24 rdns: { endpoint: '/scan/rdns', timeout: 8000 },
25 subdomains: { endpoint: '/scan/subdomains', timeout: 15000 },
26 tech: { endpoint: '/scan/tech', timeout: 15000 },
27 whois: { endpoint: '/scan/whois', timeout: 10000 },
28 geoloc: { endpoint: '/scan/geoloc', timeout: 8000 },
29 vuln: { endpoint: '/scan/vuln', timeout: 90000 },
30};
31
32// REMOVED from public access: deep, ports, banner, traceroute
33// These are dangerous in an unauthenticated context:
34// deep → scans 65,535 ports (DDoS amplifier)
35// banner → harvests software versions from targets using our IP
36// traceroute → reveals hosting infrastructure
37// ports → arbitrary port range scanning
38
39export async function GET(req: Request) {
40 // 1. Check scanner is configured
41 if (!SCANNER_KEY) {
42 return NextResponse.json({ error: 'Scanner not configured', hint: 'Set SCANNER_URL and SCANNER_KEY in .env' }, { status: 503 });
43 }
44
45 // 2. Rate limit by client IP
46 const clientIp = getClientIp(req);
47 if (isRateLimited(clientIp, 5, 60_000)) {
48 return NextResponse.json({
49 error: 'Rate limit exceeded',
50 detail: `Maximum 5 scans per minute. Please wait before scanning again.`,
51 }, { status: 429 });
52 }
53
54 // 3. Validate params
55 const { searchParams } = new URL(req.url);
56 const target = searchParams.get('target')?.trim();
57 const scanType = searchParams.get('type') || 'quick';
58
59 if (!target) {
60 return NextResponse.json({ error: 'Missing target parameter' }, { status: 400 });
61 }
62
63 // 4. Block private/internal targets (DNS-resolves before deciding so a
64 // hostname pointing at a reserved range is rejected, and IPv6 + non-
65 // canonical IPv4 forms are no longer free bypasses).
66 const guard = await validateHost(target);
67 if (!guard.ok) {
68 return NextResponse.json({
69 error: 'Target blocked',
70 detail: `Target validation failed: ${guard.reason}`,
71 }, { status: 403 });
72 }
73
74 // 5. Validate scan type (only safe scans allowed)
75 const scanConfig = ALLOWED_SCANS[scanType];
76 if (!scanConfig) {
77 return NextResponse.json({
78 error: 'Scan type not available',
79 detail: `"${scanType}" is restricted. Available: ${Object.keys(ALLOWED_SCANS).join(', ')}`,
80 available_scans: Object.keys(ALLOWED_SCANS),
81 }, { status: 403 });
82 }
83
84 // 6. Execute scan with tight timeout
85 try {
86 const params = new URLSearchParams({ key: SCANNER_KEY, target });
87 const res = await fetch(`${SCANNER_URL}${scanConfig.endpoint}?${params.toString()}`, {
88 signal: AbortSignal.timeout(scanConfig.timeout),
89 });
90 const data = await res.json();
91 return NextResponse.json(data, { status: res.status });
92 } catch (e: any) {
93 return NextResponse.json({
94 error: 'Scanner unreachable',
95 detail: e.message,
96 }, { status: 502 });
97 }
98}