Skip to content

Commit 3f3a1bf

Browse files
fix(server): reject pairing with an incompatible server protocol (#23789)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent f416175 commit 3f3a1bf

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

‎apps/server/src/cli/pair.test.ts‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import * as NodeOS from "node:os";
55
import * as NodePath from "node:path";
66

77
import * as NodeServices from "@effect/platform-node/NodeServices";
8-
import { AuthStandardClientScopes } from "@t3tools/contracts";
8+
import { AuthStandardClientScopes, ORCHESTRATION_PROTOCOL_VERSION } from "@t3tools/contracts";
99
import * as NetService from "@t3tools/shared/Net";
1010
import * as HostProcess from "@t3tools/shared/HostProcess";
1111
import { assert, describe, expect, it } from "@effect/vitest";
12+
import * as Cause from "effect/Cause";
1213
import * as ConfigProvider from "effect/ConfigProvider";
1314
import * as Effect from "effect/Effect";
15+
import * as Exit from "effect/Exit";
1416
import * as Layer from "effect/Layer";
1517
import * as Sink from "effect/Sink";
1618
import * as Stream from "effect/Stream";
@@ -89,19 +91,21 @@ const testDescriptor = {
8991
label: "pair-test",
9092
platform: { os: "linux", arch: "x64" },
9193
serverVersion: "0.0.1",
94+
orchestrationProtocolVersion: ORCHESTRATION_PROTOCOL_VERSION,
9295
capabilities: { repositoryIdentity: true },
9396
};
9497

9598
const withDescriptorServer = <A, E, R>(
9699
run: (origin: string) => Effect.Effect<A, E, R>,
97100
host = "127.0.0.1",
101+
descriptor: object = testDescriptor,
98102
) =>
99103
Effect.acquireUseRelease(
100104
Effect.callback<NodeHttp.Server>((resume) => {
101105
const server = NodeHttp.createServer((request, response) => {
102106
if (request.url === "/.well-known/t3/environment") {
103107
response.writeHead(200, { "content-type": "application/json" });
104-
response.end(JSON.stringify(testDescriptor));
108+
response.end(JSON.stringify(descriptor));
105109
return;
106110
}
107111
response.writeHead(404);
@@ -328,6 +332,40 @@ describe("t3 pair", () => {
328332
).pipe(Effect.provide(NodeServices.layer)),
329333
);
330334

335+
// regression: #15492 an older CLI minted into a V1 database the protocol 2
336+
// server never reads, so pairing must refuse before opening the database.
337+
it.effect("rejects a server on another protocol before opening its database", () =>
338+
withDescriptorServer(
339+
(origin) =>
340+
Effect.gen(function* () {
341+
const baseDir = NodeFS.mkdtempSync(
342+
NodePath.join(NodeOS.tmpdir(), "t3-pair-protocol-test-"),
343+
);
344+
yield* persistServerRuntimeState({
345+
path: NodePath.join(baseDir, "userdata", "server-runtime.json"),
346+
state: yield* makePersistedServerRuntimeState({
347+
config: { host: "127.0.0.1", devUrl: undefined },
348+
port: Number(new URL(origin).port),
349+
}),
350+
});
351+
352+
const exit = yield* provideCliTestLayers(
353+
runCli(["pair", "--base-dir", baseDir]).pipe(Effect.exit),
354+
);
355+
356+
assert.deepEqual(NodeFS.readdirSync(NodePath.join(baseDir, "userdata")), [
357+
"server-runtime.json",
358+
]);
359+
assert.isTrue(Exit.isFailure(exit));
360+
if (Exit.isFailure(exit)) {
361+
assert.include(String(Cause.squash(exit.cause)), "[email protected] pair");
362+
}
363+
}),
364+
"127.0.0.1",
365+
{ ...testDescriptor, serverVersion: "0.0.45", orchestrationProtocolVersion: 1 },
366+
).pipe(Effect.provide(NodeServices.layer)),
367+
);
368+
331369
// Without the capability `tailscale serve` blocks on an enablement URL, so
332370
// pairing must stop at status instead of spawning it.
333371
it.effect("explains a tailnet without HTTPS serve instead of running tailscale serve", () =>

‎apps/server/src/cli/pair.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
type AuthEnvironmentScope,
33
AuthStandardClientScopes,
44
ExecutionEnvironmentDescriptor,
5+
ORCHESTRATION_PROTOCOL_VERSION,
56
PortSchema,
67
} from "@t3tools/contracts";
78
import { resolveWorktreeT3Home } from "@t3tools/shared/devHome";
@@ -77,6 +78,18 @@ export class NoRunningServerError extends Schema.TaggedError<NoRunningServerErro
7778
}
7879
}
7980

81+
export class IncompatibleServerProtocolError extends Schema.TaggedError<IncompatibleServerProtocolError>()(
82+
"IncompatibleServerProtocolError",
83+
{ serverVersion: Schema.String, serverProtocolVersion: Schema.Number },
84+
) {
85+
override get message(): string {
86+
return [
87+
`The running T3 Code server (${this.serverVersion}) speaks orchestration protocol ${String(this.serverProtocolVersion)}, but this CLI speaks protocol ${String(ORCHESTRATION_PROTOCOL_VERSION)}.`,
88+
`Pair with the matching CLI instead: \`npx t3@${this.serverVersion} pair\`.`,
89+
].join("\n");
90+
}
91+
}
92+
8093
export class TailscaleUnavailableError extends Schema.TaggedError<TailscaleUnavailableError>()(
8194
"TailscaleUnavailableError",
8295
{ cause: Schema.Defect() },
@@ -493,6 +506,16 @@ export const pairCommand = Command.make("pair", {
493506
const logLevel = Option.getOrElse(cliLogLevel, () => "Warn" as const);
494507

495508
const target = yield* discoverPairTarget(Option.getOrUndefined(flags.baseDir));
509+
// Each protocol keeps auth in its own database, so a token minted here
510+
// for another protocol's server would never be accepted by it. Refuse
511+
// before any local layer opens (and migrates) that database.
512+
const serverProtocolVersion = target.descriptor.orchestrationProtocolVersion ?? 1;
513+
if (serverProtocolVersion !== ORCHESTRATION_PROTOCOL_VERSION) {
514+
return yield* new IncompatibleServerProtocolError({
515+
serverVersion: target.descriptor.serverVersion,
516+
serverProtocolVersion,
517+
});
518+
}
496519

497520
const notes: Array<string> = [];
498521
let pairingBaseUrl: string;

0 commit comments

Comments
 (0)