Skip to main content
kRouter
All posts
Fix an error

A missing PATH entry under launchd turned into "no saved password found"

MITM never came back after a reboot. The saved password was on disk and decrypted fine by hand. A LaunchAgent's PATH lacked /usr/sbin, a helper shelled out to ioreg, and the error surfaced as the wrong sentence entirely.

Kodelyth · The team behind kRouter
8 min read

After a reboot, capture was off. The dashboard said:

MITM was enabled but no saved password found, skipping auto-start

The password was on disk. It decrypted correctly by hand. The sentence was wrong about the one fact it asserted — and because it was a plausible sentence, it sent everyone looking at the wrong thing.

The chain

The encryption key for the saved sudo password is derived from a machine identifier. Getting that identifier used node-machine-id, which on macOS shells out to ioreg.

ioreg lives in /usr/sbin.

A LaunchAgent does not inherit your shell's environment. This one's PATH was:

<node>/bin:/usr/local/bin:/usr/bin:/bin

No /usr/sbin. Measured directly:

$ env -i PATH=<the plist's PATH> node -e 'machineIdSync()'
/bin/sh: ioreg: command not found

From there it degrades quietly, one honest-looking step at a time:

  1. The ioreg call throws.
  2. Key derivation catches it and falls back to hashing the salt alone — producing a different, valid-looking key.
  3. AES-GCM decrypts with the wrong key and rejects the authentication tag.
  4. The caller sees a failed decrypt and reports it as no password present.

Step 4 is where the truth is lost. "Could not decrypt" and "nothing is stored" are different facts with different fixes, and the second one is far more comforting, so that is the one that got printed.

Step 2 is where the bug actually is. A fallback that produces a different key rather than failing is not a fallback — it is a silent change of behaviour disguised as resilience. Every layer after it is working correctly on the wrong input.

Interactively, everything worked. Your shell has /usr/sbin, so ioreg runs, the real machine id comes back, the right key is derived, and the password opens. The bug exists only in the environment nobody has a terminal in.

The failure stacked with another one

MITM not auto-starting was the first bug. The second made it worse: the watchdog, finding no server running, stripped the DNS redirects it could no longer serve.

That is correct behaviour in isolation — it is the self-healing added after crashes stranded machines. But combined with a failed auto-start it means capture is not merely down after a reboot, it is actively torn down, so there is nothing half-working to notice. It is off, cleanly, until someone opens the dashboard and turns it on.

Two correct-looking behaviours composing into a silent regression is a recurring theme in this system.

The fix

The machine id now comes from a file the app already maintains at DATA_DIR/machine-id. That file exists for exactly this reason — its own helper documents it as guaranteeing that the CLI, server and middleware see the same value even when machineIdSync fails.

Reading a file needs no PATH. The entire class of failure disappears rather than being patched: no subprocess, no environment dependency, no difference between a login shell and a LaunchAgent.

Two details made it safe to change:

  • Decryption tries every historical key. A password saved under the old derivation still opens, so nobody has to re-enter anything after upgrading. Changing a key derivation without this would have logged every existing user out of their own saved credential.
  • The candidates are memoized. The old call ran on every decrypt attempt and printed ioreg: command not found 162 times in a single boot — which, notably, was visible the whole time in the logs and still did not lead anyone to the cause, because the headline error said something else.

What to take from it

Two things, and neither is about macOS.

A fallback that substitutes a different value is not error handling. If deriving a key can fail, the honest options are to fail loudly or to try a known alternative and say which one was used. Silently hashing something else produces a key that is wrong in a way no downstream layer can detect — it just looks like bad data.

An error message that states a fact should state a fact you verified. "No saved password found" was a conclusion inferred from a failed decrypt, not an observation of the filesystem. The file was right there. Checking whether it existed before claiming it did not would have turned a multi-step investigation into a one-line contradiction.

If MITM has not been coming back after reboots, this landed in 0.5.185.

Kodelyth · The team behind kRouter

Published by Kodelyth, the team that builds kRouter. Posts are drafted with AI assistance and reviewed by a person before they go out. kRouter is free and MIT licensed.

Install kRouter