Repository navigation
fix(discovery): accept a herdr socket on macOS, where its own real path cannot be read - #3943
Conversation
…th cannot be read
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
Merge passMerged head
Merge pass note for #3943 at eff53cc (fix(discovery): accept a herdr socket on macOS). A defect shipped since 0.5.2: a Mac connected as a remote PC offered none of its running herdr sessions and refused a chosen one. Under Bun 1.4.2 on macOS the real path of a socket file cannot be read, and the check read that error as "refused". Reproduced end to end on main on a Mac by the author (main's own remote-entry commands against a running herdr: Linux lists the session and says usable; the Mac listed none and said refused), and checked fixed there on this head: the session is listed, its socket is usable, and the same socket reached through a linked folder is still refused. |
#3938) ## Change Adds **the machine profile**: a small Ovis server that runs beside a herdr a machine already has, so that a person's own devices can open that machine through the relay. It is a preview: it starts only with `OVIS_PROFILE=machine` and `OVIS_PREVIEW=fleet` together, no release sets either, and nothing in the app starts it. What the profile does: - It serves classic herdr only; when herdr-core is selected (by `OVIS_RUNTIME` or by the profile's saved choice) it reports the engine as unavailable. - It listens on loopback only, with its pairing gate closed from the first start. LAN HTTPS and the direct carrier are off. Engine updates, runtime changes, Portal, pairing at the machine's own address (`/api/devices/pair`) and the routes between PCs (`/api/machines`, `/api/fleet`, `/api/bridge`) answer 409 `machine_profile`. The relay's own pairing routes are not refused. `/api/updates/*` is not on that list: its POST routes (`check`, `install`, `cancel`) answer 409 `updates_unmanaged`, whatever updater the profile is started with. - On its TCP port it answers liveness and nothing else, whatever a request carries. `OVIS_TOKEN` is ignored there, and the start says so once. - Its local owner reaches it on a unix socket in its state folder (`serve.sock`, mode 0600, in a folder of mode 0700 that it checks from the root down before it starts). A request on that socket must name `localhost` and carry no forwarding header; the profile's list of such headers is longer than an ordinary server's (it adds ten names that proxies and tunnels send, for example `x-forwarded-port`, `cf-connecting-ip`, `true-client-ip`). - It starts only on a state folder that is absent, empty, or already holds its own marker `machine-profile.json` (a leftover `serve.own` alone counts as empty). It looks before it takes the folder's lock and again under it. Any other folder is refused in one sentence and nothing is written, so a profile does not take a folder another server wrote to first. - It talks to a herdr socket only when the socket is this account's, is reached through no link, lies under this account's herdr folder, and no other account can rename or replace any folder from the root down to it (the walk it applies to its own state folder; a sticky folder such as `/tmp` may be on the way). That is checked when the socket is selected, again before every connection, and before each `herdr` command the profile starts; until it holds, the engine is reported unavailable and nothing connects to that socket. With no session selected, a candidate whose folder fails that walk is not connected to and not counted, so one safe session beside an unsafe one is the one it keeps. - When no session is selected and none or several run at its start, it looks again on its owner's or a device's next health check, session read or terminal connection, and keeps the first single session it finds. - A device that comes through the relay with its proof keeps its role (drive or watch); the controls refused above are refused to it as well. - It does not start on Windows. What changes for an ordinary server (no `OVIS_PROFILE`): - With `OVIS_PREVIEW` unset, nothing but the two exceptions below: it does what `main` does. It takes no lock, makes no new file and prints no new line; at its start it looks up one more name in its state folder (the profile's marker; with the default folder, the old folder's name once more), and anything there that is not the profile's marker changes nothing. - With `OVIS_PREVIEW=fleet`, `ovis-server serve` holds a shared lock on `serve.own` in its state folder for its lifetime (not on Windows), so that a machine profile does not start on a state folder that server holds, nor that server on one a profile holds. If the lock cannot be taken for another reason, it prints one line and starts without that protection. The two stated exceptions to "with `OVIS_PREVIEW` unset a build behaves like `main`", each accepted by the owner: - `OVIS_PROFILE=machine` without `OVIS_PREVIEW=fleet` says what to set and exits 1, where `main` ignores the variable and starts an ordinary server. Someone who asks for the locked-down profile must not get a full ordinary server without knowing it. No release and nothing in the app sets `OVIS_PROFILE`. - An ordinary server refuses to start on a state folder that holds a machine profile's marker (`machine-profile.json`): it says which folder and to give it another `OVIS_STATE_DIR`, and exits 1, where `main` starts on that folder. A profile writes the marker only into the folder it runs on. Every other ordinary start reads that one file name in its state folder and, finding no marker, goes on as on `main`. Known limits of this preview: - Running an ordinary server from before this change (0.5.4 or earlier, or a build of `main` before it) on a profile's state folder is not supported. Security item 3938-M1 (medium, open). - An ordinary server without the preview started at the same moment as a profile on one empty state folder is not refused (#3952). - A named herdr session is refused when a folder on its path can be written by a group that cannot be shown to be private to this account, or by everyone. The refusal names that folder: remove group and other write from it (`chmod go-w <folder>`), or select a session in a folder only this account can write. - A crash while the profile writes its marker can leave `machine-profile.json.<pid>.<hex>.tmp` in its state folder, and the profile refuses that folder until the owner removes that file. - A forwarder the owner puts in front of `serve.sock` (a proxy, socat, `ssh -L`) is his own exposure: its clients are the owner. ## Validation The Linux runs and the side-by-side runs below were made one commit before this pull request's head (the last commit changes one sentence of the changelog fragment). The tree holds `main` 85689e7 (with #3943): - Linux, one run of `bun test server/machine-profile.contract.test.ts server/owner-guard.test.ts server/herdr-discovery.test.ts server/relay/service.contract.test.ts server/remote-entry.test.ts server/profile-marker.test.ts`: 204 pass, 5 skip, 0 fail. - `bun run check fast` on this head: passed (workflow syntax, generated types and themes, typecheck, build; unit tests 17,606 pass, 0 fail, 54 skip). - macOS arm64 (27.0.1), Bun 1.4.2, herdr 0.9.3, in a fresh clone of this pull request's head (e8fd0d0), `bun test server/machine-profile.contract.test.ts server/owner-guard.test.ts`: 96 pass, 5 skip, 0 fail. One start of an ordinary `ovis-server serve` there: without `OVIS_PREVIEW` its state folder holds no `serve.own`; with `OVIS_PREVIEW=fleet` it does. - Windows 11 (10.0.26300), Bun 1.4.2, in a fresh clone of this pull request's head (e8fd0d0), `bun test ./server/machine-profile.contract.test.ts`: 1 pass, 80 skip, 0 fail. The one case that runs is the refusal: the profile exits 1 with one sentence and leaves its state folder untouched. An ordinary server was not started there. - An ordinary server from this head and from `main` 85689e7, side by side: - Neither `OVIS_PROFILE` nor `OVIS_PREVIEW` set: the `serve` entrypoint under `strace` (19 requests) and an in-process `createServer` (11 requests) gave the same output lines, state folder, statuses and bodies; the entrypoint also the same exit code, state files, headers and `connect`/`execve` calls, and no lock held. In the file lookups of one start the only addition is one `statx` of the marker's name (absent, so never opened), and with the default state folder one more lookup of the old folder's name. - `OVIS_PREVIEW=fleet` on both sides: the only differences are `serve.own` and its shared lock. - A state folder that holds a profile's marker: `main` starts; this head exits 1 with one sentence and leaves the folder unchanged. Six other things at the marker's name (absent, unreadable, a folder, not JSON, a link, a FIFO or a huge file) start as on `main`. - Each fix has a test that failed before it and passes with it. From the first review: the lock only under the preview, the absent socket outside the trusted folders, the socket planted after the start, the path another account can change (absent and live), the folder that is not the profile's, the empty folder that gets its marker, the leftover `serve.own`, the session that starts later, the look again after a refusal, the first `/api/session` before any health check, the ten headers, the reason in the refusal. From the second: the look under the folder's lock, no updater, the walk before the probe (an unsafe candidate gets no connection; a safe one beside it is kept), the refusal that names the folder when none is left, the marker with the wrong content, the ordinary server refused on a marked folder (profile running, and stopped), the FIFO at the marker's name. Three could not fail on their own and were shown to fail under a deliberate change instead: the relay device on a refused route, the watch device's input, and the cache-age part of the `chmod` check. The Windows case and the `/proc` guard cannot fail on Linux. The macOS admin-group rule is covered by a unit test with a stand-in group. - Read before each push by a reviewer that did not write it, six times as the fixes went in. It found two blocking flaws in new code before they were published (a folder above the nearest one that another account could rename between check and use; a FIFO at the marker's name that kept an ordinary server from starting); both are closed and its probes of them now pass. The same reader checked each sentence of this description, the changelog fragment and the commit messages against the code. Not run: the supervisor's and the remote bridge's refusal on a marked folder as real starts (they share the tested function; read); an ordinary server and a profile started at the same moment without the preview; the two places where the profile starts a `herdr` command, against a live herdr; a herdr-core machine; a real phone opening a profile through the public relay; a second local account. The local merge gate that ran on this pull request's first revision was not run again.
Change
What was wrong. On macOS, Bun 1.4.2's
realpathSyncthrowsEOPNOTSUPPfor a unix socket file (realpathSync.nativetoo).chosenHerdrSocketStatusinserver/herdr-discovery.tsranrealpathSync(path) !== pathon the socket inside itstry, and it returnsrefusedfor every error other thanENOENT. So on macOS every herdr socket that passed thelstatchecks wasrefused, even while its herdr was running and answering:discoverHerdrSessionsreturned[], so a Mac added as a remote PC offered no running herdr session (remote-entry --discover, andfound_herdr_sessionsinserver/dispatch.ts);remote-entry --socket-status, and the chosen-socket path ofremote-entry).Since when. The check came with #3409 (77e95b8), first released in 0.5.2. That release, like main, pins Bun 1.4.2.
How it was reproduced. On public main (fc6feba), the same script on Linux and on a Mac: a real herdr session in a HOME of its own (
<home>/.config/herdr/sessions/e2e/herdr.sock),herdr statusansweringrunningon both, then main's ownserver/remote-entry.ts:remote-entry --discover[{"socket_path":"…/sessions/e2e/herdr.sock","session":"e2e","label":"e2e"}][]remote-entry --socket-status <that socket>usablerefusedA separate probe on both systems:
lstatsays it is a socket owned by this user, andrealpathSyncon it returns the same path on Linux and throwsEOPNOTSUPPon macOS.The fix. The socket itself is still checked with
lstat(isSocket()is false for a link, so the last component is no link), and the real-path comparison moves to its folder:realpathSync(dirname(path)) !== dirname(path). A link at the socket path or in one of its folders is still refused. On Linux, whererealpathSyncof the socket worked, the decision is the same for the paths the tests cover.Other
realpathcalls inserver/andscripts/that can get a socket path are not on the herdr discovery path and are left as they are:server/bridge.tssocketPath()(falls back to the given path whenrealpathSyncthrows) andserver/conversation.tsreachesOwnCore(compares two hc core socket paths,falsewhen one throws).Tests (
server/herdr-discovery.test.ts):Bun.listen({ unix })under<home>/.config/herdr/sessions/work/herdr.sock:chosenHerdrSocketStatussaysusableanddiscoverHerdrSessionslists it. The probe is the realherdrAcceptsConnection, which only checks that the socket accepts a connection, so the listener is enough./tmpon macOS (tmpdir()elsewhere), because macOS reaches its temp folder through a link and caps a socket path at 104 bytes.CI. The macOS job now runs
./server/herdr-discovery.test.tstoo.Validation
bun test server/herdr-discovery.test.ts server/remote-entry.test.ts: 36 pass, 0 fail.Expected: "usable",Received: "refused". The change was then reverted.bun run typecheck: exit 0.bun run check fast: passed (actionlint included).bun test server/herdr-discovery.test.tsgives 17 pass, 0 fail, the new test among them. The end-to-end steps above on that clone:remote-entry --discoverlists the running session,--socket-statussaysusablefor its socket, andrefusedfor the same socket reached through a linked session folder.