988a3cca9a
Multi-user authorization hardening from a full security evaluation (EVALUATION.md):
- P0: /api/settings/{export,import}-db are now admin-only. Previously any signed-in user could download the whole instance DB (all bcrypt hashes + plaintext AI keys) or replace it wholesale. Per-user CSV export/import stays open.
- AI custom-URL providers (Ollama, OpenAI-compatible) are now admin-only, and every server fetch to a user-supplied URL passes through assertSafeProviderUrl (blocks link-local/cloud-metadata; private LAN allowed by design). Fixed-URL cloud providers stay per-user. Removed the dead legacy /api/ai/config route.
- Dev: fixed broken quick-start (added npm run create-admin; rewrote README; dropped dead CLAUDE_API_KEY) and the export-db 0-byte path resolution (resolveDatabasePath now matches Prisma).
ExVer bumped to 1.1.0:8 (no schema/data migration). Tests 197 pass, build green, tsc clean.
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { getCurrentUser } from "@/lib/auth";
|
|
import { getTimestampFileSuffix, resolveDatabasePath } from "@/lib/db-file";
|
|
import { readFile } from "fs/promises";
|
|
import { NextResponse } from "next/server";
|
|
import path from "path";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
/**
|
|
* GET /api/settings/export-db
|
|
* Download the current SQLite database file.
|
|
*/
|
|
export async function GET() {
|
|
try {
|
|
const user = await getCurrentUser();
|
|
if (!user) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
// Whole-instance operation: the file contains every user's data and
|
|
// password hashes. Admin-only — regular users use /api/me/export.
|
|
if (!user.isAdmin) {
|
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
}
|
|
|
|
const dbPath = resolveDatabasePath();
|
|
const data = await readFile(dbPath);
|
|
const fileName = `proof-of-work-${getTimestampFileSuffix()}.db`;
|
|
|
|
return new NextResponse(data, {
|
|
status: 200,
|
|
headers: {
|
|
"Content-Type": "application/x-sqlite3",
|
|
"Content-Disposition": `attachment; filename="${path.basename(fileName)}"`,
|
|
"Cache-Control": "no-store",
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("Database export error:", error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to export database" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|