Skip to content

fix(discovery): accept a herdr socket on macOS, where its own real path cannot be read - #3943

Merged
devswha merged 1 commit into
mainfrom
fix/herdr-discovery-macos-socket
Nov 16, 2026
Merged

devswha merged 1 commit into
mainfrom
fix/herdr-discovery-macos-socket

Conversation

@devswha

@devswha devswha commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Change

What was wrong. On macOS, Bun 1.4.2's realpathSync throws EOPNOTSUPP for a unix socket file (realpathSync.native too). chosenHerdrSocketStatus in server/herdr-discovery.ts ran realpathSync(path) !== path on the socket inside its try, and it returns refused for every error other than ENOENT. So on macOS every herdr socket that passed the lstat checks was refused, even while its herdr was running and answering:

  • discoverHerdrSessions returned [], so a Mac added as a remote PC offered no running herdr session (remote-entry --discover, and found_herdr_sessions in server/dispatch.ts);
  • a chosen socket was refused (remote-entry --socket-status, and the chosen-socket path of remote-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 status answering running on both, then main's own server/remote-entry.ts:

Linux x86_64, Bun 1.4.2, herdr 0.9.5 macOS arm64, Bun 1.4.2, herdr 0.9.3
remote-entry --discover [{"socket_path":"…/sessions/e2e/herdr.sock","session":"e2e","label":"e2e"}] []
remote-entry --socket-status <that socket> usable refused

A separate probe on both systems: lstat says it is a socket owned by this user, and realpathSync on it returns the same path on Linux and throws EOPNOTSUPP on 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, where realpathSync of the socket worked, the decision is the same for the paths the tests cover.

Other realpath calls in server/ and scripts/ that can get a socket path are not on the herdr discovery path and are left as they are: server/bridge.ts socketPath() (falls back to the given path when realpathSync throws) and server/conversation.ts reachesOwnCore (compares two hc core socket paths, false when one throws).

Tests (server/herdr-discovery.test.ts):

  • New: "accepts a real listening socket on this platform". The real function, with no faked platform, uid or probe, on a real socket from Bun.listen({ unix }) under <home>/.config/herdr/sessions/work/herdr.sock: chosenHerdrSocketStatus says usable and discoverHerdrSessions lists it. The probe is the real herdrAcceptsConnection, which only checks that the socket accepts a connection, so the listener is enough.
  • The test roots are now the real path of a folder under /tmp on macOS (tmpdir() elsewhere), because macOS reaches its temp folder through a link and caps a socket path at 104 bytes.
  • The link cases were already there and still pass: "never offers socket or parent symlinks even inside an allowed root" (a link at the socket path, and a link to a session folder) and "rejects a symlink escaping HOME and the configured XDG root".

CI. The macOS job now runs ./server/herdr-discovery.test.ts too.

Validation

  • Linux, bun test server/herdr-discovery.test.ts server/remote-entry.test.ts: 36 pass, 0 fail.
  • On Linux the new test passes before the fix as well, since the old check works there. To show that it catches the failure, the comparison was made always unequal for one run: the new test failed with Expected: "usable", Received: "refused". The change was then reverted.
  • bun run typecheck: exit 0.
  • bun run check fast: passed (actionlint included).
  • macOS arm64 (27.0.1), Bun 1.4.2, herdr 0.9.3, in a fresh clone of this pull request's head (eff53cc): bun test server/herdr-discovery.test.ts gives 17 pass, 0 fail, the new test among them. The end-to-end steps above on that clone: remote-entry --discover lists the running session, --socket-status says usable for its socket, and refused for the same socket reached through a linked session folder.
  • Not run: Windows.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where a Mac connected as a remote PC did not offer its running herdr sessions and refused selected sessions. Remote Mac sessions are now available to choose and can be accepted when herdr is running and responding.
  • Tests
    • Expanded automated checks for herdr session discovery, including validation with a listening socket on the current platform.

Socket validation now checks the socket’s parent directory’s canonical path. Tests cover real socket discovery, and the macOS CI session job runs the discovery test. A changelog entry describes the remote-Mac herdr session issue.

Changes: Herdr session discovery

Layer / File(s) Summary
Socket validation and discovery coverage
server/herdr-discovery.ts, server/herdr-discovery.test.ts, .github/workflows/ci.yml, changelog.d/remote-mac-herdr-sessions.md
Socket validation checks the socket type and UID, then checks the canonical path of its parent directory. Tests cover temporary roots and discovery of a real listening socket. The macOS session job runs the discovery test, and the changelog describes the reported remote-Mac session issue.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • devswha/ovis#3409: Defines the herdr socket discovery contract, including acceptance checks that this pull request changes.

Suggested labels: github_actions

Suggested reviewers: danipulok

Merge Risk: Minimal · up to eff53

This change lets a Mac connected as a remote PC list and accept its running herdr sessions again. Symlinked sockets and linked session folders are still refused. The change is narrow and has test and CI coverage, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: herdr socket discovery on macOS, where realpathSync on the socket path throws. It matches the diff, which moves the real-path check to the socket's parent folder. The …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the template. It has a Change section that states the problem (macOS Bun 1.4.2 realpathSync throws EOPNOTSUPP on unix sockets, causing herdr sockets to be refused), the resulti…
Full details: Title check

Explanation

The title names the main change: herdr socket discovery on macOS, where realpathSync on the socket path throws. It matches the diff, which moves the real-path check to the socket's parent folder. The title is specific and uses the conventional fix(scope) form.

Full details: Description check

Explanation

The description follows the template. It has a Change section that states the problem (macOS Bun 1.4.2 realpathSync throws EOPNOTSUPP on unix sockets, causing herdr sockets to be refused), the resulting behavior, the fix, the affected release (0.5.2), and reproduction steps with a comparison table. It has a Validation section listing the commands run on Linux and macOS, their results, the mutation check, and what was not run (Windows). It does not link a maintainer-agreed issue for the change, though the template asks for that only for features; this is a bug fix, so the gap is minor.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between fc6feba and eff53cc.

Files selected for processing: 4

  • .github/workflows/ci.yml
  • changelog.d/remote-mac-herdr-sessions.md
  • server/herdr-discovery.test.ts
  • server/herdr-discovery.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@devswha
devswha merged commit 85689e7 into main Nov 16, 2026
22 checks passed
@devswha
devswha deleted the fix/herdr-discovery-macos-socket branch November 16, 2026 04:49
@devswha

devswha commented Nov 16, 2026

Copy link
Copy Markdown
Owner Author

Merge pass

Merged head eff53cc06d86 as 85689e749502 on GitHub's checks, with no unresolved review threads.

Reviewer Verdict Note
claude CLEAR
codex CLEAR

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.
Two reviewers. The changed line is a security check, so one of them ran 32 cases on Linux with real sockets against main's function and this head's: a link at the socket path, a linked folder anywhere in the path, unnormalised spellings, another account's socket, stale files: the two never answered differently. The link is still refused because the socket is read with lstat; the folder now carries the real-path check.
The two other real-path calls that can meet a socket path are left alone and fall back safely: one answers "not my own core" on an error, the other feeds only comparisons that fail closed.
The workflow changes by one line: the discovery test file joins the macOS job's list, so the real function runs on a real socket on a Mac in CI. No check is removed or loosened.
Not run in review: macOS (the author's output), Windows.

devswha added a commit that referenced this pull request Nov 16, 2026
#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant