{"record":{"id":"e6ccede26bff3532","repo":"decolua/9router","slug":"wrong-sudo-password-failed-to-remove-dns-entry","errorCode":null,"errorMessage":"Wrong sudo password | Failed to remove DNS entry: ${error.message}","messagePattern":"Wrong sudo password \\| Failed to remove DNS entry: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/mitm/dns/dnsConfig.js","lineNumber":213,"sourceCode":"  try {\n    if (IS_WIN) {\n      const current = fs.readFileSync(HOSTS_FILE, \"utf8\");\n      const filtered = current.split(/\\r?\\n/).filter(l => !entriesToRemove.some(h => l.includes(h))).join(\"\\r\\n\");\n      const next = filtered.replace(/[\\r\\n\\s]+$/g, \"\") + \"\\r\\n\";\n      atomicWriteHostsWin(HOSTS_FILE, current, next);\n      await runElevatedPowerShell(\"ipconfig /flushdns | Out-Null\");\n    } else {\n      const current = fs.readFileSync(HOSTS_FILE, \"utf8\");\n      const filtered = current.split(/\\r?\\n/).filter(l => !entriesToRemove.some(h => l.includes(h))).join(\"\\n\");\n      const next = filtered.replace(/[\\r\\n\\s]+$/g, \"\") + \"\\n\";\n      const escaped = next.replace(/'/g, \"'\\\\''\");\n      await execWithPassword(`printf '%s' '${escaped}' | tee ${HOSTS_FILE} > /dev/null`, sudoPassword);\n      await flushDNS(sudoPassword);\n    }\n    log(`🌐 DNS ${tool}: ✅ removed ${entriesToRemove.join(\", \")}`);\n  } catch (error) {\n    const msg = error.message?.includes(\"incorrect password\") ? \"Wrong sudo password\" : `Failed to remove DNS entry: ${error.message}`;\n    throw new Error(msg);\n  }\n}\n\n/**\n * Remove ALL tool DNS entries (used when stopping server)\n */\nasync function removeAllDNSEntries(sudoPassword) {\n  for (const tool of Object.keys(TOOL_HOSTS)) {\n    try {\n      await removeDNSEntry(tool, sudoPassword);\n    } catch (e) {\n      err(`DNS ${tool}: failed to remove — ${e.message}`);\n    }\n  }\n}\n\n/**\n * Sync removal of ALL tool DNS entries — for use during process shutdown","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/decolua/9router/blob/90b52e06ffd666b7929554211474d01588f6b1f8/src/mitm/dns/dnsConfig.js#L195-L231","documentation":"removeDNSEntry wraps all hosts-file mutation (sudo tee on Unix, elevated PowerShell on Windows) and DNS flush in try/catch. If the underlying exec fails with a message containing 'incorrect password' it rethrows 'Wrong sudo password'; any other failure is rethrown as 'Failed to remove DNS entry: <detail>'. This converts OS-level errors (bad sudo credential, hosts file permissions, missing ipconfig/flush) into a single clear error for callers.","triggerScenarios":"Calling removeDNSEntry(tool, sudoPassword) where sudoPassword is wrong or stale (password changed, cached password expired); on Windows where the elevated PowerShell prompt was denied or UAC declined; hosts file locked/read-only or flushDNS command missing on the PATH.","commonSituations":"User typed the wrong sudo password into the dashboard prompt; macOS/Linux password rotated while the app cached an old one; running headless where sudo cannot prompt; Windows executed without admin rights so the hosts write fails.","solutions":["Re-enter the correct sudo password and retry the call","Clear any cached sudo password (e.g. setCachedPassword) so the user is re-prompted","Verify manual sudo access: run `sudo -k; sudo -v` in a terminal to confirm the password works","On Windows, run the process/dashboard as Administrator so the elevated PowerShell can write the hosts file","Inspect the nested error.message after the pipe to identify non-password causes (permissions, missing flushdns)"],"exampleFix":"// before\nawait removeDNSEntry('cursor', stalePassword);\n// after\ntry {\n  await removeDNSEntry('cursor', passwordFromUserPrompt);\n} catch (e) {\n  if (e.message === 'Wrong sudo password') passwordFromUserPrompt = await promptForSudoPassword();\n  else throw e;\n}","handlingStrategy":"try-catch","validationCode":"// Pre-check sudo credential before mutating hosts (Unix)\nconst { execSync } = require('child_process');\ntry { execSync(`sudo -k -S -v`, { input: sudoPassword + '\\n' }); }\ncatch { throw new Error('Provided sudo password is invalid'); }","typeGuard":null,"tryCatchPattern":"try {\n  await removeDNSEntry(tool, sudoPassword);\n} catch (e) {\n  if (e.message === 'Wrong sudo password') {\n    sudoPassword = await promptForSudoPassword(); // re-prompt and retry once\n    return removeDNSEntry(tool, sudoPassword);\n  }\n  throw e;\n}","preventionTips":["Always obtain the sudo password interactively at time of use; avoid long-lived caches","Run the process elevated (Administrator on Windows) so hosts writes succeed","Test the password with `sudo -v` before batch DNS operations","Handle both error variants ('Wrong sudo password' and 'Failed to remove DNS entry: ...') distinctly"],"tags":["dns","sudo","authentication","permissions"],"backgroundTag":"wrong-sudo-password","analyzedSha":"90b52e06ffd666b7929554211474d01588f6b1f8","analyzedAt":"2026-08-30T21:05:45.952Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}