Initial curated CertScope source from fe2bbe3ca5c98bf64be26a09c6c58ee805a7f87e
This commit is contained in:
commit
7348faf6c0
65 files changed
+12509
No files matched your search
@@ -0,0 +1,40 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { checkRateLimit, clientKeyFromHeaders, tryAcquireInspection } from "@/lib/inspect/admission";
|
||||
import { inspectHost } from "@/lib/inspect/inspect";
|
||||
import { InspectionInputError, InspectionPolicyError, InspectionResolutionError } from "@/lib/inspect/policy";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
function jsonError(error: string, status: number, headers: Record<string, string> = {}) {
|
||||
return NextResponse.json({ error }, { status, headers: { "Cache-Control": "no-store", ...headers } });
|
||||
}
|
||||
|
||||
export function classifyInspectionError(error: unknown) {
|
||||
if (error instanceof InspectionInputError || error instanceof InspectionPolicyError) return { status: 400, message: error.message };
|
||||
if (error instanceof InspectionResolutionError) return { status: 422, message: error.message };
|
||||
return { status: 500, message: "Inspection service is temporarily unavailable. Try again shortly." };
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const host = request.nextUrl.searchParams.get("host") ?? "";
|
||||
const rate = checkRateLimit(clientKeyFromHeaders(request.headers));
|
||||
if (!rate.allowed) return jsonError("Request rate limit reached. Try again shortly.", 429, { "Retry-After": String(rate.retryAfterSeconds) });
|
||||
const release = tryAcquireInspection();
|
||||
if (!release) {
|
||||
return jsonError("Inspection capacity is temporarily full. Try again shortly.", 429, { "Retry-After": "1" });
|
||||
}
|
||||
|
||||
try {
|
||||
return NextResponse.json(await inspectHost(host), { headers: { "Cache-Control": "no-store" } });
|
||||
} catch (error) {
|
||||
const classified = classifyInspectionError(error);
|
||||
if (classified.status === 500) console.error("inspection_unexpected_error", {
|
||||
name: error instanceof Error ? error.name : "UnknownError",
|
||||
code: typeof error === "object" && error !== null && "code" in error ? String((error as { code?: unknown }).code ?? "") : "",
|
||||
});
|
||||
return jsonError(classified.message, classified.status);
|
||||
} finally {
|
||||
release();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user