Skip to content

Add UNIX socket support (-s/--unixsocket) - #5

Open
alberti42 wants to merge 1 commit into
spamwax:masterfrom
alberti42:unix-socket
Open

alberti42 wants to merge 1 commit into
spamwax:masterfrom
alberti42:unix-socket

Conversation

@alberti42

@alberti42 alberti42 commented Oct 5, 2026 •

Copy link
Copy Markdown

Summary

This PR lets rmate connect to the editor over a UNIX socket, as the Ruby implementation does.

Motivation

On a shared server, such as the login node of an HPC cluster, a port opened with RemoteForward 52698 localhost:52698 listens on localhost, and every user on that machine can connect to this port:

  • If another user already holds the port, ssh prints only a warning, and rmate sends the file to that user's tunnel and editor. This can be quite annoying because it breaks the expected behavior and in the worst case, it can lead to a leakage of sensitive information like passwords and credentials.
  • While the tunnel is up, any user on the machine can open buffers in your editor. This can also be quite annoying if it happens, and certainly confusing to see in your editor a completely alien file.

Luckily there is a solution. OpenSSH can forward a UNIX socket instead:

RemoteForward /u/%r/.rmate.socket localhost:52698
ExitOnForwardFailure yes

sshd creates the socket with owner-only permissions (StreamLocalBindMask, default 0177), so only its owner can connect.

The socket works both under macOS and Linux systems.

Details about the implementation design

  • The path comes from -s/--unixsocket <PATH>, the RMATE_UNIXSOCKET environment variable, or the unixsocket key in rmate.rc. It defaults to ~/.rmate.socket, and a leading ~/ is expanded.
  • The socket takes precedence over host/port if the file exists. Otherwise rmate connects to host/port as before.
  • The precedence is the same as for host and port: command line or environment, then rmate.rc, then the default.

Changes

List of changes automatically generated by CLAUDE:

  • src/settings.rs: new unixsocket option (-s, --unixsocket, RMATE_UNIXSOCKET) and RMATE_UNIXSOCKET default. read_disk_settings() now also returns the unixsocket key from rmate.rc; the RcSettings struct already had the field but did not use it. New expand_home() helper.
  • src/main.rs: fills unixsocket from rmate.rc or the default when it is not given on the command line or in the environment.
  • src/remote_editor.rs: connect_to_editor() connects to the UNIX socket if the file exists, and otherwise to host/port. The existing socket2 dependency already enables the unix feature, so no dependency is added.
  • README.md: one entry under Features.

Testing

Tested on macOS (aarch64) against a test editor that receives the file, then sends save and close:

Case Result
socket path from -s file saved
socket path from RMATE_UNIXSOCKET file saved
socket path from rmate.rc, ~/ expanded file saved
socket file absent → falls back to host/port file saved
plain host/port, no socket file saved
socket file exists, no listener Connection refused, exit 1

Also tested end to end on Linux (RHEL 9, kernel 5.14, x86_64): rmate on the remote host connected through a socket created by RemoteForward /u/%r/.rmate.socket localhost:52698, and the file opened in the editor on macOS.

The repository has no automated tests for the connection code (cargo test runs 0 tests), so this PR adds none.

Further notes about interactions via sockets

  • As in the Ruby rmate, a stale socket file blocks the fallback: if the file exists and nobody listens on it, rmate exits with Connection refused instead of trying host/port. sshd leaves the socket behind on disconnect unless the server sets StreamLocalBindUnlink yes.
  • On macOS, socket paths are limited to 104 bytes; SockAddr::unix() returns an error for longer paths.

Connect to the editor over a UNIX socket, as the Ruby rmate does. The
path comes from -s/--unixsocket, RMATE_UNIXSOCKET, or the unixsocket key
in rmate.rc, and defaults to ~/.rmate.socket. The socket takes
precedence over host/port if the file exists; otherwise rmate connects
to host/port as before.

A forwarded TCP port on a shared server accepts connections from every
user on that machine. A socket forwarded with SSH RemoteForward is
created with owner-only permissions, so only its owner can connect.
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