Elffuss BraidHub

kikocis/osiris public

master · 1.9 KiBRawPermalink
1import { NextResponse } from 'next/server';
2import { isRateLimited, getClientIp } from '@/lib/ssrf-guard';
3import { search, type Schema } from '@/lib/sanctions';
4
5// Standalone OFAC SDN search (free, no key) backed by the OpenSanctions
6// `us_ofac_sdn` mirror. Substring + alias-aware match, schema-filterable.
7
8const ALLOWED_SCHEMAS: Schema[] = [
9 'Person',
10 'Organization',
11 'Company',
12 'Vessel',
13 'Airplane',
14 'LegalEntity',
15];
16
17export async function GET(req: Request) {
18 const { searchParams } = new URL(req.url);
19 const query = (searchParams.get('query') || '').trim();
20 const schemaParam = searchParams.get('schema');
21 const limit = Math.min(Math.max(parseInt(searchParams.get('limit') || '25', 10), 1), 100);
22
23 if (!query) {
24 return NextResponse.json({ error: 'Missing query parameter' }, { status: 400 });
25 }
26 if (query.length < 4) {
27 return NextResponse.json(
28 { error: 'Query must be at least 4 characters' },
29 { status: 400 }
30 );
31 }
32
33 const clientIp = getClientIp(req);
34 if (isRateLimited(clientIp, 20, 60_000)) {
35 return NextResponse.json({ error: 'Rate limit exceeded' }, { status: 429 });
36 }
37
38 let schema: Schema | undefined;
39 if (schemaParam) {
40 if (!ALLOWED_SCHEMAS.includes(schemaParam as Schema)) {
41 return NextResponse.json(
42 { error: `Invalid schema. Allowed: ${ALLOWED_SCHEMAS.join(', ')}` },
43 { status: 400 }
44 );
45 }
46 schema = schemaParam as Schema;
47 }
48
49 try {
50 const matches = await search(query, { schema, limit });
51 return NextResponse.json({
52 query,
53 schema: schema ?? null,
54 total: matches.length,
55 matches,
56 source: 'OpenSanctions / US OFAC SDN',
57 timestamp: new Date().toISOString(),
58 });
59 } catch (e) {
60 return NextResponse.json(
61 { error: 'Sanctions lookup failed', detail: e instanceof Error ? e.message : String(e) },
62 { status: 502 }
63 );
64 }
65}