mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
- /api/settings/database now requires current password (header for GET, body for POST) in addition to session; CLI-token requests exempt - add verifyDashboardPassword helper reusing login bcrypt check - profile UI prompts password via modal before export/import - /v1/web/fetch rejects internal/private/metadata targets via assertPublicUrl Refs GHSA-qvfm-67h2-2qfx, GHSA-qj3v-64wj-q825 Co-authored-by: Cursor <cursoragent@cursor.com>
52 lines
1.8 KiB
JavaScript
52 lines
1.8 KiB
JavaScript
import { NextResponse } from "next/server";
|
|
import { exportDb, getSettings, importDb } from "@/lib/localDb";
|
|
import { applyOutboundProxyEnv } from "@/lib/network/outboundProxy";
|
|
import { verifyDashboardPassword } from "@/lib/auth/dashboardSession";
|
|
|
|
const CLI_TOKEN_HEADER = "x-9r-cli-token";
|
|
const PASSWORD_HEADER = "x-9r-password";
|
|
|
|
// CLI token requests are already trusted (local machine); skip password re-auth.
|
|
function isCliRequest(request) {
|
|
return Boolean(request.headers.get(CLI_TOKEN_HEADER));
|
|
}
|
|
|
|
export async function GET(request) {
|
|
try {
|
|
if (!isCliRequest(request) && !(await verifyDashboardPassword(request.headers.get(PASSWORD_HEADER)))) {
|
|
return NextResponse.json({ error: "Invalid password" }, { status: 401 });
|
|
}
|
|
const payload = await exportDb();
|
|
return NextResponse.json(payload);
|
|
} catch (error) {
|
|
console.log("Error exporting database:", error);
|
|
return NextResponse.json({ error: "Failed to export database" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(request) {
|
|
try {
|
|
const { password, ...payload } = await request.json();
|
|
if (!isCliRequest(request) && !(await verifyDashboardPassword(password))) {
|
|
return NextResponse.json({ error: "Invalid password" }, { status: 401 });
|
|
}
|
|
await importDb(payload);
|
|
|
|
// Ensure proxy settings take effect immediately after a DB import.
|
|
try {
|
|
const settings = await getSettings();
|
|
applyOutboundProxyEnv(settings);
|
|
} catch (err) {
|
|
console.warn("[Settings][DatabaseImport] Failed to re-apply outbound proxy env:", err);
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.log("Error importing database:", error);
|
|
return NextResponse.json(
|
|
{ error: error?.message || "Failed to import database" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
}
|