From fe855f6af83569a81ff1932fe07436f8e4590be6 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 3 Nov 2026 13:37:57 +0000 Subject: [PATCH 1/4] test(server): reproduce linking with this environment's own pairing code These tests reproduce #30181: pasting a pairing code this environment issued into its own link request fails with the generic "unknown, expired, or already used" message, and the code is sent to the peer's OAuth register and decision endpoints. The linking environment in the testkit now has its own pairing codes, and a served peer records every path it is asked for. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/peer/PeerLinks.test.ts | 24 +++++++++++++++++++++++ apps/server/src/peer/PeerLinks.testkit.ts | 14 +++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/apps/server/src/peer/PeerLinks.test.ts b/apps/server/src/peer/PeerLinks.test.ts index 3c510c35e6904..8af1913232246 100644 --- a/apps/server/src/peer/PeerLinks.test.ts +++ b/apps/server/src/peer/PeerLinks.test.ts @@ -22,6 +22,7 @@ import { HttpServerResponse, } from "effect/http"; +import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts"; import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; import * as McpHttpServer from "../mcp/McpHttpServer.ts"; import * as McpInvocationContext from "../mcp/McpInvocationContext.ts"; @@ -86,6 +87,7 @@ const makeA = ( Layer.build, Effect.map((context) => ({ links: Context.get(context, PeerLinks.PeerLinks), + auth: Context.get(context, EnvironmentAuth.EnvironmentAuth), peer: Context.get(context, PeerMcpClient.PeerMcpClient), sql: Context.get(context, SqlClient.SqlClient), settings: Context.get(context, ServerSettings.ServerSettingsService), @@ -160,6 +162,28 @@ it.effect("links with a pairing code, and the peer holds its agents to the limit ).pipe(Effect.provide(NodeHttpServer.layerTest)), ); +it.effect( + "a pairing code this environment issued is refused by name and never sent to the peer", + () => + Effect.scoped( + Effect.gen(function* () { + const b = yield* serveB(box); + const a = yield* makeA(laptop); + const own = yield* a.auth.issuePairingCredential(); + + const refused = yield* a.links + .link({ url: b.url, pairingCode: own.credential, access: "read-only" }) + .pipe(Effect.flip); + expect(refused.reason).toBe("pairing_rejected"); + expect(refused.message).toContain("Laptop"); + expect(refused.message).toContain("Box"); + expect((yield* Ref.get(b.paths)).filter((path) => path.startsWith("/oauth/mcp/"))).toEqual( + [], + ); + }), + ).pipe(Effect.provide(NodeHttpServer.layerTest)), +); + it.effect( "each machine's Linked environments beta gates links, and a link waits while it is off", () => diff --git a/apps/server/src/peer/PeerLinks.testkit.ts b/apps/server/src/peer/PeerLinks.testkit.ts index c72c5ba8c81bc..4e47cbe4afd5b 100644 --- a/apps/server/src/peer/PeerLinks.testkit.ts +++ b/apps/server/src/peer/PeerLinks.testkit.ts @@ -68,6 +68,7 @@ export const servePeer = ( const descriptor = yield* Ref.make(initial); const bearers = yield* Ref.make>([]); const probes = yield* Ref.make(0); + const paths = yield* Ref.make>([]); const layerEnvironment = Layer.succeed(ServerEnvironment.ServerEnvironment, { getEnvironmentId: Ref.get(descriptor).pipe(Effect.map((current) => current.environmentId)), getDescriptor: Ref.get(descriptor), @@ -87,6 +88,7 @@ export const servePeer = ( (httpEffect) => Effect.gen(function* () { const request = yield* HttpServerRequest.HttpServerRequest; + yield* Ref.update(paths, (seen) => [...seen, request.url]); if (request.url.startsWith("/.well-known/t3/environment")) { yield* Ref.update(probes, (count) => count + 1); } @@ -137,6 +139,8 @@ export const servePeer = ( bearers, /** How often its descriptor was asked for, as a probe of whether it answers. */ probes, + /** Every path it was asked for, in order. */ + paths, /** The sessions linking created there, which its Connections lists. */ linkedSessions: auth .listSessions() @@ -151,8 +155,9 @@ export const servePeer = ( export type ServedPeer = Effect.Success>; /** - * The environment that links: its own database and secret store, reaching - * peers over plain HTTP. Provide it to anything built on its links. + * The environment that links: its own database, secret store and pairing + * codes, reaching peers over plain HTTP. Provide it to anything built on its + * links. */ export const layerLinkingEnvironment = ( descriptor: ExecutionEnvironmentDescriptor, @@ -160,6 +165,11 @@ export const layerLinkingEnvironment = ( ) => PeerMcpClient.layer.pipe( Layer.provideMerge(PeerLinks.layer), + Layer.provideMerge( + EnvironmentAuth.layer.pipe( + Layer.provide(Layer.mergeAll(ManagedPolicy.layerTest(), ServerEnvironment.layerIdentity)), + ), + ), Layer.provideMerge(ServerSettings.layerTest({ linkedEnvironmentsEnabled: true })), Layer.provideMerge(Sqlite.layerMemory), Layer.provide(Layer.mergeAll(secrets, FetchHttpClient.layer)), From aa30e3798157d8291e7b1ab9c1cb50f86b775634 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 3 Nov 2026 13:42:45 +0000 Subject: [PATCH 2/4] fix(server): refuse to link with this environment's own pairing code Pasting a pairing code this environment issued into its own link request sent the code to the peer's OAuth register and decision endpoints, and the peer's refusal showed the generic "unknown, expired, or already used" message. Linking now looks the code up in this environment's pairing links first and refuses by name, before contacting the peer. Fixes pingdotgg/t3code#30181 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/cli/environment.ts | 2 ++ apps/server/src/peer/PeerLinks.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/apps/server/src/cli/environment.ts b/apps/server/src/cli/environment.ts index 869c085cbad2b..f01db7e5f7039 100644 --- a/apps/server/src/cli/environment.ts +++ b/apps/server/src/cli/environment.ts @@ -17,6 +17,7 @@ import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; import * as PeerLinks from "../peer/PeerLinks.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; +import * as AuthPairingLinks from "../persistence/AuthPairingLinks.ts"; import * as SqlitePersistence from "../persistence/Sqlite.ts"; import { authLocationFlags, type CliAuthLocationFlags, resolveCliAuthConfig } from "./config.ts"; @@ -35,6 +36,7 @@ const runWithPeerLinks = Effect.fn("environment.runWithPeerLinks")(function* settings.linkedEnvironmentsEnabled), Effect.orElseSucceed(() => false), @@ -435,6 +437,16 @@ const make = Effect.gen(function* () { message: `${peer.label} runs a T3 Code version that cannot keep a linked agent's limits. Update it, then link again.`, }); } + // A code issued here, even a used or expired one, must not reach the peer. + const issuedHere = yield* pairingLinks + .getByCredential({ credential: input.pairingCode }) + .pipe(Effect.catch(failStorage)); + if (Option.isSome(issuedHere)) { + return yield* new PeerLinkError({ + reason: "pairing_rejected", + message: `That pairing code was created on ${self.label}, not on ${peer.label}. Create the link on ${peer.label} and paste its code here.`, + }); + } const issued = yield* signIn(primary, input, `T3 Code · ${self.label}`); const now = yield* DateTime.now; const linked: PeerLink = { From 3f309bc28359d3e047a5d336e0030fedb7f35ee4 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 3 Nov 2026 13:57:49 +0000 Subject: [PATCH 3/4] fix(server): a lowercase or padded copy of this environment's own code is refused too Pairing codes are case-insensitive and the peer trims them, so the issued-here lookup normalises the pasted code the same way the pairing grant store does before it looks it up. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/auth/PairingGrantStore.ts | 9 +++++---- apps/server/src/peer/PeerLinks.test.ts | 14 ++++++++------ apps/server/src/peer/PeerLinks.ts | 3 ++- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/apps/server/src/auth/PairingGrantStore.ts b/apps/server/src/auth/PairingGrantStore.ts index 3df0f6a3ea2ed..b8a5f3b6a0e3a 100644 --- a/apps/server/src/auth/PairingGrantStore.ts +++ b/apps/server/src/auth/PairingGrantStore.ts @@ -261,6 +261,10 @@ const PAIRING_TOKEN_PATTERN = new RegExp( "i", ); +/** Only credentials in the issued pairing-code format are case-insensitive. */ +export const normalizePairingCredential = (credential: string) => + PAIRING_TOKEN_PATTERN.test(credential) ? credential.toUpperCase() : credential; + const make = Effect.gen(function* () { const random = yield* SecureRandom.SecureRandom; const config = yield* ServerConfig.ServerConfig; @@ -541,10 +545,7 @@ const make = Effect.gen(function* () { return yield* seededResult.error; } - // Only credentials in the issued pairing-code format are case-insensitive. - const pairingCredential = PAIRING_TOKEN_PATTERN.test(credential) - ? credential.toUpperCase() - : credential; + const pairingCredential = normalizePairingCredential(credential); // Check scopes in the UPDATE so rejected requests cannot spend one-time links. const consumed = yield* pairingLinks diff --git a/apps/server/src/peer/PeerLinks.test.ts b/apps/server/src/peer/PeerLinks.test.ts index 8af1913232246..60a3035c6c800 100644 --- a/apps/server/src/peer/PeerLinks.test.ts +++ b/apps/server/src/peer/PeerLinks.test.ts @@ -171,12 +171,14 @@ it.effect( const a = yield* makeA(laptop); const own = yield* a.auth.issuePairingCredential(); - const refused = yield* a.links - .link({ url: b.url, pairingCode: own.credential, access: "read-only" }) - .pipe(Effect.flip); - expect(refused.reason).toBe("pairing_rejected"); - expect(refused.message).toContain("Laptop"); - expect(refused.message).toContain("Box"); + for (const pairingCode of [own.credential, ` ${own.credential.toLowerCase()} `]) { + const refused = yield* a.links + .link({ url: b.url, pairingCode, access: "read-only" }) + .pipe(Effect.flip); + expect(refused.reason).toBe("pairing_rejected"); + expect(refused.message).toContain("Laptop"); + expect(refused.message).toContain("Box"); + } expect((yield* Ref.get(b.paths)).filter((path) => path.startsWith("/oauth/mcp/"))).toEqual( [], ); diff --git a/apps/server/src/peer/PeerLinks.ts b/apps/server/src/peer/PeerLinks.ts index bc6247436581b..04b091f76fc38 100644 --- a/apps/server/src/peer/PeerLinks.ts +++ b/apps/server/src/peer/PeerLinks.ts @@ -32,6 +32,7 @@ import * as HttpApi from "effect/http-api/HttpApi"; import * as HttpApiClient from "effect/http-api/HttpApiClient"; import { isLocalLoopbackHost, isTailnetHost } from "@t3tools/shared/hostClassification"; +import { normalizePairingCredential } from "../auth/PairingGrantStore.ts"; import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; import * as AuthPairingLinks from "../persistence/AuthPairingLinks.ts"; @@ -439,7 +440,7 @@ const make = Effect.gen(function* () { } // A code issued here, even a used or expired one, must not reach the peer. const issuedHere = yield* pairingLinks - .getByCredential({ credential: input.pairingCode }) + .getByCredential({ credential: normalizePairingCredential(input.pairingCode.trim()) }) .pipe(Effect.catch(failStorage)); if (Option.isSome(issuedHere)) { return yield* new PeerLinkError({ From ea70f458645b93ea75d37d45854fa021327c24bc Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 23:31:54 +0000 Subject: [PATCH 4/4] fix(server): own-code refusal stays clear when both environments share a label Co-Authored-By: Claude Opus 5.5 --- apps/server/src/peer/PeerLinks.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/server/src/peer/PeerLinks.ts b/apps/server/src/peer/PeerLinks.ts index e16eab8da61e1..60c01e0983926 100644 --- a/apps/server/src/peer/PeerLinks.ts +++ b/apps/server/src/peer/PeerLinks.ts @@ -490,7 +490,10 @@ const make = Effect.gen(function* () { if (Option.isSome(issuedHere)) { return yield* new PeerLinkError({ reason: "pairing_rejected", - message: `That pairing code was created on ${self.label}, not on ${peer.label}. Create the link on ${peer.label} and paste its code here.`, + message: + self.label === peer.label + ? `That pairing code was created on this environment, not on the ${peer.label} at ${primary}. Create the link there and paste its code here.` + : `That pairing code was created on ${self.label}, not on ${peer.label}. Create the link on ${peer.label} and paste its code here.`, }); } const issued = yield* signIn(primary, input, `T3 Code · ${self.label}`);