fix(mitm): gate sudo prompts on server platform (#822)

This commit is contained in:
Rezky Hamid
2026-05-03 15:00:43 +07:00
committed by GitHub
parent a463ee00ff
commit e40f7ffb98
7 changed files with 62 additions and 37 deletions
@@ -9,6 +9,7 @@ import {
getCachedPassword,
setCachedPassword,
loadEncryptedPassword,
isSudoPasswordRequired,
initDbHooks,
} from "@/mitm/manager";
import { getSettings, updateSettings } from "@/lib/localDb";
@@ -40,6 +41,10 @@ function getPassword(provided) {
return provided || getCachedPassword() || null;
}
function requiresSudoPassword(pwd) {
return !isWin && !pwd && isSudoPasswordRequired();
}
function checkIsAdmin() {
if (!isWin) return true;
try {
@@ -55,13 +60,16 @@ export async function GET() {
try {
const status = await getMitmStatus();
const settings = await getSettings();
const hasCachedPassword = !!getCachedPassword() || !!(await loadEncryptedPassword());
return NextResponse.json({
running: status.running,
pid: status.pid || null,
certExists: status.certExists || false,
certTrusted: status.certTrusted || false,
dnsStatus: status.dnsStatus || {},
hasCachedPassword: !!getCachedPassword() || !!(await loadEncryptedPassword()),
hasCachedPassword,
isWin,
needsSudoPassword: !isWin && !hasCachedPassword && isSudoPasswordRequired(),
isAdmin: checkIsAdmin(),
mitmRouterBaseUrl:
(settings.mitmRouterBaseUrl && String(settings.mitmRouterBaseUrl).trim()) ||
@@ -79,9 +87,9 @@ export async function POST(request) {
const { apiKey, sudoPassword, mitmRouterBaseUrl } = await request.json();
const pwd = getPassword(sudoPassword) || await loadEncryptedPassword() || "";
if (!apiKey || (!isWin && !pwd)) {
if (!apiKey || requiresSudoPassword(pwd)) {
return NextResponse.json(
{ error: isWin ? "Missing apiKey" : "Missing apiKey or sudoPassword" },
{ error: !apiKey ? "Missing apiKey" : "Missing sudoPassword" },
{ status: 400 }
);
}
@@ -115,7 +123,7 @@ export async function DELETE(request) {
const { sudoPassword } = body;
const pwd = getPassword(sudoPassword) || await loadEncryptedPassword() || "";
if (!isWin && !pwd) {
if (requiresSudoPassword(pwd)) {
return NextResponse.json({ error: "Missing sudoPassword" }, { status: 400 });
}
@@ -138,7 +146,7 @@ export async function PATCH(request) {
if (!tool || !action) {
return NextResponse.json({ error: "tool and action required" }, { status: 400 });
}
if (!isWin && !pwd) {
if (requiresSudoPassword(pwd)) {
return NextResponse.json({ error: "Missing sudoPassword" }, { status: 400 });
}