Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions changes/pair-over-ssh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
type: new
link: docs/README.md
---

## Pair two machines over SSH, with nobody at either screen

From a machine that reaches the other over SSH, `localify machines pair <machine>
--over-ssh <destination>` starts the other side through `ssh`, compares both codes
itself, and confirms both only when they match. An agent you gave SSH access to both
machines can now pair them. It needs OpenSSH 8.7 or later on the machine you run it on.
A pairing cut short after a confirm now says what each machine saved, instead of claiming
nothing was, and pairing again completes it. A machine on 1.7.0 or earlier can't say
whether it saved, so a pairing with it reports only what this machine saved.
28 changes: 28 additions & 0 deletions desktop-ui/src/components/app/remote/remote-pairing-dialogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ function OutgoingPairingDialog() {
</DialogFooter>
</>
) : null}

{outgoing.step === "partial" ? (
<>
<DialogHeader>
<DialogTitle>Pairing with {name} did not finish</DialogTitle>
<DialogDescription>{outgoing.message}</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant="outline" onClick={dismissOutgoing}>
Close
</Button>
</DialogFooter>
</>
) : null}
</DialogContent>
</Dialog>
);
Expand Down Expand Up @@ -218,6 +232,20 @@ function IncomingPairingDialog() {
</DialogFooter>
</>
) : null}

{incoming.step === "partial" ? (
<>
<DialogHeader>
<DialogTitle>Pairing with {incoming.name} did not finish</DialogTitle>
<DialogDescription>{incoming.message}</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant="outline" onClick={dismissIncoming}>
Close
</Button>
</DialogFooter>
</>
) : null}
</DialogContent>
</Dialog>
);
Expand Down
9 changes: 9 additions & 0 deletions desktop-ui/src/lib/util.remote-controls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ test("with controls off, an action that runs in the background asks for backgrou
});
});

test("a target of a later protocol asks and relaunches as on protocol 2", () => {
assert.deepEqual(quickAction(item(), "start", 3, "B", true), { kind: "ask", wants: "control" });
const relaunch = legacy({ start: allowed, stop: allowed, restart: allowed });
assert.deepEqual(quickAction(relaunch, "restart", 3, "B", true), {
kind: "relaunch",
access: null,
});
});

