{"record":{"id":"7b8ccb47338a57f5","repo":"decolua/9router","slug":"invalid-mitm-router-url","errorCode":null,"errorMessage":"Invalid MITM router URL","messagePattern":"Invalid MITM router URL","errorType":"validation","errorClass":"Error","httpStatus":500,"severity":"warning","filePath":"src/app/api/cli-tools/antigravity-mitm/route.js","lineNumber":30,"sourceCode":"  isSudoPasswordRequired,\n  initDbHooks,\n} from \"@/mitm/manager\";\nimport { getSettings, updateSettings } from \"@/lib/localDb\";\n\ninitDbHooks(getSettings, updateSettings);\n\nconst DEFAULT_MITM_ROUTER_BASE = \"http://localhost:20128\";\n\nfunction normalizeMitmRouterBaseUrlInput(input) {\n  if (input == null || String(input).trim() === \"\") {\n    return DEFAULT_MITM_ROUTER_BASE;\n  }\n  const t = String(input).trim().replace(/\\/+$/, \"\");\n  let u;\n  try {\n    u = new URL(t);\n  } catch {\n    throw new Error(\"Invalid MITM router URL\");\n  }\n  if (u.protocol !== \"http:\" && u.protocol !== \"https:\") {\n    throw new Error(\"MITM router URL must use http or https\");\n  }\n  return t;\n}\n\nconst isWin = process.platform === \"win32\";\n\nfunction getPassword(provided) {\n  return provided || getCachedPassword() || null;\n}\n\nfunction requiresSudoPassword(pwd) {\n  return !isWin && !pwd && isSudoPasswordRequired();\n}\n\nfunction checkIsAdmin() {","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/decolua/9router/blob/90b52e06ffd666b7929554211474d01588f6b1f8/src/app/api/cli-tools/antigravity-mitm/route.js#L12-L48","documentation":"normalizeMitmRouterBaseUrlInput validates the mitmRouterBaseUrl field sent to the antigravity-mitm API. If the value is non-empty but cannot be parsed by the URL constructor (after trimming and stripping trailing slashes), it throws 'Invalid MITM router URL'. A separate error covers non-http(s) protocols; empty values fall back to the default http://localhost:20128.","triggerScenarios":"POST /api/cli-tools/antigravity-mitm with body { mitmRouterBaseUrl: '<unparseable string>' } — e.g. 'localhost:20128' (no scheme), 'http:/localhost', a value with embedded spaces or stray characters — causing new URL(t) to throw at route.js:30; the route returns 400 { error: 'Invalid MITM router URL' }.","commonSituations":"User typed 'localhost:20128' or a bare host:port without http:// into the MITM settings field; copy-pasted URL with trailing junk or hidden whitespace/newlines; accidentally passing a full tool URL or path instead of a base URL.","solutions":["Include the scheme: use 'http://localhost:20128' rather than 'localhost:20128' — new URL() requires a protocol.","Remove trailing paths/whitespace; the field expects a bare base URL (trailing slashes are stripped automatically).","Leave the field empty to use the default http://localhost:20128 if you don't need a custom value.","Ensure only http: or https: protocols are used — any other scheme yields the related protocol error."],"exampleFix":"// before\nmitmRouterBaseUrl: \"localhost:20128\"\n// after\nmitmRouterBaseUrl: \"http://localhost:20128\"","handlingStrategy":"validation","validationCode":"function normalizeMitmRouterBaseUrlClient(input) {\n  if (input == null || String(input).trim() === \"\") return \"http://localhost:20128\";\n  const t = String(input).trim().replace(/\\/+$/, \"\");\n  let u;\n  try { u = new URL(t); } catch { throw new Error(\"Invalid MITM router URL — include the scheme, e.g. http://localhost:20128\"); }\n  if (u.protocol !== \"http:\" && u.protocol !== \"https:\") throw new Error(\"MITM router URL must use http or https\");\n  return t;\n}","typeGuard":"const isValidHttpUrl = (s) => { try { const u = new URL(String(s).trim()); return u.protocol === \"http:\" || u.protocol === \"https:\"; } catch { return false; } };","tryCatchPattern":"try {\n  const res = await fetch(\"/api/cli-tools/antigravity-mitm\", { method: \"POST\", headers: { \"Content-Type\": \"application/json\" }, body: JSON.stringify({ apiKey, sudoPassword, mitmRouterBaseUrl }) });\n  const data = await res.json();\n  if (!res.ok) throw new Error(data.error || `MITM start failed (HTTP ${res.status})`);\n} catch (e) {\n  if (/Invalid MITM router URL/.test(e.message)) showFieldError(\"mitmRouterBaseUrl\", \"Enter a full URL including scheme, e.g. http://localhost:20128\");\n  else showError(e.message);\n}","preventionTips":["Pre-fill the input with the default http://localhost:20128 instead of leaving it free-text empty.","Validate the URL client-side with new URL() before submitting.","Auto-prepend http:// when the user enters a bare host:port.","Strip whitespace and trailing slashes before sending (server does this too)."],"tags":["validation","url-parsing","configuration"],"backgroundTag":"invalid-url-format","analyzedSha":"90b52e06ffd666b7929554211474d01588f6b1f8","analyzedAt":"2026-08-30T21:05:45.952Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}