Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
import { CORS_HEADERS, handleCorsOptions } from "@/shared/utils/cors";
import { getFile, getFileContent } from "@/lib/db/files";
import { NextResponse } from "next/server";
import {
getApiKeyRequestScope,
canAccessOwnedRecord,
resolveEffectiveApiKeyId,
} from "@/app/api/v1/_helpers/apiKeyScope";
import { enforceApiKeyPolicy } from "@/shared/utils/apiKeyPolicy";
import { buildErrorBody } from "@omniroute/open-sse/utils/error";
export async function OPTIONS() {
return handleCorsOptions();
}
export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
const scope = await getApiKeyRequestScope(request);
if (scope.rejection) return scope.rejection;
// Fail closed on an unresolvable OR invalid credential — the same 401 fold
// `getApiKeyRequestScope` already applies (`apiKeyId: null` for a
// revoked/expired/banned/deactivated/unresolvable key, #13881) — so the
// per-key policy check below never has to special-case that status itself.
if (scope.apiKey && !scope.apiKeyId) {
return NextResponse.json(buildErrorBody(401, "Invalid API key"), {
status: 401,
headers: CORS_HEADERS,
});
}
// per-key operator policy (endpoint allowlist, schedule, usage cap, rate
// limit) — LEDGER-2 of the omni-code-sec 2026-09-21 run / #14481. Called
// UNCONDITIONALLY, exactly like batches/delete-completed/route.ts: a bare
// `x-api-key`/`x-goog-api-key` credential (no anthropic-version, non-Claude
// UA) is accepted by the CLIENT_API auth layer but ignored by
// getApiKeyRequestScope's extractApiKey() (scope.apiKeyId stays null for
// it) — enforceApiKeyPolicy resolves it independently via
// extractUngatedClientApiKey() (GHSA-2phc-xp22-9f56), so gating this call
// on scope.apiKeyId let that transport skip the endpoint allowlist,
// schedule, usage cap, rate limit and key quota entirely (round-2 PoC
// against e543b64). A keyless caller (session/anonymous) is a no-op here.
const policy = await enforceApiKeyPolicy(request, null);
if (policy.rejection) return policy.rejection;
const { id } = await params;
const file = getFile(id);
// A key resolved only via the ungated x-api-key/x-goog-api-key transport
// never sets scope.apiKeyId — fall back to the id enforceApiKeyPolicy()
// independently resolved, so that key can still read its own file content
// (LEDGER-27, omni-code-sec round 3).
const effectiveApiKeyId = resolveEffectiveApiKeyId(scope, policy.apiKeyInfo);
// `getFileContent` has no ownership check of its own — this guard is the only
// thing between a caller and the raw bytes (GHSA-2jm2-mpx8-6523).
if (!file || !canAccessOwnedRecord({ ...scope, apiKeyId: effectiveApiKeyId }, file.apiKeyId)) {
return NextResponse.json(
{ error: { message: "File not found", type: "invalid_request_error" } },
{ status: 404, headers: CORS_HEADERS }
);
}
const content = getFileContent(id);
if (!content) {
return NextResponse.json(
{ error: { message: "File content not found", type: "invalid_request_error" } },
{ status: 404, headers: CORS_HEADERS }
);
}
const sanitizedFilename = file.filename.replace(/[^\w.\-()\[\] ]/g, "_").slice(0, 255);
const encodedFilename = encodeURIComponent(file.filename);
return new Response(content, {
headers: {
...CORS_HEADERS,
"Content-Type": file.mimeType || "application/octet-stream",
"Content-Disposition": `attachment; filename="${sanitizedFilename}"; filename*=UTF-8''${encodedFilename}`,
},
});
}