test("a target of protocol 1 only gets the command to run there", () => {
assert.deepEqual(quickAction(item(), "start", 1, "B", true), {
kind: "command",
Expand Down
9 changes: 7 additions & 2 deletions desktop-ui/src/lib/util.remote-controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function quickAction(
const control = item.controls[action];
const legacyId = item.run?.legacyId;
if (legacyId && (action === "restart" || control.code === "legacy_run")) {
if (proto !== 2) return { kind: "message", text: `Update Localify on ${machine}.` };
if (!asksAccess(proto)) return { kind: "message", text: `Update Localify on ${machine}.` };
// A relaunch is a restart: the restart's checks decide.
const restart = item.controls.restart;
if (restart.allowed) return { kind: "relaunch", access: null };
Expand All @@ -100,10 +100,15 @@ export function quickAction(
if (control.allowed) return { kind: "run" };
if (control.code === "legacy_run") return { kind: "message", text: `Relaunch it on ${machine}.` };
const wants = accessWants(control.code, runsInBackground(item, action, appRunning));
if (wants) return proto === 2 ? { kind: "ask", wants } : { kind: "command", wants };
if (wants) return asksAccess(proto) ? { kind: "ask", wants } : { kind: "command", wants };
return { kind: "message", text: refusalText(control.code, machine) };
}

/** Protocol 2 and later carry access requests and relaunches. */
function asksAccess(proto: number | null): boolean {
return proto !== null && proto >= 2;
}

function refusalText(code: string | undefined, machine: string): string {
return remoteErrorMessage(code ?? "", machine, `Not available on ${machine}.`);
}
Expand Down
17 changes: 17 additions & 0 deletions desktop-ui/src/lib/util.remote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
ownerLabel,
PAIRING_CANCELLED,
pairingCancelledMessage,
pairingPartialMessage,
remoteErrorMessage,
remoteHost,
serviceChips,
Expand Down Expand Up @@ -136,6 +137,22 @@ test("a pairing nobody can confirm says how to fix it", () => {
);
});

test("a pairing that stopped after a confirm says what each side saved", () => {
assert.equal(
pairingPartialMessage("B", "pinned", "unknown", "timeout"),
"Pairing did not finish on both machines. This Mac saved B's key; B may or may not have saved this Mac's key. Pair them again to complete it.",
);
assert.equal(
pairingPartialMessage("B", "not_pinned", "pinned", "done"),
"Pairing did not finish on both machines. This Mac did not save B's key; B saved this Mac's key. Pair them again to complete it.",
);
// A peer on 1.7.0 or earlier never says it saved: pairing again ends the same way.
assert.equal(
pairingPartialMessage("B", "pinned", "unknown", "unconfirmed"),
"This Mac saved B's key. B runs an older version of Localify, which doesn't say whether it saved this Mac's key: check on B, or update Localify there and pair again.",
);
});

test("a running service whose port does not answer yet is starting", () => {
const chips = serviceChips(item({ process: "running", owner: "background" }));
assert.deepEqual(
Expand Down
25 changes: 25 additions & 0 deletions desktop-ui/src/lib/util.remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
RemoteInventoryItemDto,
RemoteMachineDto,
RemoteOwner,
RemotePinState,
RemoteStatusDto,
} from "@/types/localify";

Expand Down Expand Up @@ -177,6 +178,30 @@ export function remoteErrorMessage(code: string, machine: string, fallback: stri

export const PAIRING_CANCELLED = "Pairing cancelled. Nothing was saved.";

/** What each side saved when a pairing stopped after a confirm (docs/remote-machines.md, "Partial results"). */
export function pairingPartialMessage(
machine: string,
local: RemotePinState,
peer: RemotePinState,
reason: string,
): string {
const here =
local === "pinned"
? `This Mac saved ${machine}'s key`
: `This Mac did not save ${machine}'s key`;
// Pairing again would end the same way.
if (reason === "unconfirmed") {
return `${here}. ${machine} runs an older version of Localify, which doesn't say whether it saved this Mac's key: check on ${machine}, or update Localify there and pair again.`;
}
const there =
peer === "pinned"
? `${machine} saved this Mac's key`
: peer === "not_pinned"
? `${machine} did not save this Mac's key`
: `${machine} may or may not have saved this Mac's key`;
return `Pairing did not finish on both machines. ${here}; ${there}. Pair them again to complete it.`;
}

/** Why a pairing this Mac asked for did not happen (docs/remote-machines.md, "Pairing"). */
export function pairingCancelledMessage(reason: string, message: string, machine: string): string {
switch (reason) {
Expand Down
30 changes: 27 additions & 3 deletions desktop-ui/src/providers/remote-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { RemotePairingDialogs } from "@/components/app/remote/remote-pairing-dia
import { remoteQueries } from "@/lib/queries";
import { openRemoteStream } from "@/lib/remote-stream";
import { tauriClient } from "@/lib/tauri-client";
import { pairingCancelledMessage } from "@/lib/util.remote";
import { pairingCancelledMessage, pairingPartialMessage } from "@/lib/util.remote";
import {
type AccessPrompt,
accessPromptAdded,
Expand All @@ -48,7 +48,9 @@ export type OutgoingPairing =
| { step: "asking"; machine: RemoteMachineDto }
| { step: "code"; machine: RemoteMachineDto; pairingId: string; code: string; answered: boolean }
| { step: "paired"; machine: RemoteMachineDto }
| { step: "cancelled"; machine: RemoteMachineDto; message: string };
| { step: "cancelled"; machine: RemoteMachineDto; message: string }
/** It stopped after a confirm: one side may hold a pin the other lacks. */
| { step: "partial"; machine: RemoteMachineDto; message: string };

/** A pairing another machine asked for, with this Mac as the target. */
export type IncomingPairing =
Expand All @@ -61,7 +63,8 @@ export type IncomingPairing =
answered: boolean;
}
| { step: "paired"; pairingId: string; name: string }
| { step: "cancelled"; pairingId: string; name: string };
| { step: "cancelled"; pairingId: string; name: string }
| { step: "partial"; pairingId: string; name: string; message: string };

/** Another machine asks this Mac to turn on remote controls (docs/remote-machines.md). */
export interface IncomingAccess {
Expand Down Expand Up @@ -140,6 +143,19 @@ export function RemoteProvider({ children }: PropsWithChildren) {
: current,
);
return;
case "pair_partial":
setIncoming((current) =>
current && current.pairingId === event.pairingId
? {
step: "partial",
pairingId: event.pairingId,
name: event.name,
message: pairingPartialMessage(event.name, event.local, event.peer, event.reason),
}
: current,
);
refreshMachines();
return;
case "access_request": {
const { event: _, expiresAt: __, ...request } = event;
setAccessQueue((queue) => accessPromptAdded(queue, request));
Expand Down Expand Up @@ -219,6 +235,14 @@ export function RemoteProvider({ children }: PropsWithChildren) {
message: pairingCancelledMessage(event.reason, event.message, machine.name),
});
return;
case "partial":
setOutgoing({
step: "partial",
machine,
message: pairingPartialMessage(machine.name, event.local, event.peer, event.reason),
});
refreshMachines();
return;
}
};
void openRemoteStream<RemotePairEventDto>(
Expand Down
26 changes: 25 additions & 1 deletion desktop-ui/src/types/localify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,17 @@ export type RemoteEventDto =
| { event: "pair_request"; pairingId: string; machineId: string; name: string; code: string }
| { event: "paired"; pairingId: string; machineId: string; name: string }
| { event: "pair_cancelled"; pairingId: string; machineId: string; reason: string }
/** The pairing ended after a confirm with only one side known to hold a pin. */
| {
event: "pair_partial";
pairingId: string;
machineId: string;
name: string;
local: RemotePinState;
peer: RemotePinState;
/** `unconfirmed`: the peer runs a version that cannot say it saved. */
reason: string;
}
| { event: "presence"; machineId: string; state: "online" | "offline" }
| { event: "revoked"; machineId: string }
| { event: "unpaired"; machineId: string }
Expand Down Expand Up @@ -1318,11 +1329,24 @@ export interface RemoteAccessSuppressedDto {
since: string;
}

/** A side's pin after a pairing: its own as it verified it, the other's as it said. */
export type RemotePinState = "pinned" | "not_pinned" | "unknown";

/** What `remote_pair` says to the machine that asked. */
export type RemotePairEventDto =
| { event: "code"; pairingId: string; machineId: string; name: string; code: string }
| { event: "paired"; machineId: string; name: string }
| { event: "cancelled"; reason: string; message: string };
| { event: "cancelled"; reason: string; message: string }
/** After a confirm, only one side may hold a pin (docs/remote-machines.md, "Partial results"). */
| {
event: "partial";
machineId: string;
name: string;
local: RemotePinState;
peer: RemotePinState;
reason: string;
message: string;
};

export interface RemoteMachineSummaryDto {
machineId: string;
Expand Down
13 changes: 11 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,17 @@ localify machines pair --accept # or wait for a request on the other one

Both machines show the same 6-digit code, and you confirm it on each. What the machines
say to each other is end-to-end encrypted, so Localify Cloud relays it without reading
it. Pairing and removing a machine need a person at a terminal or in the app; agents
can't do it. `localify machines unpair <machine>` forgets a paired machine, and
it. Pairing needs a person at a terminal or in the app, with one exception: from a
machine that can reach the other over SSH, `localify machines pair teo-desktop
--over-ssh teo@teo-desktop.local` starts the other side through `ssh`, compares both
codes itself, and confirms both only when they match. So an agent you gave SSH access to
both machines can pair them. It needs OpenSSH 8.7 or later on the machine you run it
on, and the host key must already be trusted; when ssh's shell doesn't find `localify`
there, pass its path with `--remote-localify`. It exits 0 once both machines saved each
other, 2 when nothing was saved, and 4 when only one may have: pairing again completes
it. A machine on Localify 1.7.0 or earlier can't say whether it saved, so pairing with it
always exits 4 until you update it. Removing a machine always needs a person.
`localify machines unpair <machine>` forgets a paired machine, and
`localify machines remove <machine>` signs it out of the account.

**Choose what others may see and do here.** Each machine decides for itself, in Settings
Expand Down
18 changes: 18 additions & 0 deletions docs/adr/0035-a-machine-reached-over-ssh-pairs-without-a-person.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# A Machine Reached Over SSH Pairs Without a Person

`localify machines pair` used to need a person at a terminal on both machines. The founder wants an agent that already has trusted access to both machines to pair them itself, as one did for him over SSH with a script that typed `yes`.

The short code exists to catch a relay that sits in the middle: it makes both sides derive different codes. What matters is that someone compares the two codes, not that the someone is a person. Someone who can run commands on both machines can already read both codes, so a person adds nothing there. The weak point of the script was elsewhere. It typed `yes` on each side without comparing, so a relay in the middle would have passed.

So `localify machines pair B --over-ssh <destination>` runs the whole pairing from A:
- It starts `localify machines pair --accept --for-ssh` on B through `ssh <destination>`.
- It reads both codes and compares them in Localify's own code.
- It confirms both sides only when they match, and cancels both otherwise.

The trust comes from the SSH access the caller already has, which is exactly the access a person would use, so that access must itself be authenticated. The SSH host key must already be trusted: an unknown or changed key fails, since an attacker who could answer for B over SSH and sit in the relay would otherwise pass the comparison. B's daemon must also report the machine id and the account A expects. Without `--over-ssh`, a person still confirms at a terminal or in the app, as before.

`--for-ssh` is a machine-readable interface for whoever runs it as a user that can reach B's daemon. It is not a check of its own: someone with access to B alone could feed it back its own code, which authorizes B's side only, never A's. The comparison that protects the pairing is A's. It refuses a terminal, which only keeps a person from using it by mistake.

The MCP server still has no pairing tool. An agent pairs through the CLI and its own SSH access, which a person granted. Removing a machine from the account stays a person's decision.

A lost connection after the confirmations started can leave one side pinned. That was already true of every pairing. The contract now says so: the CLI reports what each side did, and pairing the same machines again completes it.
Loading
Loading