fix(mobile): block incompatible server connections (#11974)
This commit is contained in:
1 parent
e6ae764f47
commit
2c16c1d264
20 files changed
+234
-22
No files matched your search
@@ -0,0 +1,54 @@
|
||||
import {
|
||||
EnvironmentId,
|
||||
ORCHESTRATION_PROTOCOL_VERSION,
|
||||
type ExecutionEnvironmentDescriptor,
|
||||
} from "@t3tools/contracts";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import {
|
||||
appendOrchestrationProtocol,
|
||||
orchestrationProtocolCompatibilityError,
|
||||
} from "./compatibility.ts";
|
||||
|
||||
const descriptor = (orchestrationProtocolVersion?: number): ExecutionEnvironmentDescriptor => ({
|
||||
environmentId: EnvironmentId.make("environment-remote"),
|
||||
label: "Build Mac",
|
||||
platform: { os: "darwin", arch: "arm64" },
|
||||
serverVersion: "9.0.0",
|
||||
...(orchestrationProtocolVersion === undefined ? {} : { orchestrationProtocolVersion }),
|
||||
capabilities: { repositoryIdentity: true },
|
||||
});
|
||||
|
||||
describe("orchestration protocol compatibility", () => {
|
||||
it("accepts the current protocol and announces it without disturbing socket credentials", () => {
|
||||
expect(
|
||||
orchestrationProtocolCompatibilityError(descriptor(ORCHESTRATION_PROTOCOL_VERSION)),
|
||||
).toBeNull();
|
||||
|
||||
const socketUrl = new URL(
|
||||
appendOrchestrationProtocol("wss://host.test/ws?wsTicket=secret&connectionMethod=relay"),
|
||||
);
|
||||
expect(socketUrl.searchParams.get("orchestrationProtocol")).toBe(
|
||||
String(ORCHESTRATION_PROTOCOL_VERSION),
|
||||
);
|
||||
expect(socketUrl.searchParams.get("wsTicket")).toBe("secret");
|
||||
expect(socketUrl.searchParams.get("connectionMethod")).toBe("relay");
|
||||
});
|
||||
|
||||
it("treats missing metadata as protocol 1", () => {
|
||||
const error = orchestrationProtocolCompatibilityError(descriptor());
|
||||
if (Number(ORCHESTRATION_PROTOCOL_VERSION) === 1) {
|
||||
expect(error).toBeNull();
|
||||
} else {
|
||||
expect(error).toMatchObject({ reason: "unsupported" });
|
||||
}
|
||||
});
|
||||
|
||||
it("blocks a different protocol before connecting", () => {
|
||||
const error = orchestrationProtocolCompatibilityError(
|
||||
descriptor(ORCHESTRATION_PROTOCOL_VERSION + 1),
|
||||
);
|
||||
expect(error).toMatchObject({ reason: "unsupported" });
|
||||
expect(error?.message).toContain("This client is not supported");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
import {
|
||||
ORCHESTRATION_PROTOCOL_QUERY_PARAM,
|
||||
ORCHESTRATION_PROTOCOL_VERSION,
|
||||
type ExecutionEnvironmentDescriptor,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
import { ConnectionBlockedError } from "./model.ts";
|
||||
|
||||
export function orchestrationProtocolCompatibilityError(
|
||||
descriptor: ExecutionEnvironmentDescriptor,
|
||||
): ConnectionBlockedError | null {
|
||||
// Servers shipped before negotiation use the original wire protocol.
|
||||
const serverProtocolVersion = descriptor.orchestrationProtocolVersion ?? 1;
|
||||
if (serverProtocolVersion === ORCHESTRATION_PROTOCOL_VERSION) {
|
||||
return null;
|
||||
}
|
||||
return new ConnectionBlockedError({
|
||||
reason: "unsupported",
|
||||
detail:
|
||||
serverProtocolVersion > ORCHESTRATION_PROTOCOL_VERSION
|
||||
? `This client is not supported by this server. Update your app or use a compatible release to connect to ${descriptor.label}.`
|
||||
: `This client requires a newer server. Update T3 Code on ${descriptor.label} to connect.`,
|
||||
});
|
||||
}
|
||||
|
||||
export function appendOrchestrationProtocol(socketUrl: string): string {
|
||||
const url = new URL(socketUrl);
|
||||
url.searchParams.set(ORCHESTRATION_PROTOCOL_QUERY_PARAM, String(ORCHESTRATION_PROTOCOL_VERSION));
|
||||
return url.toString();
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import * as Option from "effect/Option";
|
||||
import { BearerConnectionProfile, type ConnectionCatalogEntry } from "./catalog.ts";
|
||||
import {
|
||||
BearerConnectionTarget,
|
||||
ConnectionBlockedError,
|
||||
ConnectionTransientError,
|
||||
type SupervisorConnectionState,
|
||||
} from "./model.ts";
|
||||
@@ -51,6 +52,21 @@ function supervisorState(overrides: Partial<SupervisorConnectionState>): Supervi
|
||||
}
|
||||
|
||||
describe("connection presentation", () => {
|
||||
it("labels a blocked protocol as unsupported", () => {
|
||||
const connection = presentConnectionState(
|
||||
supervisorState({
|
||||
phase: "blocked",
|
||||
lastFailure: new ConnectionBlockedError({
|
||||
reason: "unsupported",
|
||||
detail: "Update your app.",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(connection.phase).toBe("unsupported");
|
||||
expect(connection.error).toBe("Update your app.");
|
||||
expect(connectionStatusText(connection)).toBe("Client not supported");
|
||||
});
|
||||
|
||||
it("preserves profile display information without exposing credentials", () => {
|
||||
expect(connectionCatalogDisplayUrl(ENTRY)).toBe("https://environment.example.test");
|
||||
});
|
||||
|
||||
@@ -10,7 +10,8 @@ export type EnvironmentConnectionPhase =
|
||||
| "connecting"
|
||||
| "reconnecting"
|
||||
| "connected"
|
||||
| "error";
|
||||
| "error"
|
||||
| "unsupported";
|
||||
|
||||
export interface EnvironmentConnectionPresentation {
|
||||
readonly phase: EnvironmentConnectionPhase;
|
||||
@@ -48,7 +49,7 @@ export function presentConnectionState(
|
||||
};
|
||||
case "blocked":
|
||||
return {
|
||||
phase: "error",
|
||||
phase: state.lastFailure?.reason === "unsupported" ? "unsupported" : "error",
|
||||
error: state.lastFailure?.message ?? null,
|
||||
traceId: state.lastFailure?.traceId ?? null,
|
||||
};
|
||||
@@ -69,6 +70,8 @@ export function connectionStatusText(connection: EnvironmentConnectionPresentati
|
||||
: "Reconnecting...";
|
||||
case "connected":
|
||||
return "Connected";
|
||||
case "unsupported":
|
||||
return "Client not supported";
|
||||
case "error":
|
||||
return connection.error
|
||||
? `Connection failed. Reason: ${connection.error}`
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { EnvironmentId, type DesktopSshEnvironmentTarget } from "@t3tools/contracts";
|
||||
import {
|
||||
EnvironmentId,
|
||||
ORCHESTRATION_PROTOCOL_VERSION,
|
||||
type DesktopSshEnvironmentTarget,
|
||||
} from "@t3tools/contracts";
|
||||
import { RelayClientTracer } from "@t3tools/shared/relayTracing";
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
import * as Effect from "effect/Effect";
|
||||
@@ -28,6 +32,7 @@ import {
|
||||
type ConnectionTarget,
|
||||
} from "./model.ts";
|
||||
import * as ConnectionProfileStore from "./profileStore.ts";
|
||||
import { remoteHttpClientLayer } from "../rpc/http.ts";
|
||||
import {
|
||||
GitHubRoutingPermissions,
|
||||
gitHubRoutingConnectionKey,
|
||||
@@ -75,6 +80,7 @@ const makeDependencies = Effect.fn("TestConnectionResolver.makeDependencies")((o
|
||||
readonly authorizeDpop?: RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization["Service"]["authorizeDpop"];
|
||||
readonly primaryBearerToken?: string;
|
||||
readonly prepareSsh?: ClientCapabilities.SshEnvironmentGateway["Service"]["prepare"];
|
||||
readonly descriptorProtocolVersion?: number | null | undefined;
|
||||
}) => {
|
||||
const profiles = new Map(
|
||||
(options?.profiles ?? []).map((profile) => [profile.connectionId, profile]),
|
||||
@@ -140,6 +146,21 @@ const makeDependencies = Effect.fn("TestConnectionResolver.makeDependencies")((o
|
||||
});
|
||||
|
||||
const dependencies = Layer.mergeAll(
|
||||
remoteHttpClientLayer((() =>
|
||||
Promise.resolve(
|
||||
Response.json({
|
||||
environmentId: ENVIRONMENT_ID,
|
||||
label: "Compatible environment",
|
||||
platform: { os: "linux", arch: "x64" },
|
||||
serverVersion: "0.0.0-test",
|
||||
...(options?.descriptorProtocolVersion === undefined
|
||||
? { orchestrationProtocolVersion: ORCHESTRATION_PROTOCOL_VERSION }
|
||||
: options.descriptorProtocolVersion === null
|
||||
? {}
|
||||
: { orchestrationProtocolVersion: options.descriptorProtocolVersion }),
|
||||
capabilities: { repositoryIdentity: true },
|
||||
}),
|
||||
)) satisfies typeof fetch),
|
||||
Layer.succeed(
|
||||
ConnectionProfileStore.ConnectionProfileStore,
|
||||
options?.profileStore ?? profileStore,
|
||||
@@ -166,6 +187,26 @@ const makeDependencies = Effect.fn("TestConnectionResolver.makeDependencies")((o
|
||||
});
|
||||
|
||||
describe("ConnectionResolver", () => {
|
||||
it.effect("blocks an incompatible host during discovery before opening orchestration RPC", () =>
|
||||
Effect.gen(function* () {
|
||||
const brokerLayer = yield* makeDependencies({
|
||||
descriptorProtocolVersion: ORCHESTRATION_PROTOCOL_VERSION + 1,
|
||||
});
|
||||
const broker = yield* ConnectionResolver.ConnectionResolver.pipe(Effect.provide(brokerLayer));
|
||||
const target = new PrimaryConnectionTarget({
|
||||
environmentId: ENVIRONMENT_ID,
|
||||
label: "Primary",
|
||||
httpBaseUrl: "http://127.0.0.1:3777",
|
||||
wsBaseUrl: "ws://127.0.0.1:3777",
|
||||
});
|
||||
|
||||
const error = yield* Effect.flip(broker.prepare(catalogEntry(target)));
|
||||
|
||||
expect(error).toMatchObject({ reason: "unsupported" });
|
||||
expect(error.message).toContain("This client is not supported");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("prepares a primary environment without remote capabilities", () =>
|
||||
Effect.gen(function* () {
|
||||
const brokerLayer = yield* makeDependencies();
|
||||
@@ -182,7 +223,7 @@ describe("ConnectionResolver", () => {
|
||||
label: "Primary",
|
||||
httpBaseUrl: "http://127.0.0.1:3777",
|
||||
socketUrl:
|
||||
"ws://127.0.0.1:3777/ws?clientSurface=web&clientDeviceType=desktop&connectionMethod=direct",
|
||||
"ws://127.0.0.1:3777/ws?clientSurface=web&clientDeviceType=desktop&connectionMethod=direct&orchestrationProtocol=1",
|
||||
httpAuthorization: null,
|
||||
target,
|
||||
});
|
||||
@@ -220,7 +261,7 @@ describe("ConnectionResolver", () => {
|
||||
});
|
||||
|
||||
expect(yield* broker.prepare(catalogEntry(target))).toMatchObject({
|
||||
socketUrl: "ws://127.0.0.1:3777/ws?wsTicket=desktop",
|
||||
socketUrl: "ws://127.0.0.1:3777/ws?wsTicket=desktop&orchestrationProtocol=1",
|
||||
httpAuthorization: { _tag: "Bearer", token: "desktop-bearer" },
|
||||
target,
|
||||
});
|
||||
@@ -284,7 +325,7 @@ describe("ConnectionResolver", () => {
|
||||
environmentId: ENVIRONMENT_ID,
|
||||
label: "Authorized relay environment",
|
||||
httpBaseUrl: ENDPOINT.httpBaseUrl,
|
||||
socketUrl: "wss://authorized.example.test/ws?wsTicket=dpop",
|
||||
socketUrl: `wss://authorized.example.test/ws?wsTicket=dpop&orchestrationProtocol=${ORCHESTRATION_PROTOCOL_VERSION}`,
|
||||
httpAuthorization: {
|
||||
_tag: "Dpop",
|
||||
accessToken: "dpop-access-token",
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { AuthClientPresentationMetadata } from "@t3tools/contracts";
|
||||
import { withRelayClientTracing } from "@t3tools/shared/relayTracing";
|
||||
import * as Context from "effect/Context";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as HttpClient from "effect/unstable/http/HttpClient";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Schema from "effect/Schema";
|
||||
@@ -16,7 +17,12 @@ import {
|
||||
SshConnectionProfile,
|
||||
} from "./catalog.ts";
|
||||
import * as ConnectionCredentialStore from "./credentialStore.ts";
|
||||
import { credentialMissingError, environmentMismatchError, profileMissingError } from "./errors.ts";
|
||||
import {
|
||||
credentialMissingError,
|
||||
environmentMismatchError,
|
||||
mapRemoteEnvironmentError,
|
||||
profileMissingError,
|
||||
} from "./errors.ts";
|
||||
import {
|
||||
GitHubRoutingPermissions,
|
||||
gitHubRoutingConnectionKey,
|
||||
@@ -31,6 +37,11 @@ import type {
|
||||
} from "./model.ts";
|
||||
import { ConnectionBlockedError, type ConnectionAttemptError } from "./model.ts";
|
||||
import * as ConnectionProfileStore from "./profileStore.ts";
|
||||
import {
|
||||
appendOrchestrationProtocol,
|
||||
orchestrationProtocolCompatibilityError,
|
||||
} from "./compatibility.ts";
|
||||
import { fetchRemoteEnvironmentDescriptor } from "../environment/descriptor.ts";
|
||||
|
||||
export class ConnectionResolver extends Context.Service<
|
||||
ConnectionResolver,
|
||||
@@ -234,6 +245,7 @@ export const make = Effect.gen(function* () {
|
||||
const bearer = yield* makeBearerBroker();
|
||||
const relay = yield* makeRelayBroker();
|
||||
const ssh = yield* makeSshBroker();
|
||||
const httpClient = yield* HttpClient.HttpClient;
|
||||
|
||||
const prepare = Effect.fn("clientRuntime.connection.broker.prepare")(function* (
|
||||
entry: ConnectionCatalogEntry,
|
||||
@@ -243,16 +255,35 @@ export const make = Effect.gen(function* () {
|
||||
"connection.environment.id": target.environmentId,
|
||||
"connection.target.kind": target._tag,
|
||||
});
|
||||
switch (target._tag) {
|
||||
case "PrimaryConnectionTarget":
|
||||
return yield* primary(target);
|
||||
case "BearerConnectionTarget":
|
||||
return yield* bearer({ ...entry, target });
|
||||
case "RelayConnectionTarget":
|
||||
return yield* relay(target);
|
||||
case "SshConnectionTarget":
|
||||
return yield* ssh({ ...entry, target });
|
||||
const prepared = yield* (() => {
|
||||
switch (target._tag) {
|
||||
case "PrimaryConnectionTarget":
|
||||
return primary(target);
|
||||
case "BearerConnectionTarget":
|
||||
return bearer({ ...entry, target });
|
||||
case "RelayConnectionTarget":
|
||||
return relay(target);
|
||||
case "SshConnectionTarget":
|
||||
return ssh({ ...entry, target });
|
||||
}
|
||||
})();
|
||||
const descriptor = yield* fetchRemoteEnvironmentDescriptor({
|
||||
httpBaseUrl: prepared.httpBaseUrl,
|
||||
}).pipe(
|
||||
Effect.mapError(mapRemoteEnvironmentError),
|
||||
Effect.provideService(HttpClient.HttpClient, httpClient),
|
||||
);
|
||||
if (descriptor.environmentId !== target.environmentId) {
|
||||
return yield* environmentMismatchError({
|
||||
expected: target.environmentId,
|
||||
actual: descriptor.environmentId,
|
||||
});
|
||||
}
|
||||
const compatibilityError = orchestrationProtocolCompatibilityError(descriptor);
|
||||
if (compatibilityError !== null) {
|
||||
return yield* compatibilityError;
|
||||
}
|
||||
return { ...prepared, socketUrl: appendOrchestrationProtocol(prepared.socketUrl) };
|
||||
});
|
||||
|
||||
return ConnectionResolver.of({ prepare });
|
||||
|
||||
@@ -9,6 +9,10 @@ import {
|
||||
TrimmedNonEmptyString,
|
||||
} from "./baseSchemas.ts";
|
||||
|
||||
/** Wire version for orchestration snapshots, streams, commands, and RPC payloads. */
|
||||
export const ORCHESTRATION_PROTOCOL_VERSION = 1;
|
||||
export const ORCHESTRATION_PROTOCOL_QUERY_PARAM = "orchestrationProtocol";
|
||||
|
||||
export const ExecutionEnvironmentPlatformOs = Schema.Literals([
|
||||
"darwin",
|
||||
"linux",
|
||||
@@ -175,6 +179,8 @@ export const ExecutionEnvironmentDescriptor = Schema.Struct({
|
||||
label: TrimmedNonEmptyString,
|
||||
platform: ExecutionEnvironmentPlatform,
|
||||
serverVersion: TrimmedNonEmptyString,
|
||||
/** Missing metadata denotes protocol 1. Bump this for breaking wire changes. */
|
||||
orchestrationProtocolVersion: Schema.optionalKey(Schema.Int),
|
||||
capabilities: ExecutionEnvironmentCapabilities,
|
||||
});
|
||||
export type ExecutionEnvironmentDescriptor = typeof ExecutionEnvironmentDescriptor.Type;
|
||||
|
||||
Reference in new issue
Block a user