Skip to content

Commit 7302d06

Browse files
fix(server): a 100.x peer link holds only while Tailscale runs
`tailscale status --json` still lists this machine's and its peers' addresses when Tailscale is stopped or logged out, and traffic to them then leaves unencrypted. readTailnetAddresses now returns nothing unless BackendState is "Running", and resolve re-checks each stored address with peerOrigin before it may receive the link's token, skipping one that no longer passes as it would one that does not answer. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 26970fe commit 7302d06

4 files changed

Lines changed: 72 additions & 37 deletions

File tree

‎apps/server/src/peer/PeerLinks.test.ts‎

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -469,34 +469,35 @@ it.effect(
469469
).pipe(Effect.provide(NodeHttpServer.layerTest)),
470470
);
471471

472-
/** A's `tailscale`, whose `status --json` lists `peerIps` as another machine on its tailnet. */
473-
const layerTailscalePeers = (peerIps: ReadonlyArray<string>) =>
472+
/** `tailscale status --json` with `peerIps` as another machine on its tailnet. */
473+
const tailscaleStatus = (backendState: string, peerIps: ReadonlyArray<string>) =>
474+
JSON.stringify({
475+
BackendState: backendState,
476+
Self: { DNSName: "laptop.tail1234.ts.net.", TailscaleIPs: ["100.101.0.1"] },
477+
Peer: { "nodekey:box": { DNSName: "box.tail1234.ts.net.", TailscaleIPs: peerIps } },
478+
});
479+
480+
/** A's `tailscale`, whose `status --json` prints what `status` holds at each run. */
481+
const layerTailscale = (status: Ref.Ref<string>) =>
474482
Layer.succeed(
475483
ChildProcessSpawner.ChildProcessSpawner,
476484
ChildProcessSpawner.make(() =>
477-
Effect.succeed(
478-
ChildProcessSpawner.makeHandle({
479-
pid: ChildProcessSpawner.ProcessId(1),
480-
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
481-
isRunning: Effect.succeed(false),
482-
kill: () => Effect.void,
483-
unref: Effect.succeed(Effect.void),
484-
stdin: Sink.drain,
485-
stdout: Stream.make(
486-
new TextEncoder().encode(
487-
JSON.stringify({
488-
Self: { DNSName: "laptop.tail1234.ts.net.", TailscaleIPs: ["100.101.0.1"] },
489-
Peer: {
490-
"nodekey:box": { DNSName: "box.tail1234.ts.net.", TailscaleIPs: peerIps },
491-
},
492-
}),
493-
),
494-
),
495-
stderr: Stream.empty,
496-
all: Stream.empty,
497-
getInputFd: () => Sink.drain,
498-
getOutputFd: () => Stream.empty,
499-
}),
485+
Ref.get(status).pipe(
486+
Effect.map((json) =>
487+
ChildProcessSpawner.makeHandle({
488+
pid: ChildProcessSpawner.ProcessId(1),
489+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
490+
isRunning: Effect.succeed(false),
491+
kill: () => Effect.void,
492+
unref: Effect.succeed(Effect.void),
493+
stdin: Sink.drain,
494+
stdout: Stream.make(new TextEncoder().encode(json)),
495+
stderr: Stream.empty,
496+
all: Stream.empty,
497+
getInputFd: () => Sink.drain,
498+
getOutputFd: () => Stream.empty,
499+
}),
500+
),
500501
),
501502
),
502503
);
@@ -520,15 +521,27 @@ it.effect(
520521
Effect.gen(function* () {
521522
const b = yield* serveB(box);
522523
const port = new URL(b.url).port;
524+
const peerIps = ["100.100.1.2", "fd7a:115c:a1e0::1"];
525+
const status = yield* Ref.make(tailscaleStatus("Stopped", peerIps));
523526
const a = yield* makeA(
524527
laptop,
525528
undefined,
526-
Layer.mergeAll(
527-
layerTailscalePeers(["100.100.1.2", "fd7a:115c:a1e0::1"]),
528-
layerRouteHostToLoopback("100.100.1.2"),
529-
),
529+
Layer.mergeAll(layerTailscale(status), layerRouteHostToLoopback("100.100.1.2")),
530530
);
531531

532+
// A stopped Tailscale still lists its peers, but nothing would encrypt
533+
// the traffic to them.
534+
const stopped = yield* a.links
535+
.link({
536+
url: `http://100.100.1.2:${port}`,
537+
pairingCode: (yield* b.auth.issuePairingCredential()).credential,
538+
access: "auto",
539+
})
540+
.pipe(Effect.flip);
541+
expect(stopped.message).toContain("plain http");
542+
expect(yield* linkedSessions(b)).toEqual([]);
543+
yield* Ref.set(status, tailscaleStatus("Running", peerIps));
544+
532545
// WARP and carrier-grade NAT share the range; an address Tailscale does
533546
// not know would carry the code and token in clear.
534547
const unknown = yield* a.links
@@ -552,6 +565,16 @@ it.effect(
552565
urls: [`http://100.100.1.2:${port}`],
553566
});
554567
expect((yield* linkedSessions(b)).length).toBe(1);
568+
yield* probe(a);
569+
expect((yield* Ref.get(b.bearers)).length).toBeGreaterThan(0);
570+
571+
// Its token stays here once Tailscale stops, as it would have at link time.
572+
yield* Ref.set(status, tailscaleStatus("NeedsLogin", peerIps));
573+
yield* Ref.set(b.bearers, []);
574+
const refused = yield* a.links.resolve(box.environmentId).pipe(Effect.flip);
575+
expect(refused.message).toContain("Box did not answer");
576+
yield* probe(a).pipe(Effect.flip);
577+
expect(yield* Ref.get(b.bearers)).toEqual([]);
555578
}),
556579
).pipe(Effect.provide(NodeHttpServer.layerTest)),
557580
);

‎apps/server/src/peer/PeerLinks.testkit.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ export const layerLinkingEnvironment = (
159159
machine: Layer.Layer<never> = Layer.empty,
160160
) =>
161161
PeerMcpClient.layer.pipe(
162-
Layer.provideMerge(PeerLinks.layer.pipe(Layer.provide(machine))),
162+
Layer.provideMerge(PeerLinks.layer),
163+
Layer.provide(machine),
163164
Layer.provideMerge(ServerSettings.layerTest({ linkedEnvironmentsEnabled: true })),
164165
Layer.provideMerge(Sqlite.layerMemory),
165166
Layer.provide(Layer.mergeAll(secrets, FetchHttpClient.layer)),

‎apps/server/src/peer/PeerLinks.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,15 +568,18 @@ const make = Effect.gen(function* () {
568568
});
569569
}
570570
// The token goes only to an address that proves it is this peer, so a
571-
// reused LAN address or a moved hostname never receives it.
571+
// reused LAN address or a moved hostname never receives it. Its transport
572+
// is checked again: a 100.x address is trusted only while Tailscale runs.
572573
let fenced = true;
573574
const url = yield* Effect.findFirst(link.urls, (candidate) =>
574-
describe(candidate).pipe(
575+
peerOrigin(candidate).pipe(
576+
Effect.flatMap(describe),
575577
Effect.map((found) => {
576578
if (Option.isNone(found) || found.value.environmentId !== environmentId) return false;
577579
fenced = linkLimitsHold(found.value);
578580
return true;
579581
}),
582+
Effect.orElseSucceed(() => false),
580583
),
581584
);
582585
if (Option.isNone(url)) {

‎packages/tailscale/src/tailscale.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ export const readTailscaleStatus = readTailscaleOutput("status", ["status", "--j
289289
);
290290

291291
const TailscalePeersJson = Schema.Struct({
292+
BackendState: Schema.optional(Schema.Unknown),
292293
Self: Schema.optional(Schema.Struct({ TailscaleIPs: Schema.optional(Schema.Unknown) })),
293294
ExitNodeStatus: Schema.optional(Schema.Struct({ Online: Schema.optional(Schema.Unknown) })),
294295
Peer: Schema.optional(
@@ -339,7 +340,11 @@ export const readTailscaleExitNode = readTailscaleOutput("status", ["status", "-
339340
Effect.flatMap(parseTailscaleExitNode),
340341
);
341342

342-
/** The addresses of this machine and every other machine on its tailnet. */
343+
/**
344+
* The addresses of this machine and every other machine on its tailnet. Empty
345+
* unless Tailscale is running: a stopped one still lists them, but traffic to
346+
* them would leave unencrypted.
347+
*/
343348
export const readTailnetAddresses = readTailscaleOutput("status", ["status", "--json"]).pipe(
344349
Effect.flatMap((rawStatusJson) =>
345350
decodeTailscalePeersJson(rawStatusJson).pipe(
@@ -349,10 +354,13 @@ export const readTailnetAddresses = readTailscaleOutput("status", ["status", "--
349354
Effect.map(
350355
(parsed) =>
351356
new Set(
352-
[
353-
...(parsed.Self === undefined ? [] : [parsed.Self]),
354-
...Object.values(parsed.Peer ?? {}),
355-
].flatMap((node) =>
357+
(parsed.BackendState === "Running"
358+
? [
359+
...(parsed.Self === undefined ? [] : [parsed.Self]),
360+
...Object.values(parsed.Peer ?? {}),
361+
]
362+
: []
363+
).flatMap((node) =>
356364
Array.isArray(node.TailscaleIPs)
357365
? node.TailscaleIPs.filter((address): address is string => typeof address === "string")
358366
: [],

0 commit comments

Comments
 (0)